Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -15,11 +15,10 @@ ROUTE53_ID_CHOICE = string.ascii_uppercase + string.digits
|
|||
|
||||
def create_route53_zone_id():
|
||||
# New ID's look like this Z1RWWTK7Y8UDDQ
|
||||
return ''.join([random.choice(ROUTE53_ID_CHOICE) for _ in range(0, 15)])
|
||||
return "".join([random.choice(ROUTE53_ID_CHOICE) for _ in range(0, 15)])
|
||||
|
||||
|
||||
class HealthCheck(BaseModel):
|
||||
|
||||
def __init__(self, health_check_id, health_check_args):
|
||||
self.id = health_check_id
|
||||
self.ip_address = health_check_args.get("ip_address")
|
||||
|
|
@ -36,23 +35,26 @@ class HealthCheck(BaseModel):
|
|||
return self.id
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']['HealthCheckConfig']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]["HealthCheckConfig"]
|
||||
health_check_args = {
|
||||
"ip_address": properties.get('IPAddress'),
|
||||
"port": properties.get('Port'),
|
||||
"type": properties['Type'],
|
||||
"resource_path": properties.get('ResourcePath'),
|
||||
"fqdn": properties.get('FullyQualifiedDomainName'),
|
||||
"search_string": properties.get('SearchString'),
|
||||
"request_interval": properties.get('RequestInterval'),
|
||||
"failure_threshold": properties.get('FailureThreshold'),
|
||||
"ip_address": properties.get("IPAddress"),
|
||||
"port": properties.get("Port"),
|
||||
"type": properties["Type"],
|
||||
"resource_path": properties.get("ResourcePath"),
|
||||
"fqdn": properties.get("FullyQualifiedDomainName"),
|
||||
"search_string": properties.get("SearchString"),
|
||||
"request_interval": properties.get("RequestInterval"),
|
||||
"failure_threshold": properties.get("FailureThreshold"),
|
||||
}
|
||||
health_check = route53_backend.create_health_check(health_check_args)
|
||||
return health_check
|
||||
|
||||
def to_xml(self):
|
||||
template = Template("""<HealthCheck>
|
||||
template = Template(
|
||||
"""<HealthCheck>
|
||||
<Id>{{ health_check.id }}</Id>
|
||||
<CallerReference>example.com 192.0.2.17</CallerReference>
|
||||
<HealthCheckConfig>
|
||||
|
|
@ -68,59 +70,66 @@ class HealthCheck(BaseModel):
|
|||
{% endif %}
|
||||
</HealthCheckConfig>
|
||||
<HealthCheckVersion>1</HealthCheckVersion>
|
||||
</HealthCheck>""")
|
||||
</HealthCheck>"""
|
||||
)
|
||||
return template.render(health_check=self)
|
||||
|
||||
|
||||
class RecordSet(BaseModel):
|
||||
|
||||
def __init__(self, kwargs):
|
||||
self.name = kwargs.get('Name')
|
||||
self.type_ = kwargs.get('Type')
|
||||
self.ttl = kwargs.get('TTL')
|
||||
self.records = kwargs.get('ResourceRecords', [])
|
||||
self.set_identifier = kwargs.get('SetIdentifier')
|
||||
self.weight = kwargs.get('Weight')
|
||||
self.region = kwargs.get('Region')
|
||||
self.health_check = kwargs.get('HealthCheckId')
|
||||
self.hosted_zone_name = kwargs.get('HostedZoneName')
|
||||
self.hosted_zone_id = kwargs.get('HostedZoneId')
|
||||
self.alias_target = kwargs.get('AliasTarget')
|
||||
self.name = kwargs.get("Name")
|
||||
self.type_ = kwargs.get("Type")
|
||||
self.ttl = kwargs.get("TTL")
|
||||
self.records = kwargs.get("ResourceRecords", [])
|
||||
self.set_identifier = kwargs.get("SetIdentifier")
|
||||
self.weight = kwargs.get("Weight")
|
||||
self.region = kwargs.get("Region")
|
||||
self.health_check = kwargs.get("HealthCheckId")
|
||||
self.hosted_zone_name = kwargs.get("HostedZoneName")
|
||||
self.hosted_zone_id = kwargs.get("HostedZoneId")
|
||||
self.alias_target = kwargs.get("AliasTarget")
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]
|
||||
|
||||
zone_name = properties.get("HostedZoneName")
|
||||
if zone_name:
|
||||
hosted_zone = route53_backend.get_hosted_zone_by_name(zone_name)
|
||||
else:
|
||||
hosted_zone = route53_backend.get_hosted_zone(
|
||||
properties["HostedZoneId"])
|
||||
hosted_zone = route53_backend.get_hosted_zone(properties["HostedZoneId"])
|
||||
record_set = hosted_zone.add_rrset(properties)
|
||||
return record_set
|
||||
|
||||
@classmethod
|
||||
def update_from_cloudformation_json(cls, original_resource, new_resource_name, cloudformation_json, region_name):
|
||||
def update_from_cloudformation_json(
|
||||
cls, original_resource, new_resource_name, cloudformation_json, region_name
|
||||
):
|
||||
cls.delete_from_cloudformation_json(
|
||||
original_resource.name, cloudformation_json, region_name)
|
||||
return cls.create_from_cloudformation_json(new_resource_name, cloudformation_json, region_name)
|
||||
original_resource.name, cloudformation_json, region_name
|
||||
)
|
||||
return cls.create_from_cloudformation_json(
|
||||
new_resource_name, cloudformation_json, region_name
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def delete_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
def delete_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
# this will break if you changed the zone the record is in,
|
||||
# unfortunately
|
||||
properties = cloudformation_json['Properties']
|
||||
properties = cloudformation_json["Properties"]
|
||||
|
||||
zone_name = properties.get("HostedZoneName")
|
||||
if zone_name:
|
||||
hosted_zone = route53_backend.get_hosted_zone_by_name(zone_name)
|
||||
else:
|
||||
hosted_zone = route53_backend.get_hosted_zone(
|
||||
properties["HostedZoneId"])
|
||||
hosted_zone = route53_backend.get_hosted_zone(properties["HostedZoneId"])
|
||||
|
||||
try:
|
||||
hosted_zone.delete_rrset({'Name': resource_name})
|
||||
hosted_zone.delete_rrset({"Name": resource_name})
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
|
|
@ -129,7 +138,8 @@ class RecordSet(BaseModel):
|
|||
return self.name
|
||||
|
||||
def to_xml(self):
|
||||
template = Template("""<ResourceRecordSet>
|
||||
template = Template(
|
||||
"""<ResourceRecordSet>
|
||||
<Name>{{ record_set.name }}</Name>
|
||||
<Type>{{ record_set.type_ }}</Type>
|
||||
{% if record_set.set_identifier %}
|
||||
|
|
@ -162,26 +172,25 @@ class RecordSet(BaseModel):
|
|||
{% if record_set.health_check %}
|
||||
<HealthCheckId>{{ record_set.health_check }}</HealthCheckId>
|
||||
{% endif %}
|
||||
</ResourceRecordSet>""")
|
||||
</ResourceRecordSet>"""
|
||||
)
|
||||
return template.render(record_set=self)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
''' Not exposed as part of the Route 53 API - used for CloudFormation. args are ignored '''
|
||||
hosted_zone = route53_backend.get_hosted_zone_by_name(
|
||||
self.hosted_zone_name)
|
||||
""" Not exposed as part of the Route 53 API - used for CloudFormation. args are ignored """
|
||||
hosted_zone = route53_backend.get_hosted_zone_by_name(self.hosted_zone_name)
|
||||
if not hosted_zone:
|
||||
hosted_zone = route53_backend.get_hosted_zone(self.hosted_zone_id)
|
||||
hosted_zone.delete_rrset({'Name': self.name, 'Type': self.type_})
|
||||
hosted_zone.delete_rrset({"Name": self.name, "Type": self.type_})
|
||||
|
||||
|
||||
def reverse_domain_name(domain_name):
|
||||
if domain_name.endswith('.'): # normalize without trailing dot
|
||||
if domain_name.endswith("."): # normalize without trailing dot
|
||||
domain_name = domain_name[:-1]
|
||||
return '.'.join(reversed(domain_name.split('.')))
|
||||
return ".".join(reversed(domain_name.split(".")))
|
||||
|
||||
|
||||
class FakeZone(BaseModel):
|
||||
|
||||
def __init__(self, name, id_, private_zone, comment=None):
|
||||
self.name = name
|
||||
self.id = id_
|
||||
|
|
@ -198,7 +207,11 @@ class FakeZone(BaseModel):
|
|||
def upsert_rrset(self, record_set):
|
||||
new_rrset = RecordSet(record_set)
|
||||
for i, rrset in enumerate(self.rrsets):
|
||||
if rrset.name == new_rrset.name and rrset.type_ == new_rrset.type_ and rrset.set_identifier == new_rrset.set_identifier:
|
||||
if (
|
||||
rrset.name == new_rrset.name
|
||||
and rrset.type_ == new_rrset.type_
|
||||
and rrset.set_identifier == new_rrset.set_identifier
|
||||
):
|
||||
self.rrsets[i] = new_rrset
|
||||
break
|
||||
else:
|
||||
|
|
@ -209,13 +222,16 @@ class FakeZone(BaseModel):
|
|||
self.rrsets = [
|
||||
record_set
|
||||
for record_set in self.rrsets
|
||||
if record_set.name != rrset['Name'] or
|
||||
(rrset.get('Type') is not None and record_set.type_ != rrset['Type'])
|
||||
if record_set.name != rrset["Name"]
|
||||
or (rrset.get("Type") is not None and record_set.type_ != rrset["Type"])
|
||||
]
|
||||
|
||||
def delete_rrset_by_id(self, set_identifier):
|
||||
self.rrsets = [
|
||||
record_set for record_set in self.rrsets if record_set.set_identifier != set_identifier]
|
||||
record_set
|
||||
for record_set in self.rrsets
|
||||
if record_set.set_identifier != set_identifier
|
||||
]
|
||||
|
||||
def get_record_sets(self, start_type, start_name):
|
||||
record_sets = list(self.rrsets) # Copy the list
|
||||
|
|
@ -223,11 +239,15 @@ class FakeZone(BaseModel):
|
|||
record_sets = [
|
||||
record_set
|
||||
for record_set in record_sets
|
||||
if reverse_domain_name(record_set.name) >= reverse_domain_name(start_name)
|
||||
if reverse_domain_name(record_set.name)
|
||||
>= reverse_domain_name(start_name)
|
||||
]
|
||||
if start_type:
|
||||
record_sets = [
|
||||
record_set for record_set in record_sets if record_set.type_ >= start_type]
|
||||
record_set
|
||||
for record_set in record_sets
|
||||
if record_set.type_ >= start_type
|
||||
]
|
||||
|
||||
return record_sets
|
||||
|
||||
|
|
@ -236,17 +256,17 @@ class FakeZone(BaseModel):
|
|||
return self.id
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]
|
||||
name = properties["Name"]
|
||||
|
||||
hosted_zone = route53_backend.create_hosted_zone(
|
||||
name, private_zone=False)
|
||||
hosted_zone = route53_backend.create_hosted_zone(name, private_zone=False)
|
||||
return hosted_zone
|
||||
|
||||
|
||||
class RecordSetGroup(BaseModel):
|
||||
|
||||
def __init__(self, hosted_zone_id, record_sets):
|
||||
self.hosted_zone_id = hosted_zone_id
|
||||
self.record_sets = record_sets
|
||||
|
|
@ -256,8 +276,10 @@ class RecordSetGroup(BaseModel):
|
|||
return "arn:aws:route53:::hostedzone/{0}".format(self.hosted_zone_id)
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]
|
||||
|
||||
zone_name = properties.get("HostedZoneName")
|
||||
if zone_name:
|
||||
|
|
@ -273,7 +295,6 @@ class RecordSetGroup(BaseModel):
|
|||
|
||||
|
||||
class Route53Backend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
self.zones = {}
|
||||
self.health_checks = {}
|
||||
|
|
@ -281,26 +302,25 @@ class Route53Backend(BaseBackend):
|
|||
|
||||
def create_hosted_zone(self, name, private_zone, comment=None):
|
||||
new_id = create_route53_zone_id()
|
||||
new_zone = FakeZone(
|
||||
name, new_id, private_zone=private_zone, comment=comment)
|
||||
new_zone = FakeZone(name, new_id, private_zone=private_zone, comment=comment)
|
||||
self.zones[new_id] = new_zone
|
||||
return new_zone
|
||||
|
||||
def change_tags_for_resource(self, resource_id, tags):
|
||||
if 'Tag' in tags:
|
||||
if isinstance(tags['Tag'], list):
|
||||
for tag in tags['Tag']:
|
||||
self.resource_tags[resource_id][tag['Key']] = tag['Value']
|
||||
if "Tag" in tags:
|
||||
if isinstance(tags["Tag"], list):
|
||||
for tag in tags["Tag"]:
|
||||
self.resource_tags[resource_id][tag["Key"]] = tag["Value"]
|
||||
else:
|
||||
key, value = (tags['Tag']['Key'], tags['Tag']['Value'])
|
||||
key, value = (tags["Tag"]["Key"], tags["Tag"]["Value"])
|
||||
self.resource_tags[resource_id][key] = value
|
||||
else:
|
||||
if 'Key' in tags:
|
||||
if isinstance(tags['Key'], list):
|
||||
for key in tags['Key']:
|
||||
del(self.resource_tags[resource_id][key])
|
||||
if "Key" in tags:
|
||||
if isinstance(tags["Key"], list):
|
||||
for key in tags["Key"]:
|
||||
del self.resource_tags[resource_id][key]
|
||||
else:
|
||||
del(self.resource_tags[resource_id][tags['Key']])
|
||||
del self.resource_tags[resource_id][tags["Key"]]
|
||||
|
||||
def list_tags_for_resource(self, resource_id):
|
||||
if resource_id in self.resource_tags:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue