def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True, aliases=['name']), site=dict(type='str'), state=dict(type='str', default='deploy', choices=['deploy', 'status', 'undeploy']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'undeploy', ['site']], ], ) schema = module.params.get('schema') template = module.params.get('template').replace(' ', '') site = module.params.get('site') state = module.params.get('state') mso = MSOModule(module) # Get schema id schema_id = mso.lookup_schema(schema) payload = dict( schemaId=schema_id, templateName=template, ) qs = None if state == 'deploy': path = 'execute/schema/{0}/template/{1}'.format(schema_id, template) elif state == 'status': path = 'status/schema/{0}/template/{1}'.format(schema_id, template) elif state == 'undeploy': path = 'execute/schema/{0}/template/{1}'.format(schema_id, template) site_id = mso.lookup_site(site) qs = dict(undeploy=site_id) if not module.check_mode: status = mso.request(path, method='GET', data=payload, qs=qs) mso.exit_json(**status) else: mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), state=dict(type='str', default='query', choices=['query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) schema = module.params.get('schema') state = module.params.get('state') # NOQA mso = MSOModule(module) # Get schema_id schema_id = mso.lookup_schema(schema) path = 'schemas/{id}/validate'.format(id=schema_id) mso.existing = mso.request(path, method='GET') mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), external_epg=dict(type='str', required=True), contract=dict(type='dict', options=mso_contractref_spec()), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['contract']], ['state', 'present', ['contract']], ], ) schema = module.params.get('schema') template = module.params.get('template').replace(' ', '') external_epg = module.params.get('external_epg') contract = module.params.get('contract') if contract is not None and contract.get('template') is not None: contract['template'] = contract.get('template').replace(' ', '') state = module.params.get('state') mso = MSOModule(module) if contract: if contract.get('schema') is None: contract['schema'] = schema contract['schema_id'] = mso.lookup_schema(contract.get('schema')) if contract.get('template') is None: contract['template'] = template # Get schema schema_id, schema_path, schema_obj = mso.query_schema(schema) # Get template templates = [t.get('name') for t in schema_obj.get('templates')] if template not in templates: mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates))) template_idx = templates.index(template) # Get EPG epgs = [e.get('name') for e in schema_obj.get('templates')[template_idx]['externalEpgs']] if external_epg not in epgs: mso.fail_json(msg="Provided epg '{epg}' does not exist. Existing epgs: {epgs}".format(epg=external_epg, epgs=', '.join(epgs))) epg_idx = epgs.index(external_epg) # Get Contract if contract: contracts = [(c.get('contractRef'), c.get('relationshipType')) for c in schema_obj.get('templates')[template_idx]['externalEpgs'][epg_idx]['contractRelationships']] contract_ref = mso.contract_ref(**contract) if (contract_ref, contract.get('type')) in contracts: contract_idx = contracts.index((contract_ref, contract.get('type'))) contract_path = '/templates/{0}/externalEpgs/{1}/contractRelationships/{2}'.format(template, external_epg, contract_idx) mso.existing = schema_obj.get('templates')[template_idx]['externalEpgs'][epg_idx]['contractRelationships'][contract_idx] if state == 'query': if not contract: mso.existing = schema_obj.get('templates')[template_idx]['externalEpgs'][epg_idx]['contractRelationships'] elif not mso.existing: mso.fail_json(msg="Contract '{0}' not found".format(contract_ref)) if 'contractRef' in mso.existing: mso.existing['contractRef'] = mso.dict_from_ref(mso.existing.get('contractRef')) mso.exit_json() contracts_path = '/templates/{0}/externalEpgs/{1}/contractRelationships'.format(template, external_epg) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=contract_path)) elif state == 'present': payload = dict( relationshipType=contract.get('type'), contractRef=dict( contractName=contract.get('name'), templateName=contract.get('template'), schemaId=contract.get('schema_id'), ), ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=contract_path, value=mso.sent)) else: ops.append(dict(op='add', path=contracts_path + '/-', value=mso.sent)) mso.existing = mso.proposed if 'contractRef' in mso.previous: mso.previous['contractRef'] = mso.dict_from_ref(mso.previous.get('contractRef')) if not module.check_mode and mso.proposed != mso.previous: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), contract=dict(type='str', required=True), contract_display_name=dict(type='str'), contract_scope=dict( type='str', choices=['application-profile', 'global', 'tenant', 'vrf']), contract_filter_type=dict(type='str', default='both-way', choices=['both-way', 'one-way']), filter=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects filter_directives=dict(type='list', choices=['log', 'none']), filter_template=dict(type='str'), filter_schema=dict(type='str'), filter_type=dict(type='str', default='both-way', choices=list(FILTER_KEYS), aliases=['type']), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['filter']], ['state', 'present', ['filter']], ], ) schema = module.params.get('schema') template = module.params.get('template') contract = module.params.get('contract') contract_display_name = module.params.get('contract_display_name') contract_filter_type = module.params.get('contract_filter_type') contract_scope = module.params.get('contract_scope') filter_name = module.params.get('filter') filter_directives = module.params.get('filter_directives') filter_template = module.params.get('filter_template') filter_schema = module.params.get('filter_schema') filter_type = module.params.get('filter_type') state = module.params.get('state') mso = MSOModule(module) contract_ftype = 'bothWay' if contract_filter_type == 'both-way' else 'oneWay' if contract_filter_type == 'both-way' and filter_type != 'both-way': mso.fail_json( msg="You are adding 'one-way' filters to a 'both-way' contract") elif contract_filter_type != 'both-way' and filter_type == 'both-way': mso.fail_json( msg="You are adding 'both-way' filters to a 'one-way' contract") if filter_template is None: filter_template = template if filter_schema is None: filter_schema = schema filter_key = FILTER_KEYS.get(filter_type) # Get schema object schema_obj = mso.get_obj('schemas', displayName=schema) if schema_obj: schema_id = schema_obj.get('id') else: mso.fail_json( msg="Provided schema '{0}' does not exist".format(schema)) schema_path = 'schemas/{id}'.format(**schema_obj) # Get template templates = [t.get('name') for t in schema_obj.get('templates')] if template not in templates: mso.fail_json( msg="Provided template '{0}' does not exist. Existing templates: {1}" .format(template, ', '.join(templates))) template_idx = templates.index(template) filter_schema_id = mso.lookup_schema(filter_schema) # Get contracts mso.existing = {} contract_idx = None filter_idx = None contracts = [ c.get('name') for c in schema_obj.get('templates')[template_idx]['contracts'] ] if contract in contracts: contract_idx = contracts.index(contract) filters = [ f.get('filterRef') for f in schema_obj.get('templates') [template_idx]['contracts'][contract_idx][filter_key] ] filter_ref = mso.filter_ref(schema_id=filter_schema_id, template=filter_template, filter=filter_name) if filter_ref in filters: filter_idx = filters.index(filter_ref) filter_path = '/templates/{0}/contracts/{1}/{2}/{3}'.format( template, contract, filter_key, filter_name) mso.existing = schema_obj.get('templates')[template_idx][ 'contracts'][contract_idx][filter_key][filter_idx] if state == 'query': if contract_idx is None: mso.fail_json( msg= "Provided contract '{0}' does not exist. Existing contracts: {1}" .format(contract, ', '.join(contracts))) if filter_name is None: mso.existing = schema_obj.get('templates')[template_idx][ 'contracts'][contract_idx][filter_key] elif not mso.existing: mso.fail_json(msg="FilterRef '{filter_ref}' not found".format( filter_ref=filter_ref)) mso.exit_json() ops = [] contract_path = '/templates/{0}/contracts/{1}'.format(template, contract) filters_path = '/templates/{0}/contracts/{1}/{2}'.format( template, contract, filter_key) mso.previous = mso.existing if state == 'absent': mso.proposed = mso.sent = {} if contract_idx is None: # There was no contract to begin with pass elif filter_idx is None: # There was no filter to begin with pass elif len(filters) == 1: # There is only one filter, remove contract mso.existing = {} ops.append(dict(op='remove', path=contract_path)) else: # Remove filter mso.existing = {} ops.append(dict(op='remove', path=filter_path)) elif state == 'present': if filter_directives is None: filter_directives = ['none'] payload = dict( filterRef=dict( filterName=filter_name, templateName=filter_template, schemaId=filter_schema_id, ), directives=filter_directives, ) mso.sanitize(payload, collate=True) mso.existing = mso.sent if contract_scope is None or contract_scope == 'vrf': contract_scope = 'context' if contract_idx is None: # Contract does not exist, so we have to create it if contract_display_name is None: contract_display_name = contract payload = { 'name': contract, 'displayName': contract_display_name, 'filterType': contract_ftype, 'scope': contract_scope, } ops.append( dict(op='add', path='/templates/{0}/contracts/-'.format(template), value=payload)) else: # Contract exists, but may require an update if contract_display_name is not None: ops.append( dict(op='replace', path=contract_path + '/displayName', value=contract_display_name)) ops.append( dict(op='replace', path=contract_path + '/filterType', value=contract_ftype)) ops.append( dict(op='replace', path=contract_path + '/scope', value=contract_scope)) if filter_idx is None: # Filter does not exist, so we have to add it ops.append(dict(op='add', path=filters_path + '/-', value=mso.sent)) else: # Filter exists, we have to update it ops.append(dict(op='replace', path=filter_path, value=mso.sent)) if 'filterRef' in mso.previous: mso.previous['filterRef'] = mso.dict_from_ref( mso.previous['filterRef']) if not module.check_mode: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), contract=dict(type='str', required=True), description=dict(type='str'), contract_display_name=dict(type='str'), contract_scope=dict( type='str', choices=['application-profile', 'global', 'tenant', 'vrf']), contract_filter_type=dict(type='str', default='both-way', choices=['both-way', 'one-way']), filter=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects filter_directives=dict(type='list', elements='str', choices=['log', 'none', 'policy_compression']), filter_template=dict(type='str'), filter_schema=dict(type='str'), filter_type=dict(type='str', default='both-way', choices=list(FILTER_KEYS), aliases=['type']), qos_level=dict(type='str'), action=dict(type='str', choices=['permit', 'deny']), priority=dict(type='str', choices=[ 'default', 'lowest_priority', 'medium_priority', 'highest_priority' ]), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['filter']], ['state', 'present', ['filter']], ], ) schema = module.params.get('schema') template = module.params.get('template').replace(' ', '') contract = module.params.get('contract') contract_display_name = module.params.get('contract_display_name') description = module.params.get('description') contract_filter_type = module.params.get('contract_filter_type') contract_scope = module.params.get('contract_scope') filter_name = module.params.get('filter') filter_directives = module.params.get('filter_directives') filter_template = module.params.get('filter_template') if filter_template is not None: filter_template = filter_template.replace(' ', '') filter_schema = module.params.get('filter_schema') filter_type = module.params.get('filter_type') qos_level = module.params.get('qos_level') action = module.params.get('action') priority = module.params.get('priority') state = module.params.get('state') mso = MSOModule(module) contract_ftype = 'bothWay' if contract_filter_type == 'both-way' else 'oneWay' if contract_filter_type == 'both-way' and filter_type != 'both-way': mso.fail_json( msg="You are adding 'one-way' filters to a 'both-way' contract") elif contract_filter_type != 'both-way' and filter_type == 'both-way': mso.fail_json( msg="You are adding 'both-way' filters to a 'one-way' contract") if filter_template is None: filter_template = template if filter_schema is None: filter_schema = schema filter_key = FILTER_KEYS.get(filter_type) # Get schema schema_id, schema_path, schema_obj = mso.query_schema(schema) # Get template templates = [t.get('name') for t in schema_obj.get('templates')] if template not in templates: mso.fail_json( msg="Provided template '{0}' does not exist. Existing templates: {1}" .format(template, ', '.join(templates))) template_idx = templates.index(template) filter_schema_id = mso.lookup_schema(filter_schema) # Get contracts mso.existing = {} contract_idx = None filter_idx = None contracts = [ c.get('name') for c in schema_obj.get('templates')[template_idx]['contracts'] ] if contract in contracts: contract_idx = contracts.index(contract) contract_obj = schema_obj.get( 'templates')[template_idx]['contracts'][contract_idx] filters = [ f.get('filterRef') for f in schema_obj.get('templates') [template_idx]['contracts'][contract_idx][filter_key] ] filter_ref = mso.filter_ref(schema_id=filter_schema_id, template=filter_template, filter=filter_name) if filter_ref in filters: filter_idx = filters.index(filter_ref) filter_path = '/templates/{0}/contracts/{1}/{2}/{3}'.format( template, contract, filter_key, filter_name) filter = contract_obj.get(filter_key)[filter_idx] mso.existing = mso.update_filter_obj(contract_obj, filter, filter_type) if state == 'query': if contract_idx is None: mso.fail_json( msg= "Provided contract '{0}' does not exist. Existing contracts: {1}" .format(contract, ', '.join(contracts))) if filter_name is None: mso.existing = contract_obj.get(filter_key) for filter in mso.existing: filter = mso.update_filter_obj(contract_obj, filter, filter_type) elif not mso.existing: mso.fail_json(msg="FilterRef '{filter_ref}' not found".format( filter_ref=filter_ref)) mso.exit_json() ops = [] contract_path = '/templates/{0}/contracts/{1}'.format(template, contract) filters_path = '/templates/{0}/contracts/{1}/{2}'.format( template, contract, filter_key) mso.previous = mso.existing if state == 'absent': mso.proposed = mso.sent = {} if contract_idx is None: # There was no contract to begin with pass elif filter_idx is None: # There was no filter to begin with pass elif len(filters) == 1: # There is only one filter, remove contract mso.existing = {} ops.append(dict(op='remove', path=contract_path)) else: # Remove filter mso.existing = {} ops.append(dict(op='remove', path=filter_path)) elif state == 'present': if filter_directives is None: filter_directives = ['none'] if 'policy_compression' in filter_directives: filter_directives.remove('policy_compression') filter_directives.append('no_stats') payload = dict( filterRef=dict( filterName=filter_name, templateName=filter_template, schemaId=filter_schema_id, ), directives=filter_directives, ) if action is not None: payload.update(action=action) if action == 'deny' and priority is not None: priority_map = { 'lowest_priority': 'level1', 'medium_priority': 'level2', 'highest_priority': 'level3', } payload.update(priorityOverride=priority_map[priority]) mso.sanitize( payload, collate=True, unwanted=['filterType', 'contractScope', 'contractFilterType']) mso.existing = mso.sent if contract_scope is None or contract_scope == 'vrf': contract_scope = 'context' if contract_idx is None: # Contract does not exist, so we have to create it if contract_display_name is None: contract_display_name = contract payload = { 'name': contract, 'displayName': contract_display_name, 'filterType': contract_ftype, 'scope': contract_scope, } if description is not None: payload.update(description=description) if qos_level is not None: payload.update(prio=qos_level) ops.append( dict(op='add', path='/templates/{0}/contracts/-'.format(template), value=payload)) else: # Contract exists, but may require an update if contract_display_name is not None: ops.append( dict(op='replace', path=contract_path + '/displayName', value=contract_display_name)) ops.append( dict(op='replace', path=contract_path + '/filterType', value=contract_ftype)) ops.append( dict(op='replace', path=contract_path + '/scope', value=contract_scope)) if contract_display_name: mso.existing['displayName'] = contract_display_name else: mso.existing['displayName'] = contract_obj.get('displayName') mso.existing['filterType'] = filter_type mso.existing['contractScope'] = contract_scope mso.existing['contractFilterType'] = contract_ftype if filter_idx is None: # Filter does not exist, so we have to add it ops.append(dict(op='add', path=filters_path + '/-', value=mso.sent)) else: # Filter exists, we have to update it ops.append(dict(op='replace', path=filter_path, value=mso.sent)) if not module.check_mode and mso.existing != mso.previous: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), vrf=dict(type='str', required=True), contract=dict(type='dict', options=mso_contractref_spec()), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['contract']], ['state', 'present', ['contract']], ], ) schema = module.params.get('schema') template = module.params.get('template').replace(' ', '') vrf = module.params.get('vrf') contract = module.params.get('contract') if contract is not None and contract.get('template') is not None: contract['template'] = contract.get('template').replace(' ', '') state = module.params.get('state') mso = MSOModule(module) if contract: if contract.get('schema') is None: contract['schema'] = schema contract['schema_id'] = mso.lookup_schema(contract.get('schema')) if contract.get('template') is None: contract['template'] = template # Get schema_id schema_obj = mso.get_obj('schemas', displayName=schema) if not schema_obj: mso.fail_json( msg="Provided schema '{0}' does not exist".format(schema)) schema_path = 'schemas/{id}'.format(**schema_obj) # Get template templates = [t.get('name') for t in schema_obj.get('templates')] if template not in templates: mso.fail_json( msg="Provided template '{0}' does not exist. Existing templates: {1}" .format(template, ', '.join(templates))) template_idx = templates.index(template) # Get VRF vrfs = [ e.get('name') for e in schema_obj.get('templates')[template_idx]['vrfs'] ] if vrf not in vrfs: mso.fail_json( msg="Provided vrf '{vrf}' does not exist. Existing vrfs: {vrfs}". format(vrf=vrf, vrfs=', '.join(vrfs))) vrf_idx = vrfs.index(vrf) vrf_obj = schema_obj.get('templates')[template_idx]['vrfs'][vrf_idx] if not vrf_obj.get('vzAnyEnabled'): mso.fail_json( msg="vzAny attribute on vrf '{0}' is disabled.".format(vrf)) # Get Contract if contract: provider_contracts = [ c.get('contractRef') for c in schema_obj.get('templates') [template_idx]['vrfs'][vrf_idx]['vzAnyProviderContracts'] ] consumer_contracts = [ c.get('contractRef') for c in schema_obj.get('templates') [template_idx]['vrfs'][vrf_idx]['vzAnyConsumerContracts'] ] contract_ref = mso.contract_ref(**contract) if contract_ref in provider_contracts and contract.get( 'type') == 'provider': contract_idx = provider_contracts.index(contract_ref) contract_path = '/templates/{0}/vrfs/{1}/vzAnyProviderContracts/{2}'.format( template, vrf, contract_idx) mso.existing = schema_obj.get('templates')[template_idx]['vrfs'][ vrf_idx]['vzAnyProviderContracts'][contract_idx] if contract_ref in consumer_contracts and contract.get( 'type') == 'consumer': contract_idx = consumer_contracts.index(contract_ref) contract_path = '/templates/{0}/vrfs/{1}/vzAnyConsumerContracts/{2}'.format( template, vrf, contract_idx) mso.existing = schema_obj.get('templates')[template_idx]['vrfs'][ vrf_idx]['vzAnyConsumerContracts'][contract_idx] if mso.existing.get('contractRef'): mso.existing['contractRef'] = mso.dict_from_ref( mso.existing.get('contractRef')) mso.existing['relationshipType'] = contract.get('type') if state == 'query': if not contract: provider_contracts = [ dict(contractRef=mso.dict_from_ref(c.get('contractRef')), relationshipType='provider') for c in schema_obj.get('templates')[template_idx]['vrfs'] [vrf_idx]['vzAnyProviderContracts'] ] consumer_contracts = [ dict(contractRef=mso.dict_from_ref(c.get('contractRef')), relationshipType='consumer') for c in schema_obj.get('templates')[template_idx]['vrfs'] [vrf_idx]['vzAnyConsumerContracts'] ] mso.existing = provider_contracts + consumer_contracts elif not mso.existing: mso.fail_json( msg="Contract '{0}' not found".format(contract.get('name'))) mso.exit_json() if contract.get('type') == 'provider': contracts_path = '/templates/{0}/vrfs/{1}/vzAnyProviderContracts/-'.format( template, vrf) if contract.get('type') == 'consumer': contracts_path = '/templates/{0}/vrfs/{1}/vzAnyConsumerContracts/-'.format( template, vrf) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=contract_path)) elif state == 'present': payload = dict(contractRef=dict( contractName=contract.get('name'), templateName=contract.get('template'), schemaId=contract.get('schema_id'), ), ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=contract_path, value=mso.sent)) else: ops.append(dict(op='add', path=contracts_path, value=mso.sent)) mso.existing = mso.proposed mso.existing['relationshipType'] = contract.get('type') if not module.check_mode and mso.proposed != mso.previous: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( source_schema=dict(type='str'), destination_schema=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']], ], ) source_schema = module.params.get('source_schema') destination_schema = module.params.get('destination_schema') state = module.params.get('state') mso = MSOModule(module) # Get source schema details source_schema_path = "schemas/{0}".format(mso.lookup_schema(source_schema)) source_schema_obj = mso.query_obj(source_schema_path, displayName=source_schema) source_data = source_schema_obj.get('templates') source_data = json.loads( json.dumps(source_data).replace('/{0}'.format(source_schema_path), '')) path = 'schemas' # Check if source and destination schema are named differently if source_schema == destination_schema: mso.fail_json( msg="Source and Destination schema cannot have same names.") # Query for existing object(s) if destination_schema: mso.existing = mso.get_obj(path, displayName=destination_schema) if mso.existing: mso.fail_json( msg= "Schema with the name '{0}' already exists. Please use another name." .format(destination_schema)) if state == 'clone': mso.previous = mso.existing payload = dict( displayName=destination_schema, templates=source_data, ) mso.sanitize(payload, collate=True) if not mso.existing: 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_relay_policy=dict(type="str", required=True, aliases=['name']), ip=dict(type="str"), tenant=dict(type="str"), schema=dict(type="str"), template=dict(type="str"), application_profile=dict(type="str", aliases=['anp']), endpoint_group=dict(type="str", aliases=['epg']), external_endpoint_group=dict(type="str", aliases=['ext_epg', 'external_epg']), state=dict(type="str", default="present", choices=["absent", "present", "query"]), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ["state", "present", ["ip", "tenant", "schema", "template"]], ["state", "absent", ["tenant", "schema", "template"]], ], ) dhcp_relay_policy = module.params.get("dhcp_relay_policy") ip = module.params.get("ip") tenant = module.params.get("tenant") schema = module.params.get("schema") template = module.params.get("template") if template is not None: template = template.replace(' ', '') application_profile = module.params.get("application_profile") endpoint_group = module.params.get("endpoint_group") external_endpoint_group = module.params.get("external_endpoint_group") state = module.params.get("state") mso = MSOModule(module) path = "policies/dhcp/relay" tenant_id = mso.lookup_tenant(tenant) # Get schema_id schema_id = mso.lookup_schema(schema) provider = dict( addr=ip, externalEpgRef='', epgRef='', l3Ref='', tenantId=tenant_id, ) provider_index = None previous_provider = {} if application_profile is not None and endpoint_group is not None: provider[ 'epgRef'] = '/schemas/{schemaId}/templates/{templateName}/anps/{app}/epgs/{epg}'.format( schemaId=schema_id, templateName=template, app=application_profile, epg=endpoint_group, ) elif external_endpoint_group is not None: provider[ 'externalEpgRef'] = '/schemas/{schemaId}/templates/{templateName}/externalEpgs/{ext_epg}'.format( schemaId=schema_id, templateName=template, ext_epg=external_endpoint_group) # Query for existing object(s) dhcp_relay_obj = mso.get_obj(path, name=dhcp_relay_policy, key="DhcpRelayPolicies") if 'id' not in dhcp_relay_obj: mso.fail_json( msg="DHCP Relay Policy '{0}' is not a valid DHCP Relay Policy name." .format(dhcp_relay_policy)) policy_id = dhcp_relay_obj.get("id") providers = [] if "provider" in dhcp_relay_obj: providers = dhcp_relay_obj.get('provider') for index, prov in enumerate(providers): if ((provider.get('epgRef') != '' and prov.get('epgRef') == provider.get('epgRef')) or (provider.get('externalEpgRef') != '' and prov.get('externalEpgRef') == provider.get('externalEpgRef'))): previous_provider = prov provider_index = index # If we found an existing object, continue with it path = '{0}/{1}'.format(path, policy_id) if state == "query": mso.existing = providers if endpoint_group is not None or external_endpoint_group is not None: mso.existing = previous_provider mso.exit_json() if endpoint_group is None and external_endpoint_group is None: mso.fail_json( msg= "Missing either endpoint_group or external_endpoint_group required attribute." ) mso.previous = previous_provider if state == "absent": provider = {} if previous_provider: if provider_index is not None: providers.pop(provider_index) elif state == "present": if provider_index is not None: providers[provider_index] = provider else: providers.append(provider) if module.check_mode: mso.existing = provider else: mso.existing = dhcp_relay_obj dhcp_relay_obj["provider"] = providers mso.sanitize(dhcp_relay_obj, collate=True) new_dhcp_relay_obj = mso.request(path, method="PUT", data=mso.sent) mso.existing = {} for index, prov in enumerate(new_dhcp_relay_obj.get('provider')): if ((provider.get('epgRef') != '' and prov.get('epgRef') == provider.get('epgRef')) or (provider.get('externalEpgRef') != '' and prov.get('externalEpgRef') == provider.get('externalEpgRef'))): mso.existing = prov mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), bds=dict(type='list', elements='str'), epgs=dict(type='list', elements='dict', options=mso_object_migrate_spec()), target_schema=dict(type='str', required=True), target_template=dict(type='str', required=True), state=dict(type='str', default='present'), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) schema = module.params.get('schema') template = module.params.get('template').replace(' ', '') target_schema = module.params.get('target_schema') target_template = module.params.get('target_template').replace(' ', '') bds = module.params.get('bds') epgs = module.params.get('epgs') state = module.params.get('state') mso = MSOModule(module) # Get schema_id schema_id = mso.lookup_schema(schema) target_schema_id = mso.lookup_schema(target_schema) if state == 'present': if schema_id is not None: bds_payload = [] if bds is not None: for bd in bds: bds_payload.append(dict(name=bd)) anp_dict = {} if epgs is not None: for epg in epgs: if epg.get('anp') in anp_dict: anp_dict[epg.get('anp')].append( dict(name=epg.get('epg'))) else: anp_dict[epg.get('anp')] = [dict(name=epg.get('epg'))] anps_payload = [] for anp, epgs_payload in anp_dict.items(): anps_payload.append(dict(name=anp, epgs=epgs_payload)) payload = dict( targetSchemaId=target_schema_id, targetTemplateName=target_template, bds=bds_payload, anps=anps_payload, ) template = template.replace(' ', '%20') target_template = target_template.replace( ' ', '%20') # removes API error for extra space mso.existing = mso.request( path='migrate/schema/{0}/template/{1}'.format( schema_id, template), method='POST', data=payload) mso.exit_json()
def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), template=dict(type='str', required=True), anp=dict(type='str', required=True), epg=dict(type='str', required=True), contract=dict(type='dict', options=mso_contractref_spec()), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['contract']], ['state', 'present', ['contract']], ], ) schema = module.params['schema'] template = module.params['template'] anp = module.params['anp'] epg = module.params['epg'] contract = module.params['contract'] state = module.params['state'] mso = MSOModule(module) if contract: if contract.get('schema') is None: contract['schema'] = schema contract['schema_id'] = mso.lookup_schema(contract['schema']) if contract.get('template') is None: contract['template'] = template # Get schema_id schema_obj = mso.get_obj('schemas', displayName=schema) if schema_obj: schema_id = schema_obj['id'] else: mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema)) schema_path = 'schemas/{id}'.format(**schema_obj) # Get template templates = [t['name'] for t in schema_obj['templates']] if template not in templates: mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates))) template_idx = templates.index(template) # Get ANP anps = [a['name'] for a in schema_obj['templates'][template_idx]['anps']] if anp not in anps: mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ', '.join(anps))) anp_idx = anps.index(anp) # Get EPG epgs = [e['name'] for e in schema_obj['templates'][template_idx]['anps'][anp_idx]['epgs']] if epg not in epgs: mso.fail_json(msg="Provided epg '{epg}' does not exist. Existing epgs: {epgs}".format(epg=epg, epgs=', '.join(epgs))) epg_idx = epgs.index(epg) # Get Contract if contract: contracts = [(c['contractRef'], c['relationshipType']) for c in schema_obj['templates'][template_idx]['anps'][anp_idx]['epgs'][epg_idx]['contractRelationships']] contract_ref = mso.contract_ref(**contract) if (contract_ref, contract['type']) in contracts: contract_idx = contracts.index((contract_ref, contract['type'])) contract_path = '/templates/{0}/anps/{1}/epgs/{2}/contractRelationships/{3}'.format(template, anp, epg, contract) mso.existing = schema_obj['templates'][template_idx]['anps'][anp_idx]['epgs'][epg_idx]['contractRelationships'][contract_idx] if state == 'query': if not contract: mso.existing = schema_obj['templates'][template_idx]['anps'][anp_idx]['epgs'][epg_idx]['contractRelationships'] elif not mso.existing: mso.fail_json(msg="Contract '{0}' not found".format(contract_ref)) mso.exit_json() contracts_path = '/templates/{0}/anps/{1}/epgs/{2}/contractRelationships'.format(template, anp, epg) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=contract_path)) elif state == 'present': payload = dict( relationshipType=contract['type'], contractRef=dict( contractName=contract['name'], templateName=contract['template'], schemaId=contract['schema_id'], ), ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=contract_path, value=mso.sent)) else: ops.append(dict(op='add', path=contracts_path + '/-', value=mso.sent)) mso.existing = mso.proposed if not module.check_mode: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()