Skip to content

Commit

Permalink
Implement check_mode logic and add integration tets
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Oct 3, 2024
1 parent 5d33bd9 commit eb1dd50
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 28 deletions.
53 changes: 26 additions & 27 deletions plugins/modules/ec2_vpc_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- Support for O(purge_tags) was added in release 2.0.0.
author:
- Mike Mochan (@mmochan)
- Alina Buzachis (@alinabuzachis)
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
Expand Down Expand Up @@ -475,6 +476,9 @@ def create_peering_connection(client, module: AnsibleAWSModule) -> Tuple[bool, D
if module.params.get("tags"):
params["TagSpecifications"] = boto3_tag_specifications(module.params["tags"], types="vpc-peering-connection")

if module.check_mode:
return (True, {"VpcPeeringConnectionId": ""})

try:
peering_connection = create_vpc_peering_connection(client, **params)
if module.params.get("wait"):
Expand Down Expand Up @@ -515,12 +519,13 @@ def delete_peering_connection(client, module: AnsibleAWSModule) -> NoReturn:
peering_id=peering_id,
)

try:
delete_vpc_peering_connection(client, peering_id)
if module.params.get("wait"):
wait_for_state(client, module, "deleted", peering_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)
if not module.check_mode:
try:
delete_vpc_peering_connection(client, peering_id)
if module.params.get("wait"):
wait_for_state(client, module, "deleted", peering_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

module.exit_json(changed=True, peering_id=peering_id)

Expand All @@ -545,26 +550,20 @@ def accept_reject_peering_connection(client, module: AnsibleAWSModule, state: st
vpc_peering_connection = get_peering_connection_by_id(client, module, peering_id)

if not (is_active(vpc_peering_connection) or is_rejected(vpc_peering_connection)):
try:
if state == "accept":
changed |= accept_vpc_peering_connection(client, peering_id)
target_state = "active"
else:
changed |= reject_vpc_peering_connection(client, peering_id)
target_state = "rejected"

if module.params.get("tags"):
changed |= add_ec2_tags(
client,
module,
peering_id,
module.params["tags"],
)

if module.params.get("wait"):
wait_for_state(client, module, target_state, peering_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)
if not module.check_mode:
try:
if state == "accept":
changed |= accept_vpc_peering_connection(client, peering_id)
target_state = "active"
else:
changed |= reject_vpc_peering_connection(client, peering_id)
target_state = "rejected"

if module.params.get("wait"):
wait_for_state(client, module, target_state, peering_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)
changed = True

changed |= ensure_ec2_tags(
client,
Expand Down Expand Up @@ -598,7 +597,7 @@ def main():
("state", "reject", ["peering_id"]),
]

module = AnsibleAWSModule(argument_spec=argument_spec, required_if=required_if)
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True, required_if=required_if)

state = module.params.get("state")
peering_id = module.params.get("peering_id")
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/ec2_vpc_peering_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
default: {}
author:
- Karen Cheng (@Etherdaemon)
- Alina Buzachis (@alinabuzachis)
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
Expand Down
128 changes: 127 additions & 1 deletion tests/integration/targets/ec2_vpc_peer/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@
ansible.builtin.set_fact:
connection_name: 'Peering connection for VPC {{ vpc_1 }} to VPC {{ vpc_2 }}'

- name: Create local account EC2 VPC Peering Connection request (check_mode)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
peer_vpc_id: '{{ vpc_2 }}'
state: present
tags:
Name: '{{ connection_name }}'
check_mode: true
register: vpc_peer

- name: Assert success
assert:
that:
- vpc_peer is changed

- name: Create local account EC2 VPC Peering Connection request
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
Expand All @@ -91,6 +106,21 @@
ansible.builtin.set_fact:
peer_id_1: '{{ vpc_peer.peering_id }}'

- name: Re-create local account EC2 VPC Peering Connection request (idempotency check_mode)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
peer_vpc_id: '{{ vpc_2 }}'
state: present
tags:
Name: '{{ connection_name }}'
check_mode: true
register: vpc_peer

- name: Assert success
assert:
that:
- vpc_peer is not changed

- name: Re-create local account EC2 VPC Peering Connection request (idempotency)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
Expand All @@ -107,6 +137,21 @@
- vpc_peer is successful
- vpc_peer.peering_id == peer_id_1

- name: Create local account EC2 VPC Peering Connection request with accepter/requester reversed (idempotency check_mode)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_2 }}'
peer_vpc_id: '{{ vpc_1 }}'
state: present
tags:
Name: '{{ connection_name }}'
check_mode: true
register: vpc_peer

- name: Assert success
assert:
that:
- vpc_peer is not changed

- name: Create local account EC2 VPC Peering Connection request with accepter/requester reversed (idempotency)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_2 }}'
Expand Down Expand Up @@ -216,6 +261,22 @@
acceptor_details: '{{ peer_details["accepter_vpc_info"] }}'
requester_details: '{{ peer_details["requester_vpc_info"] }}'

- name: Update tags on the EC2 VPC Peering Connection (check_mode)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
peer_vpc_id: '{{ vpc_2 }}'
state: present
tags:
Name: '{{ connection_name }}'
testPrefix: '{{ resource_prefix }}'
check_mode: true
register: tag_peer

- name: Assert success
assert:
that:
- tag_peer is changed

- name: Update tags on the EC2 VPC Peering Connection
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
Expand All @@ -233,6 +294,22 @@
- tag_peer is successful
- tag_peer.peering_id == peer_id_1

- name: Update tags on the EC2 VPC Peering Connection (idempotency check_mode)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
peer_vpc_id: '{{ vpc_2 }}'
state: present
tags:
Name: '{{ connection_name }}'
testPrefix: '{{ resource_prefix }}'
check_mode: true
register: tag_peer

- name: Assert success
assert:
that:
- tag_peer is not changed

- name: Update tags on the EC2 VPC Peering Connection (idempotency)
community.aws.ec2_vpc_peer:
vpc_id: '{{ vpc_1 }}'
Expand Down Expand Up @@ -268,11 +345,24 @@
vars:
peer_details: '{{ peer_info.vpc_peering_connections[0] }}'

- name: Accept local EC2 VPC Peering request (check_mode)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: accept
wait: true
check_mode: true
register: action_peer

- name: Assert success
assert:
that:
- action_peer is changed

- name: Accept local EC2 VPC Peering request
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: accept
wait: True
wait: true
register: action_peer

- name: Assert success
Expand Down Expand Up @@ -342,6 +432,18 @@
acceptor_details: '{{ peer_details["accepter_vpc_info"] }}'
requester_details: '{{ peer_details["requester_vpc_info"] }}'

- name: Accept local EC2 VPC Peering request (idempotency check_mode)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: accept
check_mode: true
register: action_peer

- name: Assert success
assert:
that:
- action_peer is not changed

- name: Accept local EC2 VPC Peering request (idempotency)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
Expand All @@ -356,6 +458,18 @@
- action_peer.peering_id == peer_id_1
- action_peer.vpc_peering_connection.vpc_peering_connection_id == peer_id_1

- name: Delete a local EC2 VPC Peering Connection (check_mode)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: absent
check_mode: true
register: delete_peer

- name: Assert success
assert:
that:
- delete_peer is changed

- name: Delete a local EC2 VPC Peering Connection
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
Expand Down Expand Up @@ -419,6 +533,18 @@
acceptor_details: '{{ peer_details["accepter_vpc_info"] }}'
requester_details: '{{ peer_details["requester_vpc_info"] }}'

- name: Delete a local EC2 VPC Peering Connection (idempotency check_mode)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: absent
check_mode: true
register: delete_peer

- name: Assert success
assert:
that:
- delete_peer is not changed

- name: Delete a local EC2 VPC Peering Connection (idempotency)
community.aws.ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
Expand Down

0 comments on commit eb1dd50

Please sign in to comment.