feat(config_management): Ability to add a host to a config group
!17 #42
This commit is contained in:
20
app/config_management/forms/group_hosts.py
Normal file
20
app/config_management/forms/group_hosts.py
Normal file
@ -0,0 +1,20 @@
|
||||
from django import forms
|
||||
|
||||
from itam.models.device import Device
|
||||
|
||||
from config_management.models.groups import ConfigGroups, ConfigGroupHosts
|
||||
|
||||
|
||||
class ConfigGroupHostsForm(forms.ModelForm):
|
||||
|
||||
__name__ = 'asdsa'
|
||||
|
||||
class Meta:
|
||||
|
||||
fields = [
|
||||
'host'
|
||||
]
|
||||
|
||||
model = ConfigGroupHosts
|
||||
|
||||
prefix = 'config_group_hosts'
|
@ -0,0 +1,43 @@
|
||||
# Generated by Django 5.0.6 on 2024-06-02 20:51
|
||||
|
||||
import access.fields
|
||||
import config_management.models.groups
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('access', '0002_alter_team_organization'),
|
||||
('config_management', '0001_initial'),
|
||||
('itam', '0012_alter_device_serial_number_alter_device_uuid'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='configgroups',
|
||||
options={},
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='configgroups',
|
||||
name='config',
|
||||
field=models.JSONField(blank=True, default=None, null=True, validators=[config_management.models.groups.ConfigGroups.validate_config_keys]),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ConfigGroupHosts',
|
||||
fields=[
|
||||
('is_global', models.BooleanField(default=False)),
|
||||
('id', models.AutoField(primary_key=True, serialize=False, unique=True)),
|
||||
('created', access.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False)),
|
||||
('modified', access.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False)),
|
||||
('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='config_management.configgroups')),
|
||||
('host', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='itam.device', validators=[config_management.models.groups.ConfigGroupHosts.validate_host_no_parent_group])),
|
||||
('organization', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='access.organization')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
@ -8,6 +8,8 @@ from access.models import TenancyObject
|
||||
|
||||
from core.mixin.history_save import SaveHistory
|
||||
|
||||
from itam.models.device import Device
|
||||
|
||||
|
||||
|
||||
class GroupsCommonFields(TenancyObject, models.Model):
|
||||
@ -25,10 +27,6 @@ class GroupsCommonFields(TenancyObject, models.Model):
|
||||
|
||||
modified = AutoLastModifiedField()
|
||||
|
||||
def __str__(self):
|
||||
|
||||
return self.name
|
||||
|
||||
|
||||
|
||||
class ConfigGroups(GroupsCommonFields, SaveHistory):
|
||||
@ -118,3 +116,39 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
||||
self.organization = ConfigGroups.objects.get(id=self.parent.id).organization
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
return self.name
|
||||
|
||||
|
||||
|
||||
class ConfigGroupHosts(GroupsCommonFields, SaveHistory):
|
||||
|
||||
|
||||
def validate_host_no_parent_group(self):
|
||||
""" Ensure that the host is not within any parent group
|
||||
|
||||
Raises:
|
||||
ValidationError: host exists within group chain
|
||||
"""
|
||||
|
||||
if False:
|
||||
raise ValidationError(f'host {self} is already a member of this chain as it;s a member of group ""')
|
||||
|
||||
|
||||
host = models.ForeignKey(
|
||||
Device,
|
||||
on_delete=models.CASCADE,
|
||||
null = False,
|
||||
blank= False,
|
||||
validators = [ validate_host_no_parent_group ]
|
||||
)
|
||||
|
||||
|
||||
group = models.ForeignKey(
|
||||
ConfigGroups,
|
||||
on_delete=models.CASCADE,
|
||||
null = False,
|
||||
blank= False
|
||||
)
|
||||
|
@ -35,6 +35,7 @@
|
||||
|
||||
<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'Details')">Details</button>
|
||||
<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'Children')">Child Groups</button>
|
||||
<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'Hosts')">Hosts</button>
|
||||
<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'Software')">Software</button>
|
||||
<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'Configuration')">Configuration</button>
|
||||
<button class="tablinks" onclick="openCity(event, 'Notes')">Notes</button>
|
||||
@ -58,6 +59,7 @@
|
||||
|
||||
<div id="Children" class="tabcontent">
|
||||
<h3>Child Groups</h3>
|
||||
|
||||
<table class="data">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
@ -81,14 +83,41 @@
|
||||
|
||||
</div>
|
||||
|
||||
<div id="Hosts" class="tabcontent">
|
||||
<h3>
|
||||
Hosts
|
||||
</h3>
|
||||
|
||||
<input type="button" value="Add Host" onclick="window.location='{% url 'Config Management:_group_add_host' group.id %}';">
|
||||
|
||||
<table class="data">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Organization</th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
{% if config_group_hosts %}
|
||||
{% for host in config_group_hosts %}
|
||||
<tr>
|
||||
<td><a href="{% url 'ITAM:_device_view' pk=host.host.id %}">{{ host.host }}</a></td>
|
||||
<td>{{ host.host.organization }}</td>
|
||||
<td><a href="{% url 'Config Management:_group_delete_host' group_id=group.id pk=host.id %}">Delete</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="3">Nothing Found</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</table>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="Software" class="tabcontent">
|
||||
<h3>
|
||||
Software
|
||||
</h3>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<div id="Configuration" class="tabcontent">
|
||||
|
@ -12,4 +12,5 @@ urlpatterns = [
|
||||
path('group/<int:pk>', GroupView.as_view(), name='_group_view'),
|
||||
path('group/<int:pk>/delete', GroupDelete.as_view(), name='_group_delete'),
|
||||
|
||||
path('group/<int:group_id>/host', GroupHostAdd.as_view(), name='_group_add_host'),
|
||||
]
|
||||
|
@ -1,7 +1,6 @@
|
||||
import json
|
||||
|
||||
from django.contrib.auth import decorators as auth_decorator
|
||||
|
||||
from django.db.models import Count, Q
|
||||
from django.urls import reverse
|
||||
from django.utils.decorators import method_decorator
|
||||
@ -12,9 +11,12 @@ from access.mixin import OrganizationPermission
|
||||
from core.forms.comment import AddNoteForm
|
||||
from core.models.notes import Notes
|
||||
|
||||
from itam.models.device import Device
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
from config_management.models.groups import ConfigGroups
|
||||
from config_management.forms.group_hosts import ConfigGroupHostsForm
|
||||
from config_management.models.groups import ConfigGroups, ConfigGroupHosts
|
||||
|
||||
|
||||
|
||||
@ -118,6 +120,7 @@ class GroupView(OrganizationPermission, generic.UpdateView):
|
||||
|
||||
context['config'] = json.dumps(json.loads(self.object.render_config()), indent=4, sort_keys=True)
|
||||
|
||||
context['config_group_hosts'] = ConfigGroupHosts.objects.filter(group_id = self.kwargs['pk']).order_by('-host')
|
||||
|
||||
context['notes_form'] = AddNoteForm(prefix='note')
|
||||
context['notes'] = Notes.objects.filter(config_group=self.kwargs['pk'])
|
||||
@ -129,23 +132,23 @@ class GroupView(OrganizationPermission, generic.UpdateView):
|
||||
|
||||
context['content_title'] = self.object.name
|
||||
|
||||
# if self.request.user.is_superuser:
|
||||
# if self.request.user.is_superuser:
|
||||
|
||||
# context['device_software'] = DeviceSoftware.objects.filter(
|
||||
# software=self.kwargs['pk']
|
||||
# ).order_by(
|
||||
# 'device',
|
||||
# 'organization'
|
||||
# )
|
||||
# context['device_software'] = DeviceSoftware.objects.filter(
|
||||
# software=self.kwargs['pk']
|
||||
# ).order_by(
|
||||
# 'device',
|
||||
# 'organization'
|
||||
# )
|
||||
|
||||
# elif not self.request.user.is_superuser:
|
||||
# context['device_software'] = DeviceSoftware.objects.filter(
|
||||
# Q(device__in=self.user_organizations(),
|
||||
# software=self.kwargs['pk'])
|
||||
# ).order_by(
|
||||
# 'device',
|
||||
# 'organization'
|
||||
# )
|
||||
# elif not self.request.user.is_superuser:
|
||||
# context['device_software'] = DeviceSoftware.objects.filter(
|
||||
# Q(device__in=self.user_organizations(),
|
||||
# software=self.kwargs['pk'])
|
||||
# ).order_by(
|
||||
# 'device',
|
||||
# 'organization'
|
||||
# )
|
||||
|
||||
return context
|
||||
|
||||
@ -184,10 +187,6 @@ class GroupDelete(OrganizationPermission, generic.DeleteView):
|
||||
|
||||
template_name = 'form.html.j2'
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return reverse('Config Management:_group_index')
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
@ -195,3 +194,53 @@ class GroupDelete(OrganizationPermission, generic.DeleteView):
|
||||
context['content_title'] = 'Delete ' + self.object.name
|
||||
|
||||
return context
|
||||
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return reverse('Config Management:_group_index')
|
||||
|
||||
|
||||
|
||||
class GroupHostAdd(OrganizationPermission, generic.CreateView):
|
||||
|
||||
model = ConfigGroupHosts
|
||||
|
||||
permission_required = [
|
||||
'config_management.add_hosts',
|
||||
]
|
||||
|
||||
template_name = 'form.html.j2'
|
||||
|
||||
form_class = ConfigGroupHostsForm
|
||||
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
form.instance.group_id = self.kwargs['group_id']
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['content_title'] = 'Add Host to Group'
|
||||
|
||||
return context
|
||||
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
|
||||
form_class = super().get_form(form_class=None)
|
||||
|
||||
group = ConfigGroups.objects.get(pk=self.kwargs['group_id'])
|
||||
|
||||
form_class.fields["host"].queryset = Device.objects.filter(organization=group.organization.id)
|
||||
|
||||
return form_class
|
||||
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return reverse('Config Management:_group_view', args=[self.kwargs['group_id'],])
|
||||
|
@ -125,6 +125,11 @@ class SaveHistory(models.Model):
|
||||
item_parent_pk = self.team.pk
|
||||
item_parent_class = self.team._meta.model_name
|
||||
|
||||
if self._meta.model_name == 'configgrouphosts':
|
||||
|
||||
item_parent_pk = self.group.id
|
||||
item_parent_class = self.group._meta.model_name
|
||||
|
||||
|
||||
if not before:
|
||||
|
||||
|
@ -95,7 +95,6 @@
|
||||
{% elif software.get_action_display == 'Remove'%}
|
||||
{% include 'icons/cross_text.html.j2' with icon_text=software.get_action_display icon_link=icon_link %}
|
||||
{% else %}
|
||||
|
||||
{% include 'icons/add_link.html.j2' with icon_text='Add' icon_link=icon_link %}
|
||||
{% endif %}
|
||||
</td>
|
||||
@ -152,6 +151,29 @@
|
||||
<div>
|
||||
<textarea cols="90" rows="30" readonly>{{ config }}</textarea>
|
||||
</div>
|
||||
<br />
|
||||
<hr />
|
||||
<table class="data">
|
||||
<tr>
|
||||
<th>Group</th>
|
||||
<th>Added</th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
{% if config_groups %}
|
||||
{% for group in config_groups %}
|
||||
<tr>
|
||||
<td><a href="{% url 'Config Management:_group_view' pk=group.id %}">{{ group.group }}</a></td>
|
||||
<td>{{ group.created }}</td>
|
||||
<td> </td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="3">Nothing Found</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
{% endblock %}
|
@ -12,6 +12,8 @@ from django.views import generic
|
||||
from access.mixin import OrganizationPermission
|
||||
from access.models import Organization
|
||||
|
||||
from config_management.models.groups import ConfigGroupHosts
|
||||
|
||||
from ..models.device import Device, DeviceSoftware, DeviceOperatingSystem
|
||||
from ..models.software import Software
|
||||
|
||||
@ -101,6 +103,8 @@ class View(OrganizationPermission, generic.UpdateView):
|
||||
config = self.object.get_configuration(self.kwargs['pk'])
|
||||
context['config'] = json.dumps(config, indent=4, sort_keys=True)
|
||||
|
||||
context['config_groups'] = ConfigGroupHosts.objects.filter(host = self.object.id)
|
||||
|
||||
context['model_pk'] = self.kwargs['pk']
|
||||
context['model_name'] = self.model._meta.verbose_name.replace(' ', '')
|
||||
|
||||
|
Reference in New Issue
Block a user