Converted tower_organization

This commit is contained in:
John Westcott IV
2020-01-18 16:02:01 -05:00
committed by beeankha
parent d8a9f663b1
commit aaaca63f83

View File

@@ -39,6 +39,12 @@ options:
type: str type: str
required: False required: False
default: '' default: ''
max_hosts:
description:
- The max hosts allowed in this organizations
default: "0"
type: str
required: False
state: state:
description: description:
- Desired state of the resource. - Desired state of the resource.
@@ -47,6 +53,12 @@ options:
type: str type: str
extends_documentation_fragment: awx.awx.auth extends_documentation_fragment: awx.awx.auth
''' '''
# instance_groups:
# description:
# - The name of instance groups to tie to this organization
# type: list
# default: []
# required: False
EXAMPLES = ''' EXAMPLES = '''
@@ -66,50 +78,73 @@ EXAMPLES = '''
tower_config_file: "~/tower_cli.cfg" tower_config_file: "~/tower_cli.cfg"
''' '''
from ..module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode from ..module_utils.tower_api import TowerModule
try:
import tower_cli
import tower_cli.exceptions as exc
from tower_cli.conf import settings
except ImportError:
pass
def main(): def main():
# Any additional arguments that are not fields of the item can be added here
argument_spec = dict( argument_spec = dict(
name=dict(required=True), name=dict(type='str', required=True),
description=dict(), description=dict(type='str', required=False),
custom_virtualenv=dict(type='str', required=False), custom_virtualenv=dict(type='str', required=False),
state=dict(choices=['present', 'absent'], default='present'), max_hosts=dict(type='str', required=False, default="0"),
# instance_groups=dict(type='list', required=False, default=[]),
state=dict(type='str', choices=['present', 'absent'], default='present', required=False),
) )
# 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)
# Extract our parameters
name = module.params.get('name') name = module.params.get('name')
description = module.params.get('description') description = module.params.get('description')
custom_virtualenv = module.params.get('custom_virtualenv') custom_virtualenv = module.params.get('custom_virtualenv')
max_hosts = module.params.get('max_hosts')
# instance_group_names = module.params.get('instance_groups')
state = module.params.get('state') state = module.params.get('state')
json_output = {'organization': name, 'state': state} # Attempt to lookup the org the user specified
# instance_group_objects = []
# for instance_name in instance_group_names:
# instance_group_objects.append(module.resolve_name_to_id('instance_groups', instance_name))
tower_auth = tower_auth_config(module) # Attempt to lookup organization based on the provided name and org ID
with settings.runtime_values(**tower_auth): organization = module.get_one('organizations', **{
tower_check_mode(module) 'data': {
organization = tower_cli.get_resource('organization') 'name': name,
}
})
new_org_data = { 'name': name }
if description:
new_org_data['description'] = description
if custom_virtualenv:
new_org_data['custom_virtualenv'] = custom_virtualenv
if max_hosts:
int_max_hosts = 0
try: try:
if state == 'present': int_max_hosts = int(max_hosts)
result = organization.modify(name=name, description=description, custom_virtualenv=custom_virtualenv, create_on_missing=True) except Exception as e:
json_output['id'] = result['id'] module.fail_json(msg="Unable to convert max_hosts to an integer")
elif state == 'absent': new_org_data['max_hosts'] = int_max_hosts
result = organization.delete(name=name) # if instance_group_objects:
except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: # new_org_data['instance_groups'] = instance_group_objects
module.fail_json(msg='Failed to update the organization: {0}'.format(excinfo), changed=False)
json_output['changed'] = result['changed']
module.exit_json(**json_output)
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 wikl 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 __name__ == '__main__': if __name__ == '__main__':
main() main()