From a27dfd140c04db72b5c0d5a7b2b7fe498899068f Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 17 Mar 2023 05:21:19 -0700 Subject: [PATCH] route53_info: fix "key error" for health_check operations (#1419) route53_info: fix "key error" for health_check operations SUMMARY Fixes #1396 This pull request Add new return key health_check_observations for health check operations, returned when I(query=health_check) and I(health_check_method=status) or I(health_check_method=failure_reason) Fixes "Key Error" when getting status or failure_reason of a health check. ISSUE TYPE Bugfix Pull Request COMPONENT NAME route53_info ADDITIONAL INFORMATION Reviewed-by: Mark Chappell Reviewed-by: Mandar Kulkarni Reviewed-by: Alina Buzachis --- ...ix-keyerror-for-healthcheck-operations.yml | 3 + plugins/modules/route53_info.py | 60 +++++++++++++++---- .../route53_health_check/tasks/main.yml | 30 ++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/1419-route53_info-fix-keyerror-for-healthcheck-operations.yml diff --git a/changelogs/fragments/1419-route53_info-fix-keyerror-for-healthcheck-operations.yml b/changelogs/fragments/1419-route53_info-fix-keyerror-for-healthcheck-operations.yml new file mode 100644 index 00000000000..20510697d78 --- /dev/null +++ b/changelogs/fragments/1419-route53_info-fix-keyerror-for-healthcheck-operations.yml @@ -0,0 +1,3 @@ +bugfixes: +- route53_info - Fixed "Key Error" when getting status or failure_reason of a health check (https://github.com/ansible-collections/amazon.aws/pull/1419). +- route53_info - Add new return key `health_check_observations` for health check operations (https://github.com/ansible-collections/amazon.aws/pull/1419). diff --git a/plugins/modules/route53_info.py b/plugins/modules/route53_info.py index e2091c3f953..fdfa8773945 100644 --- a/plugins/modules/route53_info.py +++ b/plugins/modules/route53_info.py @@ -378,7 +378,7 @@ version_added: 4.1.0 version_added_collection: community.aws health_check: - description: A dict of Route53 health check details returned by get_health_check_status in boto3. + description: A dict of Route53 health check details returned by get_health_check in boto3. type: dict returned: when I(query=health_check) and I(health_check_method=details) contains: @@ -448,6 +448,33 @@ sample: HTTPS version_added: 4.1.0 version_added_collection: community.aws +health_check_observations: + description: A dict of Route53 health check details returned by get_health_check_status and get_health_check_last_failure_reason in boto3. + type: list + elements: dict + returned: when I(query=health_check) and I(health_check_method=status) or I(health_check_method=failure_reason) + contains: + ip_address: + description: The IP address of the Amazon Route 53 health checker that provided the failure reason in StatusReport. + type: str + sample: '12.345.67.89' + region: + description: The region of the Amazon Route 53 health checker that provided the status in StatusReport. + type: str + sample: 'us-west-1' + status_report: + description: A complex type that contains the last failure reason and the time of the failed health check. + type: dict + contains: + checked_time: + description: The date and time that the health checker performed the health check in ISO 8601 format and Coordinated Universal Time (UTC). + type: str + sample: '2023-03-08T23:10:08.452000+00:00' + status: + description: A description of the status of the health check endpoint as reported by one of the Amazon Route 53 health checkers. + type: str + sample: 'Failure: Resolved IP: 12.345.67.89. The connection was closed by the endpoint.' + version_added: 5.4.0 ResourceRecordSets: description: A deprecated CamelCased list of resource record sets returned by list_resource_record_sets in boto3. \ This list contains same elements/parameters as it's snake_cased version mentioned above. \ @@ -484,7 +511,7 @@ elements: dict returned: when I(query=reusable_delegation_set) HealthCheck: - description: A deprecated CamelCased dict of Route53 health check details returned by get_health_check_status in boto3. \ + description: A deprecated CamelCased dict of Route53 health check details returned by get_health_check in boto3. \ This dict contains same elements/parameters as it's snake_cased version mentioned above. \ This field is deprecated and will be removed in 6.0.0 version release. type: dict @@ -624,6 +651,7 @@ def get_count(): def get_health_check(): params = dict() + results = dict() if not module.params.get('health_check_id'): module.fail_json(msg="health_check_id is required") @@ -632,16 +660,26 @@ def get_health_check(): if module.params.get('health_check_method') == 'details': results = client.get_health_check(**params) + results["health_check"] = camel_dict_to_snake_dict(results["HealthCheck"]) + module.deprecate( + "The 'CamelCase' return values with key 'HealthCheck' is deprecated \ + and will be replaced by 'snake_case' return values with key 'health_check'. \ + Both case values are returned for now.", + date="2025-01-01", + collection_name="amazon.aws", + ) + elif module.params.get('health_check_method') == 'failure_reason': - results = client.get_health_check_last_failure_reason(**params) - elif module.params.get('health_check_method') == 'status': - results = client.get_health_check_status(**params) + response = client.get_health_check_last_failure_reason(**params) + results["health_check_observations"] = [ + camel_dict_to_snake_dict(health_check) for health_check in response["HealthCheckObservations"] + ] - results['health_check'] = camel_dict_to_snake_dict(results['HealthCheck']) - module.deprecate("The 'CamelCase' return values with key 'HealthCheck' is deprecated and \ - will be replaced by 'snake_case' return values with key 'health_check'. \ - Both case values are returned for now.", - date='2025-01-01', collection_name='amazon.aws') + elif module.params.get('health_check_method') == 'status': + response = client.get_health_check_status(**params) + results["health_check_observations"] = [ + camel_dict_to_snake_dict(health_check) for health_check in response["HealthCheckObservations"] + ] return results @@ -824,7 +862,7 @@ def main(): try: results = invocations[module.params.get('query')]() except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json(msg=to_native(e)) + module.fail_json_aws(e, msg="Query failed") module.exit_json(**results) diff --git a/tests/integration/targets/route53_health_check/tasks/main.yml b/tests/integration/targets/route53_health_check/tasks/main.yml index 1cd73f6a4bf..4a13464e343 100644 --- a/tests/integration/targets/route53_health_check/tasks/main.yml +++ b/tests/integration/targets/route53_health_check/tasks/main.yml @@ -1725,6 +1725,36 @@ - create_check is changed - health_check_info.health_check.health_check_config.measure_latency == true + - pause: + seconds: 20 + + # test route53_info for health_check_method=status + - name: Get health check status + amazon.aws.route53_info: + query: health_check + health_check_id: "{{ create_check.health_check.id }}" + health_check_method: status + register: health_check_status_info + + - assert: + that: + - health_check_status_info is not failed + - '"health_check_observations" in health_check_status_info' + + # test route53_info for health_check_method=failure_reason + - name: Get health check failure_reason + amazon.aws.route53_info: + query: health_check + health_check_id: "{{ create_check.health_check.id }}" + health_check_method: failure_reason + register: health_check_failure_reason_info + + - assert: + that: + - health_check_failure_reason_info is not failed + - '"health_check_observations" in health_check_failure_reason_info' + + - name: 'Update above health check to disable latency graphs - immutable, no change' route53_health_check: state: present