def list(context): """list(context) List all users. >>> dcictl user-list """ result = user.list(context) utils.format_output(result, context.format, user.RESOURCE, user.TABLE_HEADERS)
def main(): module = AnsibleModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), # Authentication related parameters # dci_login=dict(required=False, type='str'), dci_password=dict(required=False, type='str'), dci_cs_url=dict(required=False, type='str'), # Resource related parameters # id=dict(type='str'), name=dict(type='str'), password=dict(type='str'), role=dict(choices=['user', 'admin'], type='str'), team_id=dict(type='str'), ), ) if not dciclient_found: module.fail_json(msg='The python dciclient module is required') login, password, url = get_details(module) if not login or not password: module.fail_json(msg='login and/or password have not been specified') ctx = dci_context.build_dci_context(url, login, password, 'Ansible') # Action required: List all users # Endpoint called: /users GET via dci_user.list() # # List all users if module_params_empty(module.params): res = dci_user.list(ctx) # Action required: Delete the user matching user id # Endpoint called: /users/<user_id> DELETE via dci_user.delete() # # If the user exists and it has been succesfully deleted the changed is # set to true, else if the user does not exist changed is set to False elif module.params['state'] == 'absent': if not module.params['id']: module.fail_json(msg='id parameter is required') res = dci_user.get(ctx, module.params['id']) if res.status_code not in [400, 401, 404, 409]: kwargs = { 'id': module.params['id'], 'etag': res.json()['user']['etag'] } res = dci_user.delete(ctx, **kwargs) # Action required: Retrieve user informations # Endpoint called: /user/<user_id> GET via dci_user.get() # # Get user informations elif module.params['id'] and not module.params[ 'name'] and not module.params['password'] and not module.params[ 'role'] and not module.params['team_id']: res = dci_user.get(ctx, module.params['id']) # Action required: Update an user # Endpoint called: /users/<user_id> PUT via dci_user.update() # # Update the user with the specified characteristics. elif module.params['id']: res = dci_user.get(ctx, module.params['id']) if res.status_code not in [400, 401, 404, 409]: kwargs = { 'id': module.params['id'], 'etag': res.json()['user']['etag'] } if module.params['name']: kwargs['name'] = module.params['name'] if module.params['password']: kwargs['password'] = module.params['password'] if module.params['role']: kwargs['role'] = module.params['role'] if module.params['team_id']: kwargs['team_id'] = module.params['team_id'] res = dci_user.update(ctx, **kwargs) # Action required: Create a user with the specified content # Endpoint called: /users POST via dci_user.create() # # Create the new user. else: if not module.params['name']: module.fail_json(msg='name parameter must be specified') if not module.params['password']: module.fail_json(msg='password parameter must be specified') if not module.params['team_id']: module.fail_json(msg='team_id parameter must be specified') if not module.params['role']: role = 'user' kwargs = { 'name': module.params['name'], 'password': module.params['password'], 'role': role, 'team_id': module.params['team_id'], } res = dci_user.create(ctx, **kwargs) try: result = res.json() if res.status_code == 404: module.fail_json(msg='The resource does not exist') if res.status_code == 409: result['changed'] = False else: result['changed'] = True except: result = {} result['changed'] = True module.exit_json(**result)
def list(context): result = user.list(context) utils.format_output(result.json(), context.format, user.RESOURCE, user.TABLE_HEADERS)
def list(context, args): params = {k: getattr(args, k) for k in ["sort", "limit", "offset", "where"]} return user.list(context, **params)