Exemple #1
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', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        display_name=dict(type='str'),
        intersite_bum_traffic=dict(type='bool'),
        optimize_wan_bandwidth=dict(type='bool'),
        layer2_stretch=dict(type='bool'),
        layer2_unknown_unicast=dict(type='str', choices=['flood', 'proxy']),
        layer3_multicast=dict(type='bool'),
        vrf=dict(type='dict', options=mso_reference_spec()),
        dhcp_policy=dict(type='dict', options=mso_dhcp_spec()),
        subnets=dict(type='list', options=mso_subnet_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', ['bd']],
            ['state', 'present', ['bd', 'vrf']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template')
    bd = module.params.get('bd')
    display_name = module.params.get('display_name')
    intersite_bum_traffic = module.params.get('intersite_bum_traffic')
    optimize_wan_bandwidth = module.params.get('optimize_wan_bandwidth')
    layer2_stretch = module.params.get('layer2_stretch')
    layer2_unknown_unicast = module.params.get('layer2_unknown_unicast')
    layer3_multicast = module.params.get('layer3_multicast')
    vrf = module.params.get('vrf')
    dhcp_policy = module.params.get('dhcp_policy')
    subnets = module.params.get('subnets')
    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 BDs
    bds = [
        b.get('name') for b in schema_obj.get('templates')[template_idx]['bds']
    ]

    if bd is not None and bd in bds:
        bd_idx = bds.index(bd)
        mso.existing = schema_obj.get('templates')[template_idx]['bds'][bd_idx]

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

    bds_path = '/templates/{0}/bds'.format(template)
    bd_path = '/templates/{0}/bds/{1}'.format(template, bd)
    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':
        vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)
        subnets = mso.make_subnets(subnets)
        dhcp_label = mso.make_dhcp_label(dhcp_policy)

        if display_name is None and not mso.existing:
            display_name = bd
        if subnets is None and not mso.existing:
            subnets = []

        payload = dict(
            name=bd,
            displayName=display_name,
            intersiteBumTrafficAllow=intersite_bum_traffic,
            optimizeWanBandwidth=optimize_wan_bandwidth,
            l2UnknownUnicast=layer2_unknown_unicast,
            l2Stretch=layer2_stretch,
            l3MCast=layer3_multicast,
            subnets=subnets,
            vrfRef=vrf_ref,
            dhcpLabel=dhcp_label,
        )

        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 'bdRef' in mso.previous:
        del mso.previous['bdRef']
    if 'vrfRef' in mso.previous:
        mso.previous['vrfRef'] = mso.vrf_dict_from_ref(
            mso.previous.get('vrfRef'))

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

    mso.exit_json()
Exemple #2
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', aliases=[
            'name'
        ]),  # This parameter is not required for querying all objects
        display_name=dict(type='str'),
        description=dict(type='str'),
        intersite_bum_traffic=dict(type='bool'),
        optimize_wan_bandwidth=dict(type='bool'),
        layer2_stretch=dict(type='bool', default='true'),
        layer2_unknown_unicast=dict(type='str', choices=['flood', 'proxy']),
        layer3_multicast=dict(type='bool'),
        vrf=dict(type='dict', options=mso_reference_spec()),
        dhcp_policy=dict(type='dict', options=mso_dhcp_spec()),
        dhcp_policies=dict(type='list',
                           elements='dict',
                           options=mso_dhcp_spec()),
        subnets=dict(type='list',
                     elements='dict',
                     options=mso_bd_subnet_spec()),
        unknown_multicast_flooding=dict(
            type='str', choices=['optimized_flooding', 'flood']),
        multi_destination_flooding=dict(
            type='str', choices=['flood_in_bd', 'drop', 'encap-flood']),
        ipv6_unknown_multicast_flooding=dict(
            type='str', choices=['optimized_flooding', 'flood']),
        arp_flooding=dict(type='bool'),
        virtual_mac_address=dict(type='str'),
        unicast_routing=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', ['bd']],
            ['state', 'present', ['bd', 'vrf']],
        ],
    )

    schema = module.params.get('schema')
    template = module.params.get('template').replace(' ', '')
    bd = module.params.get('bd')
    display_name = module.params.get('display_name')
    description = module.params.get('description')
    intersite_bum_traffic = module.params.get('intersite_bum_traffic')
    optimize_wan_bandwidth = module.params.get('optimize_wan_bandwidth')
    layer2_stretch = module.params.get('layer2_stretch')
    layer2_unknown_unicast = module.params.get('layer2_unknown_unicast')
    layer3_multicast = module.params.get('layer3_multicast')
    vrf = module.params.get('vrf')
    if vrf is not None and vrf.get('template') is not None:
        vrf['template'] = vrf.get('template').replace(' ', '')
    dhcp_policy = module.params.get('dhcp_policy')
    dhcp_policies = module.params.get('dhcp_policies')
    subnets = module.params.get('subnets')
    unknown_multicast_flooding = module.params.get(
        'unknown_multicast_flooding')
    multi_destination_flooding = module.params.get(
        'multi_destination_flooding')
    ipv6_unknown_multicast_flooding = module.params.get(
        'ipv6_unknown_multicast_flooding')
    arp_flooding = module.params.get('arp_flooding')
    virtual_mac_address = module.params.get('virtual_mac_address')
    unicast_routing = module.params.get('unicast_routing')
    state = module.params.get('state')

    mso = MSOModule(module)

    # Map choices
    if unknown_multicast_flooding == 'optimized_flooding':
        unknown_multicast_flooding = 'opt-flood'
    if ipv6_unknown_multicast_flooding == 'optimized_flooding':
        ipv6_unknown_multicast_flooding = 'opt-flood'
    if multi_destination_flooding == 'flood_in_bd':
        multi_destination_flooding = 'bd-flood'

    if layer2_unknown_unicast == 'flood':
        arp_flooding = True

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

    if bd is not None and bd in bds:
        bd_idx = bds.index(bd)
        mso.existing = schema_obj.get('templates')[template_idx]['bds'][bd_idx]

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

    bds_path = '/templates/{0}/bds'.format(template)
    bd_path = '/templates/{0}/bds/{1}'.format(template, bd)
    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':
        vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)
        subnets = mso.make_subnets(subnets)
        dhcp_label = mso.make_dhcp_label(dhcp_policy)
        dhcp_labels = mso.make_dhcp_label(dhcp_policies)

        if display_name is None and not mso.existing:
            display_name = bd
        if subnets is None and not mso.existing:
            subnets = []

        payload = dict(
            name=bd,
            displayName=display_name,
            intersiteBumTrafficAllow=intersite_bum_traffic,
            optimizeWanBandwidth=optimize_wan_bandwidth,
            l2UnknownUnicast=layer2_unknown_unicast,
            l2Stretch=layer2_stretch,
            l3MCast=layer3_multicast,
            subnets=subnets,
            vrfRef=vrf_ref,
            dhcpLabel=dhcp_label,
            unkMcastAct=unknown_multicast_flooding,
            multiDstPktAct=multi_destination_flooding,
            v6unkMcastAct=ipv6_unknown_multicast_flooding,
            vmac=virtual_mac_address,
            arpFlood=arp_flooding,
        )

        if dhcp_labels:
            payload.update(dhcpLabels=dhcp_labels)

        if unicast_routing:
            payload.update(unicastRouting=unicast_routing)

        if description:
            payload.update(description=description)

        mso.sanitize(payload,
                     collate=True,
                     required=['dhcpLabel', 'dhcpLabels'])
        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 'bdRef' in mso.previous:
        del mso.previous['bdRef']
    if 'vrfRef' in mso.previous:
        mso.previous['vrfRef'] = mso.vrf_dict_from_ref(
            mso.previous.get('vrfRef'))

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

    mso.exit_json()