Skip to content

Commit

Permalink
modified based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Oct 16, 2024
1 parent 809b64a commit 27275f4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
9 changes: 4 additions & 5 deletions plugins/modules/ec2_transit_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@


class AnsibleEc2Tgw:
def __init__(self, module: Any, results: Dict[str, Any]) -> None:
def __init__(self, module: AnsibleAWSModule, results: Dict[str, Any]) -> None:
self._module = module
self._results = results
retry_decorator = AWSRetry.jittered_backoff(
Expand Down Expand Up @@ -302,13 +302,12 @@ def get_matching_tgw(
:return: Transit gateway object.
"""
filters = []
params = {}
if tgw_id:
filters = ansible_dict_to_boto3_filter_list({"transit-gateway-id": tgw_id})

try:
response = describe_ec2_transit_gateways(self._connection, Filters=filters)
except AnsibleEC2Error as e:
self._module.fail_json_aws(e)
params["Filters"] = filters
response = describe_ec2_transit_gateways(self._connection, **params)

tgw = None
tgws = []
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/ec2_transit_gateway_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ def setup_module_object() -> AnsibleAWSModule:
Merge argument spec and create Ansible module object.
:return: Ansible module object
"""
argument_spec = {
"transit_gateway_ids": {"type": "list", "default": [], "elements": "str", "aliases": ["transit_gateway_id"]},
"filters": {"type": "dict", "default": {}},
}
argument_spec = dict(
transit_gateway_ids=dict(type="list", default=[], elements="str", aliases=["transit_gateway_id"]),
filters=dict(type="dict", default={}),
)

module = AnsibleAWSModule(
argument_spec=argument_spec,
Expand Down
89 changes: 49 additions & 40 deletions tests/integration/targets/ec2_transit_gateway/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: 'ec2_transit_gateway integration tests'
- name: Run 'ec2_transit_gateway integration tests'
collections:
- amazon.aws
module_defaults:
Expand All @@ -10,155 +10,164 @@
region: '{{ aws_region }}'
block:

- name: generate unique value for testing
- name: Generate unique value for testing
set_fact:
tgw_description: "{{ resource_prefix }}-tgw"

- name: test create transit gateway without tags
- name: Test create transit gateway without tags
community.aws.ec2_transit_gateway:
description: "{{ tgw_description }}"
register: create_result
- name: assert changed is True
assert:

- name: Assert changed is True
ansible.builtin.assert:
that:
- create_result.changed == True

- name: test update transit gateway with tags by description
- name: Test update transit gateway with tags by description
community.aws.ec2_transit_gateway:
description: "{{ tgw_description }}"
tags:
Name: Ansible Test TGW
register: result
- name: assert changed is True
assert:

- name: Assert changed is True
ansible.builtin.assert:
that:
- result.changed == True
- result.transit_gateway.tags | length == 1
- "'Name' in result.transit_gateway.tags"

- name: test update transit gateway with new tag and purge_tags false
- name: Test update transit gateway with new tag and purge_tags false
community.aws.ec2_transit_gateway:
transit_gateway_id: '{{ create_result.transit_gateway.transit_gateway_id }}'
purge_tags: False
tags:
status: ok to delete
register: result
- name: assert changed is True and have 2 tags
assert:

- name: Assert changed is True and have 2 tags
ansible.builtin.assert:
that:
- result.changed == True
- result.transit_gateway.tags | length == 2
- "'Name' in result.transit_gateway.tags"

- name: test update transit gateway with purge_tags true
- name: Test update transit gateway with purge_tags true
community.aws.ec2_transit_gateway:
transit_gateway_id: '{{ create_result.transit_gateway.transit_gateway_id }}'
purge_tags: True
tags:
status: ok to delete
register: result
- name: assert changed is True and TGW tag is absent
assert:

- name: Assert changed is True and TGW tag is absent
ansible.builtin.assert:
that:
- result.changed == True
- result.transit_gateway.tags | length == 1
- "'Name' not in result.transit_gateway.tags"

- name: test idempotence
- name: Test idempotence
community.aws.ec2_transit_gateway:
description: "{{ tgw_description }}"
purge_tags: True
tags:
status: ok to delete
register: result
- name: assert changed is False
assert:

- name: Assert changed is False
ansible.builtin.assert:
that:
- result.changed == False

- name: generate unique value for testing
- name: Generate unique value for testing
set_fact:
tgw_description_multicast: "{{ resource_prefix }}-tgw-multicast"

- name: test create transit gateway with multicast enabled
- name: Test create transit gateway with multicast enabled
community.aws.ec2_transit_gateway:
description: "{{ tgw_description_multicast }}"
multicast_support: true
register: create_result

- name: assert changed is True
assert:
- name: Assert changed is True
ansible.builtin.assert:
that:
- create_result.changed == True

- name: test success with filter
- name: Test success with filter
community.aws.ec2_transit_gateway_info:
filters:
options.multicast-support: enable
register: result

- name: assert success with multicast-support filter
assert:
- name: Assert success with multicast-support filter
ansible.builtin.assert:
that:
- 'result.transit_gateways != []'

# ==== Combine ec2_transit_gateway_info ======================
- name: test success with no parameters
- name: Test success with no parameters
community.aws.ec2_transit_gateway_info:
register: result
- name: assert success with no parameters
assert:
- name: Assert success with no parameters
ansible.builtin.assert:
that:
- 'result.changed == false'
- 'result.transit_gateways != []'

- name: test success with single filter
- name: Test success with single filter
community.aws.ec2_transit_gateway_info:
filters:
transit-gateway-id: "{{ create_result.transit_gateway.transit_gateway_id }}"
register: result
- name: assert success with transit_gateway_id filter
assert:

- name: Assert success with transit_gateway_id filter
ansible.builtin.assert:
that:
- 'result.changed == false'
- 'result.transit_gateways != []'

- name: test empty result set for non-existent tgw id via filter
- name: Test empty result set for non-existent tgw id via filter
community.aws.ec2_transit_gateway_info:
filters:
transit-gateway-id: tgw-00000011111111122
register: result
- name: assert success with transit_gateway_id filter
assert:

- name: Assert success with transit_gateway_id filter
ansible.builtin.assert:
that:
- 'result.changed == false'
- 'result.transit_gateways == []'

- name: test NotFound exception caught and returned empty result set
- name: Test NotFound exception caught and returned empty result set
community.aws.ec2_transit_gateway_info:
transit_gateway_id: tgw-00000011111111122
register: result
- name: assert success with transit_gateway_id filter
assert:

- name: Assert success with transit_gateway_id filter
ansible.builtin.assert:
that:
- 'result.changed == false'
- 'result.transit_gateways == []'

- name: test success with multiple filters
- name: Test success with multiple filters
community.aws.ec2_transit_gateway_info:
filters:
options.dns-support: enable
options.vpn-ecmp-support: enable
register: result
- name: assert success with transit_gateway_id filter
assert:

- name: Assert success with transit_gateway_id filter
ansible.builtin.assert:
that:
- 'result.changed == false'
- 'result.transit_gateways != []'

always:
###### TEARDOWN STARTS HERE ######
- name: delete transit gateway
- name: Delete transit gateway
community.aws.ec2_transit_gateway:
description: "{{ item }}"
state: absent
Expand Down

0 comments on commit 27275f4

Please sign in to comment.