| 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 |
--- |
| 9 |
module: api_gateway_info |
| 10 |
version_added: 6.1.0 |
| 11 |
short_description: Gather information about ec2 instances in AWS |
| 12 |
description: |
| 13 |
- Gather information about ec2 instances in AWS |
| 14 |
options: |
| 15 |
ids: |
| 16 |
description: |
| 17 |
- The list of the string identifiers of the associated RestApis. |
| 18 |
type: list |
| 19 |
elements: str |
| 20 |
author: |
| 21 |
- Aubin Bikouo (@abikouo) |
| 22 |
extends_documentation_fragment: |
| 23 |
- amazon.aws.common.modules |
| 24 |
- amazon.aws.region.modules |
| 25 |
- amazon.aws.boto3 |
| 26 |
""" |
| 27 |
|
| 28 |
EXAMPLES = r""" |
| 29 |
--- |
| 30 |
# List all API gateway |
| 31 |
- name: List all for a specific function |
| 32 |
community.aws.api_gateway_info: |
| 33 |
|
| 34 |
# Get information for a specific API gateway |
| 35 |
- name: List all for a specific function |
| 36 |
community.aws.api_gateway_info: |
| 37 |
ids: |
| 38 |
- 012345678a |
| 39 |
- abcdefghij |
| 40 |
""" |
| 41 |
|
| 42 |
RETURN = r""" |
| 43 |
--- |
| 44 |
rest_apis: |
| 45 |
description: A list of API gateway. |
| 46 |
returned: always |
| 47 |
type: complex |
| 48 |
contains: |
| 49 |
name: |
| 50 |
description: The name of the API. |
| 51 |
returned: success |
| 52 |
type: str |
| 53 |
sample: 'ansible-tmp-api' |
| 54 |
id: |
| 55 |
description: The identifier of the API. |
| 56 |
returned: success |
| 57 |
type: str |
| 58 |
sample: 'abcdefgh' |
| 59 |
api_key_source: |
| 60 |
description: The source of the API key for metering requests according to a usage plan. |
| 61 |
returned: success |
| 62 |
type: str |
| 63 |
sample: 'HEADER' |
| 64 |
created_date: |
| 65 |
description: The timestamp when the API was created. |
| 66 |
returned: success |
| 67 |
type: str |
| 68 |
sample: "2020-01-01T11:37:59+00:00" |
| 69 |
description: |
| 70 |
description: The description of the API. |
| 71 |
returned: success |
| 72 |
type: str |
| 73 |
sample: "Automatic deployment by Ansible." |
| 74 |
disable_execute_api_endpoint: |
| 75 |
description: Specifies whether clients can invoke your API by using the default execute-api endpoint. |
| 76 |
returned: success |
| 77 |
type: bool |
| 78 |
sample: False |
| 79 |
endpoint_configuration: |
| 80 |
description: The endpoint configuration of this RestApi showing the endpoint types of the API. |
| 81 |
returned: success |
| 82 |
type: dict |
| 83 |
sample: {"types": ["REGIONAL"]} |
| 84 |
tags: |
| 85 |
description: The collection of tags. |
| 86 |
returned: success |
| 87 |
type: dict |
| 88 |
sample: {"key": "value"} |
| 89 |
""" |
| 90 |
|
| 91 |
|
| 92 |
try: |
| 93 |
import botocore |
| 94 |
except ImportError: |
| 95 |
pass # caught by AnsibleAWSModule |
| 96 |
|
| 97 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 98 |
|
| 99 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 100 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 101 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 102 |
|
| 103 |
|
| 104 |
@AWSRetry.jittered_backoff() |
| 105 |
def _list_rest_apis(connection, **params): |
| 106 |
paginator = connection.get_paginator("get_rest_apis") |
| 107 |
return paginator.paginate(**params).build_full_result().get("items", []) |
| 108 |
|
| 109 |
|
| 110 |
@AWSRetry.jittered_backoff() |
| 111 |
def _describe_rest_api(connection, module, rest_api_id): |
| 112 |
try: |
| 113 |
response = connection.get_rest_api(restApiId=rest_api_id) |
| 114 |
response.pop("ResponseMetadata") |
| 115 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 116 |
response = {} |
| 117 |
except ( |
| 118 |
botocore.exceptions.ClientError, |
| 119 |
botocore.exceptions.BotoCoreError, |
| 120 |
) as e: # pylint: disable=duplicate-except |
| 121 |
module.fail_json_aws(e, msg=f"Trying to get Rest API '{rest_api_id}'.") |
| 122 |
return response |
| 123 |
|
| 124 |
|
| 125 |
def main(): |
| 126 |
argument_spec = dict( |
| 127 |
ids=dict(type="list", elements="str"), |
| 128 |
) |
| 129 |
|
| 130 |
module = AnsibleAWSModule( |
| 131 |
argument_spec=argument_spec, |
| 132 |
supports_check_mode=True, |
| 133 |
) |
| 134 |
|
| 135 |
try: |
| 136 |
connection = module.client("apigateway") |
| 137 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 138 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 139 |
|
| 140 |
ids = module.params.get("ids") |
| 141 |
if ids: |
| 142 |
rest_apis = [] |
| 143 |
for rest_api_id in ids: |
| 144 |
result = _describe_rest_api(connection, module, rest_api_id) |
| 145 |
if result: |
| 146 |
rest_apis.append(result) |
| 147 |
else: |
| 148 |
rest_apis = _list_rest_apis(connection) |
| 149 |
|
| 150 |
# Turn the boto3 result in to ansible_friendly_snaked_names |
| 151 |
snaked_rest_apis = [camel_dict_to_snake_dict(item) for item in rest_apis] |
| 152 |
module.exit_json(changed=False, rest_apis=snaked_rest_apis) |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__": |
| 156 |
main() |