diff --git a/netbox/extras/management/commands/buildschema.py b/netbox/extras/management/commands/buildschema.py new file mode 100644 index 000000000..117457e49 --- /dev/null +++ b/netbox/extras/management/commands/buildschema.py @@ -0,0 +1,40 @@ +from os import path as os_path +from jinja2 import FileSystemLoader, Environment +from json import dumps as json_dumps + +from django.core.management.base import BaseCommand + +from dcim.choices import DeviceAirflowChoices +from dcim.choices import SubdeviceRoleChoices +from dcim.choices import ConsolePortTypeChoices +from dcim.choices import PowerPortTypeChoices +from dcim.choices import PowerOutletTypeChoices, PowerOutletFeedLegChoices +from dcim.choices import InterfaceTypeChoices, InterfacePoEModeChoices, InterfacePoETypeChoices +from dcim.choices import PortTypeChoices +from dcim.choices import WeightUnitChoices + + +class Command(BaseCommand): + help = "Generate the NetBox validation schemas." + + def handle(self, *args, **options): + schemas = {} + schemas["airflow_choices"] = json_dumps(DeviceAirflowChoices.values()) + schemas["weight_unit_choices"] = json_dumps(WeightUnitChoices.values()) + schemas["subdevice_role_choices"] = json_dumps(SubdeviceRoleChoices.values()) + schemas["console_port_type_choices"] = json_dumps(ConsolePortTypeChoices.values()) # console-ports and console-server-ports + schemas["power_port_type_choices"] = json_dumps(PowerPortTypeChoices.values()) + schemas["power_outlet_type_choices"] = json_dumps(PowerOutletTypeChoices.values()) + schemas["power_outlet_feedleg_choices"] = json_dumps(PowerOutletFeedLegChoices.values()) + schemas["interface_type_choices"] = json_dumps(InterfaceTypeChoices.values()) + schemas["interface_poe_mode_choices"] = json_dumps(InterfacePoEModeChoices.values()) + schemas["interface_poe_type_choices"] = json_dumps(InterfacePoETypeChoices.values()) + schemas["port_type_choices"] = json_dumps(PortTypeChoices.values()) # front-ports and rear-ports + + template_loader = FileSystemLoader(searchpath=f'{os_path.dirname(__file__)}/../templates') + template_env = Environment(loader=template_loader) + TEMPLATE_FILE = 'generated_schema.j2' + template = template_env.get_template(TEMPLATE_FILE) + outputText = template.render(schemas=schemas) + + print(outputText) diff --git a/netbox/extras/management/templates/generated_schema.j2 b/netbox/extras/management/templates/generated_schema.j2 new file mode 100644 index 000000000..552ab0429 --- /dev/null +++ b/netbox/extras/management/templates/generated_schema.j2 @@ -0,0 +1,93 @@ +{ + "type": "object", + "additionalProperties": false, + "definitions": { + "airflow": { + "type": "string", + "enum": {{ schemas["airflow_choices"] }} + }, + "weight-unit": { + "type": "string", + "enum": {{ schemas["weight_unit_choices"] }} + }, + "subdevice-role": { + "type": "string", + "enum": {{ schemas["subdevice_role_choices"] }} + }, + "console-port": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["console_port_type_choices"] }} + } + } + }, + "console-server-port": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["console_port_type_choices"] }} + } + } + }, + "power-port": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["power_port_type_choices"] }} + } + } + }, + "power-outlet": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["power_outlet_type_choices"] }} + }, + "feed-leg": { + "type": "string", + "enum": {{ schemas["power_outlet_feedleg_choices"] }} + } + } + }, + "interface": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["interface_type_choices"] }} + }, + "poe_mode": { + "type": "string", + "enum": {{ schemas["interface_poe_mode_choices"] }} + }, + "poe_type": { + "type": "string", + "enum": {{ schemas["interface_poe_type_choices"] }} + } + } + }, + "front-port": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["port_type_choices"] }} + } + } + }, + "rear-port": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": {{ schemas["port_type_choices"]}} + } + } + } + } +} \ No newline at end of file