| 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_vgw_info |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Gather information about virtual gateways in AWS |
| 13 |
description: |
| 14 |
- Gather information about virtual gateways (VGWs) in AWS. |
| 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_DescribeVpnGateways.html) for possible filters. |
| 20 |
type: dict |
| 21 |
default: {} |
| 22 |
vpn_gateway_ids: |
| 23 |
description: One or more virtual private gateway IDs. |
| 24 |
type: list |
| 25 |
elements: str |
| 26 |
author: |
| 27 |
- "Nick Aslanidis (@naslanidis)" |
| 28 |
extends_documentation_fragment: |
| 29 |
- amazon.aws.common.modules |
| 30 |
- amazon.aws.region.modules |
| 31 |
- amazon.aws.boto3 |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 36 |
|
| 37 |
- name: Gather information about all virtual gateways for an account or profile |
| 38 |
amazon.aws.ec2_vpc_vgw_info: |
| 39 |
region: ap-southeast-2 |
| 40 |
|
| 41 |
- name: Gather information about a filtered list of Virtual Gateways |
| 42 |
amazon.aws.ec2_vpc_vgw_info: |
| 43 |
region: ap-southeast-2 |
| 44 |
profile: production |
| 45 |
filters: |
| 46 |
"tag:Name": "main-virt-gateway" |
| 47 |
|
| 48 |
- name: Gather information about a specific virtual gateway by VpnGatewayIds |
| 49 |
amazon.aws.ec2_vpc_vgw_info: |
| 50 |
region: ap-southeast-2 |
| 51 |
profile: production |
| 52 |
vpn_gateway_ids: vgw-c432f6a7 |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = r""" |
| 56 |
virtual_gateways: |
| 57 |
description: Information about one or more virtual private gateways. |
| 58 |
returned: always |
| 59 |
type: list |
| 60 |
elements: dict |
| 61 |
contains: |
| 62 |
vpn_gateway_id: |
| 63 |
description: The ID of the virtual private gateway. |
| 64 |
type: str |
| 65 |
returned: success |
| 66 |
example: "vgw-0123456789abcdef0" |
| 67 |
state: |
| 68 |
description: Informtion about the current state of the virtual private gateway. |
| 69 |
type: str |
| 70 |
returned: success |
| 71 |
example: "available" |
| 72 |
type: |
| 73 |
description: Information about type of VPN connection the virtual private gateway supports. |
| 74 |
type: str |
| 75 |
returned: success |
| 76 |
example: "ipsec.1" |
| 77 |
vpc_attachments: |
| 78 |
description: Information about the VPCs attached to the virtual private gateway. |
| 79 |
type: list |
| 80 |
elements: dict |
| 81 |
returned: success |
| 82 |
contains: |
| 83 |
state: |
| 84 |
description: The current state of the attachment. |
| 85 |
type: str |
| 86 |
returned: success |
| 87 |
example: "available" |
| 88 |
vpc_id: |
| 89 |
description: The ID of the VPC. |
| 90 |
type: str |
| 91 |
returned: success |
| 92 |
example: "vpc-12345678901234567" |
| 93 |
tags: |
| 94 |
description: |
| 95 |
- A list of dictionaries representing the tags attached to the virtual private gateway. |
| 96 |
- Represents the same details as RV(virtual_gateways.resource_tags). |
| 97 |
type: list |
| 98 |
elements: dict |
| 99 |
returned: success |
| 100 |
contains: |
| 101 |
key: |
| 102 |
description: The key of the tag. |
| 103 |
type: str |
| 104 |
returned: success |
| 105 |
example: "MyKey" |
| 106 |
value: |
| 107 |
description: The value of the tag. |
| 108 |
type: str |
| 109 |
returned: success |
| 110 |
example: "MyValue" |
| 111 |
resource_tags: |
| 112 |
description: |
| 113 |
- A dictionary representing the tags attached to the VGW. |
| 114 |
- Represents the same details as RV(virtual_gateways.tags). |
| 115 |
type: dict |
| 116 |
returned: success |
| 117 |
example: { |
| 118 |
"MyKey": "MyValue", |
| 119 |
"Env": "Dev_Test_01" |
| 120 |
} |
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 124 |
|
| 125 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 126 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpn_gateways |
| 127 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 128 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 129 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 130 |
|
| 131 |
|
| 132 |
def get_virtual_gateway_info(virtual_gateway): |
| 133 |
tags = virtual_gateway.get("Tags", []) |
| 134 |
resource_tags = boto3_tag_list_to_ansible_dict(tags) |
| 135 |
virtual_gateway_info = dict( |
| 136 |
VpnGatewayId=virtual_gateway["VpnGatewayId"], |
| 137 |
State=virtual_gateway["State"], |
| 138 |
Type=virtual_gateway["Type"], |
| 139 |
VpcAttachments=virtual_gateway["VpcAttachments"], |
| 140 |
Tags=tags, |
| 141 |
ResourceTags=resource_tags, |
| 142 |
) |
| 143 |
return virtual_gateway_info |
| 144 |
|
| 145 |
|
| 146 |
def list_virtual_gateways(client, module): |
| 147 |
params = dict() |
| 148 |
vpn_gateway_ids = module.params.get("vpn_gateway_ids") |
| 149 |
filters = module.params.get("filters") |
| 150 |
|
| 151 |
if filters: |
| 152 |
params["Filters"] = ansible_dict_to_boto3_filter_list(filters) |
| 153 |
if vpn_gateway_ids: |
| 154 |
params["VpnGatewayIds"] = vpn_gateway_ids |
| 155 |
|
| 156 |
try: |
| 157 |
all_virtual_gateways = describe_vpn_gateways(client, **params) |
| 158 |
except AnsibleEC2Error as e: |
| 159 |
module.fail_json_aws_error(e) |
| 160 |
|
| 161 |
return [ |
| 162 |
camel_dict_to_snake_dict(get_virtual_gateway_info(vgw), ignore_list=["ResourceTags"]) |
| 163 |
for vgw in all_virtual_gateways |
| 164 |
] |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
argument_spec = dict( |
| 169 |
filters=dict(type="dict", default=dict()), |
| 170 |
vpn_gateway_ids=dict(type="list", default=None, elements="str"), |
| 171 |
) |
| 172 |
|
| 173 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 174 |
|
| 175 |
connection = module.client("ec2") |
| 176 |
results = list_virtual_gateways(connection, module) |
| 177 |
|
| 178 |
module.exit_json(virtual_gateways=results) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__": |
| 182 |
main() |