chore: refactor instance creation view
parent
3318ca8da2
commit
2dc1740aac
|
@ -0,0 +1,54 @@
|
||||||
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as
|
||||||
|
# published by the Free Software Foundation, either version 3 of the
|
||||||
|
# License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
from enum import Enum, unique
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
from .models import Instance, InstanceConfiguration
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class VmErrors(Enum):
|
||||||
|
NAME_EXISTS = "Instance name exists, please try again with a different name"
|
||||||
|
NO_CONFIG = "Configuration doesn't exist, please try again."
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class VmException(Exception):
|
||||||
|
error: str
|
||||||
|
code: VmErrors
|
||||||
|
|
||||||
|
def __init__(self, code: VmErrors):
|
||||||
|
self.error = str(code)
|
||||||
|
self.code = code
|
||||||
|
|
||||||
|
|
||||||
|
def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance:
|
||||||
|
"""
|
||||||
|
Create instance view
|
||||||
|
"""
|
||||||
|
|
||||||
|
if Instance.objects.filter(name=vm_name).exists():
|
||||||
|
raise VmException(code=VmErrors.NAME_EXISTS)
|
||||||
|
|
||||||
|
if not InstanceConfiguration.objects.filter(name=configuration_name).exists():
|
||||||
|
raise VmException(code=VmErrors.NO_CONFIG)
|
||||||
|
|
||||||
|
configuration = InstanceConfiguration.objects.get(name=configuration_name)
|
||||||
|
instance = Instance(name=vm_name, configuration_id=configuration, owned_by=user)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
|
@ -21,10 +21,11 @@ from django.http import HttpResponse
|
||||||
from django.views.decorators.csrf import csrf_protect
|
from django.views.decorators.csrf import csrf_protect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from .models import Instance, InstanceConfiguration
|
|
||||||
|
|
||||||
from accounts.decorators import confirm_access
|
from accounts.decorators import confirm_access
|
||||||
|
|
||||||
|
from .models import Instance, InstanceConfiguration
|
||||||
|
from .utils import create_instance as create_instance_util, VmErrors, VmException
|
||||||
|
|
||||||
|
|
||||||
def default_ctx(title: str, username: str):
|
def default_ctx(title: str, username: str):
|
||||||
"""
|
"""
|
||||||
|
@ -73,32 +74,25 @@ def create_instance(request):
|
||||||
return render(request, "dash/instances/new/index.html", context=ctx)
|
return render(request, "dash/instances/new/index.html", context=ctx)
|
||||||
|
|
||||||
name = request.POST["name"]
|
name = request.POST["name"]
|
||||||
if Instance.objects.filter(name=name).exists():
|
|
||||||
ctx = get_ctx()
|
|
||||||
ctx["error"] = {
|
|
||||||
"title": "Can't create instance",
|
|
||||||
"reason": "Instance name exists, please try again with a different name",
|
|
||||||
}
|
|
||||||
print(ctx["error"]["reason"])
|
|
||||||
return render(request, "dash/instances/new/index.html", status=400, context=ctx)
|
|
||||||
|
|
||||||
configuration = request.POST["configuration"]
|
configuration = request.POST["configuration"]
|
||||||
if not InstanceConfiguration.objects.filter(name=configuration).exists():
|
try:
|
||||||
|
instance = create_instance_util(
|
||||||
|
vm_name=name, configuration_name=configuration, user=request.user
|
||||||
|
)
|
||||||
|
return redirect(reverse("billing.invoice.generate", args=(instance.name,)))
|
||||||
|
except VmException as e:
|
||||||
ctx = get_ctx()
|
ctx = get_ctx()
|
||||||
|
if e.code == VmErrors.NAME_EXISTS:
|
||||||
|
reason = ("Instance name exists, please try again with a different name",)
|
||||||
|
elif e.code == VmErrors.NO_CONFIG:
|
||||||
|
reason = "Configuration doesn't exist, please try again."
|
||||||
|
|
||||||
ctx["error"] = {
|
ctx["error"] = {
|
||||||
"title": "Can't create instance",
|
"title": "Can't create instance",
|
||||||
"reason": "Configuration doesn't exist, please try again.",
|
"reason": reason,
|
||||||
}
|
}
|
||||||
print(ctx["error"]["reason"])
|
|
||||||
return render(request, "dash/instances/new/index.html", status=400, context=ctx)
|
return render(request, "dash/instances/new/index.html", status=400, context=ctx)
|
||||||
|
|
||||||
configuration = get_object_or_404(InstanceConfiguration, name=configuration)
|
|
||||||
instance = Instance(
|
|
||||||
name=name, configuration_id=configuration, owned_by=request.user
|
|
||||||
)
|
|
||||||
instance.save()
|
|
||||||
return redirect(reverse("billing.invoice.generate", args=(instance.name,)))
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def list_instances(request):
|
def list_instances(request):
|
||||||
|
|
Loading…
Reference in New Issue