def main(): argument_spec = mso_argument_spec() argument_spec.update( user=dict(type='str', aliases=['name']), user_password=dict(type='str', no_log=True), first_name=dict(type='str'), last_name=dict(type='str'), email=dict(type='str'), phone=dict(type='str'), # TODO: What possible options do we have ? account_status=dict(type='str', choices=['active']), domain=dict(type='str'), roles=dict(type='list'), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['user']], ['state', 'present', ['user']], ], ) user_name = module.params.get('user') user_password = module.params.get('user_password') first_name = module.params.get('first_name') last_name = module.params.get('last_name') email = module.params.get('email') phone = module.params.get('phone') account_status = module.params.get('account_status') state = module.params.get('state') mso = MSOModule(module) roles = mso.lookup_roles(module.params.get('roles')) domain = mso.lookup_domain(module.params.get('domain')) user_id = None path = 'users' # Query for existing object(s) if user_name: mso.existing = mso.get_obj(path, username=user_name) if mso.existing: user_id = mso.existing.get('id') # If we found an existing object, continue with it path = 'users/{id}'.format(id=user_id) else: mso.existing = mso.query_objs(path) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='DELETE') elif state == 'present': mso.previous = mso.existing payload = dict( id=user_id, username=user_name, password=user_password, firstName=first_name, lastName=last_name, emailAddress=email, phoneNumber=phone, accountStatus=account_status, domainId=domain, roles=roles, # active=True, # remote=True, ) mso.sanitize(payload, collate=True) if mso.sent.get('accountStatus') is None: mso.sent['accountStatus'] = 'active' if mso.existing: if not issubset(mso.sent, mso.existing): # NOTE: Since MSO always returns '******' as password, we need to assume a change if 'password' in mso.proposed: mso.module.warn("A password change is assumed, as the MSO REST API does not return passwords we do not know.") mso.result['changed'] = True if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( description=dict(type='str'), display_name=dict(type='str'), tenant=dict(type='str', aliases=['name']), users=dict(type='list'), sites=dict(type='list'), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['tenant']], ['state', 'present', ['tenant']], ], ) description = module.params.get('description') display_name = module.params.get('display_name') tenant = module.params.get('tenant') state = module.params.get('state') mso = MSOModule(module) # Convert sites and users sites = mso.lookup_sites(module.params.get('sites')) users = mso.lookup_users(module.params.get('users')) tenant_id = None path = 'tenants' # Query for existing object(s) if tenant: mso.existing = mso.get_obj(path, name=tenant) if mso.existing: tenant_id = mso.existing.get('id') # If we found an existing object, continue with it path = 'tenants/{id}'.format(id=tenant_id) else: mso.existing = mso.query_objs(path) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='DELETE') elif state == 'present': mso.previous = mso.existing payload = dict( description=description, id=tenant_id, name=tenant, displayName=display_name, siteAssociations=sites, userAssociations=users, ) mso.sanitize(payload, collate=True) # Ensure displayName is not undefined if mso.sent.get('displayName') is None: mso.sent['displayName'] = tenant # Ensure tenant has at least admin user if mso.sent.get('userAssociations') is None: mso.sent['userAssociations'] = [ dict(userId="0000ffff0000000000000020") ] if mso.existing: if not issubset(mso.sent, mso.existing): if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( label=dict(type='str', aliases=['name']), type=dict(type='str', default='site', choices=['site']), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['label']], ['state', 'present', ['label']], ], ) label = module.params.get('label') label_type = module.params.get('type') state = module.params.get('state') mso = MSOModule(module) label_id = None path = 'labels' # Query for existing object(s) if label: mso.existing = mso.get_obj(path, displayName=label) if mso.existing: label_id = mso.existing.get('id') # If we found an existing object, continue with it path = 'labels/{id}'.format(id=label_id) else: mso.existing = mso.query_objs(path) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='DELETE') elif state == 'present': mso.previous = mso.existing payload = dict( id=label_id, displayName=label, type=label_type, ) mso.sanitize(payload, collate=True) if mso.existing: if not issubset(mso.sent, mso.existing): if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( role=dict(type='str', aliases=['name']), display_name=dict(type='str'), description=dict(type='str'), permissions=dict(type='list', choices=[ 'backup-db', 'manage-audit-records', 'manage-labels', 'manage-roles', 'manage-schemas', 'manage-sites', 'manage-tenants', 'manage-tenant-schemas', 'manage-users', 'platform-logs', 'view-all-audit-records', 'view-labels', 'view-roles', 'view-schemas', 'view-sites', 'view-tenants', 'view-tenant-schemas', 'view-users', ]), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['role']], ['state', 'present', ['role']], ], ) role = module.params.get('role') description = module.params.get('description') permissions = module.params.get('permissions') state = module.params.get('state') mso = MSOModule(module) role_id = None path = 'roles' # Query for existing object(s) if role: mso.existing = mso.get_obj(path, name=role) if mso.existing: role_id = mso.existing.get('id') # If we found an existing object, continue with it path = 'roles/{id}'.format(id=role_id) else: mso.existing = mso.query_objs(path) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='DELETE') elif state == 'present': mso.previous = mso.existing payload = dict( id=role_id, name=role, displayName=role, description=description, permissions=permissions, ) mso.sanitize(payload, collate=True) if mso.existing: if not issubset(mso.sent, mso.existing): if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent) mso.exit_json()
def main(): location_arg_spec = dict( latitude=dict(type='float'), longitude=dict(type='float'), ) argument_spec = mso_argument_spec() argument_spec.update( apic_password=dict(type='str', no_log=True), apic_site_id=dict(type='str'), apic_username=dict(type='str', default='admin'), labels=dict(type='list'), location=dict(type='dict', options=location_arg_spec), site=dict(type='str', aliases=['name']), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), urls=dict(type='list'), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['site']], ['state', 'present', ['apic_site_id', 'site']], ], ) apic_username = module.params.get('apic_username') apic_password = module.params.get('apic_password') apic_site_id = module.params.get('apic_site_id') site = module.params.get('site') location = module.params.get('location') if location is not None: latitude = module.params.get('location')['latitude'] longitude = module.params.get('location')['longitude'] state = module.params.get('state') urls = module.params.get('urls') mso = MSOModule(module) site_id = None path = 'sites' # Convert labels labels = mso.lookup_labels(module.params.get('labels'), 'site') # Query for mso.existing object(s) if site: mso.existing = mso.get_obj(path, name=site) if mso.existing: site_id = mso.existing.get('id') # If we found an existing object, continue with it path = 'sites/{id}'.format(id=site_id) else: mso.existing = mso.query_objs(path) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='DELETE', qs=dict(force='true')) elif state == 'present': mso.previous = mso.existing payload = dict( apicSiteId=apic_site_id, id=site_id, name=site, urls=urls, labels=labels, username=apic_username, password=apic_password, ) if location is not None: payload['location'] = dict( lat=latitude, long=longitude, ) mso.sanitize(payload, collate=True) if mso.existing: if not issubset(mso.sent, mso.existing): if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent) if 'password' in mso.existing: mso.existing['password'] = '******' mso.exit_json()