Commit 9e617a76 by Szabolcs Gelencser

add subnet allocation from subnet pool on network creation

parent 1616b560
......@@ -577,3 +577,4 @@ DEFAULT_SUBNETPOOL_NAME_FOR_USER = "default"
DEFAULT_SUBNETPOOL_PREFIXES = (
"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"
)
DEFAULT_SUBNETPOOL_PREFIX_LEN = 20
\ No newline at end of file
......@@ -80,14 +80,25 @@ class Vxlan(models.Model):
def get_absolute_url(self):
return reverse('network.vxlan', kwargs={'vni': self.vni})
def create_subnet_pool(sender, user, request, **kwargs):
class SubnetPool(object):
@classmethod
def get(cls, request):
subnet_pools = openstack_api.neutron.subnetpool_list(request)
subnet_pools = [sp for sp in subnet_pools if sp.name == settings.DEFAULT_SUBNETPOOL_NAME_FOR_USER]
if(len(subnet_pools) == 0):
return subnet_pools[0] if len(subnet_pools) > 0 else None
@classmethod
def create_if_not_exists(cls, sender, user, request, **kwargs):
if SubnetPool.get(request) is None:
openstack_api.neutron.subnetpool_create(
request,
settings.DEFAULT_SUBNETPOOL_NAME_FOR_USER,
settings.DEFAULT_SUBNETPOOL_PREFIXES,
default_prefixlen=settings.DEFAULT_SUBNETPOOL_PREFIX_LEN
)
user_logged_in.connect(create_subnet_pool)
\ No newline at end of file
@classmethod
def get_id(cls, request):
return SubnetPool.get(request).id
user_logged_in.connect(SubnetPool.create_if_not_exists)
......@@ -46,7 +46,7 @@ from firewall.models import (
SwitchPort, EthernetDevice, Firewall
)
from netaddr import IPNetwork
from network.models import Vxlan, EditorElement
from network.models import Vxlan, EditorElement, SubnetPool
from openstack_api.utils.lazy_encoder import LazyTranslationEncoder
from vm.models import Interface, Instance
......@@ -1002,8 +1002,20 @@ class VxlanCreate(LoginRequiredMixin, FormView):
if form.cleaned_data['isAdvancedConfig']:
pass
else:
openstack_api.neutron.subnet_create(self.request, network_created.id, ip_version=4)
try:
raise Exception
# TODO: default ip version should read from SETTINGS
openstack_api.neutron.subnet_create(
self.request,
network_created.id,
ip_version=4,
subnetpool_id=SubnetPool.get_id(self.request)
)
except:
openstack_api.neutron.network_delete(self.request, network_created.id)
#TODO: user friendly error handling
raise Exception("Could not create subnet for network, deleted network.")
return redirect(reverse_lazy('network.vxlan', kwargs={'pk': network_created.id}))
class VxlanDelete(LoginRequiredMixin, DeleteView): #TODO: check user
......
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 sign in to comment