Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY return value and check_mode implementation #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions library/insights_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,44 +106,41 @@ def run_module():
supports_check_mode=True
)

if module.check_mode:
return result

state = module.params['state']
insights_name = module.params['insights_name']
display_name = module.params['display_name']
force_reregister = module.params['force_reregister']

reg_status = subprocess.call([insights_name, '--status'])
(reg_status, _, _) = module.run_command([insights_name, '--status'])

if state == 'present':
result['original_message'] = 'Attempting to register ' + insights_name
if reg_status == 0 and not force_reregister:
result['changed'] = False
result['message'] = 'The Insights API has determined that this machine is already registered'
module.exit_json(**result)
elif reg_status == 0 and force_reregister:
subprocess.call([insights_name, '--force-reregister'])
if not module.check_mode:
module.run_command([insights_name, '--force-reregister'])
result['changed'] = True
result['message'] = 'New machine-id created - ' + insights_name + ' has been registered'
module.exit_json(**result)
else:
subprocess.call([insights_name, '--register'])
if not module.check_mode:
module.run_command([insights_name, '--register'])
result['changed'] = True
result['message'] = insights_name + ' has been registered'
module.exit_json(**result)

if state == 'absent':
result['original_message'] = 'Attempting to unregister ' + insights_name
if reg_status is not 0:
result['changed'] = False
result['message'] = insights_name + ' is already unregistered'
module.exit_json(**result)
else:
subprocess.call([insights_name, '--unregister'])
if not module.check_mode:
module.run_command([insights_name, '--unregister'])
result['changed'] = True
result['message'] = insights_name + ' has been unregistered'
module.exit_json(**result)

module.exit_json(**result)

def main():
run_module()
Expand Down