feat: bootstrap infrastructure app with create_instance delete_instance

views
pull/1/head
Aravinth Manivannan 2022-06-24 20:34:11 +05:30
parent 04ec4037a9
commit ff8a21d9dc
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
7 changed files with 99 additions and 0 deletions

View File

3
infrastructure/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
infrastructure/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class InfrastructureConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "infrastructure"

View File

3
infrastructure/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

23
infrastructure/urls.py Normal file
View File

@ -0,0 +1,23 @@
# 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 django.contrib import admin
from django.urls import path, include
from .views import create_instance, delete_instance
urlpatterns = [
path("create/<str:instance_name>/", create_instance, name="infra.create"),
path("rm/<str:instance_name>/", delete_instance, name="infra.rm"),
]

64
infrastructure/views.py Normal file
View File

@ -0,0 +1,64 @@
# 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 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()