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,6 +195,7 @@ def create_or_update_private(module, client, matching_zones, record):
if record['vpc_id'] == current_vpc_id and record['vpc_region'] == current_vpc_region: if record['vpc_id'] == current_vpc_id and record['vpc_region'] == current_vpc_region:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '') record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']: if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']:
if not module.check_mode:
try: try:
client.update_hosted_zone_comment(Id=zone_details['Id'], Comment=record['comment']) client.update_hosted_zone_comment(Id=zone_details['Id'], Comment=record['comment'])
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -205,6 +206,7 @@ def create_or_update_private(module, client, matching_zones, record):
you chose. Unable to create a new private hosted zone in the same name space." you chose. Unable to create a new private hosted zone in the same name space."
return False, record return False, record
if not module.check_mode:
try: try:
result = client.create_hosted_zone( result = client.create_hosted_zone(
Name=record['name'], Name=record['name'],
@ -220,9 +222,11 @@ def create_or_update_private(module, client, matching_zones, record):
) )
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone") module.fail_json_aws(e, msg="Could not create hosted zone")
hosted_zone = result['HostedZone'] hosted_zone = result['HostedZone']
zone_id = hosted_zone['Id'].replace('/hostedzone/', '') zone_id = hosted_zone['Id'].replace('/hostedzone/', '')
record['zone_id'] = zone_id record['zone_id'] = zone_id
changed = True changed = True
return changed, record return changed, record
@ -235,6 +239,7 @@ def create_or_update_public(module, client, matching_zones, record):
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not get details about hosted zone %s" % matching_zone['Id']) 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']: if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']:
if not module.check_mode:
try: try:
client.update_hosted_zone_comment( client.update_hosted_zone_comment(
Id=zone_details['Id'], Id=zone_details['Id'],
@ -248,6 +253,7 @@ def create_or_update_public(module, client, matching_zones, record):
break break
if zone_details is None: if zone_details is None:
if not module.check_mode:
try: try:
result = client.create_hosted_zone( result = client.create_hosted_zone(
Name=record['name'], Name=record['name'],
@ -257,11 +263,12 @@ def create_or_update_public(module, client, matching_zones, record):
}, },
CallerReference="%s-%s" % (record['name'], time.time()) CallerReference="%s-%s" % (record['name'], time.time())
) )
zone_details = result['HostedZone']
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg="Could not create hosted zone") module.fail_json_aws(e, msg="Could not create hosted zone")
zone_details = result['HostedZone']
changed = True changed = True
if not module.check_mode:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '') record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
record['name'] = zone_details['Name'] record['name'] = zone_details['Name']
@ -278,6 +285,7 @@ def delete_private(module, client, matching_zones, vpc_id, vpc_region):
vpc_details = result['VPCs'] vpc_details = result['VPCs']
if isinstance(vpc_details, dict): if isinstance(vpc_details, dict):
if vpc_details['VPC']['VPCId'] == vpc_id and vpc_region == vpc_details['VPC']['VPCRegion']: if vpc_details['VPC']['VPCId'] == vpc_id and vpc_region == vpc_details['VPC']['VPCRegion']:
if not module.check_mode:
try: try:
client.delete_hosted_zone(Id=z['Id']) client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -285,6 +293,7 @@ def delete_private(module, client, matching_zones, vpc_id, vpc_region):
return True, "Successfully deleted %s" % zone_details['Name'] return True, "Successfully deleted %s" % zone_details['Name']
else: else:
if vpc_id in [v['VPCId'] for v in vpc_details] and vpc_region in [v['VPCRegion'] for v in vpc_details]: if vpc_id in [v['VPCId'] for v in vpc_details] and vpc_region in [v['VPCRegion'] for v in vpc_details]:
if not module.check_mode:
try: try:
client.delete_hosted_zone(Id=z['Id']) client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -299,6 +308,7 @@ def delete_public(module, client, matching_zones):
changed = False changed = False
msg = "There are multiple zones that match. Use hosted_zone_id to specify the correct zone." msg = "There are multiple zones that match. Use hosted_zone_id to specify the correct zone."
else: else:
if not module.check_mode:
try: try:
client.delete_hosted_zone(Id=matching_zones[0]['Id']) client.delete_hosted_zone(Id=matching_zones[0]['Id'])
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -313,6 +323,7 @@ def delete_hosted_id(module, client, hosted_zone_id, matching_zones):
deleted = [] deleted = []
for z in matching_zones: for z in matching_zones:
deleted.append(z['Id']) deleted.append(z['Id'])
if not module.check_mode:
try: try:
client.delete_hosted_zone(Id=z['Id']) client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -320,6 +331,7 @@ def delete_hosted_id(module, client, hosted_zone_id, matching_zones):
changed = True changed = True
msg = "Successfully deleted zones: %s" % deleted msg = "Successfully deleted zones: %s" % deleted
elif hosted_zone_id in [zo['Id'].replace('/hostedzone/', '') for zo in matching_zones]: elif hosted_zone_id in [zo['Id'].replace('/hostedzone/', '') for zo in matching_zones]:
if not module.check_mode:
try: try:
client.delete_hosted_zone(Id=hosted_zone_id) client.delete_hosted_zone(Id=hosted_zone_id)
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
@ -367,7 +379,7 @@ def main():
vpc_region=dict(default=None), vpc_region=dict(default=None),
comment=dict(default=''), comment=dict(default=''),
hosted_zone_id=dict())) 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() zone_in = module.params.get('zone').lower()
state = module.params.get('state').lower() state = module.params.get('state').lower()

View file

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