From 2dc1740aac777d4828acab2e816d7f27a1eedb0c Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 27 Jun 2022 20:43:02 +0530 Subject: [PATCH] chore: refactor instance creation view --- dash/utils.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ dash/views.py | 36 ++++++++++++++-------------------- 2 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 dash/utils.py diff --git a/dash/utils.py b/dash/utils.py new file mode 100644 index 0000000..a01c6cf --- /dev/null +++ b/dash/utils.py @@ -0,0 +1,54 @@ +# Copyright © 2022 Aravinth Manivannan +# +# 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 . +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 diff --git a/dash/views.py b/dash/views.py index 89ec237..c4e2520 100644 --- a/dash/views.py +++ b/dash/views.py @@ -21,10 +21,11 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_protect from django.urls import reverse -from .models import Instance, InstanceConfiguration - 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): """ @@ -73,32 +74,25 @@ def create_instance(request): return render(request, "dash/instances/new/index.html", context=ctx) 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"] - 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() + 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"] = { "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) - 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 def list_instances(request):