Iterate over the pages of remediations available

This commit is contained in:
Jeff Bradberry
2019-04-05 14:48:20 -04:00
parent af2484cd97
commit 63209197dd

View File

@@ -19,9 +19,8 @@ class ActionModule(ActionBase):
def is_stale(self, proj_path, etag): def is_stale(self, proj_path, etag):
file_path = os.path.join(proj_path, '.version') file_path = os.path.join(proj_path, '.version')
try: try:
f = open(file_path, 'r') with open(file_path, 'r') as f:
version = f.read() version = f.read()
f.close()
return version != etag return version != etag
except IOError: except IOError:
return True return True
@@ -32,7 +31,6 @@ class ActionModule(ActionBase):
f.write(etag) f.write(etag)
def run(self, tmp=None, task_vars=None): def run(self, tmp=None, task_vars=None):
self._supports_check_mode = False self._supports_check_mode = False
result = super(ActionModule, self).run(tmp, task_vars) result = super(ActionModule, self).run(tmp, task_vars)
@@ -54,9 +52,9 @@ class ActionModule(ActionBase):
license license
) )
} }
url = '{}/api/remediations/v1/remediations?sort=-updated_at'.format(insights_url) url = '/api/remediations/v1/remediations'
while url:
res = session.get(url, headers=headers, timeout=120) res = session.get('{}{}'.format(insights_url, url), headers=headers, timeout=120)
if res.status_code != 200: if res.status_code != 200:
result['failed'] = True result['failed'] = True
@@ -66,6 +64,9 @@ class ActionModule(ActionBase):
) )
return result return result
# 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: if 'ETag' in res.headers:
version = res.headers['ETag'] version = res.headers['ETag']
if version.startswith('"') and version.endswith('"'): if version.startswith('"') and version.endswith('"'):
@@ -78,15 +79,18 @@ class ActionModule(ActionBase):
result['version'] = version result['version'] = version
return result return result
url = res.json()['links']['next'] # will be None if we're on the last page
for item in res.json()['remediations']: for item in res.json()['remediations']:
url = '{}/api/remediations/v1/remediations/{}/playbook'.format( playbook_url = '{}/api/remediations/v1/remediations/{}/playbook'.format(
insights_url, item['id']) insights_url, item['id'])
res = session.get(url, timeout=120) res = session.get(playbook_url, timeout=120)
if res.status_code != 200: if res.status_code != 200:
result['failed'] = True result['failed'] = True
result['msg'] = ( result['msg'] = (
'Expected {} to return a status code of 200 but returned status ' 'Expected {} to return a status code of 200 but returned status '
'code "{}" instead with content "{}".'.format(url, res.status_code, res.content) 'code "{}" instead with content "{}".'.format(
playbook_url, res.status_code, res.content)
) )
return result return result
self.save_playbook(proj_path, item, res.content) self.save_playbook(proj_path, item, res.content)