Commit 9e617a76 by Szabolcs Gelencser

add subnet allocation from subnet pool on network creation

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