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') site = module.params.get('site') state = module.params.get('state') mso = MSOModule(module) # Get schema 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)
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.get('schema') template = module.params.get('template') anp = module.params.get('anp') epg = module.params.get('epg') contract = module.params.get('contract') 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 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) # Get ANP anps = [ a.get('name') for a in schema_obj.get('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.get('name') for e in schema_obj.get('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.get('contractRef'), c.get('relationshipType')) for c in schema_obj.get('templates')[template_idx]['anps'] [anp_idx]['epgs'][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}/anps/{1}/epgs/{2}/contractRelationships/{3}'.format( template, anp, epg, contract) mso.existing = schema_obj.get( 'templates')[template_idx]['anps'][anp_idx]['epgs'][epg_idx][ 'contractRelationships'][contract_idx] if state == 'query': if not contract: mso.existing = schema_obj.get('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.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 not module.check_mode: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()