Modified modules to use new tower_api format

Fixed variable name typo
This commit is contained in:
John Westcott IV
2020-01-29 11:52:09 -05:00
committed by beeankha
parent 6d90cac3f9
commit c23d605a7a
6 changed files with 38 additions and 70 deletions

View File

@@ -124,8 +124,6 @@ def main():
kind = module.params.get('kind')
state = module.params.get('state')
json_output = {'credential_type': name, 'state': state}
# Deal with check mode
module.default_check_mode()
@@ -149,23 +147,17 @@ def main():
}
})
json_output['existing_credential_type'] = credential_type
if state == 'absent' and not credential_type:
# If the state was absent and we had no credential_type, we can just return
module.exit_json(**module.json_output)
elif state == 'absent' and credential_type:
# If the state was absent and we had a team, we can try to delete it, the module will handle exiting from this
module.delete_endpoint('credential_types/{0}'.format(credential_type['id']), item_type='credential type', item_name=name, **{})
elif state == 'present' and not credential_type:
# if the state was present and we couldn't find a credential_type we can build one, the module will handle exiting on its own
module.post_endpoint('credential_types', item_type='credential type', item_name=name, **{
'data': credential_type_params
})
else:
# If the state was present and we had a credential_type we can see if we need to update it
# This will handle existing on its own
module.update_if_needed(credential_type, credential_type_params)
# Add entries to json_output to match old module
module.json_output['credential_type'] = name
module.json_output['state'] = state
module.json_output['existing_credential_type'] = credential_type
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(credential_type)
elif state == 'present':
# If the state was present we can let the module build or update the existing team, this will return on its own
module.create_or_update_if_needed(credential_type, credential_type_params, endpoint='credential_types', item_type='credential type')
if __name__ == '__main__':
main()

View File

@@ -127,23 +127,16 @@ def main():
'host_filter': host_filter,
}
if state == 'absent' and not inventory:
# If the state was absent and we had no inventory, we can just return
module.exit_json(**module.json_output)
elif state == 'absent' and inventory:
# If the state was absent and we had a inventory, we can try to delete it, the module will handle exiting from this
module.delete_endpoint('inventories/{0}'.format(inventory['id']), item_type='inventory', item_name=name, **{})
elif state == 'present' and not inventory:
# If the state was present and we couldn't find a inventory we can build one, the module will handle exiting from this
module.post_endpoint('inventories', item_type='inventory', item_name=name, **{'data': inventory_fields})
else:
# Throw a more specific error message than what the API page provides.
if inventory['kind'] == '' and inventory_fields['kind'] == 'smart':
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(inventory)
elif state == 'present':
# We need to perform a check to make sure you are not trying to convert a regular inventory into a smart one.
if inventory and inventory['kind'] == '' and inventory_fields['kind'] == 'smart':
module.fail_json(msg='You cannot turn a regular inventory into a "smart" inventory.')
# If the state was present and we had a inventory, we can see if we need to update it
# This will return on its own
module.update_if_needed(inventory, inventory_fields)
# If the state was present we can let the module build or update the existing team, this will return on its own
module.create_or_update_if_needed(inventory, inventory_fields, endpoint='inventories', item_type='inventory')
if __name__ == '__main__':
main()

View File

@@ -259,23 +259,16 @@ def main():
# If we are doing a not manual project, register our on_change method
# An on_change function, if registered, will fire after an post_endpoint or update_if_needed completes successfully
on_change = None
if wait and scm_type != '':
module.on_change = wait_for_project_update
if state == 'absent' and not project:
# If the state was absent and we had no project, we can just return
module.exit_json(**module.json_output)
elif state == 'absent' and project:
# If the state was absent and we had a project, we can try to delete it, the module will handle exiting from this
module.delete_endpoint('projects/{0}'.format(project['id']), item_type='project', item_name=name, **{})
elif state == 'present' and not project:
# if the state was present and we couldn't find a project we can build one, the module wikl handle exiting from this
module.post_endpoint('projects', handle_return=False, item_type='project', item_name=name, **{'data': project_fields})
else:
# If the state was present and we had a project we can see if we need to update it
# This will return on its own
module.update_if_needed(project, project_fields)
on_change = wait_for_project_update
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(project)
elif state == 'present':
# If the state was present we can let the module build or update the existing team, this will return on its own
module.create_or_update_if_needed(project, project_fields, endpoint='projects', item_type='project', on_create=on_change, on_update=on_change)
if __name__ == '__main__':
main()

View File

@@ -109,7 +109,7 @@ def main():
}
if state == 'absent':
# If the state was absent we can let the module to delete it if needed, the module will handle exiting from this
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(team)
elif state == 'present':
# If the state was present we can let the module build or update the existing team, this will return on its own

View File

@@ -38,8 +38,8 @@ options:
type: str
email:
description:
- Email address of the user.
required: True
- Email address of the user. Required if creating a new user.
required: False
type: str
password:
description:
@@ -117,14 +117,14 @@ def main():
first_name=dict(),
last_name=dict(),
password=dict(no_log=True),
email=dict(required=True),
email=dict(required=False),
superuser=dict(type='bool', default=False),
auditor=dict(type='bool', default=False),
state=dict(choices=['present', 'absent'], default='present'),
)
# Create a module for ourselves
module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)
module = TowerModule(argument_spec=argument_spec, supports_check_mode=True, required_if=[['state', 'present', ['email']]])
# Extract our parameters
state = module.params.get('state')
@@ -145,22 +145,12 @@ def main():
}
})
if state == 'absent' and not user:
# If the state was absent and we had no user, we can just return
module.exit_json(**module.json_output)
elif state == 'absent' and user:
# If the state was absent and we had a user, we can try to delete it, the module will handle exiting from this
module.delete_endpoint('users/{0}'.format(user['id']), item_type='user', item_name=user_fields['username'], **{})
elif state == 'present' and not user:
# if the state was present and we couldn't find a user we can build one, the module will handle exiting from this
module.post_endpoint('users', item_type='user', item_name=user_fields['username'], **{
'data': user_fields
})
else:
# If the state was present and we had a user we can see if we need to update it
# This will return on its own
module.update_if_needed(user, user_fields)
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(user)
elif state == 'present':
# If the state was present we can let the module build or update the existing team, this will return on its own
module.create_or_update_if_needed(user, user_fields, endpoint='users', item_type='user')
if __name__ == '__main__':
main()