| 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_egress_igw_info |
| 10 |
version_added: 9.0.0 |
| 11 |
short_description: Gather information about AWS VPC Egress Only Internet gateway |
| 12 |
description: |
| 13 |
- Gather information about AWS VPC Egress Only Internet gateway. |
| 14 |
author: "Aubin Bikouo (@abikouo)" |
| 15 |
options: |
| 16 |
filters: |
| 17 |
description: |
| 18 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 19 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeEgressOnlyInternetGateways.html) for possible filters. |
| 20 |
type: dict |
| 21 |
default: {} |
| 22 |
egress_only_internet_gateway_ids: |
| 23 |
description: |
| 24 |
- Get details of specific VPC Egress Internet Gateway ID. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
extends_documentation_fragment: |
| 28 |
- amazon.aws.common.modules |
| 29 |
- amazon.aws.region.modules |
| 30 |
- amazon.aws.boto3 |
| 31 |
""" |
| 32 |
|
| 33 |
EXAMPLES = r""" |
| 34 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 35 |
|
| 36 |
- name: Gather information about all VPC Egress only Internet Gateways for an account or profile |
| 37 |
amazon.aws.ec2_vpc_igw_info: |
| 38 |
region: ap-southeast-2 |
| 39 |
profile: production |
| 40 |
|
| 41 |
- name: Gather information about a filtered list of VPC Egress only Internet Gateways |
| 42 |
amazon.aws.ec2_vpc_igw_info: |
| 43 |
region: ap-southeast-2 |
| 44 |
profile: production |
| 45 |
filters: |
| 46 |
"tag:Name": "igw-123" |
| 47 |
|
| 48 |
- name: Gather information about a specific VPC egress only internet gateway by EgressOnlyInternetGatewayId |
| 49 |
amazon.aws.ec2_vpc_igw_info: |
| 50 |
region: ap-southeast-2 |
| 51 |
profile: production |
| 52 |
egress_only_internet_gateway_ids: igw-c1231234 |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = r""" |
| 56 |
egress_only_internet_gateways: |
| 57 |
description: The AWS Egress only internet gateways. |
| 58 |
returned: always |
| 59 |
type: list |
| 60 |
contains: |
| 61 |
attachments: |
| 62 |
description: Information about the attachment of the egress-only internet gateway. |
| 63 |
returned: always |
| 64 |
type: list |
| 65 |
contains: |
| 66 |
state: |
| 67 |
description: The current state of the attachment. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: "available" |
| 71 |
vpc_id: |
| 72 |
description: The ID of the VPC. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: "vpc-02123b67" |
| 76 |
egress_only_internet_gateway_id: |
| 77 |
description: The ID of the egress-only internet gateway. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: "eigw-0123456789abcdef" |
| 81 |
tags: |
| 82 |
description: Any tags assigned to the egress-only internet gateway. |
| 83 |
returned: always |
| 84 |
type: dict |
| 85 |
""" |
| 86 |
|
| 87 |
from typing import Any |
| 88 |
from typing import Dict |
| 89 |
from typing import List |
| 90 |
|
| 91 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 92 |
|
| 93 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 94 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_egress_only_internet_gateways |
| 95 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 96 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 97 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 98 |
|
| 99 |
|
| 100 |
def format_egress_only_internet_gateway(internet_gateway: Dict[str, Any]) -> Dict[str, Any]: |
| 101 |
tags = boto3_tag_list_to_ansible_dict(internet_gateway.get("Tags", [])) |
| 102 |
egress_only_igw = { |
| 103 |
"EgressOnlyInternetGatewayId": internet_gateway["EgressOnlyInternetGatewayId"], |
| 104 |
"Attachments": internet_gateway["Attachments"], |
| 105 |
"Tags": tags, |
| 106 |
} |
| 107 |
return camel_dict_to_snake_dict(egress_only_igw, ignore_list=["Tags"]) |
| 108 |
|
| 109 |
|
| 110 |
def list_egress_only_internet_gateways(connection, module: AnsibleAWSModule) -> List[Dict[str, Any]]: |
| 111 |
params = dict() |
| 112 |
|
| 113 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 114 |
|
| 115 |
if module.params.get("egress_only_internet_gateway_ids"): |
| 116 |
params["EgressOnlyInternetGatewayIds"] = module.params.get("egress_only_internet_gateway_ids") |
| 117 |
|
| 118 |
try: |
| 119 |
egress_only_igws = describe_egress_only_internet_gateways(connection, **params) |
| 120 |
return [format_egress_only_internet_gateway(igw) for igw in egress_only_igws] |
| 121 |
except AnsibleEC2Error as e: |
| 122 |
module.fail_json_aws(e, "Unable to describe egress only internet gateways") |
| 123 |
|
| 124 |
|
| 125 |
def main() -> None: |
| 126 |
argument_spec = dict( |
| 127 |
filters=dict(type="dict", default=dict()), |
| 128 |
egress_only_internet_gateway_ids=dict(type="list", elements="str"), |
| 129 |
) |
| 130 |
|
| 131 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 132 |
client = module.client("ec2") |
| 133 |
|
| 134 |
results = list_egress_only_internet_gateways(client, module) |
| 135 |
module.exit_json(egress_only_internet_gateways=results) |
| 136 |
|
| 137 |
|
| 138 |
if __name__ == "__main__": |
| 139 |
main() |