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')
    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_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 '{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.º 2
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),
        l3out=dict(type='str'),
        external_epg=dict(type='str', aliases=['name']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    schema = module.params.get('schema')
    template = module.params.get('template')
    site = module.params.get('site')
    external_epg = module.params.get('external_epg')
    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 template
    templates = [t.get('name') for t in schema_obj.get('templates')]
    if template:
        if template in templates:
            template_idx = templates.index(template)
            path = 'tenants/{0}'.format(
                schema_obj.get('templates')[template_idx]['tenantId'])
            tenant_name = mso.request(path, method='GET').get('name')
    else:
        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')]
    if (site_id, template) not in sites:
        mso.fail_json(
            msg="Provided template '{0}' does not exist. Existing templates: {1}"
            .format(template, ', '.join(templates)))

    # 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 = '/sites/{0}/externalEpgs/-'.format(site_template)

    # 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 in external_epgs:
        external_epg_idx = external_epgs.index(ext_epg_ref)
        # Get External EPG
        mso.existing = schema_obj['sites'][site_idx]['externalEpgs'][
            external_epg_idx]
        op_path = '/sites/{0}/externalEpgs/{1}'.format(site_template,
                                                       external_epg)

    ops = []

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

    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':
        l3out_dn = 'uni/tn-{0}/out-{1}'.format(tenant_name, l3out)
        payload = dict(
            externalEpgRef=dict(
                schemaId=schema_id,
                templateName=template,
                externalEpgName=external_epg,
            ),
            l3outDn=l3out_dn,
            l3outRef=dict(
                schemaId=schema_id,
                templateName=template,
                l3outName=l3out,
            ),
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            ops.append(dict(op='replace', path=op_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()