feat(itim): Ability to add and configure cluster

ref: #244 #71
This commit is contained in:
2024-08-17 17:50:08 +09:30
parent efce9c0219
commit 30bd8aa483
6 changed files with 473 additions and 4 deletions

142
app/itim/forms/clusters.py Normal file
View File

@ -0,0 +1,142 @@
from django import forms
from django.forms import ValidationError
from django.urls import reverse
from itim.models.clusters import Cluster
from app import settings
from core.forms.common import CommonModelForm
class ClusterForm(CommonModelForm):
class Meta:
fields = '__all__'
model = Cluster
prefix = 'cluster'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['parent_cluster'].queryset = self.fields['parent_cluster'].queryset.exclude(
id=self.instance.pk
)
self.fields['devices'].queryset = self.fields['devices'].queryset.exclude(
is_virtual=False
)
def clean(self):
cleaned_data = super().clean()
pk = self.instance.id
parent_cluster = cleaned_data.get("parent_cluster")
if parent_cluster == pk:
raise ValidationError("Cluster can't have itself as its parent cluster")
return cleaned_data
class DetailForm(ClusterForm):
tabs: dict = {
"details": {
"name": "Details",
"slug": "details",
"sections": [
{
"layout": "double",
"left": [
'parent_cluster',
'cluster_type',
'name',
'organization',
'c_created',
'c_modified'
],
"right": [
'model_notes',
'resources',
]
},
]
},
"rendered_config": {
"name": "Rendered Config",
"slug": "rendered_config",
"sections": [
{
"layout": "single",
"fields": [
'config_variables',
],
"json": [
'config_variables'
]
}
]
},
"notes": {
"name": "Notes",
"slug": "notes",
"sections": []
}
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.fields['config_variables'] = forms.fields.JSONField(
# widget = forms.Textarea(
# attrs = {
# "cols": "80",
# "rows": "100"
# }
# ),
# label = 'Rendered Configuration',
# initial = self.instance.config_variables,
# )
self.fields['c_created'] = forms.DateTimeField(
label = 'Created',
input_formats=settings.DATETIME_FORMAT,
disabled = True,
initial = self.instance.created,
)
self.fields['c_modified'] = forms.DateTimeField(
label = 'Modified',
input_formats=settings.DATETIME_FORMAT,
disabled = True,
initial = self.instance.modified,
)
self.fields['resources'] = forms.CharField(
label = 'Available Resources',
disabled = True,
initial = 'xx/yy CPU, xx/yy RAM, xx/yy Storage',
)
self.tabs['details'].update({
"edit_url": reverse('ITIM:_cluster_change', args=(self.instance.pk,))
})
self.url_index_view = reverse('ITIM:Clusters')

View File

@ -75,10 +75,10 @@ class Cluster(TenancyObject):
ClusterType,
blank = True,
default = None,
help_text = 'Parent Cluster for this cluster',
help_text = 'Type of Cluster',
null = True,
on_delete = models.CASCADE,
verbose_name = 'Parent Cluster',
verbose_name = 'Cluster Type',
)
name = models.CharField(
@ -99,7 +99,7 @@ class Cluster(TenancyObject):
verbose_name = 'Configuration',
)
node = models.ManyToManyField(
nodes = models.ManyToManyField(
Device,
blank = True,
default = None,
@ -117,6 +117,10 @@ class Cluster(TenancyObject):
verbose_name = 'Devices',
)
created = AutoCreatedField()
modified = AutoLastModifiedField()
def __str__(self):

View File

@ -0,0 +1,79 @@
{% extends 'detail.html.j2' %}
{% load json %}
{% load markdown %}
{% block tabs %}
<div id="details" class="content-tab">
{% include 'content/section.html.j2' with tab=form.tabs.details %}
<hr />
<div style="display: block; width: 100%;">
<h3>Nodes</h3>
<table>
<tr>
<th>Name</th>
<th>Organization</th>
</tr>
{% if item.nodes.all %}
{% for node in item.nodes.all %}
<tr>
<td><a href="{% url 'ITAM:_device_view' node.pk %}">{{ node }}</a></td>
<td>{{ node.organization }}</td>
</tr>
{% endfor%}
{% else %}
<tr>
<td colspan="2"> Nothing Found</td>
</tr>
{% endif %}
</table>
</div>
<div style="display: block; width: 100%;">
<h3>Devices</h3>
<table>
<tr>
<th>Name</th>
<th>Organization</th>
</tr>
{% if item.devices.all %}
{% for device in item.devices.all %}
<tr>
<td><a href="{% url 'ITAM:_device_view' device.pk %}">{{ device }}</a></td>
<td>{{ device.organization }}</td>
</tr>
{% endfor%}
{% else %}
<tr>
<td colspan="2"> Nothing Found</td>
</tr>
{% endif %}
</table>
</div>
<div style="display: block; width: 100%;">
<h3>Config</h3>
<pre>{{ item.config | json_pretty }}</pre>
</div>
</div>
<div id="rendered_config" class="content-tab">
{% include 'content/section.html.j2' with tab=form.tabs.rendered_config %}
</div>
{% endblock %}

View File

@ -0,0 +1,53 @@
{% extends 'base.html.j2' %}
{% block content %}
<input type="button" value="New Cluster" onclick="window.location='{% url 'ITIM:_cluster_add' %}';">
<table class="data">
<tr>
<th>Title</th>
<th>Cluster / Device</th>
<th>Organization</th>
<th>&nbsp;</th>
</tr>
{% if items %}
{% for item in items %}
<tr>
<td><a href="{% url 'ITIM:_cluster_view' pk=item.id %}">{{ item.name }}</a></td>
<td>
{% if item.device %}
{{ item.device }}
{% else %}
{{ item.cluster }}
{% endif %}
</td>
<td>{{ item.organization }}</td>
<td>&nbsp;</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="4">Nothing Found</td>
</tr>
{% endif %}
</table>
<br>
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">&laquo; first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</span>
</div>
{% endblock %}

View File

@ -1,7 +1,7 @@
from django.urls import path
from itim.views import services
from itim.views import clusters, services
app_name = "ITIM"
urlpatterns = [
@ -12,4 +12,10 @@ urlpatterns = [
path("service/<int:pk>/delete", services.Delete.as_view(), name="_service_delete"),
path("service/<int:pk>", services.View.as_view(), name="_service_view"),
path("clusters", clusters.Index.as_view(), name="Clusters"),
path("clusters/add", clusters.Add.as_view(), name="_cluster_add"),
path("clusters/<int:pk>/edit", clusters.Change.as_view(), name="_cluster_change"),
path("clusters/<int:pk>/delete", clusters.Delete.as_view(), name="_cluster_delete"),
path("clusters/<int:pk>", clusters.View.as_view(), name="_cluster_view"),
]

185
app/itim/views/clusters.py Normal file
View File

@ -0,0 +1,185 @@
from django.contrib.auth import decorators as auth_decorator
from django.urls import reverse
from django.utils.decorators import method_decorator
from core.forms.comment import AddNoteForm
from core.models.notes import Notes
from core.views.common import AddView, ChangeView, DeleteView, IndexView
from itim.forms.clusters import ClusterForm, DetailForm
from itim.models.clusters import Cluster
from settings.models.user_settings import UserSettings
class Add(AddView):
form_class = ClusterForm
model = Cluster
permission_required = [
'itim.add_cluster',
]
def get_initial(self):
initial: dict = {
'organization': UserSettings.objects.get(user = self.request.user).default_organization
}
if 'pk' in self.kwargs:
if self.kwargs['pk']:
initial.update({'parent': self.kwargs['pk']})
self.model.parent.field.hidden = True
return initial
def get_success_url(self, **kwargs):
return reverse('ITIM:Clusters')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = 'New Cluster'
return context
class Change(ChangeView):
form_class = ClusterForm
model = Cluster
permission_required = [
'itim.change_cluster',
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = str(self.object)
return context
def get_success_url(self, **kwargs):
return reverse('ITIM:_cluster_view', args=(self.kwargs['pk'],))
class Delete(DeleteView):
model = Cluster
permission_required = [
'itim.delete_cluster',
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = 'Delete ' + str(self.object)
return context
def get_success_url(self, **kwargs):
return reverse('ITIM:Clusters')
class Index(IndexView):
context_object_name = "items"
model = Cluster
paginate_by = 10
permission_required = [
'itim.view_cluster'
]
template_name = 'itim/cluster_index.html.j2'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['model_docs_path'] = self.model._meta.app_label + '/' + self.model._meta.model_name
context['content_title'] = 'Clusters'
return context
class View(ChangeView):
context_object_name = "item"
form_class = DetailForm
model = Cluster
permission_required = [
'itim.view_cluster',
]
template_name = 'itim/cluster.html.j2'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['notes_form'] = AddNoteForm(prefix='note')
context['notes'] = Notes.objects.filter(service=self.kwargs['pk'])
context['model_pk'] = self.kwargs['pk']
context['model_name'] = self.model._meta.model_name
context['model_delete_url'] = reverse('ITIM:_cluster_delete', args=(self.kwargs['pk'],))
context['content_title'] = self.object.name
return context
def post(self, request, *args, **kwargs):
item = Cluster.objects.get(pk=self.kwargs['pk'])
notes = AddNoteForm(request.POST, prefix='note')
if notes.is_bound and notes.is_valid() and notes.instance.note != '':
notes.instance.service = item
notes.instance.organization = item.organization
notes.save()
def get_success_url(self, **kwargs):
return reverse('ITIM:_cluster_view', args=(self.kwargs['pk'],))