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:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
if 'Comment' in zone_details['Config'] and zone_details['Config']['Comment'] != record['comment']:
if not module.check_mode:
try:
client.update_hosted_zone_comment(Id=zone_details['Id'], Comment=record['comment'])
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."
return False, record
if not module.check_mode:
try:
result = client.create_hosted_zone(
Name=record['name'],
@ -220,9 +222,11 @@ def create_or_update_private(module, client, matching_zones, record):
)
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,6 +239,7 @@ 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']:
if not module.check_mode:
try:
client.update_hosted_zone_comment(
Id=zone_details['Id'],
@ -248,6 +253,7 @@ def create_or_update_public(module, client, matching_zones, record):
break
if zone_details is None:
if not module.check_mode:
try:
result = client.create_hosted_zone(
Name=record['name'],
@ -257,11 +263,12 @@ def create_or_update_public(module, client, matching_zones, record):
},
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")
zone_details = result['HostedZone']
changed = True
if not module.check_mode:
record['zone_id'] = zone_details['Id'].replace('/hostedzone/', '')
record['name'] = zone_details['Name']
@ -278,6 +285,7 @@ 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']:
if not module.check_mode:
try:
client.delete_hosted_zone(Id=z['Id'])
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']
else:
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:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
@ -299,6 +308,7 @@ 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:
if not module.check_mode:
try:
client.delete_hosted_zone(Id=matching_zones[0]['Id'])
except (BotoCoreError, ClientError) as e:
@ -313,6 +323,7 @@ def delete_hosted_id(module, client, hosted_zone_id, matching_zones):
deleted = []
for z in matching_zones:
deleted.append(z['Id'])
if not module.check_mode:
try:
client.delete_hosted_zone(Id=z['Id'])
except (BotoCoreError, ClientError) as e:
@ -320,6 +331,7 @@ def delete_hosted_id(module, client, hosted_zone_id, matching_zones):
changed = True
msg = "Successfully deleted zones: %s" % deleted
elif hosted_zone_id in [zo['Id'].replace('/hostedzone/', '') for zo in matching_zones]:
if not module.check_mode:
try:
client.delete_hosted_zone(Id=hosted_zone_id)
except (BotoCoreError, ClientError) as e:
@ -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"