Beispiel #1
0
def find_logical_router(client, name):
    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    routers = api_instance.list_logical_routers()._results

    for router in routers:
        if router._display_name == name:
            return router._id, router
    return None, None
Beispiel #2
0
def create_router_port(client,
                       name,
                       router_port_type,
                       logical_router,
                       logical_switch=None,
                       ip_address=None,
                       mask=None,
                       logical_router_port_id=None):

    logical_router_id, logical_router = find_logical_router(
        client, logical_router)

    lrp = LogicalRouterPort(resource_type='LogicalRouterUpLinkPort',
                            display_name=name,
                            logical_router_id=logical_router_id).to_dict()

    if router_port_type == 'uplink':
        lrp['resource_type'] = 'LogicalRouterUpLinkPort'
        lrp['edge_cluster_member_index'] = [0]
    elif router_port_type == 'downlink':
        lrp['resource_type'] = 'LogicalRouterDownLinkPort'
    elif router_port_type == 'linkt1':
        lrp['resource_type'] = 'LogicalRouterLinkPortOnTIER1'
        lrp['edge_cluster_member_index'] = [0]
    elif router_port_type == 'linkt0':
        lrp['resource_type'] = 'LogicalRouterLinkPortOnTIER0'

    if ip_address and mask:
        lrp['subnets'] = [
            IPSubnet(ip_addresses=[ip_address], prefix_length=int(mask))
        ]

    if logical_switch:
        logical_switch_id, logical_switch_params = find_logical_switch(
            client, logical_switch)
        switching_api = swagger_client.LogicalSwitchingApi(client)
        profiles = _switching_profiles()
        switch_port = switching_api.create_logical_port(
            LogicalPort(admin_state="UP",
                        logical_switch_id=logical_switch_id,
                        switching_profile_ids=profiles))
        lrp['linked_logical_switch_port_id'] = ResourceReference(
            target_id=switch_port.id)

    if logical_router_port_id:
        lrp['linked_logical_router_port_id'] = ResourceReference(
            target_id=logical_router_port_id)

    res = dict((k, v) for k, v in lrp.iteritems() if v is not None)

    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    return api_instance.create_logical_router_port(res)
Beispiel #3
0
def create_static_route(client, logical_router, network, next_hop):

    logical_router_id, logical_router = find_logical_router(
        client, logical_router)

    static_route = StaticRoute(network=network,
                               next_hops=[
                                   StaticRouteNextHop(
                                       administrative_distance=1,
                                       ip_address=next_hop)
                               ])

    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    return api_instance.add_static_route(logical_router_id=logical_router_id,
                                         static_route=static_route)
Beispiel #4
0
def create_router(client,
                  name,
                  router_type,
                  edge_cluster=None,
                  t0=None,
                  tag=None):

    if edge_cluster:
        edge_cluster_id, edge_cluster = find_edge_cluster(client, edge_cluster)
        router = LogicalRouter(display_name=name,
                               router_type=router_type,
                               edge_cluster_id=edge_cluster_id,
                               high_availability_mode='ACTIVE_STANDBY')
    else:
        router = LogicalRouter(display_name=name,
                               router_type=router_type,
                               high_availability_mode='ACTIVE_STANDBY')

    if router_type == 'TIER0':
        router.tags = parse_tags(tag)

    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    result = api_instance.create_logical_router(router)

    if router_type == 'TIER1':
        lrp_t0 = create_router_port(client, 'LinkedPort_' + name, 'linkt0', t0)
        lrp_t1 = create_router_port(client,
                                    'LinkedPort_' + t0,
                                    'linkt1',
                                    name,
                                    logical_router_port_id=lrp_t0.id)
        advertisement_config = api_instance.read_advertisement_config(
            logical_router_id=result.id)
        advertisement_config.advertise_nsx_connected_routes = True
        advertisement_config.enabled = True
        api_instance.update_advertisement_config(result.id,
                                                 advertisement_config)

    return result
Beispiel #5
0
def create_nat_rule(client, action, logical_router, original_ip,
                    translated_ip):

    logical_router_id, logical_router = find_logical_router(
        client, logical_router)

    if action == 'SNAT':
        nat_rule = NatRule(action=action,
                           enabled=True,
                           rule_priority=1024,
                           match_source_network=original_ip,
                           translated_network=translated_ip)
    else:
        nat_rule = NatRule(action=action,
                           enabled=True,
                           rule_priority=1024,
                           match_destination_network=original_ip,
                           translated_network=translated_ip)

    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    return api_instance.add_nat_rule(logical_router_id=logical_router_id,
                                     nat_rule=nat_rule)
Beispiel #6
0
def find_all_nat_rules(client, logical_router_id):
    api_instance = swagger_client.LogicalRoutingAndServicesApi(client)
    nat_rules = api_instance.list_nat_rules(logical_router_id)
    return nat_rules.results