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)
    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),
        l3out=dict(type='str', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        display_name=dict(type='str'),
        vrf=dict(type='dict', options=mso_reference_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', ['l3out']],
            ['state', 'present', ['l3out', 'vrf']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template')
    l3out = module.params.get('l3out')
    display_name = module.params.get('display_name')
    vrf = module.params.get('vrf')
    state = module.params.get('state')

    mso = MSOModule(module)

    # 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 L3out
    l3outs = [
        l.get('name')
        for l in schema_obj.get('templates')[template_idx]['intersiteL3outs']
    ]

    if l3out is not None and l3out in l3outs:
        l3out_idx = l3outs.index(l3out)
        mso.existing = schema_obj.get(
            'templates')[template_idx]['intersiteL3outs'][l3out_idx]

    if state == 'query':
        if l3out is None:
            mso.existing = schema_obj.get(
                'templates')[template_idx]['intersiteL3outs']
        elif not mso.existing:
            mso.fail_json(msg="L3out '{l3out}' not found".format(l3out=l3out))
        mso.exit_json()

    l3outs_path = '/templates/{0}/intersiteL3outs'.format(template)
    l3out_path = '/templates/{0}/intersiteL3outs/{1}'.format(template, l3out)
    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':
        vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)

        if display_name is None and not mso.existing:
            display_name = l3out

        payload = dict(
            name=l3out,
            displayName=display_name,
            vrfRef=vrf_ref,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=l3out_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=l3outs_path + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 3
0
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),
        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', ['subnet']],
            ['state', 'present', ['subnet']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    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
    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 '{template}' does not exist. Existing templates: {templates}"
            .format(template=template, templates=', '.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 '{anp}' does not exist. Existing anps: {anps}".
            format(anp=anp, anps=', '.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 Subnet
    subnets = [
        s.get('ip') for s in schema_obj.get('templates')[template_idx]['anps']
        [anp_idx]['epgs'][epg_idx]['subnets']
    ]
    if subnet in subnets:
        subnet_idx = subnets.index(subnet)
        # FIXME: Changes based on index are DANGEROUS
        subnet_path = '/templates/{0}/anps/{1}/epgs/{2}/subnets/{3}'.format(
            template, anp, epg, subnet_idx)
        mso.existing = schema_obj.get('templates')[template_idx]['anps'][
            anp_idx]['epgs'][epg_idx]['subnets'][subnet_idx]

    if state == 'query':
        if subnet is None:
            mso.existing = schema_obj.get('templates')[template_idx]['anps'][
                anp_idx]['epgs'][epg_idx]['subnets']
        elif not mso.existing:
            mso.fail_json(msg="Subnet '{subnet}' not found".format(
                subnet=subnet))
        mso.exit_json()

    subnets_path = '/templates/{0}/anps/{1}/epgs/{2}/subnets'.format(
        template, anp, epg)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            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()
Ejemplo n.º 4
0
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),
        anp=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', ['anp']],
            ['state', 'present', ['anp']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    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 ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a.get('anpRef') for a in schema_obj.get('sites')[site_idx]['anps']]

    if anp is not None and anp_ref in anps:
        anp_idx = anps.index(anp_ref)
        anp_path = '/sites/{0}/anps/{1}'.format(site_template, anp)
        mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx]

    if state == 'query':
        if anp is None:
            mso.existing = schema_obj.get('sites')[site_idx]['anps']
        elif not mso.existing:
            mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp))
        mso.exit_json()

    anps_path = '/sites/{0}/anps'.format(site_template)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=anp_path))

    elif state == 'present':

        payload = dict(anpRef=dict(
            schemaId=schema_id,
            templateName=template,
            anpName=anp,
        ), )

        mso.sanitize(payload, collate=True)

        if not mso.existing:
            ops.append(dict(op='add', path=anps_path + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 5
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        schema=dict(type='str', required=True),
        template=dict(type='str', required=True),
        site=dict(type='str', required=True),
        external_epg=dict(type='str', required=True),
        selector=dict(type='str'),
        expressions=dict(type='list', elements='dict', options=mso_expression_spec_ext_epg()),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    site = module.params.get('site')
    external_epg = module.params.get('external_epg')
    selector = module.params.get('selector')
    expressions = module.params.get('expressions')
    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 '{template}' does not exist. Existing templates: {templates}".format(template=template,
                                                                                                                  templates=', '.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')]
    sites_list = [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/siteId/template '{0}/{1}/{2}' does not exist. "
                          "Existing siteIds/templates: {3}".format(site, site_id, template, ', '.join(sites_list)))

    # 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()
    op_path = ''

    # Get External EPG
    ext_epg_ref = mso.ext_epg_ref(schema_id=schema_id, template=template, external_epg=external_epg)
    external_epgs = [e.get('externalEpgRef') for e in schema_obj.get('sites')[site_idx]['externalEpgs']]

    if ext_epg_ref not in external_epgs:
        op_path = '/sites/{0}/externalEpgs/-'.format(site_template)
        payload = dict(
            externalEpgRef=dict(
                schemaId=schema_id,
                templateName=template,
                externalEpgName=external_epg,
            ),
            l3outDn='',
        )

    else:
        external_epg_idx = external_epgs.index(ext_epg_ref)

    # Get Selector
        selectors = [s.get('name') for s in schema_obj['sites'][site_idx]['externalEpgs'][external_epg_idx]['subnets']]
        if selector in selectors:
            selector_idx = selectors.index(selector)
            selector_path = '/sites/{0}/externalEpgs/{1}/subnets/{2}'.format(site_template, external_epg, selector_idx)
            mso.existing = schema_obj['sites'][site_idx]['externalEpgs'][external_epg_idx]['subnets'][selector_idx]

    selectors_path = '/sites/{0}/externalEpgs/{1}/subnets/-'.format(site_template, external_epg)
    ops = []

    if state == 'query':
        if selector is None:
            mso.existing = schema_obj['sites'][site_idx]['externalEpgs'][external_epg_idx]['subnets']
        elif not mso.existing:
            mso.fail_json(msg="Selector '{selector}' not found".format(selector=selector))
        mso.exit_json()

    mso.previous = mso.existing

    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=selector_path))

    elif state == 'present':
        # Get expressions
        types = dict(ip_address='ipAddress')
        all_expressions = []
        if expressions:
            for expression in expressions:
                type_val = expression.get('type')
                operator = expression.get('operator')
                value = expression.get('value')
                all_expressions.append(dict(
                    key=types.get(type_val),
                    operator=operator,
                    value=value,
                ))
        else:
            mso.fail_json(msg="Missing expressions in selector")

        subnets = dict(
            name=selector,
            ip=all_expressions[0]['value']
        )

        if not external_epgs:
            payload['subnets'] = [subnets]
        else:
            payload = subnets
            op_path = selectors_path

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=selector_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=op_path, value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode and mso.proposed != mso.previous:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 6
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        schema=dict(type='str', required=True),
        template=dict(type='str', required=True),
        bd=dict(type='str', required=True),
        dhcp_policy=dict(type='str', aliases=['name']),
        version=dict(type='int'),
        dhcp_option_policy=dict(type='dict', options=mso_dhcp_option_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', ['dhcp_policy']],
            ['state', 'present', ['dhcp_policy', 'version']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    bd = module.params.get('bd')
    dhcp_policy = module.params.get('dhcp_policy')
    dhcp_option_policy = module.params.get('dhcp_option_policy')
    version = module.params.get('version')
    state = module.params.get('state')

    mso = MSOModule(module)

    # 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 BD
    bds = [b.get('name') for b in schema_obj.get('templates')[template_idx]['bds']]
    if bd 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)

    # Check if DHCP policy already exists
    if dhcp_policy:
        check_policy = mso.get_obj("policies/dhcp/relay", name=dhcp_policy, key="DhcpRelayPolicies")
        if check_policy:
            pass
        else:
            mso.fail_json(msg="DHCP policy '{dhcp_policy}' does not exist".format(dhcp_policy=dhcp_policy))

    # Check if DHCP option policy already exists
    if dhcp_option_policy:
        check_option_policy = mso.get_obj("policies/dhcp/option", name=dhcp_option_policy.get('name'), key="DhcpRelayPolicies")
        if check_option_policy:
            pass
        else:
            mso.fail_json(msg="DHCP option policy '{dhcp_option_policy}' does not exist".format(dhcp_option_policy=dhcp_option_policy.get('name')))

    # Get DHCP policies
    dhcp_policies = [s.get('name') for s in schema_obj.get('templates')[template_idx]['bds'][bd_idx]['dhcpLabels']]
    if dhcp_policy in dhcp_policies:
        dhcp_idx = dhcp_policies.index(dhcp_policy)
        # FIXME: Changes based on index are DANGEROUS
        dhcp_policy_path = '/templates/{0}/bds/{1}/dhcpLabels/{2}'.format(template, bd, dhcp_idx)
        mso.existing = schema_obj.get('templates')[template_idx]['bds'][bd_idx]['dhcpLabels'][dhcp_idx]

    if state == 'query':
        if dhcp_policy is None:
            mso.existing = schema_obj.get('templates')[template_idx]['bds'][bd_idx]['dhcpLabels']
        elif not mso.existing:
            mso.fail_json(msg="DHCP policy not associated with the bd")
        mso.exit_json()

    dhcp_policy_paths = '/templates/{0}/bds/{1}/dhcpLabels'.format(template, bd)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=dhcp_policy_path))

    elif state == 'present':
        payload = dict(
            name=dhcp_policy,
            version=version,
            dhcpOptionLabel=dhcp_option_policy,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=dhcp_policy_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=dhcp_policy_paths + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 7
0
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),
        vrf=dict(type='str', required=True),
        region=dict(type='str', required=True),
        cidr=dict(type='str', required=True),
        subnet=dict(type='str', aliases=['ip']),  # This parameter is not required for querying all objects
        zone=dict(type='str', aliases=['name']),
        vgw=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', ['subnet']],
            ['state', 'present', ['subnet']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template')
    vrf = module.params.get('vrf')
    region = module.params.get('region')
    cidr = module.params.get('cidr')
    subnet = module.params.get('subnet')
    zone = module.params.get('zone')
    vgw = module.params.get('vgw')
    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 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
    sites = [(s.get('siteId'), s.get('templateName')) for s in schema_obj.get('sites')]
    sites_list = [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/siteId/template '{0}/{1}/{2}' does not exist. "
                          "Existing siteIds/templates: {3}".format(site, site_id, template, ', '.join(sites_list)))

    # 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 VRF
    vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf)
    vrfs = [v.get('vrfRef') for v in schema_obj.get('sites')[site_idx]['vrfs']]

    # If vrf not at site level but exists at template level
    if vrf_ref not in vrfs:
        mso.fail_json(msg="Provided vrf '{0}' does not exist at site level."
                          " Use mso_schema_site_vrf_region_cidr to create it.".format(vrf))
    vrf_idx = vrfs.index(vrf_ref)

    # Get Region
    regions = [r.get('name') for r in schema_obj.get('sites')[site_idx]['vrfs'][vrf_idx]['regions']]
    if region not in regions:
        mso.fail_json(msg="Provided region '{0}' does not exist. Existing regions: {1}."
                          " Use mso_schema_site_vrf_region_cidr to create it.".format(region, ', '.join(regions)))
    region_idx = regions.index(region)

    # Get CIDR
    cidrs = [c.get('ip') for c in schema_obj.get('sites')[site_idx]['vrfs'][vrf_idx]['regions'][region_idx]['cidrs']]
    if cidr not in cidrs:
        mso.fail_json(msg="Provided CIDR IP '{0}' does not exist. Existing CIDR IPs: {1}."
                          " Use mso_schema_site_vrf_region_cidr to create it.".format(cidr, ', '.join(cidrs)))
    cidr_idx = cidrs.index(cidr)

    # Get Subnet
    subnets = [s.get('ip') for s in schema_obj.get('sites')[site_idx]['vrfs'][vrf_idx]['regions'][region_idx]['cidrs'][cidr_idx]['subnets']]
    if subnet is not None and subnet in subnets:
        subnet_idx = subnets.index(subnet)
        # FIXME: Changes based on index are DANGEROUS
        subnet_path = '/sites/{0}/vrfs/{1}/regions/{2}/cidrs/{3}/subnets/{4}'.format(site_template, vrf, region, cidr_idx, subnet_idx)
        mso.existing = schema_obj.get('sites')[site_idx]['vrfs'][vrf_idx]['regions'][region_idx]['cidrs'][cidr_idx]['subnets'][subnet_idx]

    if state == 'query':
        if subnet is None:
            mso.existing = schema_obj.get('sites')[site_idx]['vrfs'][vrf_idx]['regions'][region_idx]['cidrs'][cidr_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}/vrfs/{1}/regions/{2}/cidrs/{3}/subnets'.format(site_template, vrf, region, cidr_idx)
    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':
        payload = dict(
            ip=subnet,
            zone=""
        )

        if zone is not None:
            payload['zone'] = zone
        elif vgw is True:
            payload['usage'] = 'gateway'

        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),
        anp=dict(type='str', required=True),
        epg=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', ['epg']],
            ['state', 'present', ['epg']],
        ],
    )

    schema = module.params['schema']
    site = module.params['site']
    template = module.params['template']
    anp = module.params['anp']
    epg = module.params['epg']
    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)
    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 ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a['anpRef'] for a in schema_obj['sites'][site_idx]['anps']]
    if anp_ref 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_ref)

    # Get EPG
    epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
    epgs = [e['epgRef'] for e in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']]
    if epg is not None and epg_ref in epgs:
        epg_idx = epgs.index(epg_ref)
        epg_path = '/sites/{0}/anps/{1}/epgs/{2}'.format(site_template, anp, epg)
        mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]

    if state == 'query':
        if epg is None:
            mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']
        elif not mso.existing:
            mso.fail_json(msg="EPG '{epg}' not found".format(epg=epg))
        mso.exit_json()

    epgs_path = '/sites/{0}/anps/{1}/epgs'.format(site_template, anp)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=epg_path))

    elif state == 'present':

        payload = dict(
            epgRef=dict(
                schemaId=schema_id,
                templateName=template,
                anpName=anp,
                epgName=epg,
            ),
        )

        mso.sanitize(payload, collate=True)

        if not mso.existing:
            ops.append(dict(op='add', path=epgs_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),
        anp=dict(type='str', required=True),
        epg=dict(type='str', required=True),
        pod=dict(type='str'
                 ),  # This parameter is not required for querying all objects
        leaf=dict(type='str', aliases=['name']),
        vlan=dict(type='int'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['pod', 'leaf', 'vlan']],
            ['state', 'present', ['pod', 'leaf', 'vlan']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    pod = module.params.get('pod')
    leaf = module.params.get('leaf')
    vlan = module.params.get('vlan')
    state = module.params.get('state')

    leafpath = 'topology/{0}/node-{1}'.format(pod, leaf)

    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 ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a.get('anpRef') for a in schema_obj.get('sites')[site_idx]['anps']]
    if anp_ref 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_ref)

    # Get EPG
    epg_ref = mso.epg_ref(schema_id=schema_id,
                          template=template,
                          anp=anp,
                          epg=epg)
    epgs = [
        e.get('epgRef')
        for e in schema_obj.get('sites')[site_idx]['anps'][anp_idx]['epgs']
    ]
    if epg_ref not in epgs:
        mso.fail_json(
            msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(
                epg, ', '.join(epgs)))
    epg_idx = epgs.index(epg_ref)

    # Get Leaf
    leafs = [(leaf.get('path'), leaf.get('portEncapVlan'))
             for leaf in schema_obj.get('sites')[site_idx]['anps'][anp_idx]
             ['epgs'][epg_idx]['staticLeafs']]
    if (leafpath, vlan) in leafs:
        leaf_idx = leafs.index((leafpath, vlan))
        # FIXME: Changes based on index are DANGEROUS
        leaf_path = '/sites/{0}/anps/{1}/epgs/{2}/staticLeafs/{3}'.format(
            site_template, anp, epg, leaf_idx)
        mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx][
            'epgs'][epg_idx]['staticLeafs'][leaf_idx]

    if state == 'query':
        if leaf is None or vlan is None:
            mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx][
                'epgs'][epg_idx]['staticLeafs']
        elif not mso.existing:
            mso.fail_json(msg="Static leaf '{leaf}/{vlan}' not found".format(
                leaf=leaf, vlan=vlan))
        mso.exit_json()

    leafs_path = '/sites/{0}/anps/{1}/epgs/{2}/staticLeafs'.format(
        site_template, anp, epg)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=leaf_path))

    elif state == 'present':
        payload = dict(
            path=leafpath,
            portEncapVlan=vlan,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=leaf_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=leafs_path + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 10
0
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', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        display_name=dict(type='str'),
        layer3_multicast=dict(type='bool'),
        vzany=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', ['vrf']],
            ['state', 'present', ['vrf']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template')
    vrf = module.params.get('vrf')
    display_name = module.params.get('display_name')
    layer3_multicast = module.params.get('layer3_multicast')
    vzany = module.params.get('vzany')
    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)

    # 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
    vrfs = [
        v.get('name')
        for v in schema_obj.get('templates')[template_idx]['vrfs']
    ]

    if vrf is not None and vrf in vrfs:
        vrf_idx = vrfs.index(vrf)
        mso.existing = schema_obj.get(
            'templates')[template_idx]['vrfs'][vrf_idx]

    if state == 'query':
        if vrf is None:
            mso.existing = schema_obj.get('templates')[template_idx]['vrfs']
        elif not mso.existing:
            mso.fail_json(msg="VRF '{vrf}' not found".format(vrf=vrf))
        mso.exit_json()

    vrfs_path = '/templates/{0}/vrfs'.format(template)
    vrf_path = '/templates/{0}/vrfs/{1}'.format(template, vrf)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=vrf_path))

    elif state == 'present':
        if display_name is None and not mso.existing:
            display_name = vrf

        payload = dict(name=vrf,
                       displayName=display_name,
                       l3MCast=layer3_multicast,
                       vzAnyEnabled=vzany)

        mso.sanitize(payload, collate=True)

        if mso.existing:
            # clean contractRef to fix api issue
            for contract in mso.sent.get('vzAnyConsumerContracts'):
                contract['contractRef'] = mso.dict_from_ref(
                    contract.get('contractRef'))
            for contract in mso.sent.get('vzAnyProviderContracts'):
                contract['contractRef'] = mso.dict_from_ref(
                    contract.get('contractRef'))
            ops.append(dict(op='replace', path=vrf_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=vrfs_path + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 11
0
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', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        display_name=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', ['anp']],
            ['state', 'present', ['anp']],
        ],
    )

    schema = module.params['schema']
    template = module.params['template']
    anp = module.params['anp']
    display_name = module.params['display_name']
    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)

    # 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 is not None and anp in anps:
        anp_idx = anps.index(anp)
        mso.existing = schema_obj['templates'][template_idx]['anps'][anp_idx]

    if state == 'query':
        if anp is None:
            mso.existing = schema_obj['templates'][template_idx]['anps']
        elif not mso.existing:
            mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp))
        mso.exit_json()

    anps_path = '/templates/{0}/anps'.format(template)
    anp_path = '/templates/{0}/anps/{1}'.format(template, anp)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=anp_path))

    elif state == 'present':

        if display_name is None and not mso.existing:
            display_name = anp

        epgs = []
        if mso.existing:
            epgs = None

        payload = dict(
            name=anp,
            displayName=display_name,
            epgs=epgs,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if display_name is not None:
                ops.append(
                    dict(op='replace',
                         path=anp_path + '/displayName',
                         value=display_name))
        else:
            ops.append(dict(op='add', path=anps_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(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',
                   default='private',
                   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

        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()
Ejemplo n.º 13
0
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', 'inactive']),
        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.get('user')
    user_password = module.params.get('user_password')
    first_name = module.params.get('first_name')
    last_name = module.params.get('last_name')
    email = module.params.get('email')
    phone = module.params.get('phone')
    account_status = module.params.get('account_status')
    state = module.params.get('state')

    mso = MSOModule(module)

    roles = mso.lookup_roles(module.params.get('roles'))
    domain = mso.lookup_domain(module.params.get('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.get('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,
            firstName=first_name,
            lastName=last_name,
            emailAddress=email,
            phoneNumber=phone,
            accountStatus=account_status,
            domainId=domain,
            roles=roles,
            # active=True,
            # remote=True,
        )

        if user_password is not None:
            payload.update(password=user_password)

        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 user_password is None:
                mso.fail_json(
                    "The user {0} does not exist. The 'user_password' attribute is required to create a new user."
                    .format(user_name))
            if module.check_mode:
                mso.existing = mso.proposed
            else:
                mso.existing = mso.request(path, method='POST', data=mso.sent)

    mso.exit_json()
Ejemplo n.º 14
0
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),
        selector=dict(type='str'),
        expressions=dict(type='list',
                         elements='dict',
                         options=mso_expression_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', ['selector']],
            ['state', 'present', ['selector']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    selector = module.params.get('selector')
    expressions = module.params.get('expressions')
    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)

    # Get template
    templates = [t.get('name') for t in schema_obj.get('templates')]
    if template not in templates:
        mso.fail_json(
            msg=
            "Provided template '{template}' does not exist. Existing templates: {templates}"
            .format(template=template, templates=', '.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 '{anp}' does not exist. Existing anps: {anps}".
            format(anp=anp, anps=', '.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 Selector
    if selector and " " in selector:
        mso.fail_json(msg="There should not be any space in selector name.")
    selectors = [
        s.get('name') for s in schema_obj.get('templates')[template_idx]
        ['anps'][anp_idx]['epgs'][epg_idx]['selectors']
    ]
    if selector in selectors:
        selector_idx = selectors.index(selector)
        selector_path = '/templates/{0}/anps/{1}/epgs/{2}/selectors/{3}'.format(
            template, anp, epg, selector_idx)
        mso.existing = schema_obj.get('templates')[template_idx]['anps'][
            anp_idx]['epgs'][epg_idx]['selectors'][selector_idx]

    if state == 'query':
        if selector is None:
            mso.existing = schema_obj.get('templates')[template_idx]['anps'][
                anp_idx]['epgs'][epg_idx]['selectors']
        elif not mso.existing:
            mso.fail_json(msg="Selector '{selector}' not found".format(
                selector=selector))
        mso.exit_json()

    selectors_path = '/templates/{0}/anps/{1}/epgs/{2}/selectors/-'.format(
        template, anp, epg)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        mso.sent = mso.existing = {}
        ops.append(dict(op='remove', path=selector_path))

    elif state == 'present':
        # Get expressions
        all_expressions = []
        if expressions:
            for expression in expressions:
                tag = expression.get('type')
                operator = expression.get('operator')
                value = expression.get('value')
                if " " in tag:
                    mso.fail_json(
                        msg=
                        "There should not be any space in 'type' attribute of expression '{0}'"
                        .format(tag))
                if operator in ["has_key", "does_not_have_key"] and value:
                    mso.fail_json(
                        msg=
                        "Attribute 'value' is not supported for operator '{0}' in expression '{1}'"
                        .format(operator, tag))
                if operator in ["not_in", "in", "equals", "not_equals"
                                ] and not value:
                    mso.fail_json(
                        msg=
                        "Attribute 'value' needed for operator '{0}' in expression '{1}'"
                        .format(operator, tag))
                all_expressions.append(
                    dict(
                        key='Custom:' + tag,
                        operator=EXPRESSION_KEYS.get(operator),
                        value=value,
                    ))

        payload = dict(
            name=selector,
            expressions=all_expressions,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=selector_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=selectors_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()
Ejemplo n.º 15
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        dhcp_relay_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_relay_policy']],
            ['state', 'present', ['dhcp_relay_policy', 'tenant']],
        ],
    )

    dhcp_relay_policy = module.params.get("dhcp_relay_policy")
    description = module.params.get("description")
    tenant = module.params.get("tenant")
    state = module.params.get("state")

    mso = MSOModule(module)

    path = "policies/dhcp/relay"

    # Query for existing object(s)
    if dhcp_relay_policy:
        mso.existing = mso.get_obj(path,
                                   name=dhcp_relay_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_relay_policy,
            desc=description,
            policyType="dhcp",
            policySubtype="relay",
            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()
Ejemplo n.º 16
0
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',
                              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',
                               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(
        schema=dict(type='str', required=True),
        site=dict(type='str', required=True),
        template=dict(type='str', required=True),
        anp=dict(type='str', required=True),
        epg=dict(type='str', required=True),
        selector=dict(type='str'),
        expressions=dict(type='list',
                         elements='dict',
                         options=mso_expression_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', ['selector']],
            ['state', 'present', ['selector']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    selector = module.params.get('selector')
    expressions = module.params.get('expressions')
    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 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 cloud type
    site_type = mso.get_obj('sites', name=site).get("cloudProviders")[0]

    # 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)

    payload = dict()
    ops = []
    op_path = ''

    # Get ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a.get('anpRef') for a in schema_obj['sites'][site_idx]['anps']]
    anps_in_temp = [
        a.get('name') for a in schema_obj['templates'][template_idx]['anps']
    ]
    if anp not in anps_in_temp:
        mso.fail_json(
            msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(
                anp, ', '.join(anps_in_temp)))
    else:
        # Get anp index at template level
        template_anp_idx = anps_in_temp.index(anp)

    # If anp not at site level but exists at template level
    if anp_ref not in anps:
        op_path = '/sites/{0}/anps/-'.format(site_template)
        payload.update(anpRef=dict(
            schemaId=schema_id,
            templateName=template,
            anpName=anp,
        ), )

    else:
        # Get anp index at site level
        anp_idx = anps.index(anp_ref)

    # Get EPG
    epg_ref = mso.epg_ref(schema_id=schema_id,
                          template=template,
                          anp=anp,
                          epg=epg)

    # If anp exists at site level
    if 'anpRef' not in payload:
        epgs = [
            e.get('epgRef')
            for e in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']
        ]

    # If anp already at site level AND if epg not at site level (or) anp not at site level?
    if ('anpRef' not in payload
            and epg_ref not in epgs) or 'anpRef' in payload:
        epgs_in_temp = [
            e.get('name') for e in schema_obj['templates'][template_idx]
            ['anps'][template_anp_idx]['epgs']
        ]

        # If EPG not at template level - Fail
        if epg not in epgs_in_temp:
            mso.fail_json(
                msg="Provided EPG '{0}' does not exist. Existing EPGs: {1}".
                format(epg, ', '.join(epgs_in_temp)))

        # EPG at template level but not at site level. Create payload at site level for EPG
        else:

            new_epg = dict(epgRef=dict(
                schemaId=schema_id,
                templateName=template,
                anpName=anp,
                epgName=epg,
            ))

            # If anp not in payload then, anp already exists at site level. New payload will only have new EPG payload
            if 'anpRef' not in payload:
                op_path = '/sites/{0}/anps/{1}/epgs/-'.format(
                    site_template, anp)
                payload = new_epg
            else:
                # If anp in payload, anp exists at site level. Update payload with EPG payload
                payload['epgs'] = [new_epg]

    # Get index of EPG at site level
    else:
        epg_idx = epgs.index(epg_ref)

    # Get selectors
    # If anp at site level and epg is at site level
    if 'anpRef' not in payload and 'epgRef' not in payload:
        if selector and " " in selector:
            mso.fail_json(
                msg="There should not be any space in selector name.")
        selectors = [
            s.get('name') for s in schema_obj.get('sites')[site_idx]['anps']
            [anp_idx]['epgs'][epg_idx]['selectors']
        ]
        if selector in selectors:
            selector_idx = selectors.index(selector)
            selector_path = '/sites/{0}/anps/{1}/epgs/{2}/selectors/{3}'.format(
                site_template, anp, epg, selector_idx)
            mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx][
                'epgs'][epg_idx]['selectors'][selector_idx]

    if state == 'query':
        if 'anpRef' in payload:
            mso.fail_json(
                msg="Anp '{anp}' does not exist in site level.".format(
                    anp=anp))
        if 'epgRef' in payload:
            mso.fail_json(
                msg="Epg '{epg}' does not exist in site level.".format(
                    epg=epg))
        if selector is None:
            mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx][
                'epgs'][epg_idx]['selectors']
        elif not mso.existing:
            mso.fail_json(msg="Selector '{selector}' not found".format(
                selector=selector))
        mso.exit_json()

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=selector_path))
    elif state == 'present':
        # Get expressions
        all_expressions = []
        if expressions:
            for expression in expressions:
                type = expression.get('type')
                operator = expression.get('operator')
                value = expression.get('value')
                if " " in type:
                    mso.fail_json(
                        msg=
                        "There should not be any space in 'type' attribute of expression '{0}'"
                        .format(type))
                if operator in ["has_key", "does_not_have_key"] and value:
                    mso.fail_json(
                        msg=
                        "Attribute 'value' is not supported for operator '{0}' in expression '{1}'"
                        .format(operator, type))
                if operator in ["not_in", "in", "equals", "not_equals"
                                ] and not value:
                    mso.fail_json(
                        msg=
                        "Attribute 'value' needed for operator '{0}' in expression '{1}'"
                        .format(operator, type))
                if type in ["region", "zone", "ip_address"]:
                    if type == "zone" and site_type != "aws":
                        mso.fail_json(
                            msg="Type 'zone' is only supported for aws")
                    if operator in ["has_key", "does_not_have_key"]:
                        mso.fail_json(
                            msg=
                            "Operator '{0}' is not supported when expression type is '{1}'"
                            .format(operator, type))
                    type = EXPRESSION_KEYS.get(type)
                else:
                    type = 'Custom:' + type
                all_expressions.append(
                    dict(
                        key=type,
                        operator=EXPRESSION_OPERATORS.get(operator),
                        value=value,
                    ))
        new_selector = dict(
            name=selector,
            expressions=all_expressions,
        )

        selectors_path = '/sites/{0}/anps/{1}/epgs/{2}/selectors/-'.format(
            site_template, anp, epg)

        # if payload is empty, anp and epg already exist at site level
        if not payload:
            op_path = selectors_path
            payload = new_selector
        # if payload exist
        else:
            # if anp already exists at site level
            if 'anpRef' not in payload:
                payload['selectors'] = [new_selector]
            else:
                payload['epgs'][0]['selectors'] = [new_selector]

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=selector_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=op_path, value=mso.sent))

        mso.existing = new_selector

    if not module.check_mode and mso.existing != mso.previous:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 18
0
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),
        anp=dict(type='str', required=True),
        epg=dict(type='str', required=True),
        type=dict(type='str', default='port', choices=['port', 'vpc', 'dpc']),
        pod=dict(type='str'
                 ),  # This parameter is not required for querying all objects
        leaf=dict(type='str'
                  ),  # This parameter is not required for querying all objects
        fex=dict(type='str'
                 ),  # This parameter is not required for querying all objects
        path=dict(type='str'
                  ),  # This parameter is not required for querying all objects
        vlan=dict(type='int'
                  ),  # This parameter is not required for querying all objects
        primary_micro_segment_vlan=dict(
            type='int'
        ),  # This parameter is not required for querying all objects
        deployment_immediacy=dict(type='str',
                                  default='lazy',
                                  choices=['immediate', 'lazy']),
        mode=dict(type='str',
                  default='untagged',
                  choices=['native', 'regular', 'untagged']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['type', 'pod', 'leaf', 'path', 'vlan']],
            ['state', 'present', ['type', 'pod', 'leaf', 'path', 'vlan']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    path_type = module.params.get('type')
    pod = module.params.get('pod')
    leaf = module.params.get('leaf')
    fex = module.params.get('fex')
    path = module.params.get('path')
    vlan = module.params.get('vlan')
    primary_micro_segment_vlan = module.params.get(
        'primary_micro_segment_vlan')
    deployment_immediacy = module.params.get('deployment_immediacy')
    mode = module.params.get('mode')
    state = module.params.get('state')

    if path_type == 'port' and fex is not None:
        # Select port path for fex if fex param is used
        portpath = 'topology/{0}/paths-{1}/extpaths-{2}/pathep-[{3}]'.format(
            pod, leaf, fex, path)
    elif path_type == 'vpc':
        portpath = 'topology/{0}/protpaths-{1}/pathep-[{2}]'.format(
            pod, leaf, path)
    else:
        portpath = 'topology/{0}/paths-{1}/pathep-[{2}]'.format(
            pod, leaf, path)

    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')]
    sites_list = [
        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/siteId/template '{0}/{1}/{2}' does not exist. "
            "Existing siteIds/templates: {3}".format(site, site_id, template,
                                                     ', '.join(sites_list)))

    # 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 ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a.get('anpRef') for a in schema_obj['sites'][site_idx]['anps']]
    anps_in_temp = [
        a.get('name') for a in schema_obj['templates'][template_idx]['anps']
    ]
    if anp not in anps_in_temp:
        mso.fail_json(
            msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(
                anp, ', '.join(anps)))
    else:
        # Update anp index at template level
        template_anp_idx = anps_in_temp.index(anp)

    # If anp not at site level but exists at template level
    if anp_ref not in anps:
        op_path = '/sites/{0}/anps/-'.format(site_template)
        payload.update(anpRef=dict(
            schemaId=schema_id,
            templateName=template,
            anpName=anp,
        ), )

    else:
        # Update anp index at site level
        anp_idx = anps.index(anp_ref)

    # Get EPG
    epg_ref = mso.epg_ref(schema_id=schema_id,
                          template=template,
                          anp=anp,
                          epg=epg)

    # If anp exists at site level
    if 'anpRef' not in payload:
        epgs = [
            e.get('epgRef')
            for e in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']
        ]

    # If anp already at site level AND if epg not at site level (or) anp not at site level
    if ('anpRef' not in payload
            and epg_ref not in epgs) or 'anpRef' in payload:
        epgs_in_temp = [
            e.get('name') for e in schema_obj['templates'][template_idx]
            ['anps'][template_anp_idx]['epgs']
        ]

        # If EPG not at template level - Fail
        if epg not in epgs_in_temp:
            mso.fail_json(
                msg=
                "Provided EPG '{0}' does not exist. Existing EPGs: {1} epgref {2}"
                .format(epg, ', '.join(epgs_in_temp), epg_ref))

        # EPG at template level but not at site level. Create payload at site level for EPG
        else:

            new_epg = dict(epgRef=dict(
                schemaId=schema_id,
                templateName=template,
                anpName=anp,
                epgName=epg,
            ))

            # If anp not in payload then, anp already exists at site level. New payload will only have new EPG payload
            if 'anpRef' not in payload:
                op_path = '/sites/{0}/anps/{1}/epgs/-'.format(
                    site_template, anp)
                payload = new_epg
            else:
                # If anp in payload, anp exists at site level. Update payload with EPG payload
                payload['epgs'] = [new_epg]

    # Update index of EPG at site level
    else:
        epg_idx = epgs.index(epg_ref)

    # Get Leaf
    # If anp at site level and epg is at site level
    if 'anpRef' not in payload and 'epgRef' not in payload:
        portpaths = [
            p.get('path') for p in schema_obj.get('sites')[site_idx]['anps']
            [anp_idx]['epgs'][epg_idx]['staticPorts']
        ]
        if portpath in portpaths:
            portpath_idx = portpaths.index(portpath)
            port_path = '/sites/{0}/anps/{1}/epgs/{2}/staticPorts/{3}'.format(
                site_template, anp, epg, portpath_idx)
            mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx][
                'epgs'][epg_idx]['staticPorts'][portpath_idx]

    if state == 'query':
        if leaf is None or vlan is None:
            mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx][
                'epgs'][epg_idx]['staticPorts']
        elif not mso.existing:
            mso.fail_json(msg="Static port '{portpath}' not found".format(
                portpath=portpath))
        mso.exit_json()

    ports_path = '/sites/{0}/anps/{1}/epgs/{2}/staticPorts'.format(
        site_template, anp, epg)
    ops = []
    new_leaf = dict(
        deploymentImmediacy=deployment_immediacy,
        mode=mode,
        path=portpath,
        portEncapVlan=vlan,
        type=path_type,
        microSegVlan=primary_micro_segment_vlan,
    )

    # If payload is empty, anp and EPG already exist at site level
    if not payload:
        op_path = ports_path + '/-'
        payload = new_leaf

    # If payload exists
    else:
        # If anp already exists at site level
        if 'anpRef' not in payload:
            payload['staticPorts'] = [new_leaf]
        else:
            payload['epgs'][0]['staticPorts'] = [new_leaf]

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=port_path))

    elif 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=op_path, value=mso.sent))

        mso.existing = new_leaf

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 19
0
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()
Ejemplo n.º 20
0
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.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')

    mso = MSOModule(module)

    site_id = None
    path = 'sites'

    # Convert labels
    labels = mso.lookup_labels(module.params.get('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.get('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()
Ejemplo n.º 21
0
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()
Ejemplo n.º 22
0
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')
    vrf = module.params.get('vrf')
    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 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()
Ejemplo n.º 23
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        schema=dict(type='str', required=True),
        template=dict(type='str', required=True),
        filter=dict(type='str', required=True),
        filter_display_name=dict(type='str'),
        entry=dict(type='str', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        description=dict(type='str', aliases=['entry_description']),
        display_name=dict(type='str', aliases=['entry_display_name']),
        ethertype=dict(type='str',
                       choices=[
                           'arp', 'fcoe', 'ip', 'ipv4', 'ipv6', 'mac-security',
                           'mpls-unicast', 'trill', 'unspecified'
                       ]),
        ip_protocol=dict(type='str',
                         choices=[
                             'eigrp', 'egp', 'icmp', 'icmpv6', 'igmp', 'igp',
                             'l2tp', 'ospfigp', 'pim', 'tcp', 'udp',
                             'unspecified'
                         ]),
        tcp_session_rules=dict(type='list',
                               elements='str',
                               choices=[
                                   'acknowledgement', 'established', 'finish',
                                   'synchronize', 'reset', 'unspecified'
                               ]),
        source_from=dict(type='str'),
        source_to=dict(type='str'),
        destination_from=dict(type='str'),
        destination_to=dict(type='str'),
        arp_flag=dict(type='str', choices=['reply', 'request', 'unspecified']),
        stateful=dict(type='bool'),
        fragments_only=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', ['entry']],
            ['state', 'present', ['entry']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    filter_name = module.params.get('filter')
    filter_display_name = module.params.get('filter_display_name')
    entry = module.params.get('entry')
    display_name = module.params.get('display_name')
    description = module.params.get('description')
    ethertype = module.params.get('ethertype')
    ip_protocol = module.params.get('ip_protocol')
    tcp_session_rules = module.params.get('tcp_session_rules')
    source_from = module.params.get('source_from')
    source_to = module.params.get('source_to')
    destination_from = module.params.get('destination_from')
    destination_to = module.params.get('destination_to')
    arp_flag = module.params.get('arp_flag')
    stateful = module.params.get('stateful')
    fragments_only = module.params.get('fragments_only')
    state = module.params.get('state')

    mso = MSOModule(module)

    # 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 '{template}' does not exist. Existing templates: {templates}"
            .format(template=template, templates=', '.join(templates)))
    template_idx = templates.index(template)

    # Get filters
    mso.existing = {}
    filter_idx = None
    entry_idx = None
    filters = [
        f.get('name')
        for f in schema_obj.get('templates')[template_idx]['filters']
    ]
    if filter_name in filters:
        filter_idx = filters.index(filter_name)

        entries = [
            f.get('name') for f in schema_obj.get('templates')[template_idx]
            ['filters'][filter_idx]['entries']
        ]
        if entry in entries:
            entry_idx = entries.index(entry)
            mso.existing = schema_obj.get('templates')[template_idx][
                'filters'][filter_idx]['entries'][entry_idx]

    if state == 'query':
        if entry is None:
            if filter_idx is None:
                mso.fail_json(msg="Filter '{filter}' not found".format(
                    filter=filter_name))
            mso.existing = schema_obj.get(
                'templates')[template_idx]['filters'][filter_idx]['entries']
        elif not mso.existing:
            mso.fail_json(msg="Entry '{entry}' not found".format(entry=entry))
        mso.exit_json()

    filters_path = '/templates/{0}/filters'.format(template)
    filter_path = '/templates/{0}/filters/{1}'.format(template, filter_name)
    entries_path = '/templates/{0}/filters/{1}/entries'.format(
        template, filter_name)
    entry_path = '/templates/{0}/filters/{1}/entries/{2}'.format(
        template, filter_name, entry)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        mso.proposed = mso.sent = {}

        if filter_idx is None:
            # There was no filter to begin with
            pass
        elif entry_idx is None:
            # There was no entry to begin with
            pass
        elif len(entries) == 1:
            # There is only one entry, remove filter
            mso.existing = {}
            ops.append(dict(op='remove', path=filter_path))

        else:
            mso.existing = {}
            ops.append(dict(op='remove', path=entry_path))

    elif state == 'present':

        if not mso.existing:
            if display_name is None:
                display_name = entry
            if description is None:
                description = ''
            if ethertype is None:
                ethertype = 'unspecified'
            if ip_protocol is None:
                ip_protocol = 'unspecified'
            if tcp_session_rules is None:
                tcp_session_rules = ['unspecified']
            if source_from is None:
                source_from = 'unspecified'
            if source_to is None:
                source_to = 'unspecified'
            if destination_from is None:
                destination_from = 'unspecified'
            if destination_to is None:
                destination_to = 'unspecified'
            if arp_flag is None:
                arp_flag = 'unspecified'
            if stateful is None:
                stateful = False
            if fragments_only is None:
                fragments_only = False

        payload = dict(
            name=entry,
            displayName=display_name,
            description=description,
            etherType=ethertype,
            ipProtocol=ip_protocol,
            tcpSessionRules=tcp_session_rules,
            sourceFrom=source_from,
            sourceTo=source_to,
            destinationFrom=destination_from,
            destinationTo=destination_to,
            arpFlag=arp_flag,
            stateful=stateful,
            matchOnlyFragments=fragments_only,
        )

        mso.sanitize(payload, collate=True)

        if filter_idx is None:
            # Filter does not exist, so we have to create it
            if filter_display_name is None:
                filter_display_name = filter_name

            payload = dict(
                name=filter_name,
                displayName=filter_display_name,
                entries=[mso.sent],
            )

            ops.append(dict(op='add', path=filters_path + '/-', value=payload))

        elif entry_idx is None:
            # Entry does not exist, so we have to add it
            ops.append(dict(op='add', path=entries_path + '/-',
                            value=mso.sent))

        else:
            # Entry exists, we have to update it
            for (key, value) in mso.sent.items():
                ops.append(
                    dict(op='replace',
                         path=entry_path + '/' + key,
                         value=value))

        mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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),
        anp=dict(type='str', required=True),
        epg=dict(type='str', required=True),
        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', ['subnet']],
            ['state', 'present', ['subnet']],
        ],
    )

    schema = module.params.get('schema')
    site = module.params.get('site')
    template = module.params.get('template')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    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
    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 ANP
    anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
    anps = [a.get('anpRef') for a in schema_obj.get('sites')[site_idx]['anps']]
    if anp_ref 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_ref)

    # Get EPG
    epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
    epgs = [e.get('epgRef') for e in schema_obj.get('sites')[site_idx]['anps'][anp_idx]['epgs']]
    if epg_ref not in epgs:
        mso.fail_json(msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(epg, ', '.join(epgs)))
    epg_idx = epgs.index(epg_ref)

    # Get Subnet
    subnets = [s.get('ip') for s in schema_obj.get('sites')[site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets']]
    if subnet in subnets:
        subnet_idx = subnets.index(subnet)
        # FIXME: Changes based on index are DANGEROUS
        subnet_path = '/sites/{0}/anps/{1}/epgs/{2}/subnets/{3}'.format(site_template, anp, epg, subnet_idx)
        mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets'][subnet_idx]

    if state == 'query':
        if subnet is None:
            mso.existing = schema_obj.get('sites')[site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets']
        elif not mso.existing:
            mso.fail_json(msg="Subnet '{subnet}' not found".format(subnet=subnet))
        mso.exit_json()

    subnets_path = '/sites/{0}/anps/{1}/epgs/{2}/subnets'.format(site_template, anp, epg)
    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()
Ejemplo n.º 26
0
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),
        vrf=dict(type='str', required=True),
        region=dict(type='str', required=True),
        cidr=dict(type='str', aliases=[
            'ip'
        ]),  # This parameter is not required for querying all objects
        primary=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', ['cidr']],
            ['state', 'present', ['cidr']],
        ],
    )

    schema = module.params['schema']
    site = module.params['site']
    template = module.params['template']
    vrf = module.params['vrf']
    region = module.params['region']
    cidr = module.params['cidr']
    primary = module.params['primary']
    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 VRF
    vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf)
    vrfs = [v['vrfRef'] for v in schema_obj['sites'][site_idx]['vrfs']]
    if vrf_ref not in vrfs:
        mso.fail_json(
            msg="Provided vrf '{0}' does not exist. Existing vrfs: {1}".format(
                vrf, ', '.join(vrfs)))
    vrf_idx = vrfs.index(vrf_ref)

    # Get Region
    regions = [
        r['name']
        for r in schema_obj['sites'][site_idx]['vrfs'][vrf_idx]['regions']
    ]
    if region not in regions:
        mso.fail_json(
            msg="Provided region '{0}' does not exist. Existing regions: {1}".
            format(region, ', '.join(regions)))
    region_idx = regions.index(region)

    # Get CIDR
    cidrs = [
        c['ip'] for c in schema_obj['sites'][site_idx]['vrfs'][vrf_idx]
        ['regions'][region_idx]['cidrs']
    ]
    if cidr is not None and cidr in cidrs:
        cidr_idx = cidrs.index(cidr)
        # FIXME: Changes based on index are DANGEROUS
        cidr_path = '/sites/{0}/vrfs/{1}/regions/{2}/cidrs/{3}'.format(
            site_template, vrf, region, cidr_idx)
        mso.existing = schema_obj['sites'][site_idx]['vrfs'][vrf_idx][
            'regions'][region_idx]['cidrs'][cidr_idx]

    if state == 'query':
        if cidr is None:
            mso.existing = schema_obj['sites'][site_idx]['vrfs'][vrf_idx][
                'regions'][region_idx]['cidrs']
        elif not mso.existing:
            mso.fail_json(msg="CIDR IP '{cidr}' not found".format(cidr=cidr))
        mso.exit_json()

    cidrs_path = '/sites/{0}/vrfs/{1}/regions/{2}/cidrs'.format(
        site_template, vrf, region)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        if mso.existing:
            mso.sent = mso.existing = {}
            ops.append(dict(op='remove', path=cidr_path))

    elif state == 'present':
        if not mso.existing:
            if primary is None:
                primary = False

        payload = dict(
            ip=cidr,
            primary=primary,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=cidr_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=cidrs_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),
        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()
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', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        bd=dict(type='dict', options=mso_reference_spec()),
        vrf=dict(type='dict', options=mso_reference_spec()),
        display_name=dict(type='str'),
        useg_epg=dict(type='bool'),
        intra_epg_isolation=dict(type='str',
                                 choices=['enforced', 'unenforced']),
        intersite_multicast_source=dict(type='bool'),
        proxy_arp=dict(type='bool'),
        subnets=dict(type='list', elements='dict', options=mso_subnet_spec()),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        preferred_group=dict(type='bool'),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['epg']],
            ['state', 'present', ['epg']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    anp = module.params.get('anp')
    epg = module.params.get('epg')
    display_name = module.params.get('display_name')
    bd = module.params.get('bd')
    if bd is not None and bd.get('template') is not None:
        bd['template'] = bd.get('template').replace(' ', '')
    vrf = module.params.get('vrf')
    if vrf is not None and vrf.get('template') is not None:
        vrf['template'] = vrf.get('template').replace(' ', '')
    useg_epg = module.params.get('useg_epg')
    intra_epg_isolation = module.params.get('intra_epg_isolation')
    intersite_multicast_source = module.params.get(
        'intersite_multicast_source')
    proxy_arp = module.params.get('proxy_arp')
    subnets = module.params.get('subnets')
    state = module.params.get('state')
    preferred_group = module.params.get('preferred_group')

    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 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 is not None and epg in epgs:
        epg_idx = epgs.index(epg)
        mso.existing = schema_obj.get(
            'templates')[template_idx]['anps'][anp_idx]['epgs'][epg_idx]

    if state == 'query':
        if epg is None:
            mso.existing = schema_obj.get(
                'templates')[template_idx]['anps'][anp_idx]['epgs']
        elif not mso.existing:
            mso.fail_json(msg="EPG '{epg}' not found".format(epg=epg))

        if 'bdRef' in mso.existing:
            mso.existing['bdRef'] = mso.dict_from_ref(mso.existing['bdRef'])
        if 'vrfRef' in mso.existing:
            mso.existing['vrfRef'] = mso.dict_from_ref(mso.existing['vrfRef'])
        mso.exit_json()

    epgs_path = '/templates/{0}/anps/{1}/epgs'.format(template, anp)
    epg_path = '/templates/{0}/anps/{1}/epgs/{2}'.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=epg_path))

    elif state == 'present':
        bd_ref = mso.make_reference(bd, 'bd', schema_id, template)
        vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)
        mso.stdout = str(subnets)
        subnets = mso.make_subnets(subnets)

        if display_name is None and not mso.existing:
            display_name = epg

        payload = dict(
            name=epg,
            displayName=display_name,
            uSegEpg=useg_epg,
            intraEpg=intra_epg_isolation,
            mCastSource=intersite_multicast_source,
            proxyArp=proxy_arp,
            # FIXME: Missing functionality
            # uSegAttrs=[],
            subnets=subnets,
            bdRef=bd_ref,
            preferredGroup=preferred_group,
            vrfRef=vrf_ref,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            # Clean contractRef to fix api issue
            for contract in mso.sent.get('contractRelationships'):
                contract['contractRef'] = mso.dict_from_ref(
                    contract.get('contractRef'))
            ops.append(dict(op='replace', path=epg_path, value=mso.sent))
        else:
            ops.append(dict(op='add', path=epgs_path + '/-', value=mso.sent))

        mso.existing = mso.proposed

    if 'epgRef' in mso.previous:
        del mso.previous['epgRef']
    if 'bdRef' in mso.previous and mso.previous['bdRef'] != '':
        mso.previous['bdRef'] = mso.dict_from_ref(mso.previous['bdRef'])
    if 'vrfRef' in mso.previous and mso.previous['bdRef'] != '':
        mso.previous['vrfRef'] = mso.dict_from_ref(mso.previous['vrfRef'])

    if not module.check_mode and mso.proposed != mso.previous:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Ejemplo n.º 29
0
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')
    bd = module.params.get('bd')
    l3out = module.params.get('l3out')
    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 L3out
    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
        l3out_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]

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

    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()
Ejemplo n.º 30
0
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='/api/v1/migrate/schema/{0}/template/{1}'.format(schema_id, template), method='POST', data=payload)

    mso.exit_json()