|
1 | 1 | import sys
|
| 2 | +from typing import List |
2 | 3 |
|
3 | 4 | from dcim.models import DeviceType, Manufacturer, Region
|
| 5 | +from dcim.models.device_component_templates import ( |
| 6 | + ConsolePortTemplate, |
| 7 | + ConsoleServerPortTemplate, |
| 8 | + DeviceBayTemplate, |
| 9 | + FrontPortTemplate, |
| 10 | + InterfaceTemplate, |
| 11 | + PowerOutletTemplate, |
| 12 | + PowerPortTemplate, |
| 13 | + RearPortTemplate, |
| 14 | +) |
4 | 15 | from startup_script_utils import (
|
5 | 16 | load_yaml,
|
6 | 17 | pop_custom_fields,
|
7 | 18 | set_custom_fields_values,
|
8 | 19 | split_params,
|
9 | 20 | )
|
10 | 21 | from tenancy.models import Tenant
|
| 22 | +from utilities.forms.utils import expand_alphanumeric_pattern |
| 23 | + |
| 24 | + |
| 25 | +def expand_templates(params: List[dict], device_type: DeviceType) -> List[dict]: |
| 26 | + templateable_fields = ["name", "label", "positions", "rear_port", "rear_port_position"] |
| 27 | + |
| 28 | + expanded = [] |
| 29 | + for param in params: |
| 30 | + param["device_type"] = device_type |
| 31 | + expanded_fields = {} |
| 32 | + has_plain_fields = False |
| 33 | + |
| 34 | + for field in templateable_fields: |
| 35 | + template_value = param.pop(f"{field}_template", None) |
| 36 | + |
| 37 | + if field in param: |
| 38 | + has_plain_fields = True |
| 39 | + expanded.append(param) |
| 40 | + elif template_value: |
| 41 | + expanded_fields[field] = list(expand_alphanumeric_pattern(template_value)) |
| 42 | + |
| 43 | + if expanded_fields and has_plain_fields: |
| 44 | + raise ValueError(f"Mix of plain and template keys provided for {templateable_fields}") |
| 45 | + |
| 46 | + if not expanded_fields: |
| 47 | + continue |
| 48 | + |
| 49 | + elements = list(expanded_fields.values()) |
| 50 | + master_len = len(elements[0]) |
| 51 | + if not all([len(elem) == master_len for elem in elements]): |
| 52 | + raise ValueError( |
| 53 | + f"Number of elements in template fields " |
| 54 | + f"{list(expanded_fields.keys())} must be equal" |
| 55 | + ) |
| 56 | + |
| 57 | + for idx in range(master_len): |
| 58 | + tmp = param.copy() |
| 59 | + for field, value in expanded_fields.items(): |
| 60 | + if field in nested_assocs: |
| 61 | + model, match_key = nested_assocs[field] |
| 62 | + query = {match_key: value[idx], "device_type": device_type} |
| 63 | + tmp[field] = model.objects.get(**query) |
| 64 | + else: |
| 65 | + tmp[field] = value[idx] |
| 66 | + expanded.append(tmp) |
| 67 | + return expanded |
| 68 | + |
11 | 69 |
|
12 | 70 | device_types = load_yaml("/opt/netbox/initializers/device_types.yml")
|
13 | 71 |
|
|
17 | 75 | match_params = ["manufacturer", "model", "slug"]
|
18 | 76 | required_assocs = {"manufacturer": (Manufacturer, "name")}
|
19 | 77 | optional_assocs = {"region": (Region, "name"), "tenant": (Tenant, "name")}
|
| 78 | +nested_assocs = {"rear_port": (RearPortTemplate, "name"), "power_port": (PowerPortTemplate, "name")} |
| 79 | + |
| 80 | +supported_components = { |
| 81 | + "interfaces": (InterfaceTemplate, ["name"]), |
| 82 | + "console_ports": (ConsolePortTemplate, ["name"]), |
| 83 | + "console_server_ports": (ConsoleServerPortTemplate, ["name"]), |
| 84 | + "power_ports": (PowerPortTemplate, ["name"]), |
| 85 | + "power_outlets": (PowerOutletTemplate, ["name"]), |
| 86 | + "rear_ports": (RearPortTemplate, ["name"]), |
| 87 | + "front_ports": (FrontPortTemplate, ["name"]), |
| 88 | + "device_bays": (DeviceBayTemplate, ["name"]), |
| 89 | +} |
20 | 90 |
|
21 | 91 | for params in device_types:
|
22 | 92 | custom_field_data = pop_custom_fields(params)
|
| 93 | + components = [(v[0], v[1], params.pop(k, [])) for k, v in supported_components.items()] |
23 | 94 |
|
24 | 95 | for assoc, details in required_assocs.items():
|
25 | 96 | model, field = details
|
|
41 | 112 | print("🔡 Created device type", device_type.manufacturer, device_type.model)
|
42 | 113 |
|
43 | 114 | set_custom_fields_values(device_type, custom_field_data)
|
| 115 | + |
| 116 | + for component in components: |
| 117 | + c_model, c_match_params, c_params = component |
| 118 | + c_match_params.append("device_type") |
| 119 | + |
| 120 | + if not c_params: |
| 121 | + continue |
| 122 | + |
| 123 | + expanded_c_params = expand_templates(c_params, device_type) |
| 124 | + |
| 125 | + for n_assoc, n_details in nested_assocs.items(): |
| 126 | + n_model, n_field = n_details |
| 127 | + for c_param in expanded_c_params: |
| 128 | + if n_assoc in c_param: |
| 129 | + n_query = {n_field: c_param[n_assoc], "device_type": device_type} |
| 130 | + c_param[n_assoc] = n_model.objects.get(**n_query) |
| 131 | + |
| 132 | + for new_param in expanded_c_params: |
| 133 | + new_matching_params, new_defaults = split_params(new_param, c_match_params) |
| 134 | + new_obj, new_obj_created = c_model.objects.get_or_create( |
| 135 | + **new_matching_params, defaults=new_defaults |
| 136 | + ) |
| 137 | + if new_obj_created: |
| 138 | + print( |
| 139 | + f"🧷 Created {c_model._meta} {new_obj} component for device type {device_type}" |
| 140 | + ) |
0 commit comments