Skip to content

Commit

Permalink
Add Request time out option for collection (#14157)
Browse files Browse the repository at this point in the history
Co-authored-by: Jessica Steurer <[email protected]>
  • Loading branch information
sean-m-sullivan and jay-steurer authored Aug 3, 2023
1 parent 9bb6786 commit abc5630
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions awx_collection/plugins/doc_fragments/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class ModuleDocFragment(object):
- If value not set, will try environment variable C(CONTROLLER_VERIFY_SSL) and then config files
type: bool
aliases: [ tower_verify_ssl ]
request_timeout:
description:
- Specify the timeout Ansible should use in requests to the controller host.
- Defaults to 10s, but this is handled by the shared module_utils code
type: float
controller_config_file:
description:
- Path to the controller config file.
Expand Down
8 changes: 8 additions & 0 deletions awx_collection/plugins/doc_fragments/auth_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class ModuleDocFragment(object):
why: Collection name change
alternatives: 'CONTROLLER_VERIFY_SSL'
aliases: [ validate_certs ]
request_timeout:
description:
- Specify the timeout Ansible should use in requests to the controller host.
- Defaults to 10 seconds
- This will not work with the export or import modules.
type: float
env:
- name: CONTROLLER_REQUEST_TIMEOUT
notes:
- If no I(config_file) is provided we will attempt to use the tower-cli library
Expand Down
16 changes: 14 additions & 2 deletions awx_collection/plugins/module_utils/controller_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ControllerModule(AnsibleModule):
controller_username=dict(required=False, aliases=['tower_username'], fallback=(env_fallback, ['CONTROLLER_USERNAME', 'TOWER_USERNAME'])),
controller_password=dict(no_log=True, aliases=['tower_password'], required=False, fallback=(env_fallback, ['CONTROLLER_PASSWORD', 'TOWER_PASSWORD'])),
validate_certs=dict(type='bool', aliases=['tower_verify_ssl'], required=False, fallback=(env_fallback, ['CONTROLLER_VERIFY_SSL', 'TOWER_VERIFY_SSL'])),
request_timeout=dict(type='float', required=False, fallback=(env_fallback, ['CONTROLLER_REQUEST_TIMEOUT'])),
controller_oauthtoken=dict(
type='raw', no_log=True, aliases=['tower_oauthtoken'], required=False, fallback=(env_fallback, ['CONTROLLER_OAUTH_TOKEN', 'TOWER_OAUTH_TOKEN'])
),
Expand All @@ -63,12 +64,14 @@ class ControllerModule(AnsibleModule):
'username': 'controller_username',
'password': 'controller_password',
'verify_ssl': 'validate_certs',
'request_timeout': 'request_timeout',
'oauth_token': 'controller_oauthtoken',
}
host = '127.0.0.1'
username = None
password = None
verify_ssl = True
request_timeout = 10
oauth_token = None
oauth_token_id = None
authenticated = False
Expand Down Expand Up @@ -304,7 +307,7 @@ def __init__(self, argument_spec, direct_params=None, error_callback=None, warn_
kwargs['supports_check_mode'] = True

super().__init__(argument_spec=argument_spec, direct_params=direct_params, error_callback=error_callback, warn_callback=warn_callback, **kwargs)
self.session = Request(cookies=CookieJar(), validate_certs=self.verify_ssl)
self.session = Request(cookies=CookieJar(), timeout=self.request_timeout, validate_certs=self.verify_ssl)

if 'update_secrets' in self.params:
self.update_secrets = self.params.pop('update_secrets')
Expand Down Expand Up @@ -500,7 +503,14 @@ def make_request(self, method, endpoint, *args, **kwargs):
data = dumps(kwargs.get('data', {}))

try:
response = self.session.open(method, url.geturl(), headers=headers, validate_certs=self.verify_ssl, follow_redirects=True, data=data)
response = self.session.open(
method, url.geturl(),
headers=headers,
timeout=self.request_timeout,
validate_certs=self.verify_ssl,
follow_redirects=True,
data=data
)
except (SSLValidationError) as ssl_err:
self.fail_json(msg="Could not establish a secure connection to your host ({1}): {0}.".format(url.netloc, ssl_err))
except (ConnectionError) as con_err:
Expand Down Expand Up @@ -612,6 +622,7 @@ def authenticate(self, **kwargs):
'POST',
api_token_url,
validate_certs=self.verify_ssl,
timeout=self.request_timeout,
follow_redirects=True,
force_basic_auth=True,
url_username=self.username,
Expand Down Expand Up @@ -988,6 +999,7 @@ def logout(self):
'DELETE',
api_token_url,
validate_certs=self.verify_ssl,
timeout=self.request_timeout,
follow_redirects=True,
force_basic_auth=True,
url_username=self.username,
Expand Down
17 changes: 17 additions & 0 deletions awx_collection/tests/integration/targets/project/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@
that:
- result is not changed

- name: Create a git project and wait with short request timeout.
project:
name: "{{ project_name1 }}"
organization: Default
scm_type: git
scm_url: https://github.com/ansible/test-playbooks
wait: true
state: exists
request_timeout: .001
register: result
ignore_errors: true

- assert:
that:
- result is failed
- "'timed out' in result.msg"

- name: Delete a git project without credentials and wait
project:
name: "{{ project_name1 }}"
Expand Down

0 comments on commit abc5630

Please sign in to comment.