| 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 |
--- |
| 9 |
module: ec2_vpc_igw |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage an AWS VPC Internet gateway |
| 12 |
description: |
| 13 |
- Manage an AWS VPC Internet gateway. |
| 14 |
author: Robert Estelle (@erydo) |
| 15 |
options: |
| 16 |
internet_gateway_id: |
| 17 |
version_added: 7.0.0 |
| 18 |
description: |
| 19 |
- The ID of Internet Gateway to manage. |
| 20 |
required: false |
| 21 |
type: str |
| 22 |
vpc_id: |
| 23 |
description: |
| 24 |
- The VPC ID for the VPC to attach (when O(state=present)). |
| 25 |
- VPC ID can also be provided to find the internet gateway to manage that the VPC is attached to. |
| 26 |
required: false |
| 27 |
type: str |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Create or terminate the IGW. |
| 31 |
default: present |
| 32 |
choices: [ 'present', 'absent' ] |
| 33 |
type: str |
| 34 |
force_attach: |
| 35 |
version_added: 7.0.0 |
| 36 |
description: |
| 37 |
- Force attaching VPC to O(vpc_id). |
| 38 |
- Setting this option to true will detach an existing VPC attachment and attach to the supplied O(vpc_id). |
| 39 |
- Ignored when O(state=absent). |
| 40 |
- O(vpc_id) must be specified when O(force_attach=true). |
| 41 |
default: false |
| 42 |
type: bool |
| 43 |
detach_vpc: |
| 44 |
version_added: 7.0.0 |
| 45 |
description: |
| 46 |
- Remove attached VPC from gateway. |
| 47 |
default: false |
| 48 |
type: bool |
| 49 |
notes: |
| 50 |
- Support for O(purge_tags) was added in release 1.3.0. |
| 51 |
extends_documentation_fragment: |
| 52 |
- amazon.aws.common.modules |
| 53 |
- amazon.aws.region.modules |
| 54 |
- amazon.aws.tags |
| 55 |
- amazon.aws.boto3 |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 60 |
|
| 61 |
# Ensure that the VPC has an Internet Gateway. |
| 62 |
# The Internet Gateway ID is can be accessed via {{igw.gateway_id}} for use in setting up NATs etc. |
| 63 |
- name: Create Internet gateway |
| 64 |
amazon.aws.ec2_vpc_igw: |
| 65 |
vpc_id: vpc-abcdefgh |
| 66 |
state: present |
| 67 |
register: igw |
| 68 |
|
| 69 |
- name: Create Internet gateway with tags |
| 70 |
amazon.aws.ec2_vpc_igw: |
| 71 |
vpc_id: vpc-abcdefgh |
| 72 |
state: present |
| 73 |
tags: |
| 74 |
Tag1: tag1 |
| 75 |
Tag2: tag2 |
| 76 |
register: igw |
| 77 |
|
| 78 |
- name: Create a detached gateway |
| 79 |
amazon.aws.ec2_vpc_igw: |
| 80 |
state: present |
| 81 |
register: igw |
| 82 |
|
| 83 |
- name: Change the VPC the gateway is attached to |
| 84 |
amazon.aws.ec2_vpc_igw: |
| 85 |
internet_gateway_id: igw-abcdefgh |
| 86 |
vpc_id: vpc-stuvwxyz |
| 87 |
force_attach: true |
| 88 |
state: present |
| 89 |
register: igw |
| 90 |
|
| 91 |
- name: Delete Internet gateway using the attached vpc id |
| 92 |
amazon.aws.ec2_vpc_igw: |
| 93 |
state: absent |
| 94 |
vpc_id: vpc-abcdefgh |
| 95 |
register: vpc_igw_delete |
| 96 |
|
| 97 |
- name: Delete Internet gateway with gateway id |
| 98 |
amazon.aws.ec2_vpc_igw: |
| 99 |
state: absent |
| 100 |
internet_gateway_id: igw-abcdefgh |
| 101 |
register: vpc_igw_delete |
| 102 |
|
| 103 |
- name: Delete Internet gateway ensuring attached VPC is correct |
| 104 |
amazon.aws.ec2_vpc_igw: |
| 105 |
state: absent |
| 106 |
internet_gateway_id: igw-abcdefgh |
| 107 |
vpc_id: vpc-abcdefgh |
| 108 |
register: vpc_igw_delete |
| 109 |
""" |
| 110 |
|
| 111 |
RETURN = r""" |
| 112 |
changed: |
| 113 |
description: If any changes have been made to the Internet Gateway. |
| 114 |
type: bool |
| 115 |
returned: always |
| 116 |
sample: |
| 117 |
changed: false |
| 118 |
gateway_id: |
| 119 |
description: The unique identifier for the Internet Gateway. |
| 120 |
type: str |
| 121 |
returned: O(state=present) |
| 122 |
sample: |
| 123 |
gateway_id: "igw-XXXXXXXX" |
| 124 |
tags: |
| 125 |
description: The tags associated the Internet Gateway. |
| 126 |
type: dict |
| 127 |
returned: O(state=present) |
| 128 |
sample: |
| 129 |
tags: |
| 130 |
"Ansible": "Test" |
| 131 |
vpc_id: |
| 132 |
description: The VPC ID associated with the Internet Gateway. |
| 133 |
type: str |
| 134 |
returned: O(state=present) |
| 135 |
sample: |
| 136 |
vpc_id: "vpc-XXXXXXXX" |
| 137 |
""" |
| 138 |
|
| 139 |
from typing import Any |
| 140 |
from typing import Dict |
| 141 |
from typing import Optional |
| 142 |
|
| 143 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 144 |
|
| 145 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 146 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import attach_internet_gateway |
| 147 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_internet_gateway |
| 148 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_internet_gateway |
| 149 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_internet_gateways |
| 150 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpcs |
| 151 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import detach_internet_gateway |
| 152 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 153 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 154 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 155 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 156 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import wait_for_resource_state |
| 157 |
|
| 158 |
|
| 159 |
class AnsibleEc2Igw: |
| 160 |
def __init__(self, module: AnsibleAWSModule) -> None: |
| 161 |
self._module = module |
| 162 |
self._results = {"changed": False} |
| 163 |
self._connection = self._module.client("ec2") |
| 164 |
self._check_mode = self._module.check_mode |
| 165 |
|
| 166 |
def process(self) -> None: |
| 167 |
internet_gateway_id = self._module.params.get("internet_gateway_id") |
| 168 |
vpc_id = self._module.params.get("vpc_id") |
| 169 |
state = self._module.params.get("state", "present") |
| 170 |
tags = self._module.params.get("tags") |
| 171 |
purge_tags = self._module.params.get("purge_tags") |
| 172 |
force_attach = self._module.params.get("force_attach") |
| 173 |
detach_vpc = self._module.params.get("detach_vpc") |
| 174 |
|
| 175 |
if state == "present": |
| 176 |
self.ensure_igw_present(internet_gateway_id, vpc_id, tags, purge_tags, force_attach, detach_vpc) |
| 177 |
elif state == "absent": |
| 178 |
self.ensure_igw_absent(internet_gateway_id, vpc_id) |
| 179 |
self._module.exit_json(**self._results) |
| 180 |
|
| 181 |
def get_matching_igw(self, vpc_id: str, gateway_id: Optional[str] = None) -> Optional[Dict[str, Any]]: |
| 182 |
""" |
| 183 |
Returns the internet gateway found. |
| 184 |
Parameters: |
| 185 |
vpc_id (str): VPC ID |
| 186 |
gateway_id (str): Internet Gateway ID, if specified |
| 187 |
Returns: |
| 188 |
igw (dict): dict of igw found, None if none found |
| 189 |
""" |
| 190 |
|
| 191 |
# If we know the gateway_id, use it to avoid bugs with using filters |
| 192 |
# See https://github.com/ansible-collections/amazon.aws/pull/766 |
| 193 |
params = ( |
| 194 |
{"InternetGatewayIds": [gateway_id]} |
| 195 |
if gateway_id |
| 196 |
else {"Filters": ansible_dict_to_boto3_filter_list({"attachment.vpc-id": vpc_id})} |
| 197 |
) |
| 198 |
|
| 199 |
igw = None |
| 200 |
igws = describe_internet_gateways(self._connection, **params) |
| 201 |
if igws: |
| 202 |
if len(igws) > 1: |
| 203 |
self._module.fail_json(msg=f"EC2 returned more than one Internet Gateway for VPC {vpc_id}, aborting") |
| 204 |
igw = camel_dict_to_snake_dict(igws[0]) |
| 205 |
return igw |
| 206 |
|
| 207 |
def get_matching_vpc(self, vpc_id: str) -> Dict[str, Any]: |
| 208 |
""" |
| 209 |
Returns the virtual private cloud found. |
| 210 |
Parameters: |
| 211 |
vpc_id (str): VPC ID |
| 212 |
Returns: |
| 213 |
vpc (dict): dict of vpc found, None if none found |
| 214 |
""" |
| 215 |
vpcs = describe_vpcs(self._connection, VpcIds=[vpc_id]) |
| 216 |
if not vpcs: |
| 217 |
self._module.fail_json(msg=f"VPC with Id {vpc_id} not found, aborting") |
| 218 |
|
| 219 |
if len(vpcs) > 1: |
| 220 |
self._module.fail_json(msg=f"EC2 returned more than one VPC for {vpc_id}, aborting") |
| 221 |
return camel_dict_to_snake_dict(vpcs[0]) |
| 222 |
|
| 223 |
@staticmethod |
| 224 |
def get_igw_info(igw, vpc_id): |
| 225 |
return { |
| 226 |
"gateway_id": igw["internet_gateway_id"], |
| 227 |
"tags": boto3_tag_list_to_ansible_dict(igw["tags"]), |
| 228 |
"vpc_id": vpc_id, |
| 229 |
} |
| 230 |
|
| 231 |
def attach_vpc(self, igw_id, vpc_id): |
| 232 |
self._results["changed"] |= attach_internet_gateway(self._connection, internet_gateway_id=igw_id, vpc_id=vpc_id) |
| 233 |
wait_for_resource_state( |
| 234 |
self._connection, self._module, "internet_gateway_attached", InternetGatewayIds=[igw_id] |
| 235 |
) |
| 236 |
|
| 237 |
def ensure_igw_absent(self, igw_id, vpc_id): |
| 238 |
igw = self.get_matching_igw(vpc_id, gateway_id=igw_id) |
| 239 |
if igw is None: |
| 240 |
return self._results |
| 241 |
|
| 242 |
igw_vpc_id = "" |
| 243 |
|
| 244 |
if len(igw["attachments"]) > 0: |
| 245 |
igw_vpc_id = igw["attachments"][0]["vpc_id"] |
| 246 |
|
| 247 |
if vpc_id and (igw_vpc_id != vpc_id): |
| 248 |
self._module.fail_json(msg=f"Supplied VPC ({vpc_id}) does not match found VPC ({igw_vpc_id}), aborting") |
| 249 |
|
| 250 |
if self._check_mode: |
| 251 |
self._results["changed"] = True |
| 252 |
return self._results |
| 253 |
|
| 254 |
if igw_vpc_id: |
| 255 |
self._results["changed"] |= detach_internet_gateway( |
| 256 |
self._connection, internet_gateway_id=igw["internet_gateway_id"], vpc_id=igw_vpc_id |
| 257 |
) |
| 258 |
|
| 259 |
self._results["changed"] |= delete_internet_gateway(self._connection, igw["internet_gateway_id"]) |
| 260 |
|
| 261 |
return self._results |
| 262 |
|
| 263 |
def ensure_igw_present(self, igw_id, vpc_id, tags, purge_tags, force_attach, detach_vpc): |
| 264 |
igw = None |
| 265 |
|
| 266 |
if igw_id: |
| 267 |
igw = self.get_matching_igw(None, gateway_id=igw_id) |
| 268 |
elif vpc_id: |
| 269 |
igw = self.get_matching_igw(vpc_id) |
| 270 |
|
| 271 |
if igw is None: |
| 272 |
if self._check_mode: |
| 273 |
self._results["changed"] = True |
| 274 |
self._results["gateway_id"] = None |
| 275 |
return self._results |
| 276 |
|
| 277 |
if vpc_id: |
| 278 |
self.get_matching_vpc(vpc_id) |
| 279 |
|
| 280 |
response = create_internet_gateway(self._connection, tags=tags) |
| 281 |
self._results["changed"] = True |
| 282 |
# Ensure the gateway exists before trying to attach it or add tags |
| 283 |
wait_for_resource_state( |
| 284 |
self._connection, |
| 285 |
self._module, |
| 286 |
"internet_gateway_exists", |
| 287 |
InternetGatewayIds=[response["InternetGatewayId"]], |
| 288 |
) |
| 289 |
|
| 290 |
igw = camel_dict_to_snake_dict(response) |
| 291 |
|
| 292 |
if vpc_id: |
| 293 |
self.attach_vpc(igw["internet_gateway_id"], vpc_id) |
| 294 |
else: |
| 295 |
igw_vpc_id = None |
| 296 |
|
| 297 |
if len(igw["attachments"]) > 0: |
| 298 |
igw_vpc_id = igw["attachments"][0]["vpc_id"] |
| 299 |
|
| 300 |
if detach_vpc: |
| 301 |
if self._check_mode: |
| 302 |
self._results["changed"] = True |
| 303 |
self._results["gateway_id"] = igw["internet_gateway_id"] |
| 304 |
return self._results |
| 305 |
|
| 306 |
self._results["changed"] |= detach_internet_gateway( |
| 307 |
self._connection, internet_gateway_id=igw["internet_gateway_id"], vpc_id=igw_vpc_id |
| 308 |
) |
| 309 |
|
| 310 |
elif igw_vpc_id != vpc_id: |
| 311 |
if self._check_mode: |
| 312 |
self._results["changed"] = True |
| 313 |
self._results["gateway_id"] = igw["internet_gateway_id"] |
| 314 |
return self._results |
| 315 |
|
| 316 |
if force_attach: |
| 317 |
self.get_matching_vpc(vpc_id) |
| 318 |
|
| 319 |
self._results["changed"] |= detach_internet_gateway( |
| 320 |
self._connection, internet_gateway_id=igw["internet_gateway_id"], vpc_id=igw_vpc_id |
| 321 |
) |
| 322 |
self.attach_vpc(igw["internet_gateway_id"], vpc_id) |
| 323 |
else: |
| 324 |
self._module.fail_json(msg="VPC already attached, but does not match requested VPC.") |
| 325 |
|
| 326 |
elif vpc_id: |
| 327 |
if self._check_mode: |
| 328 |
self._results["changed"] = True |
| 329 |
self._results["gateway_id"] = igw["internet_gateway_id"] |
| 330 |
return self._results |
| 331 |
|
| 332 |
self.get_matching_vpc(vpc_id) |
| 333 |
self.attach_vpc(igw["internet_gateway_id"], vpc_id) |
| 334 |
|
| 335 |
# Modify tags |
| 336 |
self._results["changed"] |= ensure_ec2_tags( |
| 337 |
self._connection, |
| 338 |
self._module, |
| 339 |
igw["internet_gateway_id"], |
| 340 |
resource_type="internet-gateway", |
| 341 |
tags=tags, |
| 342 |
purge_tags=purge_tags, |
| 343 |
retry_codes="InvalidInternetGatewayID.NotFound", |
| 344 |
) |
| 345 |
|
| 346 |
# Update igw |
| 347 |
igw = self.get_matching_igw(vpc_id, gateway_id=igw["internet_gateway_id"]) |
| 348 |
igw_info = self.get_igw_info(igw, vpc_id) |
| 349 |
self._results.update(igw_info) |
| 350 |
|
| 351 |
return self._results |
| 352 |
|
| 353 |
|
| 354 |
def main() -> None: |
| 355 |
argument_spec = dict( |
| 356 |
internet_gateway_id=dict(), |
| 357 |
vpc_id=dict(), |
| 358 |
state=dict(default="present", choices=["present", "absent"]), |
| 359 |
tags=dict(required=False, type="dict", aliases=["resource_tags"]), |
| 360 |
purge_tags=dict(default=True, type="bool"), |
| 361 |
force_attach=dict(default=False, type="bool"), |
| 362 |
detach_vpc=dict(default=False, type="bool"), |
| 363 |
) |
| 364 |
|
| 365 |
required_if = [ |
| 366 |
("force_attach", True, ("vpc_id",), False), |
| 367 |
("state", "absent", ("internet_gateway_id", "vpc_id"), True), |
| 368 |
("detach_vpc", True, ("internet_gateway_id", "vpc_id"), True), |
| 369 |
] |
| 370 |
|
| 371 |
mutually_exclusive = [("force_attach", "detach_vpc")] |
| 372 |
|
| 373 |
module = AnsibleAWSModule( |
| 374 |
argument_spec=argument_spec, |
| 375 |
supports_check_mode=True, |
| 376 |
required_if=required_if, |
| 377 |
mutually_exclusive=mutually_exclusive, |
| 378 |
) |
| 379 |
|
| 380 |
igw_manager = AnsibleEc2Igw(module=module) |
| 381 |
try: |
| 382 |
igw_manager.process() |
| 383 |
except AnsibleEC2Error as e: |
| 384 |
module.fail_json_aws_error(e) |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == "__main__": |
| 388 |
main() |