Clean up choices mapping

This commit is contained in:
Jeremy Stretch 2023-08-07 16:08:19 -04:00
parent ebca49f8e3
commit 22ecc96de2
2 changed files with 36 additions and 29 deletions

View File

@ -15,6 +15,22 @@ from dcim.choices import (
TEMPLATE_FILENAME = 'generated_schema.json'
OUTPUT_FILENAME = 'contrib/generated_schema.json'
CHOICES_MAP = {
'airflow_choices': DeviceAirflowChoices,
'weight_unit_choices': WeightUnitChoices,
'subdevice_role_choices': SubdeviceRoleChoices,
'console_port_type_choices': ConsolePortTypeChoices,
'console_server_port_type_choices': ConsolePortTypeChoices, # Reusing ConsolePortTypeChoices
'power_port_type_choices': PowerPortTypeChoices,
'power_outlet_type_choices': PowerOutletTypeChoices,
'power_outlet_feedleg_choices': PowerOutletFeedLegChoices,
'interface_type_choices': InterfaceTypeChoices,
'interface_poe_mode_choices': InterfacePoEModeChoices,
'interface_poe_type_choices': InterfacePoETypeChoices,
'front_port_type_choices': PortTypeChoices,
'rear_port_type_choices': PortTypeChoices, # Reusing PortTypeChoices
}
class Command(BaseCommand):
help = "Generate the NetBox validation schemas."
@ -27,31 +43,22 @@ class Command(BaseCommand):
)
def handle(self, *args, **kwargs):
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'{settings.TEMPLATES_DIR}/extras/')
template_env = Environment(loader=template_loader)
template = template_env.get_template(TEMPLATE_FILENAME)
outputText = template.render(schemas=schemas)
context = {
key: json_dumps(choices.values())
for key, choices in CHOICES_MAP.items()
}
rendered = template.render(**context)
if kwargs['write']:
# $root/contrib/generated_schema.json
filename = os.path.join(os.path.split(settings.BASE_DIR)[0], OUTPUT_FILENAME)
with open(filename, mode='w', encoding='UTF-8') as f:
f.write(json_dumps(json_loads(outputText), indent=4))
f.write(json_dumps(json_loads(rendered), indent=4))
f.write('\n')
f.close()
self.stdout.write(self.style.SUCCESS(f"Schema written to {filename}."))
else:
self.stdout.write(outputText)
self.stdout.write(rendered)

View File

@ -4,22 +4,22 @@
"definitions": {
"airflow": {
"type": "string",
"enum": {{ schemas["airflow_choices"] }}
"enum": {{ airflow_choices }}
},
"weight-unit": {
"type": "string",
"enum": {{ schemas["weight_unit_choices"] }}
"enum": {{ weight_unit_choices }}
},
"subdevice-role": {
"type": "string",
"enum": {{ schemas["subdevice_role_choices"] }}
"enum": {{ subdevice_role_choices }}
},
"console-port": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["console_port_type_choices"] }}
"enum": {{ console_port_type_choices }}
}
}
},
@ -28,7 +28,7 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["console_port_type_choices"] }}
"enum": {{ console_server_port_type_choices }}
}
}
},
@ -37,7 +37,7 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["power_port_type_choices"] }}
"enum": {{ power_port_type_choices }}
}
}
},
@ -46,11 +46,11 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["power_outlet_type_choices"] }}
"enum": {{ power_outlet_type_choices }}
},
"feed-leg": {
"type": "string",
"enum": {{ schemas["power_outlet_feedleg_choices"] }}
"enum": {{ power_outlet_feedleg_choices }}
}
}
},
@ -59,15 +59,15 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["interface_type_choices"] }}
"enum": {{ interface_type_choices }}
},
"poe_mode": {
"type": "string",
"enum": {{ schemas["interface_poe_mode_choices"] }}
"enum": {{ interface_poe_mode_choices }}
},
"poe_type": {
"type": "string",
"enum": {{ schemas["interface_poe_type_choices"] }}
"enum": {{ interface_poe_type_choices }}
}
}
},
@ -76,7 +76,7 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["port_type_choices"] }}
"enum": {{ front_port_type_choices }}
}
}
},
@ -85,7 +85,7 @@
"properties": {
"type": {
"type": "string",
"enum": {{ schemas["port_type_choices"]}}
"enum": {{ rear_port_type_choices}}
}
}
}