Route53: Implement GetHealthCheckStatus (#6460)

This commit is contained in:
Viren Nadkarni 2023-07-03 15:33:48 +05:30 committed by GitHub
parent 4f091efcce
commit db002cb958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 3 deletions

View File

@ -5776,7 +5776,7 @@
- [X] get_health_check
- [ ] get_health_check_count
- [ ] get_health_check_last_failure_reason
- [ ] get_health_check_status
- [X] get_health_check_status
- [X] get_hosted_zone
- [X] get_hosted_zone_count
- [ ] get_hosted_zone_limit
@ -7444,4 +7444,4 @@
- workspaces
- workspaces-web
- xray
</details>
</details>

View File

@ -65,7 +65,7 @@ route53
- [X] get_health_check
- [ ] get_health_check_count
- [ ] get_health_check_last_failure_reason
- [ ] get_health_check_status
- [X] get_health_check_status
- [X] get_hosted_zone
- [X] get_hosted_zone_count
- [ ] get_hosted_zone_limit

View File

@ -1,4 +1,6 @@
"""Handles Route53 API requests, invokes method and returns response."""
import datetime
import re
from urllib.parse import parse_qs, urlparse
from jinja2 import Template
@ -7,6 +9,7 @@ import xmltodict
from moto.core.common_types import TYPE_RESPONSE
from moto.core.responses import BaseResponse
from moto.core.utils import iso_8601_datetime_with_milliseconds
from moto.route53.exceptions import InvalidChangeBatch
from moto.route53.models import route53_backends, Route53Backend
@ -342,6 +345,31 @@ class Route53(BaseResponse):
template = Template(UPDATE_HEALTH_CHECK_RESPONSE)
return 200, headers, template.render(health_check=health_check)
def health_check_status_response(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return]
self.setup_class(request, full_url, headers)
parsed_url = urlparse(full_url)
method = request.method
health_check_id = re.search(
r"healthcheck/(?P<health_check_id>[^/]+)/status$", parsed_url.path
).group( # type: ignore[union-attr]
"health_check_id"
)
if method == "GET":
self.backend.get_health_check(health_check_id)
template = Template(GET_HEALTH_CHECK_STATUS_RESPONSE)
return (
200,
headers,
template.render(
timestamp=iso_8601_datetime_with_milliseconds(
datetime.datetime.utcnow()
),
),
)
def not_implemented_response(
self, request: Any, full_url: str, headers: Any
) -> TYPE_RESPONSE:
@ -846,6 +874,21 @@ GET_HEALTH_CHECK_RESPONSE = """<?xml version="1.0"?>
</GetHealthCheckResponse>
"""
GET_HEALTH_CHECK_STATUS_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<GetHealthCheckStatusResponse>
<HealthCheckObservations>
<HealthCheckObservation>
<IPAddress>127.0.13.37</IPAddress>
<Region>us-east-1</Region>
<StatusReport>
<CheckedTime>{{ timestamp }}</CheckedTime>
<Status>Success: HTTP Status Code: 200. Resolved IP: 127.0.13.37. OK</Status>
</StatusReport>
</HealthCheckObservation>
</HealthCheckObservations>
</GetHealthCheckStatusResponse>
"""
UPDATE_HOSTED_ZONE_COMMENT_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<UpdateHostedZoneCommentResponse>
<HostedZone>

View File

@ -32,6 +32,7 @@ url_paths = {
r"{0}/(?P<api_version>[\d_-]+)/hostedzonecount": Route53().get_hosted_zone_count_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_-]+)/healthcheck/(?P<health_check_id>[^/]+)/status$": Route53().health_check_status_response,
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,

View File

@ -296,3 +296,36 @@ def test_update_health_check():
config.should.have.key("Disabled").equals(False)
config.should.have.key("ChildHealthChecks").equals(["child"])
config.should.have.key("Regions").equals(["us-east-1", "us-east-2", "us-west-1"])
@mock_route53
def test_health_check_status():
client = boto3.client("route53", region_name="us-east-1")
hc_id = client.create_health_check(
CallerReference="callref",
HealthCheckConfig={
"Type": "CALCULATED",
"Inverted": False,
"Disabled": False,
"HealthThreshold": 1,
},
)["HealthCheck"]["Id"]
resp = client.get_health_check_status(HealthCheckId=hc_id)
resp["HealthCheckObservations"].should.have.length_of(1)
observation = resp["HealthCheckObservations"][0]
observation.should.have.key("Region").being.equals("us-east-1")
observation.should.have.key("IPAddress").being.equals("127.0.13.37")
observation.should.have.key("StatusReport")
observation["StatusReport"].should.have.key("Status").being.equals(
"Success: HTTP Status Code: 200. Resolved IP: 127.0.13.37. OK"
)
with pytest.raises(ClientError) as exc:
client.get_health_check_status(HealthCheckId="bad-id")
err = exc.value.response["Error"]
err["Code"].should.equal("NoSuchHealthCheck")
err["Message"].should.equal("A health check with id bad-id does not exist.")