Skip to content

Commit

Permalink
feat(itam): Ability to add device configuration
Browse files Browse the repository at this point in the history
!43 fixes #44
  • Loading branch information
jon-nfc committed Aug 11, 2024
1 parent f77313f commit 6d88939
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/itam/forms/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Meta:
'device_type',
'organization',
'model_notes',
'config',
]


Expand Down
19 changes: 19 additions & 0 deletions app/itam/migrations/0002_device_config.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
27 changes: 27 additions & 0 deletions app/itam/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down
7 changes: 7 additions & 0 deletions app/itam/templates/itam/device.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@
<input type="submit" name="{{operating_system.prefix}}" value="Submit" />
</div>

<div style="display: block; width: 100%;">
<h3>Device Config</h3>
<br>
<textarea cols="90" rows="30" readonly>{{ device.config }}</textarea>
</div>

<input type="button" value="Edit" onclick="window.location='{% url 'ITAM:_device_change' device.id %}';">
{% if not tab %}
<script>
// Get the element with id="defaultOpen" and click on it
Expand Down
2 changes: 2 additions & 0 deletions docs/projects/centurion_erp/user/itam/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ This tab displays in `JSON` format configuration that is ready for use. Config f

This configuration can also be obtained from API endpoint `/api/config/<machine-slug>` where `<machine-slug>` would match the Ansible `inventory_hostname`.

The device can also have configuration defined. this configuration is intended to replicate Ansible `hostvars`. After all of the configuration has been rendered from the other modules that contain config, the devices configuration is applied last. In essence the devices configuration will always override any duplicated configuration.


### Inventory

Expand Down

0 comments on commit 6d88939

Please sign in to comment.