| 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_customer_gateway_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about customer gateways in AWS |
| 12 |
description: |
| 13 |
- Gather information about customer gateways in AWS. |
| 14 |
author: |
| 15 |
- Madhura Naniwadekar (@Madhura-CSI) |
| 16 |
options: |
| 17 |
filters: |
| 18 |
description: |
| 19 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 20 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeCustomerGateways.html) for possible filters. |
| 21 |
type: dict |
| 22 |
default: {} |
| 23 |
customer_gateway_ids: |
| 24 |
description: |
| 25 |
- Get details of a specific customer gateways using customer gateway ID/IDs. This value should be provided as a list. |
| 26 |
type: list |
| 27 |
elements: str |
| 28 |
default: [] |
| 29 |
extends_documentation_fragment: |
| 30 |
- amazon.aws.common.modules |
| 31 |
- amazon.aws.region.modules |
| 32 |
- amazon.aws.boto3 |
| 33 |
""" |
| 34 |
|
| 35 |
EXAMPLES = r""" |
| 36 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 37 |
|
| 38 |
- name: Gather information about all customer gateways |
| 39 |
community.aws.ec2_customer_gateway_info: |
| 40 |
|
| 41 |
- name: Gather information about a filtered list of customer gateways, based on tags |
| 42 |
community.aws.ec2_customer_gateway_info: |
| 43 |
region: ap-southeast-2 |
| 44 |
filters: |
| 45 |
"tag:Name": test-customer-gateway |
| 46 |
"tag:AltName": test-customer-gateway-alt |
| 47 |
register: cust_gw_info |
| 48 |
|
| 49 |
- name: Gather information about a specific customer gateway by specifying customer gateway ID |
| 50 |
community.aws.ec2_customer_gateway_info: |
| 51 |
region: ap-southeast-2 |
| 52 |
customer_gateway_ids: |
| 53 |
- 'cgw-48841a09' |
| 54 |
- 'cgw-fec021ce' |
| 55 |
register: cust_gw_info |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = r""" |
| 59 |
customer_gateways: |
| 60 |
description: List of one or more customer gateways. |
| 61 |
returned: always |
| 62 |
type: list |
| 63 |
sample: [ |
| 64 |
{ |
| 65 |
"bgp_asn": "65000", |
| 66 |
"customer_gateway_id": "cgw-fec844ce", |
| 67 |
"customer_gateway_name": "test-customer-gw", |
| 68 |
"ip_address": "110.112.113.120", |
| 69 |
"state": "available", |
| 70 |
"tags": [ |
| 71 |
{ |
| 72 |
"key": "Name", |
| 73 |
"value": "test-customer-gw" |
| 74 |
} |
| 75 |
], |
| 76 |
"type": "ipsec.1" |
| 77 |
} |
| 78 |
] |
| 79 |
""" |
| 80 |
|
| 81 |
import json |
| 82 |
|
| 83 |
try: |
| 84 |
from botocore.exceptions import BotoCoreError |
| 85 |
from botocore.exceptions import ClientError |
| 86 |
except ImportError: |
| 87 |
pass # caught by AnsibleAWSModule |
| 88 |
|
| 89 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 90 |
|
| 91 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 92 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 93 |
|
| 94 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 95 |
|
| 96 |
|
| 97 |
def date_handler(obj): |
| 98 |
return obj.isoformat() if hasattr(obj, "isoformat") else obj |
| 99 |
|
| 100 |
|
| 101 |
def list_customer_gateways(connection, module): |
| 102 |
params = dict() |
| 103 |
|
| 104 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 105 |
params["CustomerGatewayIds"] = module.params.get("customer_gateway_ids") |
| 106 |
|
| 107 |
try: |
| 108 |
result = json.loads(json.dumps(connection.describe_customer_gateways(**params), default=date_handler)) |
| 109 |
except (ClientError, BotoCoreError) as e: |
| 110 |
module.fail_json_aws(e, msg="Could not describe customer gateways") |
| 111 |
snaked_customer_gateways = [camel_dict_to_snake_dict(gateway) for gateway in result["CustomerGateways"]] |
| 112 |
if snaked_customer_gateways: |
| 113 |
for customer_gateway in snaked_customer_gateways: |
| 114 |
customer_gateway["tags"] = boto3_tag_list_to_ansible_dict(customer_gateway.get("tags", [])) |
| 115 |
customer_gateway_name = customer_gateway["tags"].get("Name") |
| 116 |
if customer_gateway_name: |
| 117 |
customer_gateway["customer_gateway_name"] = customer_gateway_name |
| 118 |
module.exit_json(changed=False, customer_gateways=snaked_customer_gateways) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
argument_spec = dict( |
| 123 |
customer_gateway_ids=dict(default=[], type="list", elements="str"), filters=dict(default={}, type="dict") |
| 124 |
) |
| 125 |
|
| 126 |
module = AnsibleAWSModule( |
| 127 |
argument_spec=argument_spec, |
| 128 |
mutually_exclusive=[ |
| 129 |
["customer_gateway_ids", "filters"], |
| 130 |
], |
| 131 |
supports_check_mode=True, |
| 132 |
) |
| 133 |
|
| 134 |
connection = module.client("ec2") |
| 135 |
|
| 136 |
list_customer_gateways(connection, module) |
| 137 |
|
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
main() |