| 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_service_info |
| 9 |
short_description: Retrieves AWS VPC endpoint service details |
| 10 |
version_added: 1.5.0 |
| 11 |
description: |
| 12 |
- Gets details related to AWS VPC Endpoint Services. |
| 13 |
options: |
| 14 |
filters: |
| 15 |
description: |
| 16 |
- A dict of filters to apply. |
| 17 |
- Each dict item consists of a filter key and a filter value. |
| 18 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcEndpointServices.html) |
| 19 |
for possible filters. |
| 20 |
type: dict |
| 21 |
default: {} |
| 22 |
service_names: |
| 23 |
description: |
| 24 |
- A list of service names which can be used to narrow the search results. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
author: |
| 28 |
- Mark Chappell (@tremble) |
| 29 |
extends_documentation_fragment: |
| 30 |
- amazon.aws.common.modules |
| 31 |
- amazon.aws.region.modules |
| 32 |
- amazon.aws.boto3 |
| 33 |
""" |
| 34 |
|
| 35 |
EXAMPLES = r""" |
| 36 |
# Simple example of listing all supported AWS services for VPC endpoints |
| 37 |
- name: List supported AWS endpoint services |
| 38 |
amazon.aws.ec2_vpc_endpoint_service_info: |
| 39 |
region: ap-southeast-2 |
| 40 |
register: supported_endpoint_services |
| 41 |
""" |
| 42 |
|
| 43 |
RETURN = r""" |
| 44 |
service_names: |
| 45 |
description: List of supported AWS VPC endpoint service names. |
| 46 |
returned: success |
| 47 |
type: list |
| 48 |
sample: |
| 49 |
service_names: |
| 50 |
- com.amazonaws.ap-southeast-2.s3 |
| 51 |
service_details: |
| 52 |
description: Detailed information about the AWS VPC endpoint services. |
| 53 |
returned: success |
| 54 |
type: complex |
| 55 |
contains: |
| 56 |
service_name: |
| 57 |
returned: success |
| 58 |
description: The ARN of the endpoint service. |
| 59 |
type: str |
| 60 |
service_id: |
| 61 |
returned: success |
| 62 |
description: The ID of the endpoint service. |
| 63 |
type: str |
| 64 |
service_type: |
| 65 |
returned: success |
| 66 |
description: The type of the service |
| 67 |
type: list |
| 68 |
availability_zones: |
| 69 |
returned: success |
| 70 |
description: The Availability Zones in which the service is available. |
| 71 |
type: list |
| 72 |
owner: |
| 73 |
returned: success |
| 74 |
description: The AWS account ID of the service owner. |
| 75 |
type: str |
| 76 |
base_endpoint_dns_names: |
| 77 |
returned: success |
| 78 |
description: The DNS names for the service. |
| 79 |
type: list |
| 80 |
private_dns_name: |
| 81 |
returned: success |
| 82 |
description: The private DNS name for the service. |
| 83 |
type: str |
| 84 |
private_dns_names: |
| 85 |
returned: success |
| 86 |
description: The private DNS names assigned to the VPC endpoint service. |
| 87 |
type: list |
| 88 |
vpc_endpoint_policy_supported: |
| 89 |
returned: success |
| 90 |
description: Whether the service supports endpoint policies. |
| 91 |
type: bool |
| 92 |
acceptance_required: |
| 93 |
returned: success |
| 94 |
description: |
| 95 |
Whether VPC endpoint connection requests to the service must be |
| 96 |
accepted by the service owner. |
| 97 |
type: bool |
| 98 |
manages_vpc_endpoints: |
| 99 |
returned: success |
| 100 |
description: Whether the service manages its VPC endpoints. |
| 101 |
type: bool |
| 102 |
tags: |
| 103 |
returned: success |
| 104 |
description: A dict of tags associated with the service |
| 105 |
type: dict |
| 106 |
private_dns_name_verification_state: |
| 107 |
returned: success |
| 108 |
description: |
| 109 |
- The verification state of the VPC endpoint service. |
| 110 |
- Consumers of an endpoint service cannot use the private name when the state is not C(verified). |
| 111 |
type: str |
| 112 |
supported_ip_address_types: |
| 113 |
returned: success |
| 114 |
description: The supported IP address types. |
| 115 |
type: str |
| 116 |
""" |
| 117 |
|
| 118 |
try: |
| 119 |
import botocore |
| 120 |
except ImportError: |
| 121 |
pass # Handled by AnsibleAWSModule |
| 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_vpc_endpoint_services |
| 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 normalize_service(service): |
| 133 |
normalized = camel_dict_to_snake_dict(service, ignore_list=["Tags"]) |
| 134 |
normalized["tags"] = boto3_tag_list_to_ansible_dict(service.get("Tags")) |
| 135 |
return normalized |
| 136 |
|
| 137 |
|
| 138 |
def normalize_result(result): |
| 139 |
normalized = {} |
| 140 |
normalized["service_details"] = [normalize_service(service) for service in result.get("ServiceDetails")] |
| 141 |
normalized["service_names"] = result.get("ServiceNames", []) |
| 142 |
return normalized |
| 143 |
|
| 144 |
|
| 145 |
def list_endpoint_services(client, module: AnsibleAWSModule) -> None: |
| 146 |
try: |
| 147 |
filters = None |
| 148 |
if module.params.get("filters"): |
| 149 |
filters = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 150 |
results = describe_vpc_endpoint_services( |
| 151 |
client, filters=filters, service_names=module.params.get("service_names") |
| 152 |
) |
| 153 |
if results: |
| 154 |
results = normalize_result(results) |
| 155 |
except AnsibleEC2Error as e: |
| 156 |
module.fail_json_aws(e, msg="Failed to connect to retrieve service details") |
| 157 |
module.exit_json(changed=False, **results) |
| 158 |
|
| 159 |
|
| 160 |
def main() -> None: |
| 161 |
argument_spec = dict( |
| 162 |
filters=dict(default={}, type="dict"), |
| 163 |
service_names=dict(type="list", elements="str"), |
| 164 |
) |
| 165 |
|
| 166 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 167 |
|
| 168 |
# Validate Requirements |
| 169 |
try: |
| 170 |
client = module.client("ec2") |
| 171 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 172 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 173 |
|
| 174 |
list_endpoint_services(client, module) |
| 175 |
|
| 176 |
|
| 177 |
if __name__ == "__main__": |
| 178 |
main() |