-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new module api_gateway_info, api_gateway: new parameter lookup
- Loading branch information
Showing
8 changed files
with
508 additions
and
29 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
changelogs/fragments/20230620-api_gateway-add-optional-name.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
minor_changes: | ||
- api_gateway - add parameter `name` allowing user to choose the name of the API gateway to create, default to `ansible-temp-api` for backward compatibility. | ||
|
||
- api_gateway - add parameter `name` allowing user to choose the name of the API gateway to create, default to `ansible-temp-api` for backward compatibility (https://github.com/ansible-collections/community.aws/pull/1845). | ||
- api_gateway - add parameter `lookup` to create API based on supplied tags (https://github.com/ansible-collections/community.aws/pull/1845). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: Contributors to the Ansible project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
DOCUMENTATION = r""" | ||
--- | ||
module: api_gateway_info | ||
version_added: 6.1.0 | ||
short_description: Gather information about ec2 instances in AWS | ||
description: | ||
- Gather information about ec2 instances in AWS | ||
options: | ||
ids: | ||
description: | ||
- The list of the string identifiers of the associated RestApis. | ||
type: list | ||
elements: str | ||
author: | ||
- Aubin Bikouo (@abikouo) | ||
extends_documentation_fragment: | ||
- amazon.aws.common.modules | ||
- amazon.aws.region.modules | ||
- amazon.aws.boto3 | ||
""" | ||
|
||
EXAMPLES = r""" | ||
--- | ||
# List all API gateway | ||
- name: List all for a specific function | ||
community.aws.api_gateway_info: | ||
# Get information for a specific API gateway | ||
- name: List all for a specific function | ||
community.aws.api_gateway_info: | ||
ids: | ||
- 012345678a | ||
- abcdefghij | ||
""" | ||
|
||
RETURN = r""" | ||
--- | ||
rest_apis: | ||
description: A list of API gateway. | ||
returned: always | ||
type: complex | ||
contains: | ||
name: | ||
description: The name of the API. | ||
returned: success | ||
type: str | ||
sample: 'ansible-tmp-api' | ||
id: | ||
description: The identifier of the API. | ||
returned: success | ||
type: str | ||
sample: 'abcdefgh' | ||
api_key_source: | ||
description: The source of the API key for metering requests according to a usage plan. | ||
returned: success | ||
type: str | ||
sample: 'HEADER' | ||
created_date: | ||
description: The timestamp when the API was created. | ||
returned: success | ||
type: str | ||
sample: "2020-01-01T11:37:59+00:00" | ||
description: | ||
description: The description of the API. | ||
returned: success | ||
type: str | ||
sample: "Automatic deployment by Ansible." | ||
disable_execute_api_endpoint: | ||
description: Specifies whether clients can invoke your API by using the default execute-api endpoint. | ||
returned: success | ||
type: bool | ||
sample: False | ||
endpoint_configuration: | ||
description: The endpoint configuration of this RestApi showing the endpoint types of the API. | ||
returned: success | ||
type: dict | ||
sample: {"types": ["REGIONAL"]} | ||
tags: | ||
description: The collection of tags. | ||
returned: success | ||
type: dict | ||
sample: {"key": "value"} | ||
""" | ||
|
||
|
||
try: | ||
import botocore | ||
except ImportError: | ||
pass # caught by AnsibleAWSModule | ||
|
||
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict | ||
|
||
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code | ||
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule | ||
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry | ||
|
||
|
||
@AWSRetry.jittered_backoff() | ||
def _list_rest_apis(connection, **params): | ||
paginator = connection.get_paginator("get_rest_apis") | ||
return paginator.paginate(**params).build_full_result().get("items", []) | ||
|
||
|
||
@AWSRetry.jittered_backoff() | ||
def _describe_rest_api(connection, module, rest_api_id): | ||
try: | ||
response = connection.get_rest_api(restApiId=rest_api_id) | ||
response.pop("ResponseMetadata") | ||
except is_boto3_error_code("ResourceNotFoundException"): | ||
response = {} | ||
except ( | ||
botocore.exceptions.ClientError, | ||
botocore.exceptions.BotoCoreError, | ||
) as e: # pylint: disable=duplicate-except | ||
module.fail_json_aws(e, msg="Trying to get Rest API '{0}'.".format(rest_api_id)) | ||
return response | ||
|
||
|
||
def main(): | ||
argument_spec = dict( | ||
ids=dict(type="list", elements="str"), | ||
) | ||
|
||
module = AnsibleAWSModule( | ||
argument_spec=argument_spec, | ||
supports_check_mode=True, | ||
) | ||
|
||
try: | ||
connection = module.client("apigateway") | ||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: | ||
module.fail_json_aws(e, msg="Failed to connect to AWS") | ||
|
||
ids = module.params.get("ids") | ||
if ids: | ||
rest_apis = [] | ||
for rest_api_id in ids: | ||
result = _describe_rest_api(connection, module, rest_api_id) | ||
if result: | ||
rest_apis.append(result) | ||
else: | ||
rest_apis = _list_rest_apis(connection) | ||
|
||
# Turn the boto3 result in to ansible_friendly_snaked_names | ||
snaked_rest_apis = [camel_dict_to_snake_dict(item) for item in rest_apis] | ||
module.exit_json(changed=False, rest_apis=snaked_rest_apis) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
api_names: | ||
- "ansible-api-{{ resource_prefix }}-1" | ||
- "ansible-api-{{ resource_prefix }}-2" | ||
resource_tags: | ||
- gateway_name: "ansible-api-{{ resource_prefix }}" | ||
ansible_test: "{{ resource_prefix }}-1" | ||
- gateway_name: "ansible-api-{{ resource_prefix }}" | ||
ansible_test: "{{ resource_prefix }}-2" |
Oops, something went wrong.