Skip to content

Commit

Permalink
add check_mode and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Sep 20, 2024
1 parent 6f91509 commit d4b9146
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
31 changes: 17 additions & 14 deletions plugins/modules/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def ensure_present(
)

if domain_name:
changed, update_reverse_dns_record_result = update_reverse_dns_record_of_eip(client, module, address["AllocationId"], domain_name)
changed, update_reverse_dns_record_result = update_reverse_dns_record_of_eip(client, module, address, domain_name)
result.update({"update_reverse_dns_record_result": update_reverse_dns_record_result})

# Associate address to instance
Expand Down Expand Up @@ -529,21 +529,24 @@ def ensure_present(
return result


def update_reverse_dns_record_of_eip(client, module: AnsibleAWSModule, allocation_id, domain_name):
def update_reverse_dns_record_of_eip(client, module: AnsibleAWSModule, address, domain_name):
changed = False

try:
update_reverse_dns_record_result = client.modify_address_attribute(
AllocationId=allocation_id, DomainName=domain_name
)
changed = True
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

if "ResponseMetadata" in update_reverse_dns_record_result:
del update_reverse_dns_record_result["ResponseMetadata"]

return changed, camel_dict_to_snake_dict(update_reverse_dns_record_result)
if module.check_mode:
return True, {}
else:
try:
update_reverse_dns_record_result = client.modify_address_attribute(
AllocationId=address["AllocationId"], DomainName=domain_name
)
changed = True
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

if "ResponseMetadata" in update_reverse_dns_record_result:
del update_reverse_dns_record_result["ResponseMetadata"]

return changed, camel_dict_to_snake_dict(update_reverse_dns_record_result)


def main():
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_eip/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ eip_test_tags:
AnsibleEIPTestPrefix: "{{ resource_prefix }}"
eip_info_filters:
tag:AnsibleEIPTestPrefix: "{{ resource_prefix }}"
test_domain: test-xyz.com
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_eip/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- ansible.builtin.include_tasks: tasks/release.yml
- ansible.builtin.include_tasks: tasks/attach_detach_to_eni.yml
- ansible.builtin.include_tasks: tasks/attach_detach_to_instance.yml
- ansible.builtin.include_tasks: tasks/update_reverse_dns_record.yml

always:
- ansible.builtin.include_tasks: tasks/teardown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- name: Test EIP allocation
block:
# ------------------------------------------------------------------------------------------
# Allocate EIP with reverse DNS record
# ------------------------------------------------------------------------------------------
- name: Allocate a new EIP and modify it's reverse DNS record - check_mode
amazon.aws.ec2_eip:
state: present
domain_name: "{{ test_domain }}"
tags: "{{ eip_test_tags }}"
register: eip
check_mode: true

- ansible.builtin.assert:
that:
- eip is changed

- name: Ensure no new EIP was created
ansible.builtin.include_tasks: tasks/common.yml
vars:
has_no_new_eip: true

- name: Allocate a new EIP and modify it's reverse DNS record
amazon.aws.ec2_eip:
state: present
domain_name: "{{ test_domain }}"
tags: "{{ eip_test_tags }}"
register: eip

- ansible.builtin.assert:
that:
- eip is changed
- eip.public_ip is defined and ( eip.public_ip | ansible.utils.ipaddr )
- eip.allocation_id is defined and eip.allocation_id.startswith("eipalloc-")
- eip.update_reverse_dns_record_result is defined
- eip.update_reverse_dns_record_result.address.ptr_record_update is defined
- eip.update_reverse_dns_record_result.address.ptr_record_update.value == "{{ test_domain }}"

always:
- name: Delete EIP
ansible.builtin.include_tasks: tasks/common.yml
vars:
delete_eips: true

0 comments on commit d4b9146

Please sign in to comment.