Skip to content

Commit

Permalink
fixed tf tests: TestAccRoute53HealthCheck (getmoto#5241)
Browse files Browse the repository at this point in the history
  • Loading branch information
macnev2013 authored and sahilshah6196 committed Jul 1, 2022
1 parent 338c966 commit c8389f1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
62 changes: 59 additions & 3 deletions moto/route53/models.py
Expand Up @@ -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):
Expand Down Expand Up @@ -135,10 +148,17 @@ def to_xml(self):
{% if health_check.children %}
<ChildHealthChecks>
{% for child in health_check.children %}
<member>{{ child }}</member>
<ChildHealthCheck>{{ child }}</ChildHealthCheck>
{% endfor %}
</ChildHealthChecks>
{% endif %}
{% if health_check.regions %}
<Regions>
{% for region in health_check.regions %}
<Region>{{ region }}</Region>
{% endfor %}
</Regions>
{% endif %}
</HealthCheckConfig>
<HealthCheckVersion>1</HealthCheckVersion>
</HealthCheck>"""
Expand Down Expand Up @@ -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()

Expand Down
35 changes: 31 additions & 4 deletions moto/route53/responses.py
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -697,6 +718,12 @@ def reusable_delegation_set(self, request, full_url, headers):
{{ health_check.to_xml() }}
</CreateHealthCheckResponse>"""

UPDATE_HEALTH_CHECK_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<UpdateHealthCheckResponse>
{{ health_check.to_xml() }}
</UpdateHealthCheckResponse>
"""

LIST_HEALTH_CHECKS_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<ListHealthChecksResponse xmlns="{{ xmlns }}">
<HealthChecks>
Expand Down
4 changes: 2 additions & 2 deletions moto/route53/urls.py
Expand Up @@ -22,8 +22,8 @@ def tag_response2(*args, **kwargs):
r"{0}/(?P<api_version>[\d_-]+)/hostedzonesbyname": Route53().list_hosted_zones_by_name_response,
r"{0}/(?P<api_version>[\d_-]+)/hostedzonesbyvpc": Route53().list_hosted_zones_by_vpc_response,
r"{0}/(?P<api_version>[\d_-]+)/hostedzonecount": Route53().get_hosted_zone_count_response,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck$": Route53().health_check_response,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck/(?P<health_check_id>[^/]+)$": Route53().get_or_delete_health_check_response,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck$": Route53().health_check_response1,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck/(?P<health_check_id>[^/]+)$": Route53().health_check_response2,
r"{0}/(?P<api_version>[\d_-]+)/tags/healthcheck/(?P<zone_id>[^/]+)$": tag_response1,
r"{0}/(?P<api_version>[\d_-]+)/tags/hostedzone/(?P<zone_id>[^/]+)$": tag_response2,
r"{0}/(?P<api_version>[\d_-]+)/trafficpolicyinstances/*": Route53().not_implemented_response,
Expand Down
10 changes: 10 additions & 0 deletions tests/terraformtests/terraform-tests.success.txt
Expand Up @@ -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
Expand Down

0 comments on commit c8389f1

Please sign in to comment.