Skip to content

Commit

Permalink
Add check_mode support and integration tests for aws_region_info (#139)
Browse files Browse the repository at this point in the history
* Add integration tests for aws_region_info

* Add support for check_mode

* aws_region_info: clarify "-" vs "_" precedence and make the implementation deterministic
  • Loading branch information
tremble authored Jul 30, 2020
1 parent d4e4d3e commit 39b140a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
18 changes: 12 additions & 6 deletions plugins/modules/aws_region_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
options:
filters:
description:
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. See
U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRegions.html) for
possible filters. Filter names and values are case sensitive. You can also use underscores
instead of dashes (-) in the filter keys, which will take precedence in case of conflict.
- A dict of filters to apply.
- Each dict item consists of a filter key and a filter value.
- See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRegions.html) for possible filters.
- Filter names and values are case sensitive.
- You can use underscores instead of dashes (-) in the filter keys.
- Filter keys with underscores will take precedence in case of conflict.
default: {}
type: dict
extends_documentation_fragment:
Expand Down Expand Up @@ -69,14 +71,18 @@ def main():
filters=dict(default={}, type='dict')
)

module = AnsibleAWSModule(argument_spec=argument_spec)
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
if module._name == 'aws_region_facts':
module.deprecate("The 'aws_region_facts' module has been renamed to 'aws_region_info'", date='2021-12-01', collection_name='community.aws')

connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff())

# Replace filter key underscores with dashes, for compatibility
sanitized_filters = dict((k.replace('_', '-'), v) for k, v in module.params.get('filters').items())
sanitized_filters = dict(module.params.get('filters'))
for k in module.params.get('filters').keys():
if "_" in k:
sanitized_filters[k.replace('_', '-')] = sanitized_filters[k]
del sanitized_filters[k]

try:
regions = connection.describe_regions(
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/aws_region_info/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
shippable/aws/group4
5 changes: 5 additions & 0 deletions tests/integration/targets/aws_region_info/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- hosts: localhost
connection: local
environment: "{{ ansible_test.environment }}"
tasks:
- include_tasks: 'tasks/tests.yml'
2 changes: 2 additions & 0 deletions tests/integration/targets/aws_region_info/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_remote_tmp_dir
107 changes: 107 additions & 0 deletions tests/integration/targets/aws_region_info/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
- module_defaults:
group/aws:
aws_access_key: '{{ aws_access_key | default(omit) }}'
aws_secret_key: '{{ aws_secret_key | default(omit) }}'
security_token: '{{ security_token | default(omit) }}'
region: '{{ aws_region | default(omit) }}'

block:
- name: 'List available Regions'
aws_region_info:
register: regions

- name: check task return attributes
vars:
first_region: '{{ regions.regions[0] }}'
assert:
that:
- regions is successful
- regions is not changed
- '"regions" in regions'
- '"endpoint" in first_region'
- '"opt_in_status" in first_region'
- '"region_name" in first_region'

- name: 'List available Regions - check_mode'
aws_region_info:
register: check_regions

- name: check task return attributes - check_mode
vars:
first_region: '{{ check_regions.regions[0] }}'
assert:
that:
- check_regions is successful
- check_regions is not changed
- '"regions" in check_regions'
- '"endpoint" in first_region'
- '"opt_in_status" in first_region'
- '"region_name" in first_region'

- name: 'Filter available Regions using - ("region-name")'
aws_region_info:
filters:
region-name: 'us-west-1'
register: us_west_1

- name: check task return attributes - filtering using -
vars:
first_region: '{{ us_west_1.regions[0] }}'
assert:
that:
- us_west_1 is successful
- us_west_1 is not changed
- '"regions" in us_west_1'
- us_west_1.regions | length == 1
- '"endpoint" in first_region'
- first_region.endpoint == 'ec2.us-west-1.amazonaws.com'
- '"opt_in_status" in first_region'
- first_region.opt_in_status == 'opt-in-not-required'
- '"region_name" in first_region'
- first_region.region_name == 'us-west-1'

- name: 'Filter available Regions using _ ("region_name")'
aws_region_info:
filters:
region_name: 'us-west-2'
register: us_west_2

- name: check task return attributes - filtering using _
vars:
first_region: '{{ us_west_2.regions[0] }}'
assert:
that:
- us_west_2 is successful
- us_west_2 is not changed
- '"regions" in us_west_2'
- us_west_2.regions | length == 1
- '"endpoint" in first_region'
- first_region.endpoint == 'ec2.us-west-2.amazonaws.com'
- '"opt_in_status" in first_region'
- first_region.opt_in_status == 'opt-in-not-required'
- '"region_name" in first_region'
- first_region.region_name == 'us-west-2'

- name: 'Filter available Regions using _ and - to check precedence'
aws_region_info:
filters:
region-name: 'eu-west-1'
region_name: 'eu-central-1'
register: regions_prededence

- name: check task return attributes - precedence
vars:
first_region: '{{ regions_prededence.regions[0] }}'
assert:
that:
- regions_prededence is successful
- regions_prededence is not changed
- '"regions" in regions_prededence'
- regions_prededence.regions | length == 1
- '"endpoint" in first_region'
- first_region.endpoint == 'ec2.eu-central-1.amazonaws.com'
- '"opt_in_status" in first_region'
- first_region.opt_in_status == 'opt-in-not-required'
- '"region_name" in first_region'
- first_region.region_name == 'eu-central-1'

0 comments on commit 39b140a

Please sign in to comment.