From c8389f1cf549352b397766e333d802f8ee65950a Mon Sep 17 00:00:00 2001 From: Macwan Nevil Date: Sun, 19 Jun 2022 16:51:28 +0530 Subject: [PATCH] fixed tf tests: TestAccRoute53HealthCheck (#5241) --- moto/route53/models.py | 62 ++++++++++++++++++- moto/route53/responses.py | 35 +++++++++-- moto/route53/urls.py | 4 +- .../terraform-tests.success.txt | 10 +++ 4 files changed, 102 insertions(+), 9 deletions(-) diff --git a/moto/route53/models.py b/moto/route53/models.py index e420b1c8abb..8e6be240987 100644 --- a/moto/route53/models.py +++ b/moto/route53/models.py @@ -64,9 +64,22 @@ def __init__(self, health_check_id, caller_reference, health_check_args): self.measure_latency = health_check_args.get("measure_latency") or False self.inverted = health_check_args.get("inverted") or False self.disabled = health_check_args.get("disabled") or False - self.enable_sni = health_check_args.get("enable_sni") or False - self.children = health_check_args.get("children") or None + self.enable_sni = health_check_args.get("enable_sni") or True self.caller_reference = caller_reference + self.children = None + self.regions = None + + def set_children(self, children): + if children and isinstance(children, list): + self.children = children + elif children and isinstance(children, str): + self.children = [children] + + def set_regions(self, regions): + if regions and isinstance(regions, list): + self.regions = regions + elif regions and isinstance(regions, str): + self.regions = [regions] @property def physical_resource_id(self): @@ -135,10 +148,17 @@ def to_xml(self): {% if health_check.children %} {% for child in health_check.children %} - {{ child }} + {{ child }} {% endfor %} {% endif %} + {% if health_check.regions %} + + {% for region in health_check.regions %} + {{ region }} + {% endfor %} + + {% endif %} 1 """ @@ -617,9 +637,45 @@ def update_hosted_zone_comment(self, id_, comment): def create_health_check(self, caller_reference, health_check_args): health_check_id = str(uuid.uuid4()) health_check = HealthCheck(health_check_id, caller_reference, health_check_args) + health_check.set_children(health_check_args.get("children")) + health_check.set_regions(health_check_args.get("regions")) self.health_checks[health_check_id] = health_check return health_check + def update_health_check(self, health_check_id, health_check_args): + health_check = self.health_checks.get(health_check_id) + if not health_check: + raise NoSuchHealthCheck() + + if health_check_args.get("ip_address"): + health_check.ip_address = health_check_args.get("ip_address") + if health_check_args.get("port"): + health_check.port = health_check_args.get("port") + if health_check_args.get("resource_path"): + health_check.resource_path = health_check_args.get("resource_path") + if health_check_args.get("fqdn"): + health_check.fqdn = health_check_args.get("fqdn") + if health_check_args.get("search_string"): + health_check.search_string = health_check_args.get("search_string") + if health_check_args.get("request_interval"): + health_check.request_interval = health_check_args.get("request_interval") + if health_check_args.get("failure_threshold"): + health_check.failure_threshold = health_check_args.get("failure_threshold") + if health_check_args.get("health_threshold"): + health_check.health_threshold = health_check_args.get("health_threshold") + if health_check_args.get("inverted"): + health_check.inverted = health_check_args.get("inverted") + if health_check_args.get("disabled"): + health_check.disabled = health_check_args.get("disabled") + if health_check_args.get("enable_sni"): + health_check.enable_sni = health_check_args.get("enable_sni") + if health_check_args.get("children"): + health_check.set_children(health_check_args.get("children")) + if health_check_args.get("regions"): + health_check.set_regions(health_check_args.get("regions")) + + return health_check + def list_health_checks(self): return self.health_checks.values() diff --git a/moto/route53/responses.py b/moto/route53/responses.py index 81bae77e17f..86ea8302c61 100644 --- a/moto/route53/responses.py +++ b/moto/route53/responses.py @@ -248,7 +248,7 @@ def rrset_response(self, request, full_url, headers): ) return 200, headers, template - def health_check_response(self, request, full_url, headers): + def health_check_response1(self, request, full_url, headers): self.setup_class(request, full_url, headers) parsed_url = urlparse(full_url) @@ -273,6 +273,7 @@ def health_check_response(self, request, full_url, headers): "disabled": config.get("Disabled"), "enable_sni": config.get("EnableSNI"), "children": config.get("ChildHealthChecks", {}).get("ChildHealthCheck"), + "regions": config.get("Regions", {}).get("Region"), } health_check = route53_backend.create_health_check( caller_reference, health_check_args @@ -293,22 +294,42 @@ def health_check_response(self, request, full_url, headers): template.render(health_checks=health_checks, xmlns=XMLNS), ) - def get_or_delete_health_check_response(self, request, full_url, headers): + def health_check_response2(self, request, full_url, headers): self.setup_class(request, full_url, headers) parsed_url = urlparse(full_url) method = request.method + health_check_id = parsed_url.path.split("/")[-1] if method == "GET": - health_check_id = parsed_url.path.split("/")[-1] health_check = route53_backend.get_health_check(health_check_id) template = Template(GET_HEALTH_CHECK_RESPONSE) return 200, headers, template.render(health_check=health_check) elif method == "DELETE": - health_check_id = parsed_url.path.split("/")[-1] route53_backend.delete_health_check(health_check_id) template = Template(DELETE_HEALTH_CHECK_RESPONSE) return 200, headers, template.render(xmlns=XMLNS) + elif method == "POST": + config = xmltodict.parse(self.body)["UpdateHealthCheckRequest"] + health_check_args = { + "ip_address": config.get("IPAddress"), + "port": config.get("Port"), + "resource_path": config.get("ResourcePath"), + "fqdn": config.get("FullyQualifiedDomainName"), + "search_string": config.get("SearchString"), + "failure_threshold": config.get("FailureThreshold"), + "health_threshold": config.get("HealthThreshold"), + "inverted": config.get("Inverted"), + "disabled": config.get("Disabled"), + "enable_sni": config.get("EnableSNI"), + "children": config.get("ChildHealthChecks", {}).get("ChildHealthCheck"), + "regions": config.get("Regions", {}).get("Region"), + } + health_check = route53_backend.update_health_check( + health_check_id, health_check_args + ) + template = Template(UPDATE_HEALTH_CHECK_RESPONSE) + return 200, headers, template.render(health_check=health_check) def not_implemented_response(self, request, full_url, headers): self.setup_class(request, full_url, headers) @@ -697,6 +718,12 @@ def reusable_delegation_set(self, request, full_url, headers): {{ health_check.to_xml() }} """ +UPDATE_HEALTH_CHECK_RESPONSE = """ + + {{ health_check.to_xml() }} + +""" + LIST_HEALTH_CHECKS_RESPONSE = """ diff --git a/moto/route53/urls.py b/moto/route53/urls.py index 386d90e92b9..a8679154d90 100644 --- a/moto/route53/urls.py +++ b/moto/route53/urls.py @@ -22,8 +22,8 @@ def tag_response2(*args, **kwargs): r"{0}/(?P[\d_-]+)/hostedzonesbyname": Route53().list_hosted_zones_by_name_response, r"{0}/(?P[\d_-]+)/hostedzonesbyvpc": Route53().list_hosted_zones_by_vpc_response, r"{0}/(?P[\d_-]+)/hostedzonecount": Route53().get_hosted_zone_count_response, - r"{0}/(?P[\d_-]+)/healthcheck$": Route53().health_check_response, - r"{0}/(?P[\d_-]+)/healthcheck/(?P[^/]+)$": Route53().get_or_delete_health_check_response, + r"{0}/(?P[\d_-]+)/healthcheck$": Route53().health_check_response1, + r"{0}/(?P[\d_-]+)/healthcheck/(?P[^/]+)$": Route53().health_check_response2, r"{0}/(?P[\d_-]+)/tags/healthcheck/(?P[^/]+)$": tag_response1, r"{0}/(?P[\d_-]+)/tags/hostedzone/(?P[^/]+)$": tag_response2, r"{0}/(?P[\d_-]+)/trafficpolicyinstances/*": Route53().not_implemented_response, diff --git a/tests/terraformtests/terraform-tests.success.txt b/tests/terraformtests/terraform-tests.success.txt index 45fa316af54..600def69064 100644 --- a/tests/terraformtests/terraform-tests.success.txt +++ b/tests/terraformtests/terraform-tests.success.txt @@ -179,6 +179,16 @@ route53: - TestAccRoute53ZoneDataSource_name - TestAccRoute53ZoneDataSource_tags - TestAccRoute53ZoneDataSource_vpc + - TestAccRoute53HealthCheck_basic + - TestAccRoute53HealthCheck_tags + - TestAccRoute53HealthCheck_withSearchString + - TestAccRoute53HealthCheck_withChildHealthChecks + - TestAccRoute53HealthCheck_withHealthCheckRegions + - TestAccRoute53HealthCheck_ip + - TestAccRoute53HealthCheck_ipv6 + - TestAccRoute53HealthCheck_withSNI + - TestAccRoute53HealthCheck_disabled + - TestAccRoute53HealthCheck_disappears s3: - TestAccS3BucketPolicy - TestAccS3BucketPublicAccessBlock