Skip to content

Commit

Permalink
Ensure that waiters.get_waiter() can support a wrapped client (ansibl…
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble authored Oct 12, 2020
1 parent dc7e232 commit 711c33c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/module_utils/waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
except ImportError:
pass # caught by HAS_BOTO3

import ansible_collections.amazon.aws.plugins.module_utils.core as aws_core


ec2_data = {
"version": 2,
Expand Down Expand Up @@ -422,6 +424,8 @@ def rds_model(name):


def get_waiter(client, waiter_name):
if isinstance(client, aws_core._RetryingBotoClientWrapper):
return get_waiter(client.client, waiter_name)
try:
return waiters_by_name[(client.__class__.__name__, waiter_name)](client)
except KeyError:
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/module_utils_waiter/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
shippable/aws/group2
6 changes: 6 additions & 0 deletions tests/integration/targets/module_utils_waiter/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tests]
localhost

[all:vars]
ansible_connection=local
ansible_python_interpreter="{{ ansible_playbook_python }}"
7 changes: 7 additions & 0 deletions tests/integration/targets/module_utils_waiter/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- hosts: all
gather_facts: no
collections:
- amazon.aws
roles:
# Test the behaviour of module_utils.core.AnsibleAWSModule.client (boto3)
- 'get_waiter'
3 changes: 3 additions & 0 deletions tests/integration/targets/module_utils_waiter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
- prepare_tests
- setup_ec2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python
# Copyright (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# A bare-minimum Ansible Module based on AnsibleAWSModule used for testing some
# of the core behaviour around AWS/Boto3 connection details

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type


try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter


def main():
argument_spec = dict(
client=dict(required=True, type='str'),
waiter_name=dict(required=True, type='str'),
with_decorator=dict(required=False, type='bool', default=False),
)
module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True,
)

decorator = None
if module.params.get('with_decorator'):
decorator = AWSRetry.jittered_backoff()

client = module.client(module.params.get('client'), retry_decorator=decorator)
waiter = get_waiter(client, module.params.get('waiter_name'))

module.exit_json(changed=False, waiter_attributes=dir(waiter))


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies:
- prepare_tests
- setup_ec2
collections:
- amazon.aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- module_defaults:
example_module:
region: '{{ aws_region }}'
access_key: '{{ aws_access_key }}'
secret_key: '{{ aws_secret_key }}'
security_token: '{{ security_token | default(omit) }}'
block:
- name: 'Attempt to get a waiter (no retry decorator)'
example_module:
client: 'ec2'
waiter_name: 'internet_gateway_exists'
register: test_no_decorator

- assert:
that:
- test_no_decorator is succeeded
# Standard methods on a boto3 wrapper
- '"wait" in test_no_decorator.waiter_attributes'
- '"name" in test_no_decorator.waiter_attributes'
- '"config" in test_no_decorator.waiter_attributes'

- name: 'Attempt to get a waiter (with decorator)'
example_module:
client: 'ec2'
waiter_name: 'internet_gateway_exists'
with_decorator: True
register: test_with_decorator

- assert:
that:
- test_with_decorator is succeeded
# Standard methods on a boto3 wrapper
- '"wait" in test_with_decorator.waiter_attributes'
- '"name" in test_with_decorator.waiter_attributes'
- '"config" in test_with_decorator.waiter_attributes'
8 changes: 8 additions & 0 deletions tests/integration/targets/module_utils_waiter/runme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -eux

ANSIBLE_ROLES_PATH="../"
export ANSIBLE_ROLES_PATH

ansible-playbook main.yml -i inventory "$@"

0 comments on commit 711c33c

Please sign in to comment.