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( 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), 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()