Add support for specifying unique hosted zone identifier

This commit is contained in:
Juho-Mikko Pellinen 2015-04-15 13:43:00 +03:00 committed by Matt Clay
parent 62b6cf85d3
commit 3f2d1e6bfa

View file

@ -35,6 +35,12 @@ options:
required: true required: true
default: null default: null
aliases: [] aliases: []
hosted_zone_id:
description:
- The Hosted Zone ID of the DNS zone to modify
required: false
default: null
aliases: []
record: record:
description: description:
- The full DNS record to create or delete - The full DNS record to create or delete
@ -208,6 +214,17 @@ EXAMPLES = '''
weight: 100 weight: 100
health_check: "d994b780-3150-49fd-9205-356abdd42e75" health_check: "d994b780-3150-49fd-9205-356abdd42e75"
# Add an AAAA record with Hosted Zone ID. Note that because there are colons in the value
# that the entire parameter list must be quoted:
- route53:
command: "create"
zone: "foo.com"
hostes_zone_id: "Z2AABBCCDDEEFF"
record: "localhost.foo.com"
type: "AAAA"
ttl: "7200"
value: "::1"
''' '''
import time import time
@ -251,6 +268,7 @@ def main():
argument_spec.update(dict( argument_spec.update(dict(
command = dict(choices=['get', 'create', 'delete'], required=True), command = dict(choices=['get', 'create', 'delete'], required=True),
zone = dict(required=True), zone = dict(required=True),
hosted_zone_id = dict(required=False),
record = dict(required=True), record = dict(required=True),
ttl = dict(required=False, default=3600), ttl = dict(required=False, default=3600),
type = dict(choices=['A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS'], required=True), type = dict(choices=['A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS'], required=True),
@ -274,6 +292,7 @@ def main():
command_in = module.params.get('command') command_in = module.params.get('command')
zone_in = module.params.get('zone').lower() zone_in = module.params.get('zone').lower()
hosted_zone_id_in = module.params.get('hosted_zone_id')
ttl_in = int(module.params.get('ttl')) ttl_in = int(module.params.get('ttl'))
record_in = module.params.get('record').lower() record_in = module.params.get('record').lower()
type_in = module.params.get('type') type_in = module.params.get('type')
@ -318,11 +337,26 @@ def main():
except boto.exception.BotoServerError, e: except boto.exception.BotoServerError, e:
module.fail_json(msg = e.error_message) module.fail_json(msg = e.error_message)
# Find the named zone ID # Get all the existing hosted zones and save their ID's
zone = get_zone_by_name(conn, module, zone_in, private_zone_in) zones = {}
results = conn.get_all_hosted_zones()
for r53zone in results['ListHostedZonesResponse']['HostedZones']:
# only save this zone id if the private status of the zone matches
# the private_zone_in boolean specified in the params
if module.boolean(r53zone['Config'].get('PrivateZone', False)) == private_zone_in:
zone_id = r53zone['Id'].replace('/hostedzone/', '')
# only save when unique hosted_zone_id is given and is equal
# hosted_zone_id_in is specified in the params
if hosted_zone_id_in and zone_id == hosted_zone_id_in:
zones[r53zone['Name']] = zone_id
elif not hosted_zone_id_in:
zones[r53zone['Name']] = zone_id
# Verify that the requested zone is already defined in Route53 # Verify that the requested zone is already defined in Route53
if zone is None: if not zone_in in zones and hosted_zone_id_in:
errmsg = "Hosted_zone_id %s does not exist in Route53" % hosted_zone_id_in
module.fail_json(msg = errmsg)
if not zone_in in zones:
errmsg = "Zone %s does not exist in Route53" % zone_in errmsg = "Zone %s does not exist in Route53" % zone_in
module.fail_json(msg = errmsg) module.fail_json(msg = errmsg)
@ -358,6 +392,8 @@ def main():
record['region'] = rset.region record['region'] = rset.region
record['failover'] = rset.failover record['failover'] = rset.failover
record['health_check'] = rset.health_check record['health_check'] = rset.health_check
if hosted_zone_id_in:
record['hosted_zone_id'] = hosted_zone_id_in
if rset.alias_dns_name: if rset.alias_dns_name:
record['alias'] = True record['alias'] = True
record['value'] = rset.alias_dns_name record['value'] = rset.alias_dns_name