| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: ec2_vpc_peering |
| 9 |
short_description: create, delete, accept, and reject VPC peering connections between two VPCs. |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
description: |
| 13 |
- Read the AWS documentation for VPC Peering Connections |
| 14 |
U(https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-peering.html). |
| 15 |
options: |
| 16 |
vpc_id: |
| 17 |
description: |
| 18 |
- VPC id of the requesting VPC. |
| 19 |
required: false |
| 20 |
type: str |
| 21 |
peering_id: |
| 22 |
description: |
| 23 |
- Peering connection id. |
| 24 |
required: false |
| 25 |
type: str |
| 26 |
peer_region: |
| 27 |
description: |
| 28 |
- Region of the accepting VPC. |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
peer_vpc_id: |
| 32 |
description: |
| 33 |
- VPC id of the accepting VPC. |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
peer_owner_id: |
| 37 |
description: |
| 38 |
- The AWS account number for cross account peering. |
| 39 |
required: false |
| 40 |
type: str |
| 41 |
state: |
| 42 |
description: |
| 43 |
- Create, delete, accept, reject a peering connection. |
| 44 |
required: false |
| 45 |
default: present |
| 46 |
choices: ['present', 'absent', 'accept', 'reject'] |
| 47 |
type: str |
| 48 |
wait: |
| 49 |
description: |
| 50 |
- Wait for peering state changes to complete. |
| 51 |
required: false |
| 52 |
default: false |
| 53 |
type: bool |
| 54 |
notes: |
| 55 |
- Support for O(purge_tags) was added in release 2.0.0. |
| 56 |
author: |
| 57 |
- Mike Mochan (@mmochan) |
| 58 |
- Alina Buzachis (@alinabuzachis) |
| 59 |
extends_documentation_fragment: |
| 60 |
- amazon.aws.common.modules |
| 61 |
- amazon.aws.region.modules |
| 62 |
- amazon.aws.tags |
| 63 |
- amazon.aws.boto3 |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
# Complete example to create and accept a local peering connection. |
| 68 |
- name: Create local account EC2 VPC Peering Connection |
| 69 |
amazon.aws.ec2_vpc_peering: |
| 70 |
region: "ap-southeast-2" |
| 71 |
vpc_id: "vpc-12345678" |
| 72 |
peer_vpc_id: "vpc-87654321" |
| 73 |
state: "present" |
| 74 |
tags: |
| 75 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 76 |
CostCode: "CC1234" |
| 77 |
Project: "phoenix" |
| 78 |
register: vpc_peer |
| 79 |
|
| 80 |
- name: Accept local EC2 VPC Peering request |
| 81 |
amazon.aws.ec2_vpc_peering: |
| 82 |
region: "ap-southeast-2" |
| 83 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 84 |
state: "accept" |
| 85 |
register: action_peer |
| 86 |
|
| 87 |
# Complete example to delete a local peering connection. |
| 88 |
- name: Create local account EC2 VPC Peering Connection |
| 89 |
amazon.aws.ec2_vpc_peering: |
| 90 |
region: "ap-southeast-2" |
| 91 |
vpc_id: "vpc-12345678" |
| 92 |
peer_vpc_id: "vpc-87654321" |
| 93 |
state: "present" |
| 94 |
tags: |
| 95 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 96 |
CostCode: "CC1234" |
| 97 |
Project: "phoenix" |
| 98 |
register: vpc_peer |
| 99 |
|
| 100 |
- name: Delete a local EC2 VPC Peering Connection |
| 101 |
amazon.aws.ec2_vpc_peering: |
| 102 |
region: "ap-southeast-2" |
| 103 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 104 |
state: "absent" |
| 105 |
register: vpc_peer |
| 106 |
|
| 107 |
# Complete example to create and accept a cross account peering connection. |
| 108 |
- name: Create cross account EC2 VPC Peering Connection |
| 109 |
amazon.aws.ec2_vpc_peering: |
| 110 |
region: "ap-southeast-2" |
| 111 |
vpc_id: "vpc-12345678" |
| 112 |
peer_vpc_id: "vpc-12345678" |
| 113 |
peer_owner_id: "123456789012" |
| 114 |
state: "present" |
| 115 |
tags: |
| 116 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 117 |
CostCode: "CC1234" |
| 118 |
Project: "phoenix" |
| 119 |
register: vpc_peer |
| 120 |
|
| 121 |
- name: Accept EC2 VPC Peering Connection from remote account |
| 122 |
amazon.aws.ec2_vpc_peering: |
| 123 |
region: "ap-southeast-2" |
| 124 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 125 |
profile: "bot03_profile_for_cross_account" |
| 126 |
state: "accept" |
| 127 |
register: vpc_peer |
| 128 |
|
| 129 |
# Complete example to create and accept an intra-region peering connection. |
| 130 |
- name: Create intra-region EC2 VPC Peering Connection |
| 131 |
amazon.aws.ec2_vpc_peering: |
| 132 |
region: "us-east-1" |
| 133 |
vpc_id: "vpc-12345678" |
| 134 |
peer_vpc_id: "vpc-87654321" |
| 135 |
peer_region: "us-west-2" |
| 136 |
state: "present" |
| 137 |
tags: |
| 138 |
Name: "Peering connection for us-east-1 VPC to us-west-2 VPC" |
| 139 |
CostCode: "CC1234" |
| 140 |
Project: "phoenix" |
| 141 |
register: vpc_peer |
| 142 |
|
| 143 |
- name: Accept EC2 VPC Peering Connection from peer region |
| 144 |
amazon.aws.ec2_vpc_peer: |
| 145 |
region: "us-west-2" |
| 146 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 147 |
state: "accept" |
| 148 |
register: vpc_peer |
| 149 |
|
| 150 |
# Complete example to create and reject a local peering connection. |
| 151 |
- name: Create local account EC2 VPC Peering Connection |
| 152 |
amazon.aws.ec2_vpc_peering: |
| 153 |
region: "ap-southeast-2" |
| 154 |
vpc_id: "vpc-12345678" |
| 155 |
peer_vpc_id: "vpc-87654321" |
| 156 |
state: "present" |
| 157 |
tags: |
| 158 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 159 |
CostCode: "CC1234" |
| 160 |
Project: "phoenix" |
| 161 |
register: vpc_peer |
| 162 |
|
| 163 |
- name: Reject a local EC2 VPC Peering Connection |
| 164 |
amazon.aws.ec2_vpc_peering: |
| 165 |
region: "ap-southeast-2" |
| 166 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 167 |
state: "reject" |
| 168 |
|
| 169 |
# Complete example to create and accept a cross account peering connection. |
| 170 |
- name: Create cross account EC2 VPC Peering Connection |
| 171 |
amazon.aws.ec2_vpc_peering: |
| 172 |
region: "ap-southeast-2" |
| 173 |
vpc_id: "vpc-12345678" |
| 174 |
peer_vpc_id: "vpc-12345678" |
| 175 |
peer_owner_id: "123456789012" |
| 176 |
state: "present" |
| 177 |
tags: |
| 178 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 179 |
CostCode: "CC1234" |
| 180 |
Project: "phoenix" |
| 181 |
register: vpc_peer |
| 182 |
|
| 183 |
- name: Accept a cross account EC2 VPC Peering Connection request |
| 184 |
amazon.aws.ec2_vpc_peering: |
| 185 |
region: "ap-southeast-2" |
| 186 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 187 |
profile: "bot03_profile_for_cross_account" |
| 188 |
state: "accept" |
| 189 |
tags: |
| 190 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 191 |
CostCode: "CC1234" |
| 192 |
Project: "phoenix" |
| 193 |
|
| 194 |
# Complete example to create and reject a cross account peering connection. |
| 195 |
- name: Create cross account EC2 VPC Peering Connection |
| 196 |
amazon.aws.ec2_vpc_peering: |
| 197 |
region: "ap-southeast-2" |
| 198 |
vpc_id: "vpc-12345678" |
| 199 |
peer_vpc_id: "vpc-12345678" |
| 200 |
peer_owner_id: "123456789012" |
| 201 |
state: "present" |
| 202 |
tags: |
| 203 |
Name: "Peering connection for VPC 21 to VPC 22" |
| 204 |
CostCode: "CC1234" |
| 205 |
Project: "phoenix" |
| 206 |
register: vpc_peer |
| 207 |
|
| 208 |
- name: Reject a cross account EC2 VPC Peering Connection |
| 209 |
amazon.aws.ec2_vpc_peeriing: |
| 210 |
region: "ap-southeast-2" |
| 211 |
peering_id: "{{ vpc_peer.peering_id }}" |
| 212 |
profile: "bot03_profile_for_cross_account" |
| 213 |
state: "reject" |
| 214 |
""" |
| 215 |
|
| 216 |
RETURN = r""" |
| 217 |
peering_id: |
| 218 |
description: The id of the VPC peering connection created/deleted. |
| 219 |
returned: always |
| 220 |
type: str |
| 221 |
sample: "pcx-034223d7c0aec3cde" |
| 222 |
vpc_peering_connection: |
| 223 |
description: The details of the VPC peering connection. |
| 224 |
returned: success |
| 225 |
type: dict |
| 226 |
contains: |
| 227 |
accepter_vpc_info: |
| 228 |
description: Information about the VPC which accepted the connection. |
| 229 |
returned: success |
| 230 |
type: dict |
| 231 |
contains: |
| 232 |
cidr_block: |
| 233 |
description: The primary CIDR for the VPC. |
| 234 |
returned: when connection is in the accepted state. |
| 235 |
type: str |
| 236 |
sample: "10.10.10.0/23" |
| 237 |
cidr_block_set: |
| 238 |
description: A list of all CIDRs for the VPC. |
| 239 |
returned: when connection is in the accepted state. |
| 240 |
type: list |
| 241 |
elements: dict |
| 242 |
contains: |
| 243 |
cidr_block: |
| 244 |
description: A CIDR block used by the VPC. |
| 245 |
returned: success |
| 246 |
type: str |
| 247 |
sample: "10.10.10.0/23" |
| 248 |
owner_id: |
| 249 |
description: The AWS account that owns the VPC. |
| 250 |
returned: success |
| 251 |
type: str |
| 252 |
sample: "123456789012" |
| 253 |
peering_options: |
| 254 |
description: Additional peering configuration. |
| 255 |
returned: when connection is in the accepted state. |
| 256 |
type: dict |
| 257 |
contains: |
| 258 |
allow_dns_resolution_from_remote_vpc: |
| 259 |
description: Indicates whether a VPC can resolve public DNS hostnames to private IP addresses when queried from instances in a peer VPC. |
| 260 |
returned: success |
| 261 |
type: bool |
| 262 |
allow_egress_from_local_classic_link_to_remote_vpc: |
| 263 |
description: Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering connection. |
| 264 |
returned: success |
| 265 |
type: bool |
| 266 |
allow_egress_from_local_vpc_to_remote_classic_link: |
| 267 |
description: Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering connection. |
| 268 |
returned: success |
| 269 |
type: bool |
| 270 |
region: |
| 271 |
description: The AWS region that the VPC is in. |
| 272 |
returned: success |
| 273 |
type: str |
| 274 |
sample: "us-east-1" |
| 275 |
vpc_id: |
| 276 |
description: The ID of the VPC |
| 277 |
returned: success |
| 278 |
type: str |
| 279 |
sample: "vpc-0123456789abcdef0" |
| 280 |
requester_vpc_info: |
| 281 |
description: Information about the VPC which requested the connection. |
| 282 |
returned: success |
| 283 |
type: dict |
| 284 |
contains: |
| 285 |
cidr_block: |
| 286 |
description: The primary CIDR for the VPC. |
| 287 |
returned: when connection is not in the deleted state. |
| 288 |
type: str |
| 289 |
sample: "10.10.10.0/23" |
| 290 |
cidr_block_set: |
| 291 |
description: A list of all CIDRs for the VPC. |
| 292 |
returned: when connection is not in the deleted state. |
| 293 |
type: list |
| 294 |
elements: dict |
| 295 |
contains: |
| 296 |
cidr_block: |
| 297 |
description: A CIDR block used by the VPC |
| 298 |
returned: success |
| 299 |
type: str |
| 300 |
sample: "10.10.10.0/23" |
| 301 |
owner_id: |
| 302 |
description: The AWS account that owns the VPC. |
| 303 |
returned: success |
| 304 |
type: str |
| 305 |
sample: "123456789012" |
| 306 |
peering_options: |
| 307 |
description: Additional peering configuration. |
| 308 |
returned: when connection is not in the deleted state. |
| 309 |
type: dict |
| 310 |
contains: |
| 311 |
allow_dns_resolution_from_remote_vpc: |
| 312 |
description: Indicates whether a VPC can resolve public DNS hostnames to private IP addresses when queried from instances in a peer VPC. |
| 313 |
returned: success |
| 314 |
type: bool |
| 315 |
allow_egress_from_local_classic_link_to_remote_vpc: |
| 316 |
description: Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering connection. |
| 317 |
returned: success |
| 318 |
type: bool |
| 319 |
allow_egress_from_local_vpc_to_remote_classic_link: |
| 320 |
description: Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering connection. |
| 321 |
returned: success |
| 322 |
type: bool |
| 323 |
region: |
| 324 |
description: The AWS region that the VPC is in. |
| 325 |
returned: success |
| 326 |
type: str |
| 327 |
sample: "us-east-1" |
| 328 |
vpc_id: |
| 329 |
description: The ID of the VPC |
| 330 |
returned: success |
| 331 |
type: str |
| 332 |
sample: "vpc-0123456789abcdef0" |
| 333 |
status: |
| 334 |
description: Details of the current status of the connection. |
| 335 |
returned: success |
| 336 |
type: complex |
| 337 |
contains: |
| 338 |
code: |
| 339 |
description: A short code describing the status of the connection. |
| 340 |
returned: success |
| 341 |
type: str |
| 342 |
sample: "active" |
| 343 |
message: |
| 344 |
description: Additional information about the status of the connection. |
| 345 |
returned: success |
| 346 |
type: str |
| 347 |
sample: "Pending Acceptance by 123456789012" |
| 348 |
tags: |
| 349 |
description: Tags applied to the connection. |
| 350 |
returned: success |
| 351 |
type: dict |
| 352 |
expiration_time: |
| 353 |
description: The time that an unaccepted VPC peering connection will expire. |
| 354 |
type: str |
| 355 |
sample: "2024-10-01T12:11:12+00:00" |
| 356 |
vpc_peering_connection_id: |
| 357 |
description: The ID of the VPC peering connection. |
| 358 |
returned: success |
| 359 |
type: str |
| 360 |
sample: "pcx-0123456789abcdef0" |
| 361 |
""" |
| 362 |
|
| 363 |
try: |
| 364 |
import botocore |
| 365 |
except ImportError: |
| 366 |
pass # Handled by AnsibleAWSModule |
| 367 |
|
| 368 |
from typing import Any |
| 369 |
from typing import Dict |
| 370 |
from typing import NoReturn |
| 371 |
from typing import Tuple |
| 372 |
|
| 373 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 374 |
|
| 375 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 376 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import accept_vpc_peering_connection |
| 377 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_vpc_peering_connection |
| 378 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_vpc_peering_connection |
| 379 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_peering_connections |
| 380 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 381 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import reject_vpc_peering_connection |
| 382 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 383 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 384 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications |
| 385 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 386 |
|
| 387 |
|
| 388 |
def wait_for_state(client, module: AnsibleAWSModule, state: str, peering_id: str) -> NoReturn: |
| 389 |
waiter = client.get_waiter("vpc_peering_connection_exists") |
| 390 |
filters = { |
| 391 |
"vpc-peering-connection-id": peering_id, |
| 392 |
"status-code": state, |
| 393 |
} |
| 394 |
try: |
| 395 |
waiter.wait(Filters=ansible_dict_to_boto3_filter_list(filters)) |
| 396 |
except botocore.exceptions.WaiterError as e: |
| 397 |
module.fail_json_aws(e, "Failed to wait for state change") |
| 398 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 399 |
module.fail_json_aws(e, "Unable to describe Peering Connection while waiting for state to change") |
| 400 |
|
| 401 |
|
| 402 |
def describe_peering_connections(client, module: AnsibleAWSModule, params) -> Dict[str, Any]: |
| 403 |
peering_connections: Dict = {} |
| 404 |
|
| 405 |
filters = { |
| 406 |
"requester-vpc-info.vpc-id": params["VpcId"], |
| 407 |
"accepter-vpc-info.vpc-id": params["PeerVpcId"], |
| 408 |
} |
| 409 |
|
| 410 |
peering_connections = describe_vpc_peering_connections(client, Filters=ansible_dict_to_boto3_filter_list(filters)) |
| 411 |
if peering_connections == []: |
| 412 |
# Try again with the VPC/Peer relationship reversed |
| 413 |
filters = { |
| 414 |
"requester-vpc-info.vpc-id": params["PeerVpcId"], |
| 415 |
"accepter-vpc-info.vpc-id": params["VpcId"], |
| 416 |
} |
| 417 |
peering_connections = describe_vpc_peering_connections( |
| 418 |
client, Filters=ansible_dict_to_boto3_filter_list(filters) |
| 419 |
) |
| 420 |
|
| 421 |
return peering_connections |
| 422 |
|
| 423 |
|
| 424 |
def is_active(peering_connection: Dict[str, Any]) -> bool: |
| 425 |
return peering_connection["Status"]["Code"] == "active" |
| 426 |
|
| 427 |
|
| 428 |
def is_rejected(peering_connection: Dict[str, Any]) -> bool: |
| 429 |
return peering_connection["Status"]["Code"] == "rejected" |
| 430 |
|
| 431 |
|
| 432 |
def is_pending(peering_connection: Dict[str, Any]) -> bool: |
| 433 |
return peering_connection["Status"]["Code"] == "pending-acceptance" |
| 434 |
|
| 435 |
|
| 436 |
def is_deleted(peering_connection: Dict[str, Any]) -> bool: |
| 437 |
return peering_connection["Status"]["Code"] == "deleted" |
| 438 |
|
| 439 |
|
| 440 |
def create_peering_connection(client, module: AnsibleAWSModule) -> Tuple[bool, Dict[str, Any]]: |
| 441 |
changed: bool = False |
| 442 |
params: Dict = {} |
| 443 |
|
| 444 |
params["VpcId"] = module.params.get("vpc_id") |
| 445 |
params["PeerVpcId"] = module.params.get("peer_vpc_id") |
| 446 |
|
| 447 |
if module.params.get("peer_region"): |
| 448 |
params["PeerRegion"] = module.params["peer_region"] |
| 449 |
|
| 450 |
if module.params.get("peer_owner_id"): |
| 451 |
params["PeerOwnerId"] = module.params["peer_owner_id"] |
| 452 |
|
| 453 |
peering_connections = describe_peering_connections(client, module, params) |
| 454 |
for peering_connection in peering_connections: |
| 455 |
changed |= ensure_ec2_tags( |
| 456 |
client, |
| 457 |
module, |
| 458 |
peering_connection["VpcPeeringConnectionId"], |
| 459 |
purge_tags=module.params.get("purge_tags"), |
| 460 |
tags=module.params.get("tags"), |
| 461 |
) |
| 462 |
|
| 463 |
if is_active(peering_connection): |
| 464 |
return (changed, peering_connection) |
| 465 |
|
| 466 |
if is_pending(peering_connection): |
| 467 |
return (changed, peering_connection) |
| 468 |
|
| 469 |
if module.params.get("tags"): |
| 470 |
params["TagSpecifications"] = boto3_tag_specifications(module.params["tags"], types="vpc-peering-connection") |
| 471 |
|
| 472 |
if module.check_mode: |
| 473 |
return (True, {"VpcPeeringConnectionId": ""}) |
| 474 |
|
| 475 |
peering_connection = create_vpc_peering_connection(client, **params) |
| 476 |
if module.params.get("wait"): |
| 477 |
wait_for_state(client, module, "pending-acceptance", peering_connection["VpcPeeringConnectionId"]) |
| 478 |
changed = True |
| 479 |
return (changed, peering_connection) |
| 480 |
|
| 481 |
|
| 482 |
def delete_peering_connection(client, module: AnsibleAWSModule) -> NoReturn: |
| 483 |
peering_id = module.params.get("peering_id") |
| 484 |
if peering_id: |
| 485 |
peering_connection = get_peering_connection_by_id(client, module, peering_id) |
| 486 |
else: |
| 487 |
params: Dict = {} |
| 488 |
params["VpcId"] = module.params.get("vpc_id") |
| 489 |
params["PeerVpcId"] = module.params.get("peer_vpc_id") |
| 490 |
params["PeerRegion"] = module.params.get("peer_region") |
| 491 |
|
| 492 |
if module.params.get("peer_owner_id"): |
| 493 |
params["PeerOwnerId"] = module.params["peer_owner_id"] |
| 494 |
|
| 495 |
peering_connection = describe_peering_connections(client, module, params)[0] |
| 496 |
|
| 497 |
if not peering_connection: |
| 498 |
module.exit_json(changed=False) |
| 499 |
else: |
| 500 |
peering_id = peering_id or peering_connection["VpcPeeringConnectionId"] |
| 501 |
|
| 502 |
if is_deleted(peering_connection): |
| 503 |
module.exit_json(msg="Connection in deleted state.", changed=False, peering_id=peering_id) |
| 504 |
|
| 505 |
if is_rejected(peering_connection): |
| 506 |
module.exit_json( |
| 507 |
msg="Connection has been rejected. State cannot be changed and will be removed automatically by AWS", |
| 508 |
changed=False, |
| 509 |
peering_id=peering_id, |
| 510 |
) |
| 511 |
|
| 512 |
if not module.check_mode: |
| 513 |
delete_vpc_peering_connection(client, peering_id) |
| 514 |
if module.params.get("wait"): |
| 515 |
wait_for_state(client, module, "deleted", peering_id) |
| 516 |
|
| 517 |
module.exit_json(changed=True, peering_id=peering_id) |
| 518 |
|
| 519 |
|
| 520 |
def get_peering_connection_by_id(client, module: AnsibleAWSModule, peering_id: str) -> Dict[str, Any]: |
| 521 |
filters: Dict = {} |
| 522 |
filters["VpcPeeringConnectionIds"] = [peering_id] |
| 523 |
|
| 524 |
try: |
| 525 |
result = describe_vpc_peering_connections(client, VpcPeeringConnectionIds=[peering_id]) |
| 526 |
return result[0] |
| 527 |
except is_boto3_error_code("InvalidVpcPeeringConnectionId.Malformed") as e: |
| 528 |
module.fail_json_aws(e, msg="Malformed connection ID") |
| 529 |
|
| 530 |
|
| 531 |
def accept_reject_peering_connection(client, module: AnsibleAWSModule, state: str) -> Tuple[bool, Dict[str, Any]]: |
| 532 |
changed: bool = False |
| 533 |
|
| 534 |
peering_id = module.params.get("peering_id") |
| 535 |
vpc_peering_connection = get_peering_connection_by_id(client, module, peering_id) |
| 536 |
|
| 537 |
if not (is_active(vpc_peering_connection) or is_rejected(vpc_peering_connection)): |
| 538 |
if not module.check_mode: |
| 539 |
if state == "accept": |
| 540 |
changed |= accept_vpc_peering_connection(client, peering_id) |
| 541 |
target_state = "active" |
| 542 |
else: |
| 543 |
changed |= reject_vpc_peering_connection(client, peering_id) |
| 544 |
target_state = "rejected" |
| 545 |
|
| 546 |
if module.params.get("wait"): |
| 547 |
wait_for_state(client, module, target_state, peering_id) |
| 548 |
|
| 549 |
changed = True |
| 550 |
|
| 551 |
changed |= ensure_ec2_tags( |
| 552 |
client, |
| 553 |
module, |
| 554 |
peering_id, |
| 555 |
purge_tags=module.params.get("purge_tags"), |
| 556 |
tags=module.params.get("tags"), |
| 557 |
) |
| 558 |
|
| 559 |
# Reload peering conection info to return latest state/params |
| 560 |
vpc_peering_connection = get_peering_connection_by_id(client, module, peering_id) |
| 561 |
|
| 562 |
return (changed, vpc_peering_connection) |
| 563 |
|
| 564 |
|
| 565 |
def main(): |
| 566 |
argument_spec = dict( |
| 567 |
vpc_id=dict(type="str"), |
| 568 |
peer_vpc_id=dict(type="str"), |
| 569 |
peer_region=dict(type="str"), |
| 570 |
peering_id=dict(type="str"), |
| 571 |
peer_owner_id=dict(type="str"), |
| 572 |
tags=dict(required=False, type="dict", aliases=["resource_tags"]), |
| 573 |
purge_tags=dict(default=True, type="bool"), |
| 574 |
state=dict(default="present", type="str", choices=["present", "absent", "accept", "reject"]), |
| 575 |
wait=dict(default=False, type="bool"), |
| 576 |
) |
| 577 |
required_if = [ |
| 578 |
("state", "present", ["vpc_id", "peer_vpc_id"]), |
| 579 |
("state", "accept", ["peering_id"]), |
| 580 |
("state", "reject", ["peering_id"]), |
| 581 |
] |
| 582 |
|
| 583 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True, required_if=required_if) |
| 584 |
|
| 585 |
state = module.params.get("state") |
| 586 |
peering_id = module.params.get("peering_id") |
| 587 |
vpc_id = module.params.get("vpc_id") |
| 588 |
peer_vpc_id = module.params.get("peer_vpc_id") |
| 589 |
|
| 590 |
client = module.client("ec2") |
| 591 |
|
| 592 |
if state == "present": |
| 593 |
(changed, results) = create_peering_connection(client, module) |
| 594 |
elif state == "absent": |
| 595 |
if not peering_id and (not vpc_id or not peer_vpc_id): |
| 596 |
module.fail_json( |
| 597 |
msg="state is absent but one of the following is missing: peering_id or [vpc_id, peer_vpc_id]" |
| 598 |
) |
| 599 |
|
| 600 |
delete_peering_connection(client, module) |
| 601 |
else: |
| 602 |
(changed, results) = accept_reject_peering_connection(client, module, state) |
| 603 |
|
| 604 |
formatted_results = camel_dict_to_snake_dict(results) |
| 605 |
# Turn the resource tags from boto3 into an ansible friendly tag dictionary |
| 606 |
formatted_results["tags"] = boto3_tag_list_to_ansible_dict(formatted_results.get("tags", [])) |
| 607 |
|
| 608 |
module.exit_json( |
| 609 |
changed=changed, vpc_peering_connection=formatted_results, peering_id=results["VpcPeeringConnectionId"] |
| 610 |
) |
| 611 |
|
| 612 |
|
| 613 |
if __name__ == "__main__": |
| 614 |
main() |