Commit 2059f27c authored by Christos Stavrakakis's avatar Christos Stavrakakis

cyclades: Add list of subnet IDs to network model

Extend Network model with 'subnet_ids' attribute. This attribute is a
list with the IDs of the subnet that belong to this network and is
stored in DB as a comma seperated list of integers.

This information is already contained in the Subnet model. However, we
also store it in the DB model to make GET /networks API call faster by
avoiding use of 'prefetch_related'.

This commit also extends 'create_subnet' function to update the
network's 'subnet_ids' field.

Refs #5109
parent dda1563d
# Copyright 2011-2013 GRNET S.A. All rights reserved.
# Copyright 2011-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -107,8 +107,6 @@ def list_networks(request, detail=True):
user_networks = Network.objects.filter(Q(userid=request.user_uniq) |
Q(public=True))\
.order_by('id')
if detail:
user_networks = user_networks.prefetch_related("subnets")
user_networks = api.utils.filter_modified_since(request,
objects=user_networks)
......@@ -193,12 +191,6 @@ def network_to_dict(network, detail=True):
d = {'id': str(network.id), 'name': network.name}
d['links'] = util.network_to_links(network.id)
if detail:
# Loop over subnets. Do not perform any extra query because of prefetch
# related!
subnet_ids = []
for subnet in network.subnets.all():
subnet_ids.append(subnet.id)
state = "SNF:DRAINED" if network.drained else network.state
d['user_id'] = network.userid
d['tenant_id'] = network.project
......@@ -209,7 +201,7 @@ def network_to_dict(network, detail=True):
d['public'] = network.public
d['router:external'] = network.external_router
d['admin_state_up'] = True
d['subnets'] = subnet_ids
d['subnets'] = network.subnet_ids
d['SNF:floating_ip_pool'] = network.floating_ip_pool
d['deleted'] = network.deleted
return d
......@@ -219,7 +211,7 @@ def network_to_dict(network, detail=True):
def reassign_network(request, network, args):
project = args.get("project")
if project is None:
raise faults.BadRequest("Missing 'project' attribute.")
raise api.faults.BadRequest("Missing 'project' attribute.")
networks.reassign(network, project)
return HttpResponse(status=200)
......
......@@ -506,6 +506,7 @@ class Network(models.Model):
external_router = models.BooleanField(default=False)
serial = models.ForeignKey(QuotaHolderSerial, related_name='network',
null=True, on_delete=models.SET_NULL)
subnet_ids = fields.SeparatedValuesField("Subnet IDs", null=True)
def __unicode__(self):
return "<Network: %s>" % str(self.id)
......
......@@ -83,7 +83,7 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None,
"""
try:
network = Network.objects.get(id=network_id)
network = Network.objects.select_for_update().get(id=network_id)
except Network.DoesNotExist:
raise api.faults.ItemNotFound("No network found with that id")
......@@ -121,6 +121,9 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None,
dhcp=dhcp, host_routes=host_routes,
dns_nameservers=dns_nameservers)
network.subnet_ids.append(sub.id)
network.save()
gateway_ip = ipaddr.IPAddress(gateway) if gateway else None
if allocation_pools is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment