Skip to content

Commit

Permalink
ec2_vpc_peer - Fix idempotency when accepter/requester is reversed (a…
Browse files Browse the repository at this point in the history
…nsible-collections#1346)

ec2_vpc_peer - Fix idempotency when accepter/requester is reversed

SUMMARY
fixes: ansible-collections#580
Fixes a bug where a new peering request would be created when the accepter/requester is reversed
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
plugins/modules/ec2_vpc_peer.py
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@1c3ad28
  • Loading branch information
tremble authored and alinabuzachis committed Oct 11, 2024
1 parent f6d4fcd commit 7fee7a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
5 changes: 5 additions & 0 deletions plugins/modules/ec2_vpc_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ def describe_peering_connections(params, client):
Filters=ansible_dict_to_boto3_filter_list(peer_filter),
)
if result['VpcPeeringConnections'] == []:
# Try again with the VPC/Peer relationship reversed
peer_filter = {
'requester-vpc-info.vpc-id': params['PeerVpcId'],
'accepter-vpc-info.vpc-id': params['VpcId'],
}
result = client.describe_vpc_peering_connections(
aws_retry=True,
Filters=ansible_dict_to_boto3_filter_list(peer_filter),
Expand Down
43 changes: 34 additions & 9 deletions tests/integration/targets/ec2_vpc_peer/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@
- vpc_peer is successful
- vpc_peer.peering_id == peer_id_1

- name: (re-) Create local account VPC peering Connection request with accepter/requester reversed (idempotency)
ec2_vpc_peer:
vpc_id: '{{ vpc_2 }}'
peer_vpc_id: '{{ vpc_1 }}'
state: present
tags:
Name: '{{ connection_name }}'
register: vpc_peer

- name: Assert success
assert:
that:
- vpc_peer is not changed
- vpc_peer is successful
- vpc_peer.peering_id == peer_id_1

- name: Get details on specific VPC peer
ec2_vpc_peering_info:
peer_connection_ids:
Expand Down Expand Up @@ -458,21 +474,30 @@
- delete_peer is successful

always:
# ============================================================

- name: delete a local VPC peering Connection
ec2_vpc_peer:
peering_id: "{{ vpc_peer.peering_id }}"
state: absent
register: delete_peer
- name: Find all VPC Peering connections for our VPCs
ec2_vpc_peering_info:
filters:
accepter-vpc-info.vpc-id: '{{ item }}'
register: peering_info
loop:
- '{{ vpc_1 }}'
- '{{ vpc_2 }}'

- set_fact:
vpc_peering_connection_ids: '{{ _vpc_peering_connections | map(attribute="vpc_peering_connection_id") | list }}'
vars:
_vpc_peering_connections: '{{ peering_info.results | map(attribute="vpc_peering_connections") | flatten }}'
ignore_errors: True

- name: delete a local VPC peering Connection
# ============================================================

- name: Delete remaining Peering connections
ec2_vpc_peer:
peering_id: "{{ vpc_peer2.peering_id }}"
peering_id: "{{ item }}"
state: absent
register: delete_peer
ignore_errors: True
loop: '{{ vpc_peering_connection_ids }}'

- name: tidy up VPC 2
ec2_vpc_net:
Expand Down

0 comments on commit 7fee7a8

Please sign in to comment.