mirror of
https://github.com/ansible/awx.git
synced 2024-10-30 22:21:13 +03:00
Iterate over the pages of remediations available
This commit is contained in:
parent
af2484cd97
commit
63209197dd
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user