Commit 0cfffed4 by Szabolcs Gelencser

Refactor default network resource creations. Add Internet Access checkbox to plain VM creation,

parent c4d12b46
...@@ -582,6 +582,10 @@ DEFAULT_SUBNETPOOL_PREFIXES = ( ...@@ -582,6 +582,10 @@ DEFAULT_SUBNETPOOL_PREFIXES = (
) )
DEFAULT_SUBNETPOOL_PREFIX_LEN = 20 DEFAULT_SUBNETPOOL_PREFIX_LEN = 20
DEFAULT_PUBLIC_NETWORK_NAME = "public"
DEFAULT_PUBLIC_ROUTER_NAME_FOR_USER = "default_public"
DEFAULT_PUBLIC_ROUTED_NET_NAME_FOR_USER = "default_public_routed"
OPENSTACK_KEYSTONE_DEFAULT_DOMAIN="bme" OPENSTACK_KEYSTONE_DEFAULT_DOMAIN="bme"
OPENSTACK_KEYSTONE_URL="https://oscircle.guest.ik.bme.hu:5000" OPENSTACK_KEYSTONE_URL="https://oscircle.guest.ik.bme.hu:5000"
WEBSSO_ENABLED = True #TODO: it is always enabled, refactor openstack_auth WEBSSO_ENABLED = True #TODO: it is always enabled, refactor openstack_auth
......
...@@ -120,9 +120,7 @@ class VmFromPlainImageForm(forms.Form): ...@@ -120,9 +120,7 @@ class VmFromPlainImageForm(forms.Form):
'class': "form-control input-tags", 'class': "form-control input-tags",
})) }))
network = forms.ChoiceField([], widget=forms.Select(attrs={ internet_access = forms.BooleanField(initial=True, required=False)
'class': "form-control input-tags",
}))
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(VmFromPlainImageForm, self).__init__(*args, **kwargs) super(VmFromPlainImageForm, self).__init__(*args, **kwargs)
...@@ -145,10 +143,6 @@ class VmFromPlainImageForm(forms.Form): ...@@ -145,10 +143,6 @@ class VmFromPlainImageForm(forms.Form):
(flavor.id, '%s - %s CPUs, %s MB RAM' % (flavor.name, flavor.vcpus, flavor.ram)) for flavor in flavors (flavor.id, '%s - %s CPUs, %s MB RAM' % (flavor.name, flavor.vcpus, flavor.ram)) for flavor in flavors
) )
networks = openstack_api.neutron.network_list_for_tenant(request, request.user.tenant_id)
self.fields['network'].choices = (
(network.id, '%s' % (network.name)) for network in networks
)
class VmCustomizeForm(forms.Form): class VmCustomizeForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={ name = forms.CharField(widget=forms.TextInput(attrs={
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
{{ form.name|as_crispy_field }} {{ form.name|as_crispy_field }}
{{ form.image|as_crispy_field }} {{ form.image|as_crispy_field }}
{{ form.flavor|as_crispy_field }} {{ form.flavor|as_crispy_field }}
{# {{ form.network|as_crispy_field }}#} {{ form.internet_access|as_crispy_field }}
<button class="btn btn-success btn-xs vm-create-start pull-right text-right" type="submit"> <button class="btn btn-success btn-xs vm-create-start pull-right text-right" type="submit">
<i class="fa fa-play"></i> {% trans "Start" %} <i class="fa fa-play"></i> {% trans "Start" %}
</button> </button>
......
...@@ -48,6 +48,7 @@ from common.models import ( ...@@ -48,6 +48,7 @@ from common.models import (
) )
from firewall.models import Vlan, Host, Rule from firewall.models import Vlan, Host, Rule
from manager.scheduler import SchedulerError from manager.scheduler import SchedulerError
from network.models import DefaultPublicRouter, DefaultPublicRoutedNet
from openstack_api.nova import Server from openstack_api.nova import Server
from request.forms import TemplateRequestForm from request.forms import TemplateRequestForm
from request.models import TemplateAccessType from request.models import TemplateAccessType
...@@ -1049,18 +1050,25 @@ class VmPlainImageCreate(LoginRequiredMixin, TemplateView): ...@@ -1049,18 +1050,25 @@ class VmPlainImageCreate(LoginRequiredMixin, TemplateView):
return self.render_to_response(context) return self.render_to_response(context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
#TODO: if there is a network available use it, of there isn't use none server_created = None
# there could be some checkboxes for the user to decide if request.POST.get("internet_access"):
# could create default network, setup internet access default_public_routed_net_id = DefaultPublicRoutedNet.get_id(request)
server_created = openstack_api.nova.server_create( server_created = openstack_api.nova.server_create(
request, request,
request.POST.get("name"), request.POST.get("name"),
request.POST.get("image"), request.POST.get("image"),
request.POST.get("flavor"), request.POST.get("flavor"),
# nics=({ nics=({
# 'uuid': 'fc7ba14c-53d8-4810-9030-a0f00985c036' 'net-id': default_public_routed_net_id,
# }) },)
) )
else:
server_created = openstack_api.nova.server_create(
request,
request.POST.get("name"),
request.POST.get("image"),
request.POST.get("flavor"),
)
return HttpResponseRedirect("vm/%s#activity" % server_created.id) return HttpResponseRedirect("vm/%s#activity" % server_created.id)
......
...@@ -78,23 +78,101 @@ class Vxlan(models.Model): ...@@ -78,23 +78,101 @@ class Vxlan(models.Model):
class SubnetPool(object): class SubnetPool(object):
@classmethod @classmethod
def get(cls, request): def __get(cls, request):
subnet_pools = openstack_api.neutron.subnetpool_list(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] subnet_pools = [sp for sp in subnet_pools if sp.name == settings.DEFAULT_SUBNETPOOL_NAME_FOR_USER]
return subnet_pools[0] if len(subnet_pools) > 0 else None return subnet_pools[0] if len(subnet_pools) > 0 else None
@classmethod @classmethod
def create_if_not_exists(cls, sender, user, request, **kwargs): def __create_if_not_exists(cls, request):
if SubnetPool.get(request) is None: default_subnet_pool = SubnetPool.__get(request)
openstack_api.neutron.subnetpool_create( if default_subnet_pool is None:
return openstack_api.neutron.subnetpool_create(
request, request,
settings.DEFAULT_SUBNETPOOL_NAME_FOR_USER, settings.DEFAULT_SUBNETPOOL_NAME_FOR_USER,
settings.DEFAULT_SUBNETPOOL_PREFIXES, settings.DEFAULT_SUBNETPOOL_PREFIXES,
default_prefixlen=settings.DEFAULT_SUBNETPOOL_PREFIX_LEN default_prefixlen=settings.DEFAULT_SUBNETPOOL_PREFIX_LEN
) )
else:
return default_subnet_pool
@classmethod @classmethod
def get_id(cls, request): def get_id(cls, request):
return SubnetPool.get(request).id return SubnetPool.__create_if_not_exists(request).id
class DefaultPublicRouter(object):
@classmethod
def __get_default_public_network(cls, request):
nets = openstack_api.neutron.network_list(request)
nets = [n for n in nets if n.name == settings.DEFAULT_PUBLIC_NETWORK_NAME]
return nets[0] if len(nets) > 0 else None
user_logged_in.connect(SubnetPool.create_if_not_exists) @classmethod
def __get(cls, request):
routers = openstack_api.neutron.router_list(request)
routers = [rtr for rtr in routers if rtr.name == settings.DEFAULT_PUBLIC_ROUTER_NAME_FOR_USER]
return routers[0] if len(routers) > 0 else None
@classmethod
def create_if_not_exists(cls, request, subnet_id):
default_public_router = DefaultPublicRouter.__get(request)
if default_public_router is None:
default_public_router = openstack_api.neutron.router_create(
request,
name=settings.DEFAULT_PUBLIC_ROUTER_NAME_FOR_USER,
external_gateway_info={
"network_id": DefaultPublicRouter.__get_default_public_network(request).id
}
)
openstack_api.neutron.router_add_interface(
request,
default_public_router.id,
subnet_id=subnet_id
)
return default_public_router
class DefaultPublicRoutedNet(object):
@classmethod
def __connect_router(cls, request, default_routed_net):
DefaultPublicRouter.create_if_not_exists(request, default_routed_net.subnets[0].id)
@classmethod
def __create_network(cls, request):
network_created = openstack_api.neutron.network_create(
request,
name=settings.DEFAULT_PUBLIC_ROUTED_NET_NAME_FOR_USER
)
try:
# TODO: default ip version should read from SETTINGS
subnet_created = openstack_api.neutron.subnet_create(
request,
network_created.id,
ip_version=4,
subnetpool_id=SubnetPool.get_id(request)
)
network_created.subnets.append(subnet_created)
except:
openstack_api.neutron.network_delete(request, network_created.id)
# TODO: user friendly error handling
raise Exception("Could not create subnet for network, deleted network.")
return network_created
@classmethod
def __get(cls, request):
nets = openstack_api.neutron.network_list_for_tenant(request, request.user.project_id)
nets = [n for n in nets if n.name == settings.DEFAULT_PUBLIC_ROUTED_NET_NAME_FOR_USER]
return nets[0] if len(nets) > 0 else None
@classmethod
def __create_if_not_exists(cls, request):
default_routed_net = DefaultPublicRoutedNet.__get(request)
if default_routed_net is None:
default_routed_net = DefaultPublicRoutedNet.__create_network(request)
DefaultPublicRoutedNet.__connect_router(request, default_routed_net)
return default_routed_net
@classmethod
def get_id(cls, request):
return DefaultPublicRoutedNet.__create_if_not_exists(request).id
\ No newline at end of file
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