| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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 |
module: ec2_vpc_endpoint_info |
| 9 |
short_description: Retrieves AWS VPC endpoints details using AWS methods |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Gets various details related to AWS VPC endpoints. |
| 13 |
options: |
| 14 |
vpc_endpoint_ids: |
| 15 |
description: |
| 16 |
- The IDs of specific endpoints to retrieve the details of. |
| 17 |
type: list |
| 18 |
elements: str |
| 19 |
filters: |
| 20 |
description: |
| 21 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 22 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcEndpoints.html) |
| 23 |
for possible filters. |
| 24 |
type: dict |
| 25 |
default: {} |
| 26 |
author: |
| 27 |
- Karen Cheng (@Etherdaemon) |
| 28 |
extends_documentation_fragment: |
| 29 |
- amazon.aws.common.modules |
| 30 |
- amazon.aws.region.modules |
| 31 |
- amazon.aws.boto3 |
| 32 |
notes: |
| 33 |
- Support for the I(query) parameter was dropped in release 6.0.0. This module now only queries |
| 34 |
for endpoints. Information about endpoint services can be retrieved using the |
| 35 |
M(amazon.aws.ec2_vpc_endpoint_service_info) module. |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
# Simple example of listing all support AWS services for VPC endpoints |
| 40 |
- name: Get all endpoints in ap-southeast-2 region |
| 41 |
amazon.aws.ec2_vpc_endpoint_info: |
| 42 |
region: ap-southeast-2 |
| 43 |
register: existing_endpoints |
| 44 |
|
| 45 |
- name: Get all endpoints with specific filters |
| 46 |
amazon.aws.ec2_vpc_endpoint_info: |
| 47 |
region: ap-southeast-2 |
| 48 |
filters: |
| 49 |
vpc-id: |
| 50 |
- vpc-12345678 |
| 51 |
- vpc-87654321 |
| 52 |
vpc-endpoint-state: |
| 53 |
- available |
| 54 |
- pending |
| 55 |
register: existing_endpoints |
| 56 |
|
| 57 |
- name: Get details on specific endpoint |
| 58 |
amazon.aws.ec2_vpc_endpoint_info: |
| 59 |
region: ap-southeast-2 |
| 60 |
vpc_endpoint_ids: |
| 61 |
- vpce-12345678 |
| 62 |
register: endpoint_details |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = r""" |
| 66 |
vpc_endpoints: |
| 67 |
description: |
| 68 |
- A list of matching endpoints. |
| 69 |
returned: always |
| 70 |
type: list |
| 71 |
elements: dict |
| 72 |
contains: |
| 73 |
creation_timestamp: |
| 74 |
description: The date and time that the endpoint was created. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
dns_entries: |
| 78 |
description: List of DNS entires for the endpoint. |
| 79 |
returned: always |
| 80 |
type: list |
| 81 |
elements: dict |
| 82 |
contains: |
| 83 |
dns_name: |
| 84 |
description: The DNS name. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
hosted_zone_id: |
| 88 |
description: The ID of the private hosted zone. |
| 89 |
type: str |
| 90 |
groups: |
| 91 |
description: List of security groups associated with the network interface. |
| 92 |
returned: always |
| 93 |
type: list |
| 94 |
elements: dict |
| 95 |
contains: |
| 96 |
group_id: |
| 97 |
description: The ID of the security group. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
group_name: |
| 101 |
description: The name of the security group. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
ip_address_type: |
| 105 |
description: The IP address type for the endpoint. |
| 106 |
type: str |
| 107 |
network_interface_ids: |
| 108 |
description: List of network interfaces for the endpoint. |
| 109 |
returned: always |
| 110 |
type: list |
| 111 |
elements: str |
| 112 |
owner_id: |
| 113 |
description: The ID of the AWS account that owns the endpoint. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
policy_document: |
| 117 |
description: The policy document associated with the endpoint. |
| 118 |
returned: always |
| 119 |
type: str |
| 120 |
private_dns_enabled: |
| 121 |
description: Indicates whether the VPC is associated with a private hosted zone. |
| 122 |
returned: always |
| 123 |
type: bool |
| 124 |
requester_managed: |
| 125 |
description: Indicated whether the endpoint is being managed by its service. |
| 126 |
returned: always |
| 127 |
type: bool |
| 128 |
route_table_ids: |
| 129 |
description: List of route table IDs associated with the endpoint. |
| 130 |
returned: always |
| 131 |
type: list |
| 132 |
elements: str |
| 133 |
service_name: |
| 134 |
description: The name of the service to which the endpoint is associated. |
| 135 |
returned: always |
| 136 |
type: str |
| 137 |
state: |
| 138 |
description: The state of the endpoint. |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
subnet_ids: |
| 142 |
description: List of subnets associated with the endpoint. |
| 143 |
returned: always |
| 144 |
type: list |
| 145 |
tags: |
| 146 |
description: List of tags associated with the endpoint. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
elements: dict |
| 150 |
vpc_endpoint_id: |
| 151 |
description: The ID of the endpoint. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
vpc_endpoint_type: |
| 155 |
description: The type of endpoint. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
vpc_id: |
| 159 |
description: The ID of the VPC. |
| 160 |
returned: always |
| 161 |
type: str |
| 162 |
sample: |
| 163 |
vpc_endpoints: |
| 164 |
- creation_timestamp: "2017-02-16T11:06:48+00:00" |
| 165 |
policy_document: > |
| 166 |
"{\"Version\":\"2012-10-17\",\"Id\":\"Policy1450910922815\", |
| 167 |
\"Statement\":[{\"Sid\":\"Stmt1450910920641\",\"Effect\":\"Allow\", |
| 168 |
\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":[\"arn:aws:s3:::*/*\",\"arn:aws:s3:::*\"]}]}" |
| 169 |
route_table_ids: |
| 170 |
- rtb-abcd1234 |
| 171 |
service_name: "com.amazonaws.ap-southeast-2.s3" |
| 172 |
state: "available" |
| 173 |
vpc_endpoint_id: "vpce-abbad0d0" |
| 174 |
vpc_id: "vpc-1111ffff" |
| 175 |
""" |
| 176 |
|
| 177 |
from typing import Any |
| 178 |
from typing import Dict |
| 179 |
from typing import List |
| 180 |
|
| 181 |
try: |
| 182 |
import botocore |
| 183 |
except ImportError: |
| 184 |
pass # Handled by AnsibleAWSModule |
| 185 |
|
| 186 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 187 |
|
| 188 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import normalize_boto3_result |
| 189 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 190 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_endpoints |
| 191 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 192 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 193 |
|
| 194 |
|
| 195 |
def get_endpoints(client, module: AnsibleAWSModule) -> Dict[str, List[Dict[str, Any]]]: |
| 196 |
results = list() |
| 197 |
params = dict() |
| 198 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 199 |
if module.params.get("vpc_endpoint_ids"): |
| 200 |
params["VpcEndpointIds"] = module.params.get("vpc_endpoint_ids") |
| 201 |
try: |
| 202 |
results = describe_vpc_endpoints(client, **params) |
| 203 |
if not results: |
| 204 |
module.exit_json( |
| 205 |
msg=f"VpcEndpoint {module.params.get('vpc_endpoint_ids')} does not exist", vpc_endpoints=[] |
| 206 |
) |
| 207 |
results = normalize_boto3_result(results) |
| 208 |
except AnsibleEC2Error as e: |
| 209 |
module.fail_json_aws(e, msg="Failed to get endpoints") |
| 210 |
|
| 211 |
return dict(vpc_endpoints=[camel_dict_to_snake_dict(result) for result in results]) |
| 212 |
|
| 213 |
|
| 214 |
def main() -> None: |
| 215 |
argument_spec = dict( |
| 216 |
filters=dict(default={}, type="dict"), |
| 217 |
vpc_endpoint_ids=dict(type="list", elements="str"), |
| 218 |
) |
| 219 |
|
| 220 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 221 |
|
| 222 |
# Validate Requirements |
| 223 |
try: |
| 224 |
connection = module.client("ec2") |
| 225 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 226 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 227 |
|
| 228 |
results = get_endpoints(connection, module) |
| 229 |
|
| 230 |
module.exit_json(**results) |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
main() |