diff options
author | John Westcott IV <john.westcott.iv@redhat.com> | 2020-06-01 19:35:28 +0200 |
---|---|---|
committer | John Westcott IV <john.westcott.iv@redhat.com> | 2020-06-05 19:53:20 +0200 |
commit | 30346618f161ed9eefc1563d25b6924017f26c29 (patch) | |
tree | b346c939fdd392e0254abaf7c3a24d9e40edb9df /awx_collection | |
parent | Adding more details about tower_oauthtoken parameter (diff) | |
download | awx-30346618f161ed9eefc1563d25b6924017f26c29.tar.xz awx-30346618f161ed9eefc1563d25b6924017f26c29.zip |
Adding more through testing of tower_oauthtoken including failure messages
Diffstat (limited to 'awx_collection')
-rw-r--r-- | awx_collection/plugins/module_utils/tower_api.py | 16 | ||||
-rw-r--r-- | awx_collection/tests/integration/targets/tower_token/tasks/main.yml | 26 |
2 files changed, 37 insertions, 5 deletions
diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index f950829328..7c2f52d1ca 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -3,7 +3,7 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule, env_fallback from ansible.module_utils.urls import Request, SSLValidationError, ConnectionError -from ansible.module_utils.six import PY2 +from ansible.module_utils.six import PY2, string_types from ansible.module_utils.six.moves import StringIO from ansible.module_utils.six.moves.urllib.parse import urlparse, urlencode from ansible.module_utils.six.moves.urllib.error import HTTPError @@ -98,10 +98,16 @@ class TowerModule(AnsibleModule): # Perform magic depending on whether tower_oauthtoken is a string or a dict if self.params.get('tower_oauthtoken'): - if type(self.params.get('tower_oauthtoken')) is dict: - setattr(self, 'oauth_token', self.params.get('tower_oauthtoken')['token']) - elif 'token' in self.params.get('tower_oauthtoken'): - setattr(self, 'oauth_token', self.params.get('tower_oauthtoken')) + token_param = self.params.get('tower_oauthtoken') + if type(token_param) is dict: + if 'token' in token_param: + self.oauth_token = self.params.get('tower_oauthtoken')['token'] + else: + self.fail_json(msg="The provided dict in tower_oauthtoken did not properly contain the token entry") + elif isinstance(token_param, string_types): + self.oauth_token = self.params.get('tower_oauthtoken') + else: + self.fail_json(msg="The provided tower_oauthtoken type was not valid ({0}), please refer to ansible-doc for valid options".format(type(token_param).__name__)) # Perform some basic validation if not re.match('^https{0,1}://', self.host): diff --git a/awx_collection/tests/integration/targets/tower_token/tasks/main.yml b/awx_collection/tests/integration/targets/tower_token/tasks/main.yml index 86edd620d7..52e5bb41d8 100644 --- a/awx_collection/tests/integration/targets/tower_token/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_token/tasks/main.yml @@ -3,6 +3,30 @@ set_fact: token_description: "AWX-Collection-tests-tower_token-description-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" +- name: Try to use a token as a dict which is missing the token parameter + tower_job_list: + tower_oauthtoken: + not_token: "This has no token entry" + register: results + ignore_errors: True + +- assert: + that: + - results is failed + - '"The provided dict in tower_oauthtoken did not properly contain the token entry" == results.msg' + +- name: Try to use a token as a list + tower_job_list: + tower_oauthtoken: + - dummy_token + register: results + ignore_errors: True + +- assert: + that: + - results is failed + - '"The provided tower_oauthtoken type was not valid (list), please refer to ansible-doc for valid options" == results.msg' + - block: - name: Create a Token tower_token: @@ -14,10 +38,12 @@ - name: Validate our token works by token tower_job_list: tower_oauthtoken: "{{ tower_token.token }}" + register: job_list - name: Validate out token works by object tower_job_list: tower_oauthtoken: "{{ tower_token }}" + register: job_list always: - name: Delete our Token with our own token |