route53_zone: enable check mode (#37201)

This commit is contained in:
Julien Vey 2018-03-12 19:43:43 +01:00 committed by Sloane Hertel
parent d54675b44b
commit 099d8f0b56
2 changed files with 244 additions and 65 deletions

View file

@ -195,34 +195,38 @@ def create_or_update_private(module, client, matching_zones, record):
if record['vpc_id'] == current_vpc_id and record['vpc_region'] == current_vpc_region:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']:
try:
client.update_hosted_zone_comment(Id=zone_details['Id'], Comment=record['comment'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not update comment for hosted zone %s" % zone_details['Id'])
if not module.check_mode:
try:
client.update_hosted_zone_comment(Id=zone_details['Id'], Comment=record['comment'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not update comment for hosted zone %s" % zone_details['Id'])
return True, record
else:
record['msg'] = "There is already a private hosted zone in the same region with the same VPC \
you chose. Unable to create a new private hosted zone in the same name space."
return False, record
try:
result = client.create_hosted_zone(
Name=record['name'],
HostedZoneConfig={
'Comment': record['comment'] if record['comment'] is not None else "",
'PrivateZone': True,
},
VPC={
'VPCRegion': record['vpc_region'],
'VPCId': record['vpc_id'],
},
CallerReference="%s-%s" % (record['name'], time.time()),
)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone")
hosted_zone = result['HostedZone']
zone_id = hosted_zone['Id'].replace('/hostedzone/', '')
record['zone_id'] = zone_id
if not module.check_mode:
try:
result = client.create_hosted_zone(
Name=record['name'],
HostedZoneConfig={
'Comment': record['comment'] if record['comment'] is not None else "",
'PrivateZone': True,
},
VPC={
'VPCRegion': record['vpc_region'],
'VPCId': record['vpc_id'],
},
CallerReference="%s-%s" % (record['name'], time.time()),
)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone")
hosted_zone = result['HostedZone']
zone_id = hosted_zone['Id'].replace('/hostedzone/', '')
record['zone_id'] = zone_id
changed = True
return changed, record
@ -235,35 +239,38 @@ def create_or_update_public(module, client, matching_zones, record):
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not get details about hosted zone %s" % matching_zone['Id'])
if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']:
try:
client.update_hosted_zone_comment(
Id=zone_details['Id'],
Comment=record['comment']
)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not update comment for hosted zone %s" % zone_details['Id'])
if not module.check_mode:
try:
client.update_hosted_zone_comment(
Id=zone_details['Id'],
Comment=record['comment']
)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not update comment for hosted zone %s" % zone_details['Id'])
changed = True
else:
changed = False
break
if zone_details is None:
try:
result = client.create_hosted_zone(
Name=record['name'],
HostedZoneConfig={
'Comment': record['comment'] if record['comment'] is not None else "",
'PrivateZone': False,
},
CallerReference="%s-%s" % (record['name'], time.time())
)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone")
zone_details = result['HostedZone']
if not module.check_mode:
try:
result = client.create_hosted_zone(
Name=record['name'],
HostedZoneConfig={
'Comment': record['comment'] if record['comment'] is not None else "",
'PrivateZone': False,
},
CallerReference="%s-%s" % (record['name'], time.time())
)
zone_details = result['HostedZone']
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone")
changed = True
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
record['name'] = zone_details['Name']
if not module.check_mode:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
record['name'] = zone_details['Name']
return changed, record
@ -278,17 +285,19 @@ def delete_private(module, client, matching_zones, vpc_id, vpc_region):
vpc_details = result['VPCs']
if isinstance(vpc_details, dict):
if vpc_details['VPC']['VPCId'] == vpc_id and vpc_region == vpc_details['VPC']['VPCRegion']:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
if not module.check_mode:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
return True, "Successfully deleted %s" % zone_details['Name']
else:
if vpc_id in [v['VPCId'] for v in vpc_details] and vpc_region in [v['VPCRegion'] for v in vpc_details]:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
if not module.check_mode:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
return True, "Successfully deleted %s" % zone_details['Name']
return False, "The vpc_id and the vpc_region do not match a private hosted zone."
@ -299,10 +308,11 @@ def delete_public(module, client, matching_zones):
changed = False
msg = "There are multiple zones that match. Use hosted_zone_id to specify the correct zone."
else:
try:
client.delete_hosted_zone(Id=matching_zones[0]['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not get delete hosted zone %s" % matching_zones[0]['Id'])
if not module.check_mode:
try:
client.delete_hosted_zone(Id=matching_zones[0]['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not get delete hosted zone %s" % matching_zones[0]['Id'])
changed = True
msg = "Successfully deleted %s" % matching_zones[0]['Id']
return changed, msg
@ -313,17 +323,19 @@ def delete_hosted_id(module, client, hosted_zone_id, matching_zones):
deleted = []
for z in matching_zones:
deleted.append(z['Id'])
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
if not module.check_mode:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % z['Id'])
changed = True
msg = "Successfully deleted zones: %s" % deleted
elif hosted_zone_id in [zo['Id'].replace('/hostedzone/', '') for zo in matching_zones]:
try:
client.delete_hosted_zone(Id=hosted_zone_id)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % hosted_zone_id)
if not module.check_mode:
try:
client.delete_hosted_zone(Id=hosted_zone_id)
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not delete hosted zone %s" % hosted_zone_id)
changed = True
msg = "Successfully deleted zone: %s" % hosted_zone_id
else:
@ -367,7 +379,7 @@ def main():
vpc_region=dict(default=None),
comment=dict(default=''),
hosted_zone_id=dict()))
module = AnsibleAWSModule(argument_spec=argument_spec)
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
zone_in = module.params.get('zone').lower()
state = module.params.get('state').lower()

View file

@ -37,6 +37,23 @@
- output.name == '{{ resource_prefix }}.public.'
- not output.private_zone
# ============================================================
- name: Create a public zone (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.check.public"
comment: original comment
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- output.comment == 'original comment'
- output.name == '{{ resource_prefix }}.check.public.'
- not output.private_zone
# ============================================================
- name: Do an idemptotent update of a public zone
route53_zone:
@ -53,6 +70,22 @@
- output.name == '{{ resource_prefix }}.public.'
- not output.private_zone
- name: Do an idemptotent update of a public zone (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.public"
comment: original comment
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- not output.changed
- output.comment == 'original comment'
- output.name == '{{ resource_prefix }}.public.'
- not output.private_zone
# ============================================================
- name: Update comment of a public zone
route53_zone:
@ -67,19 +100,62 @@
- output.changed
- output.result.comment == "updated comment"
- name: Update comment of a public zone (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.public"
comment: updated comment for check
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- output.result.comment == "updated comment for check"
# ============================================================
- name: Delete public zone (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.public"
state: absent
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- "'Successfully deleted' in output.result"
- name: Delete public zone
route53_zone:
zone: "{{ resource_prefix }}.public"
state: absent
<<: *aws_connection_info
register: output
- assert:
that:
- output.changed
- "'Successfully deleted' in output.result"
# ============================================================
- name: Create a private zone (CHECK MODE)
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
vpc_region: "{{ aws_region }}"
zone: "{{ resource_prefix }}.private"
comment: original comment
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- name: Create a private zone
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
@ -88,8 +164,11 @@
comment: original comment
state: present
<<: *aws_connection_info
register: output
- assert:
that:
- output.changed
# ============================================================
- name: Idemptotent update a private zone
route53_zone:
@ -106,6 +185,22 @@
- not output.changed
- "'There is already a private hosted zone in the same region with the same VPC' in output.msg"
- name: Idemptotent update a private zone (CHECK MODE)
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
vpc_region: "{{ aws_region }}"
zone: "{{ resource_prefix }}.private"
comment: original comment
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- not output.changed
- "'There is already a private hosted zone in the same region with the same VPC' in output.msg"
# ============================================================
- name: Update private zone comment
route53_zone:
@ -122,6 +217,22 @@
- output.changed
- output.result.comment == "updated_comment"
- name: Update private zone comment (CHECK MODE)
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
vpc_region: "{{ aws_region }}"
zone: "{{ resource_prefix }}.private"
comment: updated_comment check
state: present
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- output.result.comment == "updated_comment check"
# ============================================================
- name: Try to delete private zone without setting vpc_id and vpc_region
route53_zone:
@ -135,6 +246,19 @@
- not output.changed
- "output.result == 'No zone to delete.'"
- name: Try to delete private zone without setting vpc_id and vpc_region (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.private"
state: absent
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- not output.changed
- "output.result == 'No zone to delete.'"
# ============================================================
- name: Try to delete a public zone that does not exists
route53_zone:
@ -149,7 +273,36 @@
- not output.changed
- "output.result == 'No zone to delete.'"
- name: Try to delete a public zone that does not exists (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.publicfake"
comment: original comment
state: absent
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- not output.changed
- "output.result == 'No zone to delete.'"
# ============================================================
- name: Delete private zone (CHECK MODE)
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
vpc_region: "{{ aws_region }}"
zone: "{{ resource_prefix }}.private"
state: absent
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- "'Successfully deleted' in output.result"
- name: Delete private zone
route53_zone:
vpc_id: "{{ testing_vpc.vpc.id }}"
@ -174,6 +327,20 @@
register: new_zone
# Delete zone using its id
- name: Delete zone using attribute hosted_zone_id (CHECK MODE)
route53_zone:
zone: "{{ resource_prefix }}.public2"
hosted_zone_id: "{{new_zone.zone_id}}"
state: absent
<<: *aws_connection_info
register: output
check_mode: yes
- assert:
that:
- output.changed
- "'Successfully deleted' in output.result"
- name: Delete zone using attribute hosted_zone_id
route53_zone:
zone: "{{ resource_prefix }}.public2"