From ff8a21d9dc7c6b3bb2298e17d020017af3aa6aa4 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 24 Jun 2022 20:34:11 +0530 Subject: [PATCH] feat: bootstrap infrastructure app with create_instance delete_instance views --- infrastructure/__init__.py | 0 infrastructure/admin.py | 3 ++ infrastructure/apps.py | 6 +++ infrastructure/migrations/__init__.py | 0 infrastructure/models.py | 3 ++ infrastructure/urls.py | 23 ++++++++++ infrastructure/views.py | 64 +++++++++++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 infrastructure/__init__.py create mode 100644 infrastructure/admin.py create mode 100644 infrastructure/apps.py create mode 100644 infrastructure/migrations/__init__.py create mode 100644 infrastructure/models.py create mode 100644 infrastructure/urls.py create mode 100644 infrastructure/views.py diff --git a/infrastructure/__init__.py b/infrastructure/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infrastructure/admin.py b/infrastructure/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/infrastructure/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/infrastructure/apps.py b/infrastructure/apps.py new file mode 100644 index 0000000..039f2dd --- /dev/null +++ b/infrastructure/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class InfrastructureConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "infrastructure" diff --git a/infrastructure/migrations/__init__.py b/infrastructure/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infrastructure/models.py b/infrastructure/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/infrastructure/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/infrastructure/urls.py b/infrastructure/urls.py new file mode 100644 index 0000000..b1cd20a --- /dev/null +++ b/infrastructure/urls.py @@ -0,0 +1,23 @@ +# 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 django.contrib import admin +from django.urls import path, include + +from .views import create_instance, delete_instance + +urlpatterns = [ + path("create//", create_instance, name="infra.create"), + path("rm//", delete_instance, name="infra.rm"), +] diff --git a/infrastructure/views.py b/infrastructure/views.py new file mode 100644 index 0000000..d315f18 --- /dev/null +++ b/infrastructure/views.py @@ -0,0 +1,64 @@ +# 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 django.shortcuts import render, redirect, get_object_or_404 +from django.utils.http import urlencode +from django.contrib.auth import authenticate, login, logout +from django.contrib.auth import get_user_model +from django.contrib.auth.decorators import login_required +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_protect +from django.urls import reverse + +from accounts.decorators import confirm_access +from dash.models import Instance + +from .utils import Infra + + +def default_ctx(title: str, username: str): + """ + Default context for all dashboard pages + """ + return { + "title": title, + "username": username, + "open_instances": "open", + } + + +@login_required +def create_instance(request, instance_name: str): + """ + Dashboard homepage view + """ + instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user) + infra = Infra() + infra.add_vm(instance=instance) + # TODO: push isn't implemented yet + + return HttpResponse() + + +@login_required +@confirm_access +def delete_instance(request, instance_name: str): + """ + Dashboard homepage view + """ + instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user) + infra = Infra() + infra.remove_vm(instance=instance) + # TODO: push isn't implemented yet + return HttpResponse()