def __init__(self, *args, **kwargs): super(API, self).__init__(*args, **kwargs) network_api.API()._register_callback( base_api._callback_reasons.post_add, self._create_network_router) network_api.API()._register_callback( base_api._callback_reasons.check_delete, self._check_delete_network) network_api.API()._register_callback( base_api._callback_reasons.pre_delete, self._delete_network_router)
def _create_network_router(self, context, network, subnet_id): public_network_id = network_api.API().get_public_network_id(context) client = clients.neutron(context) router = client.create_router( body={ "router": { "name": network["name"], "admin_state_up": True, "external_gateway_info": { "network_id": public_network_id }, } })["router"] client.add_interface_router(router["id"], {"subnet_id": subnet_id})
def _get_os_routes(self, context): client = clients.neutron(context) routers = client.list_routers(tenant_id=context.project_id)["routers"] routers = dict((r["id"], r) for r in routers) ports = client.list_ports( tenant_id=context.project_id, device_owner="network:router_interface")["ports"] ports = dict((p["network_id"], p) for p in ports) gateway_ports = client.list_ports( device_owner="network:router_gateway")["ports"] gateway_ports = dict((p["device_id"], p) for p in gateway_ports) routes = {} networks = network_api.API().get_items(context) for network in networks: # NOTE(ft): append local route network_id = network["id"] routes[network_id] = self._init_local_route(network) port = ports.get(network_id) if port is None: continue router = routers.get(port["device_id"]) if router is None: continue key_prefix = network_id + port["id"] # NOTE(ft): append internet route external_gateway_info = router.get("external_gateway_info") gateway_port = gateway_ports.get(router["id"]) if (external_gateway_info is not None and gateway_port is not None): key = key_prefix + ALL_IP_CIDR + gateway_port["id"] routes[key] = self._init_internet_route( network, port, gateway_port["id"], external_gateway_info) # NOTE(ft): append other routes for route in router["routes"]: destination = route["destination"] nexthop = route["nexthop"] key = key_prefix + destination + nexthop routes[key] = self._init_custom_route(network, port, destination, nexthop) return routes
def add_item(self, context, name, body, scope=None): if any(x["name"] == name for x in self._get_floating_ips(context, scope)): raise exception.InvalidInput( _("The resource '%s' already exists.") % name) public_network_id = network_api.API().get_public_network_id(context) operation_util.start_operation(context) floating_ip = clients.neutron(context).create_floatingip( {"floatingip": { "floating_network_id": public_network_id }}) floating_ip = self._prepare_floating_ip(clients.nova(context), floating_ip["floatingip"], scope) floating_ip["name"] = body["name"] if "description" in body: floating_ip["description"] = body["description"] floating_ip = self._add_db_item(context, floating_ip) return floating_ip
def _create_internet_route(self, context, network, body): client = clients.neutron(context) port, router = self._get_network_objects(client, network) public_network_id = network_api.API().get_public_network_id(context) external_gateway_info = {"network_id": public_network_id} router = client.add_gateway_router(router["id"], external_gateway_info)["router"] # TODO(alexey-mr): ?admin needed - router_gateway ports haven't tenant ports = client.list_ports(device_id=router["id"], device_owner="network:router_gateway") gateway_port = ports["ports"][0] route = self._add_gce_route(context, network, port, body, is_default=False, destination=gateway_port["id"], nexthop=ALL_IP_CIDR) route["network"] = network route["port"] = port route["external_gateway_info"] = external_gateway_info return route
def add_item(self, context, name, body, scope=None): routes, dummy = self._sync_routes(context) if name in routes: raise exception.InvalidInput( _("The resource '%s' already exists.") % name) # NOTE(ft): check network is plugged to router network_name = utils._extract_name_from_url(body["network"]) network = network_api.API().get_item(context, network_name) nexthop = body.get("nextHopGateway") if (nexthop is not None and (utils._extract_name_from_url(nexthop) == "default-internet-gateway") and # NOTE(ft): OS doesn't support IP mask for external gateway body.get("destRange") == ALL_IP_CIDR): operation_util.start_operation(context) return self._create_internet_route(context, network, body) nexthop = body.get("nextHopIp") if nexthop is not None: operation_util.start_operation(context) return self._create_custom_route(context, network, body) raise exception.InvalidInput(_("Unsupported route."))
def __init__(self, *args, **kwargs): super(API, self).__init__(*args, **kwargs) network_api.API()._register_callback( base_api._callback_reasons.pre_delete, self.delete_network_firewalls)
def _get_network_by_url(self, context, url): # NOTE(apavlov): Check existence of such network network_name = utils._extract_name_from_url(url) return network_api.API().get_item(context, network_name)
def __init__(self, *args, **kwargs): super(Controller, self).__init__(network_api.API(), *args, **kwargs)