From 9668e811c5bf85d87d510c1aad707048eb971393 Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 17 Jul 2024 16:58:50 +0930 Subject: [PATCH] feat(itam): Ability to add device configuration !43 fixes #44 --- app/itam/forms/device/device.py | 1 + app/itam/migrations/0002_device_config.py | 19 +++++++++++++ app/itam/models/device.py | 27 +++++++++++++++++++ app/itam/templates/itam/device.html.j2 | 7 +++++ .../centurion_erp/user/itam/device.md | 2 ++ 5 files changed, 56 insertions(+) create mode 100644 app/itam/migrations/0002_device_config.py diff --git a/app/itam/forms/device/device.py b/app/itam/forms/device/device.py index 8f6c1626f..78d3fcf0e 100644 --- a/app/itam/forms/device/device.py +++ b/app/itam/forms/device/device.py @@ -23,6 +23,7 @@ class Meta: 'device_type', 'organization', 'model_notes', + 'config', ] diff --git a/app/itam/migrations/0002_device_config.py b/app/itam/migrations/0002_device_config.py new file mode 100644 index 000000000..4992b6fe7 --- /dev/null +++ b/app/itam/migrations/0002_device_config.py @@ -0,0 +1,19 @@ +# Generated by Django 5.0.7 on 2024-07-17 07:17 + +import itam.models.device +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('itam', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='device', + name='config', + field=models.JSONField(blank=True, default=None, help_text='Configuration for this device', null=True, validators=[itam.models.device.Device.validate_config_keys_not_reserved], verbose_name='Host Configuration'), + ), + ] diff --git a/app/itam/models/device.py b/app/itam/models/device.py index 4bbc292b8..bb6606233 100644 --- a/app/itam/models/device.py +++ b/app/itam/models/device.py @@ -44,6 +44,20 @@ def __str__(self): class Device(DeviceCommonFieldsName, SaveHistory): + reserved_config_keys: list = [ + 'software' + ] + + def validate_config_keys_not_reserved(self): + + value: dict = self + + for invalid_key in Device.reserved_config_keys: + + if invalid_key in value.keys(): + raise ValidationError(f'json key "{invalid_key}" is a reserved configuration key') + + def validate_uuid_format(self): pattern = r'[0-9|a-f]{8}\-[0-9|a-f]{4}\-[0-9|a-f]{4}\-[0-9|a-f]{4}\-[0-9|a-f]{12}' @@ -113,6 +127,15 @@ def validate_hostname_format(self): ) + config = models.JSONField( + blank = True, + default = None, + null = True, + validators=[ validate_config_keys_not_reserved ], + verbose_name = 'Host Configuration', + help_text = 'Configuration for this device' + ) + inventorydate = models.DateTimeField( verbose_name = 'Last Inventory Date', null = True, @@ -254,6 +277,10 @@ def get_configuration(self, id): config['software'] = merge_software(group_software, host_software) + if self.config: + + config.update(self.config) + return config diff --git a/app/itam/templates/itam/device.html.j2 b/app/itam/templates/itam/device.html.j2 index 15c9beb5d..67ecd1e69 100644 --- a/app/itam/templates/itam/device.html.j2 +++ b/app/itam/templates/itam/device.html.j2 @@ -184,6 +184,13 @@ +
+

Device Config

+
+ +
+ + {% if not tab %}