Skip to content

Commit

Permalink
properly report change if the manifest was only downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Jul 12, 2024
1 parent 73ab49e commit a7ebe34
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1473-redhat_manifest-export_changed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- redhat_manifest - properly report ``changed`` when manifest is only downloaded, not updated (https://github.com/theforeman/foreman-ansible-modules/issues/1473)
35 changes: 26 additions & 9 deletions plugins/modules/redhat_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@

import json
import os
import tempfile

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
Expand Down Expand Up @@ -277,13 +278,27 @@ def export_manifest(module, manifest):
path = "/subscription/consumers/%s/export" % (manifest['uuid'])
try:
resp, info = fetch_portal(module, path, 'GET', accept_header='application/zip')
if not module.check_mode:
with open(module.params['path'], 'wb') as f:
while True:
data = resp.read(65536) # 64K
if not data:
break
f.write(data)
fd, tempname = tempfile.mkstemp(dir=module.tmpdir)
with os.fdopen(fd, 'wb') as f:
while True:
data = resp.read(65536) # 64K
if not data:
break
f.write(data)

checksum_src = module.sha1(tempname)
checksum_dest = module.sha1(module.params['path'])

changed = checksum_src != checksum_dest

if changed and not module.check_mode:
module.atomic_move(tempname, module.params['path'])

if os.path.exists(tempname):
os.remove(tempname)

return changed

except Exception as e:
module.fail_json(msg="Failure downloading manifest, {0}".format(to_native(e)))

Expand Down Expand Up @@ -329,14 +344,16 @@ def main():
sub_changed = False

if module.params['path'] and manifest:
export_manifest(module, manifest)
export_changed = export_manifest(module, manifest)
else:
export_changed = False

if manifest:
manifest_uuid = manifest.get('uuid')
else:
manifest_uuid = None

changed = man_changed or sub_changed
changed = man_changed or sub_changed or export_changed
module.exit_json(changed=changed, uuid=manifest_uuid)


Expand Down

0 comments on commit a7ebe34

Please sign in to comment.