mirror of
https://github.com/ansible/awx.git
synced 2026-02-01 01:28:09 -03:30
Updating tower_org to use the new tower_api format
Pass sanity and unit tests, update tests Remove placeholder test function, convert tower_host module, fix misc typos
This commit is contained in:
committed by
beeankha
parent
c23d605a7a
commit
f89061da41
@@ -140,7 +140,7 @@ def main():
|
||||
if module.params.get('injectors'):
|
||||
credential_type_params['injectors'] = module.params.get('injectors')
|
||||
|
||||
# Attempt to lookup credential_type based on the provided name and org ID
|
||||
# Attempt to look up credential_type based on the provided name
|
||||
credential_type = module.get_one('credential_types', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
@@ -156,8 +156,9 @@ def main():
|
||||
# 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
|
||||
# If the state was present and we can let the module build or update the existing credential type, 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()
|
||||
|
||||
@@ -28,6 +28,11 @@ options:
|
||||
- The name to use for the host.
|
||||
required: True
|
||||
type: str
|
||||
new_name:
|
||||
description:
|
||||
- To use when changing a hosts's name.
|
||||
required: True
|
||||
type: str
|
||||
description:
|
||||
description:
|
||||
- The description to use for the host.
|
||||
@@ -52,6 +57,11 @@ options:
|
||||
choices: ["present", "absent"]
|
||||
default: "present"
|
||||
type: str
|
||||
tower_oauthtoken:
|
||||
description:
|
||||
- The Tower OAuth token to use.
|
||||
required: False
|
||||
type: str
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
@@ -71,29 +81,27 @@ EXAMPLES = '''
|
||||
|
||||
import os
|
||||
|
||||
from ..module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode
|
||||
|
||||
try:
|
||||
import tower_cli
|
||||
import tower_cli.exceptions as exc
|
||||
|
||||
from tower_cli.conf import settings
|
||||
except ImportError:
|
||||
pass
|
||||
from ..module_utils.tower_api import TowerModule
|
||||
|
||||
|
||||
def main():
|
||||
# Any additional arguments that are not fields of the item can be added here
|
||||
argument_spec = dict(
|
||||
name=dict(required=True),
|
||||
description=dict(),
|
||||
new_name=dict(required=False),
|
||||
description=dict(default=''),
|
||||
inventory=dict(required=True),
|
||||
enabled=dict(type='bool', default=True),
|
||||
variables=dict(),
|
||||
variables=dict(default=''),
|
||||
state=dict(choices=['present', 'absent'], default='present'),
|
||||
)
|
||||
|
||||
# Create a module for ourselves
|
||||
module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
# Extract our parameters
|
||||
name = module.params.get('name')
|
||||
new_name = module.params.get('new_name')
|
||||
description = module.params.get('description')
|
||||
inventory = module.params.get('inventory')
|
||||
enabled = module.params.get('enabled')
|
||||
@@ -106,30 +114,30 @@ def main():
|
||||
with open(filename, 'r') as f:
|
||||
variables = f.read()
|
||||
|
||||
json_output = {'host': name, 'state': state}
|
||||
# Attempt to lookup the related items the user specified (these will fail the module if not found)
|
||||
inventory_id = module.resolve_name_to_id('inventories', inventory)
|
||||
|
||||
tower_auth = tower_auth_config(module)
|
||||
with settings.runtime_values(**tower_auth):
|
||||
tower_check_mode(module)
|
||||
host = tower_cli.get_resource('host')
|
||||
# Attempt to lookup host based on the provided name and org ID
|
||||
host = module.get_one('hosts', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
'inventory': inventory_id
|
||||
}
|
||||
})
|
||||
|
||||
try:
|
||||
inv_res = tower_cli.get_resource('inventory')
|
||||
inv = inv_res.get(name=inventory)
|
||||
# Create data to send to create and update
|
||||
host_fields = {
|
||||
'name': new_name if new_name else name,
|
||||
'description': description,
|
||||
'inventory': inventory_id
|
||||
}
|
||||
|
||||
if state == 'present':
|
||||
result = host.modify(name=name, inventory=inv['id'], enabled=enabled,
|
||||
variables=variables, description=description, create_on_missing=True)
|
||||
json_output['id'] = result['id']
|
||||
elif state == 'absent':
|
||||
result = host.delete(name=name, inventory=inv['id'])
|
||||
except (exc.NotFound) as excinfo:
|
||||
module.fail_json(msg='Failed to update host, inventory not found: {0}'.format(excinfo), changed=False)
|
||||
except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo:
|
||||
module.fail_json(msg='Failed to update host: {0}'.format(excinfo), changed=False)
|
||||
|
||||
json_output['changed'] = result['changed']
|
||||
module.exit_json(**json_output)
|
||||
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(host)
|
||||
elif state == 'present':
|
||||
# If the state was present we can let the module build or update the existing host, this will return on its own
|
||||
module.create_or_update_if_needed(host, host_fields, endpoint='hosts', item_type='host')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -106,10 +106,10 @@ def main():
|
||||
kind = module.params.get('kind')
|
||||
host_filter = module.params.get('host_filter')
|
||||
|
||||
# Attempt to lookup the related items the user specified (these will fail the module if not found)
|
||||
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||
org_id = module.resolve_name_to_id('organizations', organization)
|
||||
|
||||
# Attempt to lookup inventory based on the provided name and org ID
|
||||
# Attempt to look up inventory based on the provided name and org ID
|
||||
inventory = module.get_one('inventories', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
@@ -135,8 +135,9 @@ def main():
|
||||
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 we can let the module build or update the existing team, this will return on its own
|
||||
# If the state was present and we can let the module build or update the existing inventory, this will return on its own
|
||||
module.create_or_update_if_needed(inventory, inventory_fields, endpoint='inventories', item_type='inventory')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -125,7 +125,7 @@ def main():
|
||||
else:
|
||||
job_list = module.get_endpoint('jobs', **{'data': job_search_data})
|
||||
|
||||
# Attempt to lookup jobs based on the status
|
||||
# Attempt to look up jobs based on the status
|
||||
module.exit_json(**job_list['json'])
|
||||
|
||||
|
||||
|
||||
@@ -101,46 +101,37 @@ def main():
|
||||
# instance_group_names = module.params.get('instance_groups')
|
||||
state = module.params.get('state')
|
||||
|
||||
# Attempt to lookup the related items the user specified (these will fail the module if not found)
|
||||
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||
# instance_group_objects = []
|
||||
# for instance_name in instance_group_names:
|
||||
# instance_group_objects.append(module.resolve_name_to_id('instance_groups', instance_name))
|
||||
|
||||
# Attempt to lookup organization based on the provided name and org ID
|
||||
# Attempt to look up organization based on the provided name
|
||||
organization = module.get_one('organizations', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
}
|
||||
})
|
||||
|
||||
new_org_data = {'name': name}
|
||||
org_fields = {'name': name}
|
||||
if description:
|
||||
new_org_data['description'] = description
|
||||
org_fields['description'] = description
|
||||
if custom_virtualenv:
|
||||
new_org_data['custom_virtualenv'] = custom_virtualenv
|
||||
org_fields['custom_virtualenv'] = custom_virtualenv
|
||||
if max_hosts:
|
||||
int_max_hosts = 0
|
||||
try:
|
||||
int_max_hosts = int(max_hosts)
|
||||
except Exception:
|
||||
module.fail_json(msg="Unable to convert max_hosts to an integer")
|
||||
new_org_data['max_hosts'] = int_max_hosts
|
||||
org_fields['max_hosts'] = int_max_hosts
|
||||
|
||||
if state == 'absent' and not organization:
|
||||
# If the state was absent and we had no organization, we can just return
|
||||
module.exit_json(**module.json_output)
|
||||
elif state == 'absent' and organization:
|
||||
# If the state was absent and we had a organization, we can try to delete it, the module will handle exiting from this
|
||||
module.delete_endpoint('organizations/{0}'.format(organization['id']), item_type='organization', item_name=name, **{})
|
||||
elif state == 'present' and not organization:
|
||||
# if the state was present and we couldn't find a organization we can build one, the module will handle exiting from this
|
||||
module.post_endpoint('organizations', item_type='organization', item_name=name, **{
|
||||
'data': new_org_data,
|
||||
})
|
||||
else:
|
||||
# If the state was present and we had a organization we can see if we need to update it
|
||||
# This will return on its own
|
||||
module.update_if_needed(organization, new_org_data)
|
||||
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(organization)
|
||||
elif state == 'present':
|
||||
# If the state was present and we can let the module build or update the existing organization, this will return on its own
|
||||
module.create_or_update_if_needed(organization, org_fields, endpoint='organizations', item_type='organization')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -219,12 +219,12 @@ def main():
|
||||
state = module.params.get('state')
|
||||
wait = module.params.get('wait')
|
||||
|
||||
# Attempt to lookup the related items the user specified (these will fail the module if not found)
|
||||
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||
org_id = module.resolve_name_to_id('organizations', organization)
|
||||
if scm_credential is not None:
|
||||
scm_credential_id = module.resolve_name_to_id('credentials', scm_credential)
|
||||
|
||||
# Attempt to lookup project based on the provided name and org ID
|
||||
# Attempt to look up project based on the provided name and org ID
|
||||
project = module.get_one('projects', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
@@ -267,8 +267,9 @@ def main():
|
||||
# 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
|
||||
# If the state was present and we can let the module build or update the existing project, 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()
|
||||
|
||||
@@ -90,10 +90,10 @@ def main():
|
||||
organization = module.params.get('organization')
|
||||
state = module.params.get('state')
|
||||
|
||||
# Attempt to lookup the related items the user specified (these will fail the module if not found)
|
||||
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||
org_id = module.resolve_name_to_id('organizations', organization)
|
||||
|
||||
# Attempt to lookup team based on the provided name and org ID
|
||||
# Attempt to look up team based on the provided name and org ID
|
||||
team = module.get_one('teams', **{
|
||||
'data': {
|
||||
'name': name,
|
||||
@@ -112,7 +112,7 @@ def main():
|
||||
# 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
|
||||
# If the state was present and we can let the module build or update the existing team, this will return on its own
|
||||
module.create_or_update_if_needed(team, team_fields, endpoint='teams', item_type='team')
|
||||
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ def main():
|
||||
'auditor': module.params.get('auditor'),
|
||||
}
|
||||
|
||||
# Attempt to lookup team based on the provided name and org ID
|
||||
# Attempt to look up user based on the provided username
|
||||
user = module.get_one('users', **{
|
||||
'data': {
|
||||
'username': user_fields['username'],
|
||||
@@ -149,8 +149,9 @@ def main():
|
||||
# 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
|
||||
# If the state was present and we can let the module build or update the existing user, this will return on its own
|
||||
module.create_or_update_if_needed(user, user_fields, endpoint='users', item_type='user')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user