| 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_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about internet gateways in AWS |
| 12 |
description: |
| 13 |
- Gather information about internet gateways in AWS. |
| 14 |
author: "Nick Aslanidis (@naslanidis)" |
| 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_DescribeInternetGateways.html) for possible filters. |
| 20 |
type: dict |
| 21 |
default: {} |
| 22 |
internet_gateway_ids: |
| 23 |
description: |
| 24 |
- Get details of specific Internet Gateway ID. Provide this value as a list. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
convert_tags: |
| 28 |
description: |
| 29 |
- Convert tags from boto3 format (list of dictionaries) to the standard dictionary format. |
| 30 |
- Prior to release 4.0.0 this defaulted to V(false). |
| 31 |
default: true |
| 32 |
type: bool |
| 33 |
version_added: 1.3.0 |
| 34 |
extends_documentation_fragment: |
| 35 |
- amazon.aws.common.modules |
| 36 |
- amazon.aws.region.modules |
| 37 |
- amazon.aws.boto3 |
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 42 |
|
| 43 |
- name: Gather information about all Internet Gateways for an account or profile |
| 44 |
amazon.aws.ec2_vpc_igw_info: |
| 45 |
region: ap-southeast-2 |
| 46 |
profile: production |
| 47 |
register: igw_info |
| 48 |
|
| 49 |
- name: Gather information about a filtered list of Internet Gateways |
| 50 |
amazon.aws.ec2_vpc_igw_info: |
| 51 |
region: ap-southeast-2 |
| 52 |
profile: production |
| 53 |
filters: |
| 54 |
"tag:Name": "igw-123" |
| 55 |
register: igw_info |
| 56 |
|
| 57 |
- name: Gather information about a specific internet gateway by InternetGatewayId |
| 58 |
amazon.aws.ec2_vpc_igw_info: |
| 59 |
region: ap-southeast-2 |
| 60 |
profile: production |
| 61 |
internet_gateway_ids: igw-c1231234 |
| 62 |
register: igw_info |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = r""" |
| 66 |
changed: |
| 67 |
description: True if listing the internet gateways succeeds. |
| 68 |
type: bool |
| 69 |
returned: always |
| 70 |
sample: false |
| 71 |
internet_gateways: |
| 72 |
description: The internet gateways for the account. |
| 73 |
returned: always |
| 74 |
type: complex |
| 75 |
contains: |
| 76 |
attachments: |
| 77 |
description: Any VPCs attached to the internet gateway. |
| 78 |
returned: always |
| 79 |
type: complex |
| 80 |
contains: |
| 81 |
state: |
| 82 |
description: The current state of the attachment. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: available |
| 86 |
vpc_id: |
| 87 |
description: The ID of the VPC. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: vpc-02123b67 |
| 91 |
internet_gateway_id: |
| 92 |
description: The ID of the internet gateway. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: igw-2123634d |
| 96 |
tags: |
| 97 |
description: Any tags assigned to the internet gateway. |
| 98 |
returned: always |
| 99 |
type: dict |
| 100 |
sample: |
| 101 |
tags: |
| 102 |
"Ansible": "Test" |
| 103 |
""" |
| 104 |
|
| 105 |
from typing import Any |
| 106 |
from typing import Dict |
| 107 |
from typing import List |
| 108 |
|
| 109 |
try: |
| 110 |
import botocore |
| 111 |
except ImportError: |
| 112 |
pass # Handled by AnsibleAWSModule |
| 113 |
|
| 114 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 115 |
|
| 116 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_internet_gateways |
| 117 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 118 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 119 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 120 |
|
| 121 |
|
| 122 |
def get_internet_gateway_info(internet_gateway: Dict[str, Any], convert_tags: bool) -> Dict[str, Any]: |
| 123 |
if convert_tags: |
| 124 |
tags = boto3_tag_list_to_ansible_dict(internet_gateway["Tags"]) |
| 125 |
ignore_list = ["Tags"] |
| 126 |
else: |
| 127 |
tags = internet_gateway["Tags"] |
| 128 |
ignore_list = [] |
| 129 |
internet_gateway_info = { |
| 130 |
"InternetGatewayId": internet_gateway["InternetGatewayId"], |
| 131 |
"Attachments": internet_gateway["Attachments"], |
| 132 |
"Tags": tags, |
| 133 |
} |
| 134 |
|
| 135 |
internet_gateway_info = camel_dict_to_snake_dict(internet_gateway_info, ignore_list=ignore_list) |
| 136 |
return internet_gateway_info |
| 137 |
|
| 138 |
|
| 139 |
def list_internet_gateways(connection, module: AnsibleAWSModule) -> List[Dict[str, Any]]: |
| 140 |
params = dict() |
| 141 |
|
| 142 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 143 |
convert_tags = module.params.get("convert_tags") |
| 144 |
|
| 145 |
if module.params.get("internet_gateway_ids"): |
| 146 |
params["InternetGatewayIds"] = module.params.get("internet_gateway_ids") |
| 147 |
|
| 148 |
try: |
| 149 |
all_internet_gateways = describe_internet_gateways(connection, **params) |
| 150 |
if all_internet_gateways is None: |
| 151 |
module.fail_json("InternetGateway not found") |
| 152 |
except ( |
| 153 |
botocore.exceptions.ClientError, |
| 154 |
botocore.exceptions.BotoCoreError, |
| 155 |
) as e: # pylint: disable=duplicate-except |
| 156 |
module.fail_json_aws(e, "Unable to describe internet gateways") |
| 157 |
|
| 158 |
return [get_internet_gateway_info(igw, convert_tags) for igw in all_internet_gateways] |
| 159 |
|
| 160 |
|
| 161 |
def main() -> None: |
| 162 |
argument_spec = dict( |
| 163 |
filters=dict(type="dict", default=dict()), |
| 164 |
internet_gateway_ids=dict(type="list", default=None, elements="str"), |
| 165 |
convert_tags=dict(type="bool", default=True), |
| 166 |
) |
| 167 |
|
| 168 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 169 |
|
| 170 |
# Validate Requirements |
| 171 |
try: |
| 172 |
connection = module.client("ec2") |
| 173 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 174 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 175 |
|
| 176 |
# call your function here |
| 177 |
results = list_internet_gateways(connection, module) |
| 178 |
|
| 179 |
module.exit_json(internet_gateways=results) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
main() |