From aaaca63f831b22140c9b898725b1b2b6e1975b5b Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Sat, 18 Jan 2020 16:02:01 -0500 Subject: [PATCH] Converted tower_organization --- .../plugins/modules/tower_organization.py | 89 +++++++++++++------ 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/awx_collection/plugins/modules/tower_organization.py b/awx_collection/plugins/modules/tower_organization.py index 201c7fc326..e31ccc979e 100644 --- a/awx_collection/plugins/modules/tower_organization.py +++ b/awx_collection/plugins/modules/tower_organization.py @@ -39,6 +39,12 @@ options: type: str required: False default: '' + max_hosts: + description: + - The max hosts allowed in this organizations + default: "0" + type: str + required: False state: description: - Desired state of the resource. @@ -47,6 +53,12 @@ options: type: str 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 = ''' @@ -66,50 +78,73 @@ EXAMPLES = ''' tower_config_file: "~/tower_cli.cfg" ''' -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(), + name=dict(type='str', required=True), + description=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) + # Extract our parameters name = module.params.get('name') description = module.params.get('description') 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') - 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) - with settings.runtime_values(**tower_auth): - tower_check_mode(module) - organization = tower_cli.get_resource('organization') + # Attempt to lookup organization based on the provided name and org ID + organization = module.get_one('organizations', **{ + 'data': { + '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: - if state == 'present': - result = organization.modify(name=name, description=description, custom_virtualenv=custom_virtualenv, create_on_missing=True) - json_output['id'] = result['id'] - elif state == 'absent': - result = organization.delete(name=name) - except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: - module.fail_json(msg='Failed to update the organization: {0}'.format(excinfo), changed=False) - - json_output['changed'] = result['changed'] - module.exit_json(**json_output) + int_max_hosts = int(max_hosts) + except Exception as e: + module.fail_json(msg="Unable to convert max_hosts to an integer") + new_org_data['max_hosts'] = int_max_hosts + # if instance_group_objects: + # new_org_data['instance_groups'] = instance_group_objects + 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__': main()