From 63209197ddc6acc622b5bc6bae04a2a5506de098 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Fri, 5 Apr 2019 14:48:20 -0400 Subject: [PATCH] Iterate over the pages of remediations available --- awx/playbooks/action_plugins/insights.py | 68 +++++++++++++----------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/awx/playbooks/action_plugins/insights.py b/awx/playbooks/action_plugins/insights.py index 5cb19b45cf..3a465f3c5d 100644 --- a/awx/playbooks/action_plugins/insights.py +++ b/awx/playbooks/action_plugins/insights.py @@ -19,9 +19,8 @@ class ActionModule(ActionBase): def is_stale(self, proj_path, etag): file_path = os.path.join(proj_path, '.version') try: - f = open(file_path, 'r') - version = f.read() - f.close() + with open(file_path, 'r') as f: + version = f.read() return version != etag except IOError: return True @@ -32,7 +31,6 @@ class ActionModule(ActionBase): f.write(etag) def run(self, tmp=None, task_vars=None): - self._supports_check_mode = False result = super(ActionModule, self).run(tmp, task_vars) @@ -54,34 +52,10 @@ class ActionModule(ActionBase): license ) } - url = '{}/api/remediations/v1/remediations?sort=-updated_at'.format(insights_url) + url = '/api/remediations/v1/remediations' + while url: + res = session.get('{}{}'.format(insights_url, url), headers=headers, timeout=120) - res = session.get(url, headers=headers, timeout=120) - - if res.status_code != 200: - result['failed'] = True - result['msg'] = ( - 'Expected {} to return a status code of 200 but returned status ' - 'code "{}" instead with content "{}".'.format(url, res.status_code, res.content) - ) - return result - - if 'ETag' in res.headers: - version = res.headers['ETag'] - if version.startswith('"') and version.endswith('"'): - version = version[1:-1] - else: - version = "ETAG_NOT_FOUND" - - if not self.is_stale(proj_path, version): - result['changed'] = False - result['version'] = version - return result - - for item in res.json()['remediations']: - url = '{}/api/remediations/v1/remediations/{}/playbook'.format( - insights_url, item['id']) - res = session.get(url, timeout=120) if res.status_code != 200: result['failed'] = True result['msg'] = ( @@ -89,7 +63,37 @@ class ActionModule(ActionBase): 'code "{}" instead with content "{}".'.format(url, res.status_code, res.content) ) return result - self.save_playbook(proj_path, item, res.content) + + # FIXME: ETags are (maybe?) not yet supported in the new + # API, and even if they are we'll need to put some thought + # into how to deal with them in combination with pagination. + if 'ETag' in res.headers: + version = res.headers['ETag'] + if version.startswith('"') and version.endswith('"'): + version = version[1:-1] + else: + version = "ETAG_NOT_FOUND" + + if not self.is_stale(proj_path, version): + result['changed'] = False + result['version'] = version + return result + + url = res.json()['links']['next'] # will be None if we're on the last page + + for item in res.json()['remediations']: + playbook_url = '{}/api/remediations/v1/remediations/{}/playbook'.format( + insights_url, item['id']) + res = session.get(playbook_url, timeout=120) + if res.status_code != 200: + result['failed'] = True + result['msg'] = ( + 'Expected {} to return a status code of 200 but returned status ' + 'code "{}" instead with content "{}".'.format( + playbook_url, res.status_code, res.content) + ) + return result + self.save_playbook(proj_path, item, res.content) self.write_version(proj_path, version)