mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 14:57:39 -02:30
Iterate over the pages of remediations available
This commit is contained in:
@@ -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,34 +52,10 @@ 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('{}{}'.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:
|
if res.status_code != 200:
|
||||||
result['failed'] = True
|
result['failed'] = True
|
||||||
result['msg'] = (
|
result['msg'] = (
|
||||||
@@ -89,7 +63,37 @@ class ActionModule(ActionBase):
|
|||||||
'code "{}" instead with content "{}".'.format(url, res.status_code, res.content)
|
'code "{}" instead with content "{}".'.format(url, res.status_code, res.content)
|
||||||
)
|
)
|
||||||
return result
|
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)
|
self.write_version(proj_path, version)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user