The all_subnets field

- when used, reduces follow-up calls on list_ports.

RM#6295

original author: mpath
This commit is contained in:
jmeridth
2014-05-15 06:02:51 +00:00
parent a47126bed2
commit 4cf67cc748
3 changed files with 20 additions and 22 deletions

View File

@@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import netaddr
from neutron.common import exceptions from neutron.common import exceptions
from neutron.extensions import providernet as pnet from neutron.extensions import providernet as pnet
from neutron.openstack.common import importutils from neutron.openstack.common import importutils
@@ -33,7 +31,6 @@ from quark import plugin_views as v
from quark import utils from quark import utils
CONF = cfg.CONF CONF = cfg.CONF
DEFAULT_ROUTE = netaddr.IPNetwork("0.0.0.0/0")
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
STRATEGY = network_strategy.STRATEGY STRATEGY = network_strategy.STRATEGY
@@ -159,7 +156,7 @@ def get_network(context, id, fields=None):
scope=db_api.ONE) scope=db_api.ONE)
if not network: if not network:
raise exceptions.NetworkNotFound(net_id=id) raise exceptions.NetworkNotFound(net_id=id)
return v._make_network_dict(network) return v._make_network_dict(network, fields=fields)
def get_networks(context, filters=None, fields=None): def get_networks(context, filters=None, fields=None):
@@ -184,7 +181,7 @@ def get_networks(context, filters=None, fields=None):
LOG.info("get_networks for tenant %s with filters %s, fields %s" % LOG.info("get_networks for tenant %s with filters %s, fields %s" %
(context.tenant_id, filters, fields)) (context.tenant_id, filters, fields))
nets = db_api.network_find(context, join_subnets=True, **filters) or [] nets = db_api.network_find(context, join_subnets=True, **filters) or []
nets = [v._make_network_dict(net) for net in nets] nets = [v._make_network_dict(net, fields=fields) for net in nets]
return nets return nets

View File

@@ -33,7 +33,6 @@ from quark import plugin_views as v
from quark import utils from quark import utils
CONF = cfg.CONF CONF = cfg.CONF
DEFAULT_ROUTE = netaddr.IPNetwork("0.0.0.0/0")
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
STRATEGY = network_strategy.STRATEGY STRATEGY = network_strategy.STRATEGY
@@ -164,8 +163,7 @@ def create_subnet(context, subnet):
new_subnet["ip_policy"] = db_api.ip_policy_create(context, new_subnet["ip_policy"] = db_api.ip_policy_create(context,
exclude=cidrs) exclude=cidrs)
subnet_dict = v._make_subnet_dict(new_subnet, subnet_dict = v._make_subnet_dict(new_subnet)
default_route=routes.DEFAULT_ROUTE)
subnet_dict["gateway_ip"] = gateway_ip subnet_dict["gateway_ip"] = gateway_ip
notifier_api.notify(context, notifier_api.notify(context,
@@ -236,7 +234,7 @@ def update_subnet(context, id, subnet):
context, cidr=route["destination"], gateway=route["nexthop"])) context, cidr=route["destination"], gateway=route["nexthop"]))
subnet = db_api.subnet_update(context, subnet_db, **s) subnet = db_api.subnet_update(context, subnet_db, **s)
return v._make_subnet_dict(subnet, default_route=routes.DEFAULT_ROUTE) return v._make_subnet_dict(subnet)
def get_subnet(context, id, fields=None): def get_subnet(context, id, fields=None):
@@ -261,7 +259,7 @@ def get_subnet(context, id, fields=None):
net_id = STRATEGY.get_parent_network(net_id) net_id = STRATEGY.get_parent_network(net_id)
subnet["network_id"] = net_id subnet["network_id"] = net_id
return v._make_subnet_dict(subnet, default_route=routes.DEFAULT_ROUTE) return v._make_subnet_dict(subnet)
def get_subnets(context, filters=None, fields=None): def get_subnets(context, filters=None, fields=None):
@@ -287,8 +285,7 @@ def get_subnets(context, filters=None, fields=None):
(context.tenant_id, filters, fields)) (context.tenant_id, filters, fields))
subnets = db_api.subnet_find(context, join_dns=True, join_routes=True, subnets = db_api.subnet_find(context, join_dns=True, join_routes=True,
**filters) **filters)
return v._make_subnets_list(subnets, fields=fields, return v._make_subnets_list(subnets, fields=fields)
default_route=routes.DEFAULT_ROUTE)
def get_subnets_count(context, filters=None): def get_subnets_count(context, filters=None):

View File

@@ -42,6 +42,10 @@ quark_view_opts = [
CONF.register_opts(quark_view_opts, "QUARK") CONF.register_opts(quark_view_opts, "QUARK")
def _is_default_route(route):
return route.value == 0
def _make_network_dict(network, fields=None): def _make_network_dict(network, fields=None):
shared_net = STRATEGY.is_parent_network(network["id"]) shared_net = STRATEGY.is_parent_network(network["id"])
res = {"id": network["id"], res = {"id": network["id"],
@@ -50,11 +54,12 @@ def _make_network_dict(network, fields=None):
"admin_state_up": None, "admin_state_up": None,
"ipam_strategy": network.get("ipam_strategy"), "ipam_strategy": network.get("ipam_strategy"),
"status": "ACTIVE", "status": "ACTIVE",
"shared": shared_net, "shared": shared_net}
#TODO(mdietz): this is the expected return. Then the client if fields and "all_subnets" in fields:
# foolishly turns around and asks for the entire res["subnets"] = [_make_subnet_dict(s)
# subnet list anyway! Plz2fix for s in network.get("subnets", [])]
"subnets": [s["id"] for s in network.get("subnets", [])]} else:
res["subnets"] = [s["id"] for s in network.get("subnets", [])]
return res return res
@@ -80,7 +85,7 @@ def _pools_from_cidr(cidr):
return pools return pools
def _make_subnet_dict(subnet, default_route=None, fields=None): def _make_subnet_dict(subnet, fields=None):
dns_nameservers = [str(netaddr.IPAddress(dns["ip"])) dns_nameservers = [str(netaddr.IPAddress(dns["ip"]))
for dns in subnet.get("dns_nameservers")] for dns in subnet.get("dns_nameservers")]
net_id = STRATEGY.get_parent_network(subnet["network_id"]) net_id = STRATEGY.get_parent_network(subnet["network_id"])
@@ -116,7 +121,7 @@ def _make_subnet_dict(subnet, default_route=None, fields=None):
default_found = False default_found = False
for route in subnet["routes"]: for route in subnet["routes"]:
netroute = netaddr.IPNetwork(route["cidr"]) netroute = netaddr.IPNetwork(route["cidr"])
if netroute.value == default_route.value: if _is_default_route(netroute):
#NOTE(mdietz): This has the potential to find more than one default #NOTE(mdietz): This has the potential to find more than one default
# route. Quark normally won't allow you to create # route. Quark normally won't allow you to create
# more than one, but it's plausible one exists # more than one, but it's plausible one exists
@@ -201,11 +206,10 @@ def _make_ports_list(query, fields=None):
return ports return ports
def _make_subnets_list(query, default_route=None, fields=None): def _make_subnets_list(query, fields=None):
subnets = [] subnets = []
for subnet in query: for subnet in query:
subnet_dict = _make_subnet_dict(subnet, default_route=default_route, subnet_dict = _make_subnet_dict(subnet, fields=fields)
fields=fields)
subnets.append(subnet_dict) subnets.append(subnet_dict)
return subnets return subnets