| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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_egress_igw |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Manage an AWS VPC Egress Only Internet gateway |
| 13 |
description: |
| 14 |
- Manage an AWS VPC Egress Only Internet gateway |
| 15 |
author: |
| 16 |
- Daniel Shepherd (@shepdelacreme) |
| 17 |
options: |
| 18 |
vpc_id: |
| 19 |
description: |
| 20 |
- The VPC ID for the VPC that this Egress Only Internet Gateway should be attached. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Create or delete the EIGW. |
| 26 |
default: present |
| 27 |
choices: [ 'present', 'absent' ] |
| 28 |
type: str |
| 29 |
notes: |
| 30 |
- Support for O(tags) and O(purge_tags) was added in release 9.0.0. |
| 31 |
extends_documentation_fragment: |
| 32 |
- amazon.aws.common.modules |
| 33 |
- amazon.aws.region.modules |
| 34 |
- amazon.aws.boto3 |
| 35 |
- amazon.aws.tags.modules |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 40 |
|
| 41 |
# Ensure that the VPC has an Internet Gateway. |
| 42 |
# The Internet Gateway ID is can be accessed via {{eigw.gateway_id}} for use in setting up NATs etc. |
| 43 |
- name: Create Egress internet only gateway |
| 44 |
amazon.aws.ec2_vpc_egress_igw: |
| 45 |
vpc_id: vpc-abcdefgh |
| 46 |
state: present |
| 47 |
|
| 48 |
- name: Delete Egress internet only gateway |
| 49 |
amazon.aws.ec2_vpc_egress_igw: |
| 50 |
vpc_id: vpc-abcdefgh |
| 51 |
state: absent |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = r""" |
| 55 |
gateway_id: |
| 56 |
description: The ID of the Egress Only Internet Gateway or Null. |
| 57 |
returned: always |
| 58 |
type: str |
| 59 |
sample: "eigw-0e00cf111ba5bc11e" |
| 60 |
vpc_id: |
| 61 |
description: The ID of the VPC to attach or detach gateway from. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
sample: "vpc-012345678" |
| 65 |
tags: |
| 66 |
description: Any tags assigned to the internet gateway. |
| 67 |
returned: always |
| 68 |
type: dict |
| 69 |
""" |
| 70 |
|
| 71 |
from typing import Any |
| 72 |
from typing import Dict |
| 73 |
from typing import Optional |
| 74 |
from typing import Union |
| 75 |
|
| 76 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 77 |
|
| 78 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 79 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_egress_only_internet_gateway |
| 80 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_egress_only_internet_gateway |
| 81 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_egress_only_internet_gateways |
| 82 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 83 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 84 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 85 |
|
| 86 |
|
| 87 |
def delete_eigw(module: AnsibleAWSModule, connection, eigw_id: str) -> Dict[str, Union[str, bool]]: |
| 88 |
""" |
| 89 |
Delete EIGW. |
| 90 |
|
| 91 |
module : AnsibleAWSModule object |
| 92 |
connection : boto3 client connection object |
| 93 |
eigw_id : ID of the EIGW to delete |
| 94 |
""" |
| 95 |
|
| 96 |
if module.check_mode: |
| 97 |
return dict( |
| 98 |
changed=True, msg=f"Would have deleted Egress internet only Gateway id '{eigw_id}' if not in check mode." |
| 99 |
) |
| 100 |
|
| 101 |
try: |
| 102 |
changed = delete_egress_only_internet_gateway(connection, egress_only_internet_gateway_id=eigw_id) |
| 103 |
except AnsibleEC2Error as e: |
| 104 |
module.fail_json_aws(e) |
| 105 |
|
| 106 |
return dict(changed=changed) |
| 107 |
|
| 108 |
|
| 109 |
def create_eigw(module: AnsibleAWSModule, connection, vpc_id: str) -> Dict[str, Union[str, bool]]: |
| 110 |
""" |
| 111 |
Create EIGW. |
| 112 |
|
| 113 |
module : AnsibleAWSModule object |
| 114 |
connection : boto3 client connection object |
| 115 |
vpc_id : ID of the VPC we are operating on |
| 116 |
""" |
| 117 |
|
| 118 |
if module.check_mode: |
| 119 |
return dict(changed=True, msg="Would have created Egress internet only Gateway if not in check mode.") |
| 120 |
|
| 121 |
gateway_id = None |
| 122 |
changed = False |
| 123 |
|
| 124 |
try: |
| 125 |
response = create_egress_only_internet_gateway(connection, vpc_id=vpc_id, tags=module.params.get("tags")) |
| 126 |
changed = True |
| 127 |
except AnsibleEC2Error as e: |
| 128 |
module.fail_json_aws(e) |
| 129 |
|
| 130 |
gateway = response.get("EgressOnlyInternetGateway", {}) |
| 131 |
state = gateway.get("Attachments", [{}])[0].get("State") |
| 132 |
gateway_id = gateway.get("EgressOnlyInternetGatewayId") |
| 133 |
tags = boto3_tag_list_to_ansible_dict(gateway.get("Tags", [])) |
| 134 |
|
| 135 |
if not gateway_id or state not in ("attached", "attaching"): |
| 136 |
# EIGW gave back a bad attachment state or an invalid response so we error out |
| 137 |
module.fail_json( |
| 138 |
msg=f"Unable to create and attach Egress Only Internet Gateway to VPCId: {vpc_id}. Bad or no state in response", |
| 139 |
**camel_dict_to_snake_dict(response), |
| 140 |
) |
| 141 |
|
| 142 |
return dict(changed=changed, gateway_id=gateway_id, tags=tags) |
| 143 |
|
| 144 |
|
| 145 |
def find_egress_only_igw(module: AnsibleAWSModule, connection, vpc_id: str) -> Optional[Dict[str, Any]]: |
| 146 |
""" |
| 147 |
Describe EIGWs. |
| 148 |
|
| 149 |
module : AnsibleAWSModule object |
| 150 |
connection : boto3 client connection object |
| 151 |
vpc_id : ID of the VPC we are operating on |
| 152 |
""" |
| 153 |
result = None |
| 154 |
|
| 155 |
try: |
| 156 |
for eigw in describe_egress_only_internet_gateways(connection): |
| 157 |
for attachment in eigw.get("Attachments", []): |
| 158 |
if attachment.get("VpcId") == vpc_id and attachment.get("State") in ("attached", "attaching"): |
| 159 |
return { |
| 160 |
"gateway_id": eigw.get("EgressOnlyInternetGatewayId"), |
| 161 |
"tags": boto3_tag_list_to_ansible_dict(eigw.get("Tags", [])), |
| 162 |
} |
| 163 |
except AnsibleEC2Error as e: |
| 164 |
module.fail_json_aws(e) |
| 165 |
|
| 166 |
return result |
| 167 |
|
| 168 |
|
| 169 |
def ensure_present(connection, module: AnsibleAWSModule, existing: Optional[Dict[str, Any]]) -> None: |
| 170 |
vpc_id = module.params.get("vpc_id") |
| 171 |
result = dict(vpc_id=vpc_id, changed=False) |
| 172 |
|
| 173 |
if not existing: |
| 174 |
result.update(create_eigw(module, connection, vpc_id)) |
| 175 |
else: |
| 176 |
egress_only_igw_id = existing.get("gateway_id") |
| 177 |
changed = False |
| 178 |
result = existing |
| 179 |
tags = module.params.get("tags") |
| 180 |
purge_tags = module.params.get("purge_tags") |
| 181 |
if tags is not None: |
| 182 |
changed = ensure_ec2_tags( |
| 183 |
connection, |
| 184 |
module, |
| 185 |
egress_only_igw_id, |
| 186 |
resource_type="egress-only-internet-gateway", |
| 187 |
tags=tags, |
| 188 |
purge_tags=purge_tags, |
| 189 |
) |
| 190 |
result.update(dict(changed=changed, vpc_id=vpc_id)) |
| 191 |
|
| 192 |
module.exit_json(**result) |
| 193 |
|
| 194 |
|
| 195 |
def ensure_absent(connection, module: AnsibleAWSModule, existing: Optional[Dict[str, Any]]) -> None: |
| 196 |
vpc_id = module.params.get("vpc_id") |
| 197 |
if not existing: |
| 198 |
module.exit_json(changed=False, msg=f"No Egress only internet gateway attached to the VPC id '{vpc_id}'") |
| 199 |
|
| 200 |
egress_only_igw_id = existing.get("gateway_id") |
| 201 |
result = dict(gateway_id=egress_only_igw_id, vpc_id=vpc_id, changed=False) |
| 202 |
result.update(delete_eigw(module, connection, egress_only_igw_id)) |
| 203 |
module.exit_json(**result) |
| 204 |
|
| 205 |
|
| 206 |
def main(): |
| 207 |
argument_spec = dict( |
| 208 |
vpc_id=dict(required=True), |
| 209 |
state=dict(default="present", choices=["present", "absent"]), |
| 210 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 211 |
purge_tags=dict(type="bool", default=True), |
| 212 |
) |
| 213 |
|
| 214 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 215 |
|
| 216 |
connection = module.client("ec2") |
| 217 |
|
| 218 |
vpc_id = module.params.get("vpc_id") |
| 219 |
state = module.params.get("state") |
| 220 |
|
| 221 |
existing_egress_only_igw = find_egress_only_igw(module, connection, vpc_id) |
| 222 |
|
| 223 |
if state == "present": |
| 224 |
ensure_present(connection, module, existing_egress_only_igw) |
| 225 |
else: |
| 226 |
ensure_absent(connection, module, existing_egress_only_igw) |
| 227 |
|
| 228 |
|
| 229 |
if __name__ == "__main__": |
| 230 |
main() |