def main(): argument_spec = mso_argument_spec() argument_spec.update( schema=dict(type='str', required=True), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', required=True), l3out=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['l3out']], ['state', 'present', ['l3out']], ], ) schema = module.params['schema'] site = module.params['site'] template = module.params['template'] bd = module.params['bd'] l3out = module.params['l3out'] state = module.params['state'] mso = MSOModule(module) # 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) schema_id = schema_obj['id'] # Get site site_id = mso.lookup_site(site) # Get site_idx sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']] if (site_id, template) not in sites: mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites))) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v['bdRef'] for v in schema_obj['sites'][site_idx]['bds']] if bd_ref not in bds: mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ', '.join(bds))) bd_idx = bds.index(bd_ref) # Get L3out l3outs = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs'] if l3out is not None and l3out in l3outs: l3out_idx = l3outs.index(l3out) # FIXME: Changes based on index are DANGEROUS l3out_path = '/sites/{0}/bds/{1}/l3Outs/{2}'.format(site_template, bd, l3out_idx) mso.existing = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs'][l3out_idx] if state == 'query': if l3out is None: mso.existing = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs'] elif not mso.existing: mso.fail_json(msg="L3out '{l3out}' not found".format(l3out=l3out)) mso.exit_json() l3outs_path = '/sites/{0}/bds/{1}/l3Outs'.format(site_template, bd) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=l3out_path)) elif state == 'present': mso.sent = l3out if not mso.existing: ops.append(dict(op='add', path=l3outs_path + '/-', value=l3out)) mso.existing = mso.sent 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), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects host_route=dict(type='bool'), svi_mac=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', ['bd']], ['state', 'present', ['bd']], ], ) schema = module.params.get('schema') site = module.params.get('site') template = module.params.get('template').replace(' ', '') bd = module.params.get('bd') host_route = module.params.get('host_route') svi_mac = module.params.get('svi_mac') state = module.params.get('state') mso = MSOModule(module) # Get schema objects 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))) # Get site site_id = mso.lookup_site(site) # Get site_idx if 'sites' not in schema_obj: mso.fail_json( msg= "No site associated with template '{0}'. Associate the site with the template using mso_schema_site." .format(template)) sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')] if (site_id, template) not in sites: mso.fail_json( msg="Provided site-template association '{0}-{1}' does not exist.". format(site, template)) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v.get('bdRef') for v in schema_obj.get('sites')[site_idx]['bds']] if bd is not None and bd_ref in bds: bd_idx = bds.index(bd_ref) bd_path = '/sites/{0}/bds/{1}'.format(site_template, bd) mso.existing = schema_obj.get('sites')[site_idx]['bds'][bd_idx] mso.existing['bdRef'] = mso.dict_from_ref(mso.existing.get('bdRef')) if state == 'query': if bd is None: mso.existing = schema_obj.get('sites')[site_idx]['bds'] for bd in mso.existing: bd['bdRef'] = mso.dict_from_ref(bd.get('bdRef')) elif not mso.existing: mso.fail_json(msg="BD '{bd}' not found".format(bd=bd)) mso.exit_json() bds_path = '/sites/{0}/bds'.format(site_template) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=bd_path)) elif state == 'present': if not mso.existing: if host_route is None: host_route = False payload = dict( bdRef=dict( schemaId=schema_id, templateName=template, bdName=bd, ), hostBasedRouting=host_route, ) if svi_mac is not None: payload.update(mac=svi_mac) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=bd_path, value=mso.sent)) else: ops.append(dict(op='add', path=bds_path + '/-', value=mso.sent)) mso.existing = mso.proposed 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(mso_subnet_spec()) argument_spec.update( schema=dict(type='str', required=True), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', aliases=['name'], required=True), subnet=dict(type='str', aliases=['ip']), description=dict(type='str'), scope=dict(type='str', choices=['private', 'public']), shared=dict(type='bool', default=False), no_default_gateway=dict(type='bool', default=False), querier=dict(type='bool', default=False), is_virtual_ip=dict(type='bool', default=False), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['subnet']], ['state', 'present', ['subnet']], ], ) schema = module.params.get('schema') site = module.params.get('site') template = module.params.get('template').replace(' ', '') bd = module.params.get('bd') subnet = module.params.get('subnet') description = module.params.get('description') scope = module.params.get('scope') shared = module.params.get('shared') no_default_gateway = module.params.get('no_default_gateway') querier = module.params.get('querier') is_virtual_ip = module.params.get('is_virtual_ip') state = module.params.get('state') mso = MSOModule(module) # Get schema objects 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 template BDs template_bds = [ b.get('name') for b in schema_obj.get('templates')[template_idx]['bds'] ] # Get template BD if bd not in template_bds: mso.fail_json( msg="Provided BD '{0}' does not exist. Existing template BDs: {1}". format(bd, ', '.join(template_bds))) template_bd_idx = template_bds.index(bd) template_bd = schema_obj.get( 'templates')[template_idx]['bds'][template_bd_idx] if template_bd.get('l2Stretch') is True and state == 'present': mso.fail_json( msg= "The l2Stretch of template bd should be false in order to create a site bd subnet. Set l2Stretch as false using mso_schema_template_bd" ) # Get site site_id = mso.lookup_site(site) # Get site_idx if 'sites' not in schema_obj: mso.fail_json( msg= "No site associated with template '{0}'. Associate the site with the template using mso_schema_site." .format(template)) sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')] if (site_id, template) not in sites: mso.fail_json( msg="Provided site/template '{0}-{1}' does not exist.".format( site, template)) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v.get('bdRef') for v in schema_obj.get('sites')[site_idx]['bds']] if bd_ref not in bds: mso.fail_json( msg="Provided BD '{0}' does not exist. Existing site BDs: {1}". format(bd, ', '.join(bds))) bd_idx = bds.index(bd_ref) # Get Subnet subnets = [ s.get('ip') for s in schema_obj.get('sites')[site_idx]['bds'][bd_idx]['subnets'] ] if subnet in subnets: subnet_idx = subnets.index(subnet) # FIXME: Changes based on index are DANGEROUS subnet_path = '/sites/{0}/bds/{1}/subnets/{2}'.format( site_template, bd, subnet_idx) mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['subnets'][subnet_idx] if state == 'query': if subnet is None: mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['subnets'] elif not mso.existing: mso.fail_json(msg="Subnet IP '{subnet}' not found".format( subnet=subnet)) mso.exit_json() subnets_path = '/sites/{0}/bds/{1}/subnets'.format(site_template, bd) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=subnet_path)) elif state == 'present': if not mso.existing: if description is None: description = subnet if scope is None: scope = 'private' payload = dict( ip=subnet, description=description, scope=scope, shared=shared, noDefaultGateway=no_default_gateway, virtual=is_virtual_ip, querier=querier, ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=subnet_path, value=mso.sent)) else: ops.append(dict(op='add', path=subnets_path + '/-', value=mso.sent)) mso.existing = mso.proposed 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), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) argument_spec.update(mso_subnet_spec()) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['bd']], ['state', 'present', ['bd']], ], ) schema = module.params.get('schema') site = module.params.get('site') template = module.params.get('template') bd = module.params.get('bd') subnet = module.params.get('subnet') description = module.params.get('description') scope = module.params.get('scope') shared = module.params.get('shared') no_default_gateway = module.params.get('no_default_gateway') querier = module.params.get('querier') state = module.params.get('state') mso = MSOModule(module) # 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) schema_id = schema_obj.get('id') # Get site site_id = mso.lookup_site(site) # Get site_idx if 'sites' not in schema_obj: mso.fail_json( msg= "No site associated with template '{0}'. Associate the site with the template using mso_schema_site." .format(template)) sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')] if (site_id, template) not in sites: mso.fail_json( msg= "Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}" .format(site, template, ', '.join(sites))) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v.get('bdRef') for v in schema_obj.get('sites')[site_idx]['bds']] if bd_ref not in bds: mso.fail_json( msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format( bd, ', '.join(bds))) bd_idx = bds.index(bd_ref) # Get Subnet subnets = [ s.get('ip') for s in schema_obj.get('sites')[site_idx]['bds'][bd_idx]['subnets'] ] if subnet in subnets: subnet_idx = subnets.index(subnet) # FIXME: Changes based on index are DANGEROUS subnet_path = '/sites/{0}/bds/{1}/subnets/{2}'.format( site_template, bd, subnet_idx) mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['subnets'][subnet_idx] if state == 'query': if subnet is None: mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['subnets'] elif not mso.existing: mso.fail_json(msg="Subnet IP '{subnet}' not found".format( subnet=subnet)) mso.exit_json() subnets_path = '/sites/{0}/bds/{1}/subnets'.format(site_template, bd) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=subnet_path)) elif state == 'present': if not mso.existing: if description is None: description = subnet if scope is None: scope = 'private' if shared is None: shared = False if no_default_gateway is None: no_default_gateway = False if querier is None: querier = False payload = dict( ip=subnet, description=description, scope=scope, shared=shared, noDefaultGateway=no_default_gateway, querier=querier, ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=subnet_path, value=mso.sent)) else: ops.append(dict(op='add', path=subnets_path + '/-', value=mso.sent)) mso.existing = mso.proposed 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), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects host_route=dict(type='bool'), state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['bd']], ['state', 'present', ['bd']], ], ) schema = module.params.get('schema') site = module.params.get('site') template = module.params.get('template') bd = module.params.get('bd') host_route = module.params.get('host_route') state = module.params.get('state') mso = MSOModule(module) # 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) schema_id = schema_obj.get('id') # Get site site_id = mso.lookup_site(site) # Get site_idx sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')] if (site_id, template) not in sites: mso.fail_json( msg= "Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}" .format(site, template, ', '.join(sites))) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v.get('bdRef') for v in schema_obj.get('sites')[site_idx]['bds']] if bd is not None and bd_ref in bds: bd_idx = bds.index(bd_ref) bd_path = '/sites/{0}/bds/{1}'.format(site_template, bd) mso.existing = schema_obj.get('sites')[site_idx]['bds'][bd_idx] if state == 'query': if bd is None: mso.existing = schema_obj.get('sites')[site_idx]['bds'] elif not mso.existing: mso.fail_json(msg="BD '{bd}' not found".format(bd=bd)) mso.exit_json() bds_path = '/sites/{0}/bds'.format(site_template) ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=bd_path)) elif state == 'present': if not mso.existing: if host_route is None: host_route = False payload = dict( bdRef=dict( schemaId=schema_id, templateName=template, bdName=bd, ), hostBasedRouting=host_route, ) mso.sanitize(payload, collate=True) if mso.existing: ops.append(dict(op='replace', path=bd_path, value=mso.sent)) else: ops.append(dict(op='add', path=bds_path + '/-', value=mso.sent)) mso.existing = mso.proposed 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), site=dict(type='str', required=True), template=dict(type='str', required=True), bd=dict(type='str', required=True), l3out=dict(type='str', aliases=[ 'name' ]), # This parameter is not required for querying all objects state=dict(type='str', default='present', choices=['absent', 'present', 'query']), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, required_if=[ ['state', 'absent', ['l3out']], ['state', 'present', ['l3out']], ], ) schema = module.params.get('schema') site = module.params.get('site') template = module.params.get('template').replace(' ', '') bd = module.params.get('bd') l3out = module.params.get('l3out') state = module.params.get('state') mso = MSOModule(module) # Get schema objects 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 site site_id = mso.lookup_site(site) # Get site_idx if 'sites' not in schema_obj: mso.fail_json( msg= "No site associated with template '{0}'. Associate the site with the template using mso_schema_site." .format(template)) sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')] if (site_id, template) not in sites: mso.fail_json( msg= "Provided site/template '{0}-{1}' does not exist. Existing sites/templates '{2}'" .format(site, template, ', '.join(sites))) # Schema-access uses indexes site_idx = sites.index((site_id, template)) # Path-based access uses site_id-template site_template = '{0}-{1}'.format(site_id, template) payload = dict() ops = [] op_path = '' # Get BD bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd) bds = [v.get('bdRef') for v in schema_obj.get('sites')[site_idx]['bds']] bds_in_temp = [ a.get('name') for a in schema_obj['templates'][template_idx]['bds'] ] if bd not in bds_in_temp: mso.fail_json( msg="Provided BD '{0}' does not exist. Existing BDs '{1}'".format( bd, ', '.join(bds_in_temp))) # If bd not at site level but exists at template level if bd_ref not in bds: op_path = '/sites/{0}/bds'.format(site_template) payload.update(bdRef=dict( schemaId=schema_id, templateName=template, bdName=bd, ), ) else: # Get bd index at site level bd_idx = bds.index(bd_ref) # Get L3out # If bd is at site level if 'bdRef' not in payload: l3outs = schema_obj.get('sites')[site_idx]['bds'][bd_idx]['l3Outs'] if l3out is not None and l3out in l3outs: l3out_idx = l3outs.index(l3out) # FIXME: Changes based on index are DANGEROUS op_path = '/sites/{0}/bds/{1}/l3Outs/{2}'.format( site_template, bd, l3out_idx) mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['l3Outs'][l3out_idx] else: op_path = '/sites/{0}/bds/{1}/l3Outs'.format(site_template, bd) if state == 'query': if l3out is None: mso.existing = schema_obj.get( 'sites')[site_idx]['bds'][bd_idx]['l3Outs'] elif not mso.existing: mso.fail_json(msg="L3out '{l3out}' not found".format(l3out=l3out)) mso.exit_json() ops = [] mso.previous = mso.existing if state == 'absent': if mso.existing: mso.sent = mso.existing = {} ops.append(dict(op='remove', path=op_path)) elif state == 'present': if not payload: payload = l3out else: # If bd in payload, add l3out to payload payload['l3Outs'] = [l3out] mso.sanitize(payload, collate=True) if not mso.existing: ops.append(dict(op='add', path=op_path + '/-', value=payload)) mso.existing = l3out if not module.check_mode: mso.request(schema_path, method='PATCH', data=ops) mso.exit_json()