| 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_carrier_gateway_info |
| 10 |
version_added: 6.0.0 |
| 11 |
short_description: Gather information about carrier gateways in AWS |
| 12 |
description: |
| 13 |
- Gather information about carrier gateways in AWS. |
| 14 |
author: |
| 15 |
- "Marco Braga (@mtulio)" |
| 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_DescribeCarrierGateways.html) for possible filters. |
| 21 |
required: false |
| 22 |
default: {} |
| 23 |
type: dict |
| 24 |
carrier_gateway_ids: |
| 25 |
description: |
| 26 |
- Get details of specific Carrier Gateway ID. |
| 27 |
required: false |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
extends_documentation_fragment: |
| 31 |
- amazon.aws.common.modules |
| 32 |
- amazon.aws.region.modules |
| 33 |
- amazon.aws.boto3 |
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = r""" |
| 37 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 38 |
|
| 39 |
- name: Gather information about all Carrier Gateways for an account or profile |
| 40 |
community.aws.ec2_carrier_gateway_info: |
| 41 |
region: ap-southeast-2 |
| 42 |
register: cagw_info |
| 43 |
|
| 44 |
- name: Gather information about a filtered list of Carrier Gateways |
| 45 |
community.aws.ec2_carrier_gateway_info: |
| 46 |
region: ap-southeast-2 |
| 47 |
filters: |
| 48 |
"tag:Name": "cagw-123" |
| 49 |
register: cagw_info |
| 50 |
|
| 51 |
- name: Gather information about a specific carrier gateway by CarrierGatewayId |
| 52 |
community.aws.ec2_carrier_gateway_info: |
| 53 |
region: ap-southeast-2 |
| 54 |
carrier_gateway_ids: cagw-c1231234 |
| 55 |
register: cagw_info |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = r""" |
| 59 |
changed: |
| 60 |
description: True if listing the carrier gateways succeeds. |
| 61 |
type: bool |
| 62 |
returned: always |
| 63 |
sample: "false" |
| 64 |
carrier_gateways: |
| 65 |
description: The carrier gateways for the account. |
| 66 |
returned: always |
| 67 |
type: complex |
| 68 |
contains: |
| 69 |
vpc_id: |
| 70 |
description: The ID of the VPC. |
| 71 |
returned: I(state=present) |
| 72 |
type: str |
| 73 |
sample: vpc-02123b67 |
| 74 |
carrier_gateway_id: |
| 75 |
description: The ID of the carrier gateway. |
| 76 |
returned: I(state=present) |
| 77 |
type: str |
| 78 |
sample: cagw-2123634d |
| 79 |
tags: |
| 80 |
description: Any tags assigned to the carrier gateway. |
| 81 |
returned: I(state=present) |
| 82 |
type: dict |
| 83 |
sample: |
| 84 |
tags: |
| 85 |
"Ansible": "Test" |
| 86 |
""" |
| 87 |
|
| 88 |
try: |
| 89 |
import botocore |
| 90 |
except ImportError: |
| 91 |
pass # Handled by AnsibleAWSModule |
| 92 |
|
| 93 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 94 |
|
| 95 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 96 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 97 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 98 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 99 |
|
| 100 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 101 |
|
| 102 |
|
| 103 |
def get_carrier_gateway_info(carrier_gateway): |
| 104 |
tags = boto3_tag_list_to_ansible_dict(carrier_gateway["Tags"]) |
| 105 |
ignore_list = [] |
| 106 |
carrier_gateway_info = { |
| 107 |
"CarrierGatewayId": carrier_gateway["CarrierGatewayId"], |
| 108 |
"VpcId": carrier_gateway["VpcId"], |
| 109 |
"Tags": tags, |
| 110 |
} |
| 111 |
|
| 112 |
carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, ignore_list=ignore_list) |
| 113 |
return carrier_gateway_info |
| 114 |
|
| 115 |
|
| 116 |
def list_carrier_gateways(connection, module): |
| 117 |
params = dict() |
| 118 |
|
| 119 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 120 |
if module.params.get("carrier_gateway_ids"): |
| 121 |
params["CarrierGatewayIds"] = module.params.get("carrier_gateway_ids") |
| 122 |
|
| 123 |
try: |
| 124 |
all_carrier_gateways = connection.describe_carrier_gateways(aws_retry=True, **params) |
| 125 |
except is_boto3_error_code("InvalidCarrierGatewayID.NotFound"): |
| 126 |
module.fail_json("CarrierGateway not found") |
| 127 |
except ( |
| 128 |
botocore.exceptions.ClientError, |
| 129 |
botocore.exceptions.BotoCoreError, |
| 130 |
) as e: # pylint: disable=duplicate-except |
| 131 |
module.fail_json_aws(e, "Unable to describe carrier gateways") |
| 132 |
|
| 133 |
return [get_carrier_gateway_info(cagw) for cagw in all_carrier_gateways["CarrierGateways"]] |
| 134 |
|
| 135 |
|
| 136 |
def main(): |
| 137 |
argument_spec = dict( |
| 138 |
carrier_gateway_ids=dict(default=None, elements="str", type="list"), |
| 139 |
filters=dict(default={}, type="dict"), |
| 140 |
) |
| 141 |
|
| 142 |
module = AnsibleAWSModule( |
| 143 |
argument_spec=argument_spec, |
| 144 |
supports_check_mode=True, |
| 145 |
) |
| 146 |
|
| 147 |
# Validate Requirements |
| 148 |
try: |
| 149 |
connection = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff()) |
| 150 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 151 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 152 |
|
| 153 |
results = list_carrier_gateways(connection, module) |
| 154 |
|
| 155 |
module.exit_json(carrier_gateways=results) |
| 156 |
|
| 157 |
|
| 158 |
if __name__ == "__main__": |
| 159 |
main() |