def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', aliases=['name']), # messages=dict(type='dict'), # associations=dict(type='list'), # health_faults=dict(type='list'), # references=dict(type='dict'), # policy_states=dict(type='list'), state=dict(type='str', default='query', choices=['absent', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['schema']], ], ) schema = module.params.get('schema') state = module.params.get('state') mso = MSOModule(module) schema_id = None path = 'schemas' # Query for existing object(s) if schema: mso.existing = mso.get_obj(path, displayName=schema) if mso.existing: schema_id = mso.existing.get('id') path = 'schemas/{id}'.format(id=schema_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') mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( tenant=dict(type='str', aliases=['name'], required=True), site=dict(type='str', aliases=['name']), cloud_account=dict(type='str'), security_domains=dict(type='list', elements='str', default=[]), aws_trusted=dict(type='bool'), azure_access_type=dict(type='str', default='shared', choices=['managed', 'unmanaged', 'shared']), azure_active_directory_id=dict(type='str'), aws_access_key=dict(type='str'), aws_account_org=dict(type='bool', default='false'), azure_active_directory_name=dict(type='str'), azure_subscription_id=dict(type='str'), azure_application_id=dict(type='str'), azure_credential_name=dict(type='str'), secret_key=dict(type='str'), 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', 'site']], ['state', 'present', ['tenant', 'site']], ], ) state = module.params.get('state') security_domains = module.params.get('security_domains') cloud_account = module.params.get('cloud_account') azure_access_type = module.params.get('azure_access_type') azure_credential_name = module.params.get('azure_credential_name') azure_application_id = module.params.get('azure_application_id') azure_active_directory_id = module.params.get('azure_active_directory_id') azure_active_directory_name = module.params.get('azure_active_directory_name') azure_subscription_id = module.params.get('azure_subscription_id') secret_key = module.params.get('secret_key') aws_account_org = module.params.get('aws_account_org') aws_access_key = module.params.get('aws_access_key') aws_trusted = module.params.get('aws_trusted') mso = MSOModule(module) # Get tenant_id and site_id tenant_id = mso.lookup_tenant(module.params.get('tenant')) site_id = mso.lookup_site(module.params.get('site')) tenants = [(t.get('id')) for t in mso.query_objs('tenants')] tenant_idx = tenants.index((tenant_id)) # set tenent and port paths tenant_path = 'tenants/{0}'.format(tenant_id) ops = [] ports_path = '/siteAssociations/-' port_path = '/siteAssociations/{0}'.format(site_id) payload = dict( siteId=site_id, securityDomains=security_domains, cloudAccount=cloud_account, ) if cloud_account: if 'azure' in cloud_account: azure_account = dict( accessType=azure_access_type, securityDomains=security_domains, vendor='azure', ) payload['azureAccount'] = [azure_account] cloudSubscription = dict( cloudSubscriptionId=azure_subscription_id, cloudApplicationId=azure_application_id, ) payload['azureAccount'][0]['cloudSubscription'] = cloudSubscription if azure_access_type == 'shared': payload['azureAccount'] = [] if azure_access_type == 'managed': if not azure_subscription_id: mso.fail_json(msg="azure_susbscription_id is required when in managed mode.") if not azure_application_id: mso.fail_json(msg="azure_application_id is required when in managed mode.") payload['azureAccount'][0]['cloudApplication'] = [] payload['azureAccount'][0]['cloudActiveDirectory'] = [] if azure_access_type == 'unmanaged': if not azure_subscription_id: mso.fail_json(msg="azure_subscription_id is required when in unmanaged mode.") if not azure_application_id: mso.fail_json(msg="azure_application_id is required when in unmanaged mode.") if not secret_key: mso.fail_json(msg="secret_key is required when in unmanaged mode.") if not azure_active_directory_id: mso.fail_json(msg="azure_active_directory_id is required when in unmanaged mode.") if not azure_active_directory_name: mso.fail_json(msg="azure_active_directory_name is required when in unmanaged mode.") if not azure_credential_name: mso.fail_json(msg="azure_credential_name is required when in unmanaged mode.") azure_account.update( accessType='credentials', ) cloudApplication = dict( cloudApplicationId=azure_application_id, cloudCredentialName=azure_credential_name, secretKey=secret_key, cloudActiveDirectoryId=azure_active_directory_id ) cloudActiveDirectory = dict( cloudActiveDirectoryId=azure_active_directory_id, cloudActiveDirectoryName=azure_active_directory_name ) payload['azureAccount'][0]['cloudApplication'] = [cloudApplication] payload['azureAccount'][0]['cloudActiveDirectory'] = [cloudActiveDirectory] else: aws_account = dict( accountId=cloud_account, isTrusted=aws_trusted, accessKeyId=aws_access_key, secretKey=secret_key, isAccountInOrg=aws_account_org, ) if not aws_trusted: if not aws_access_key: mso.fail_json(msg="aws_access_key is a required field in untrusted mode.") if not secret_key: mso.fail_json(msg="secret_key is a required field in untrusted mode.") payload['awsAccount'] = [aws_account] sites = [(s.get('siteId')) for s in mso.query_objs('tenants')[tenant_idx]['siteAssociations']] if site_id in sites: site_idx = sites.index((site_id)) mso.existing = mso.query_objs('tenants')[tenant_idx]['siteAssociations'][site_idx] if state == 'query': if len(sites) == 0: mso.fail_json(msg="No site associated with tenant Id {0}".format(tenant_id)) elif site_id not in sites and site_id is not None: mso.fail_json(msg="Site Id {0} not associated with tenant Id {1}".format(site_id, tenant_id)) elif site_id is None: mso.existing = mso.query_objs('tenants')[tenant_idx]['siteAssociations'] mso.exit_json() mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=port_path)) if state == 'present': mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=port_path, value=mso.sent)) else: ops.append(dict(op='add', path=ports_path, value=mso.sent)) mso.existing = mso.proposed if not module.check_mode and mso.proposed != mso.previous: mso.request(tenant_path, method='PATCH', data=ops) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', aliases=['name']), templates=dict(type='list'), sites=dict(type='list'), # messages=dict(type='dict'), # associations=dict(type='list'), # health_faults=dict(type='list'), # references=dict(type='dict'), # policy_states=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', ['schema']], ['state', 'present', ['schema', 'templates']], ], ) schema = module.params['schema'] templates = module.params['templates'] sites = module.params['sites'] state = module.params['state'] mso = MSOModule(module) schema_id = None path = 'schemas' # Query for existing object(s) if schema: mso.existing = mso.get_obj(path, displayName=schema) if mso.existing: schema_id = mso.existing['id'] path = 'schemas/{id}'.format(id=schema_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=schema_id, displayName=schema, templates=templates, sites=sites, ) mso.sanitize(payload, collate=True) if mso.existing: if mso.check_changed(): 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( 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['user'] user_password = module.params['user_password'] first_name = module.params['first_name'] last_name = module.params['last_name'] email = module.params['email'] phone = module.params['phone'] account_status = module.params['account_status'] state = module.params['state'] mso = MSOModule(module) roles = mso.lookup_roles(module.params['roles']) domain = mso.lookup_domain(module.params['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['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(): 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'), apic_login_domain=dict(type='str'), labels=dict(type='list', elements='str'), 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', elements='str'), ) 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') apic_login_domain = module.params.get('apic_login_domain') mso = MSOModule(module) site_id = None path = 'sites' api_version = 'v1' if mso.platform == 'nd': api_version = 'v2' # Convert labels labels = mso.lookup_labels(module.params.get('labels'), 'site') # Query for mso.existing object(s) if site: if mso.platform == 'nd': site_info = mso.get_obj(path, api_version=api_version, common=dict(name=site)) path = 'sites/manage' if site_info: # If we found an existing object, continue with it site_id = site_info.get('id') if site_id is not None and site_id != '': # Checking if site is managed by MSO mso.existing = site_info path = 'sites/manage/{id}'.format(id=site_id) else: mso.existing = mso.get_obj(path, name=site) if mso.existing: # If we found an existing object, continue with it site_id = mso.existing.get('id') path = 'sites/{id}'.format(id=site_id) else: mso.existing = mso.query_objs(path, api_version=api_version) if state == 'query': pass elif state == 'absent': mso.previous = mso.existing if mso.existing: if module.check_mode: mso.existing = {} else: mso.request(path, method='DELETE', qs=dict(force='true'), api_version=api_version) mso.existing = {} elif state == 'present': mso.previous = mso.existing if mso.platform == 'nd': if mso.existing: payload = mso.existing else: if site_info: payload = site_info payload['common']['siteId'] = apic_site_id else: mso.fail_json( msg= "Site '{0}' is not a valid Site configured at ND-level. Add Site to ND first." .format(site)) else: 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, ) if apic_login_domain is not None and apic_login_domain not in [ '', 'local', 'Local' ]: payload['username'] = '******'.format( apic_login_domain, apic_username) mso.sanitize(payload, collate=True) if mso.existing: if mso.check_changed(): if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='PUT', data=mso.sent, api_version=api_version) else: if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=mso.sent, api_version=api_version) if 'password' in mso.existing: mso.existing['password'] = '******' mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( location_type=dict(type='str', default='local', choices=['local', 'remote']), description=dict(type='str'), backup=dict(type='str', aliases=['name']), remote_location=dict(type='str'), remote_path=dict(type='str'), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[['location_type', 'remote', ['remote_location']], ['state', 'absent', ['backup']], ['state', 'present', ['backup']]]) description = module.params.get('description') location_type = module.params.get('location_type') state = module.params.get('state') backup = module.params.get('backup') remote_location = module.params.get('remote_location') remote_path = module.params.get('remote_path') mso = MSOModule(module) backup_names = [] mso.existing = mso.query_objs('backups/backupRecords', key='backupRecords') if backup: if mso.existing: data = mso.existing mso.existing = [] for backup_info in data: if backup == backup_info.get('name').split( '_')[0] or backup == backup_info.get('name'): mso.existing.append(backup_info) backup_names.append(backup_info.get('name')) if state == 'query': mso.exit_json() if state == 'absent': mso.previous = mso.existing if len(mso.existing) > 1: mso.module.fail_json( msg= "Multiple backups with same name found. Existing backups with similar names: {0}" .format(', '.join(backup_names))) elif len(mso.existing) == 1: if module.check_mode: mso.existing = {} else: mso.existing = mso.request('backups/backupRecords/{id}'.format( id=mso.existing[0].get('id')), method='DELETE') mso.exit_json() path = 'backups' if state == 'present': mso.previous = mso.existing payload = dict(name=backup, description=description, locationType=location_type) if location_type == 'remote': remote_location_info = mso.lookup_remote_location(remote_location) payload.update(remoteLocationId=remote_location_info.get('id')) if remote_path: remote_path = '{0}/{1}'.format( remote_location_info.get('path'), remote_path) payload.update(remotePath=remote_path) mso.proposed = payload if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request(path, method='POST', data=payload) 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['apic_username'] apic_password = module.params['apic_password'] apic_site_id = module.params['apic_site_id'] site = module.params['site'] location = module.params['location'] if location is not None: latitude = module.params['location']['latitude'] longitude = module.params['location']['longitude'] state = module.params['state'] urls = module.params['urls'] mso = MSOModule(module) site_id = None path = 'sites' # Convert labels labels = mso.lookup_labels(module.params['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['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 mso.check_changed(): 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()
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['label'] label_type = module.params['type'] state = module.params['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['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 mso.check_changed(): 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 if mso.existing: if mso.check_changed(): 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'), read_permissions=dict(type='list', elements='str', 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', ]), write_permissions=dict(type='list', elements='str', aliases=['permissions'], 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') read_permissions = module.params.get('read_permissions') write_permissions = module.params.get('write_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, readPermissions=read_permissions, writePermissions=write_permissions, ) mso.sanitize(payload, collate=True) if mso.existing: if mso.check_changed(): 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( dhcp_option_policy=dict(type="str", aliases=['name']), description=dict(type="str"), tenant=dict(type="str"), state=dict(type="str", default="present", choices=["absent", "present", "query"]), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['dhcp_option_policy']], ['state', 'present', ['dhcp_option_policy', 'tenant']], ], ) dhcp_option_policy = module.params.get("dhcp_option_policy") description = module.params.get("description") tenant = module.params.get("tenant") state = module.params.get("state") mso = MSOModule(module) path = "policies/dhcp/option" # Query for existing object(s) if dhcp_option_policy: mso.existing = mso.get_obj(path, name=dhcp_option_policy, key="DhcpRelayPolicies") if mso.existing: policy_id = mso.existing.get("id") # If we found an existing object, continue with it path = '{0}/{1}'.format(path, policy_id) else: mso.existing = mso.query_objs(path, key="DhcpRelayPolicies") mso.previous = mso.existing if state == "absent": if mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method="DELETE", data=mso.sent) elif state == "present": tenant_id = mso.lookup_tenant(tenant) payload = dict( name=dhcp_option_policy, desc=description, policyType="dhcp", policySubtype="option", tenantId=tenant_id, ) mso.sanitize(payload, collate=True) if mso.existing: if mso.check_changed(): 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(location_type=dict(type='str', default='local', choices=['local', 'remote']), description=dict(type='str'), backup=dict(type='str', aliases=['name']), remote_location=dict(type='str'), remote_path=dict(type='str'), state=dict(type='str', default='present', choices=[ 'absent', 'present', 'query', 'upload', 'restore', 'download', 'move' ]), destination=dict(type='str')) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[['location_type', 'remote', ['remote_location']], ['state', 'absent', ['backup']], ['state', 'present', ['backup']], ['state', 'upload', ['backup']], ['state', 'restore', ['backup']], ['state', 'download', ['backup', 'destination']], [ 'state', 'move', ['backup', 'remote_location', 'remote_path'] ]]) description = module.params.get('description') location_type = module.params.get('location_type') state = module.params.get('state') backup = module.params.get('backup') remote_location = module.params.get('remote_location') remote_path = module.params.get('remote_path') destination = module.params.get('destination') mso = MSOModule(module) backup_names = [] mso.existing = mso.query_objs('backups/backupRecords', key='backupRecords') if backup: if mso.existing: data = mso.existing mso.existing = [] for backup_info in data: if backup == backup_info.get('name').split( '_')[0] or backup == backup_info.get('name'): mso.existing.append(backup_info) backup_names.append(backup_info.get('name')) if state == 'query': mso.exit_json() elif state == 'absent': mso.previous = mso.existing if len(mso.existing) > 1: mso.module.fail_json( msg= "Multiple backups with same name found. Existing backups with similar names: {0}" .format(', '.join(backup_names))) elif len(mso.existing) == 1: if module.check_mode: mso.existing = {} else: mso.existing = mso.request('backups/backupRecords/{id}'.format( id=mso.existing[0].get('id')), method='DELETE') mso.exit_json() elif state == 'present': mso.previous = mso.existing payload = dict(name=backup, description=description, locationType=location_type) if location_type == 'remote': remote_location_info = mso.lookup_remote_location(remote_location) payload.update(remoteLocationId=remote_location_info.get('id')) if remote_path: remote_path = '{0}/{1}'.format( remote_location_info.get('path'), remote_path) payload.update(remotePath=remote_path) mso.proposed = payload if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request('backups', method='POST', data=payload) mso.exit_json() elif state == 'upload': mso.previous = mso.existing if module.check_mode: mso.existing = mso.proposed else: try: payload = dict(name=(os.path.basename(backup), open(backup, 'rb'), 'application/x-gzip')) mso.existing = mso.request_upload('backups/upload', fields=payload) except Exception: mso.module.fail_json(msg="Backup file '{0}' not found!".format( ', '.join(backup.split('/')[-1:]))) mso.exit_json() if len(mso.existing) == 0: mso.module.fail_json(msg="Backup '{0}' does not exist".format(backup)) elif len(mso.existing) > 1: mso.module.fail_json( msg= "Multiple backups with same name found. Existing backups with similar names: {0}" .format(', '.join(backup_names))) elif state == 'restore': mso.previous = mso.existing if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request( 'backups/{id}/restore'.format(id=mso.existing[0].get('id')), method='PUT') elif state == 'download': mso.previous = mso.existing if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request_download( 'backups/{id}/download'.format(id=mso.existing[0].get('id')), destination=destination) elif state == 'move': mso.previous = mso.existing remote_location_info = mso.lookup_remote_location(remote_location) remote_path = '{0}/{1}'.format(remote_location_info.get('path'), remote_path) payload = dict(remoteLocationId=remote_location_info.get('id'), remotePath=remote_path, backupRecordId=mso.existing[0].get('id')) if module.check_mode: mso.existing = mso.proposed else: mso.existing = mso.request('backups/remote-location', method='POST', data=payload) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( source_schema=dict(type='str'), destination_schema=dict(type='str'), destination_tenant=dict(type='str'), source_template_name=dict(type='str'), destination_template_name=dict(type='str'), destination_template_display_name=dict(type='str'), state=dict(type='str', default='clone', choices=['clone']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'clone', ['destination_schema', 'destination_tenant']], ], ) source_schema = module.params.get('source_schema') destination_schema = module.params.get('destination_schema') destination_tenant = module.params.get('destination_tenant') source_template_name = module.params.get('source_template_name') destination_template_name = module.params.get('destination_template_name') destination_template_display_name = module.params.get( 'destination_template_display_name') state = module.params.get('state') mso = MSOModule(module) destination_schema_id = None # Get source schema id and destination schema id schema_summary = mso.query_objs('schemas/list-identity', key='schemas') for schema in schema_summary: if schema.get('displayName') == source_schema: source_schema_id = schema.get('id') if schema.get('displayName') == destination_schema: destination_schema_id = schema.get('id') destination_schema = None break if destination_schema_id is None: mso.fail_json(msg="Schema with the name '{0}' does not exist.".format( destination_schema)) # Get destination tenant id destination_tenant_id = mso.lookup_tenant(destination_tenant) path = 'schemas/cloneTemplates' if state == 'clone': if destination_template_display_name is None: destination_template_display_name = destination_template_name payload = dict( destTenantId=destination_tenant_id, destSchemaId=destination_schema_id, destSchemaName=destination_schema, templatesToBeCloned=[ dict( schemaId=source_schema_id, templateName=source_template_name, destTemplateName=destination_template_name, destTempDisplayName=destination_template_display_name, ) ], ) mso.sanitize(payload, collate=True) mso.previous = {} if not mso.existing: if module.check_mode: mso.existing = {} else: mso.existing = mso.request(path, method='POST', data=mso.sent) mso.exit_json()