diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 43c6482..3ea6214 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -14,6 +14,9 @@ jobs: name: Checks syntax of our code steps: - uses: actions/checkout@v2 + with: + # Full git history is needed to get a proper list of changed files within `super-linter` + fetch-depth: 0 - uses: actions/setup-python@v2 - name: Lint Code Base uses: github/super-linter@v3 diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index f1b275c..eb733ca 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -10,12 +10,12 @@ ## Examples: # - name: link_to_repo -# text: 'Link to Netbox Docker' -# url: 'https://github.com/netbox-community/netbox-docker' +# link_text: 'Link to Netbox Docker' +# link_url: 'https://github.com/netbox-community/netbox-docker' # new_window: False # content_type: device # - name: link_to_localhost -# text: 'Link to localhost' -# url: 'http://localhost' +# link_text: 'Link to localhost' +# link_url: 'http://localhost' # new_window: True # content_type: device diff --git a/initializers/devices.yml b/initializers/devices.yml index 0de0504..8324c36 100644 --- a/initializers/devices.yml +++ b/initializers/devices.yml @@ -42,3 +42,12 @@ # position: 3 # custom_field_data: # text_field: Description +# - name: server04 +# device_role: server +# device_type: Other +# site: SING 1 +# location: cage 101 +# face: front +# position: 3 +# custom_field_data: +# text_field: Description diff --git a/initializers/rack_groups.yml b/initializers/locations.yml similarity index 100% rename from initializers/rack_groups.yml rename to initializers/locations.yml diff --git a/initializers/power_panels.yml b/initializers/power_panels.yml index 1205851..a670ba7 100644 --- a/initializers/power_panels.yml +++ b/initializers/power_panels.yml @@ -2,4 +2,4 @@ # site: AMS 1 # - name: power panel SING 1 # site: SING 1 -# rack_group: cage 101 +# location: cage 101 diff --git a/initializers/racks.yml b/initializers/racks.yml index 379553d..9071e19 100644 --- a/initializers/racks.yml +++ b/initializers/racks.yml @@ -32,7 +32,7 @@ # text_field: Description # - site: SING 1 # name: rack-03 -# group: cage 101 +# location: cage 101 # role: Role 3 # type: 4-post-cabinet # width: 19 diff --git a/initializers/vlan_groups.yml b/initializers/vlan_groups.yml index 3e0bb00..facd34f 100644 --- a/initializers/vlan_groups.yml +++ b/initializers/vlan_groups.yml @@ -1,6 +1,24 @@ # - name: VLAN group 1 -# site: AMS 1 +# scope_type: dcim.region +# scope: Amsterdam # slug: vlan-group-1 # - name: VLAN group 2 -# site: AMS 1 +# scope_type: dcim.site +# scope: AMS 1 # slug: vlan-group-2 +# - name: VLAN group 3 +# scope_type: dcim.location +# scope: cage 101 +# slug: vlan-group-3 +# - name: VLAN group 4 +# scope_type: dcim.rack +# scope: rack-01 +# slug: vlan-group-4 +# - name: VLAN group 5 +# scope_type: virtualization.cluster +# scope: cluster1 +# slug: vlan-group-5 +# - name: VLAN group 6 +# scope_type: virtualization.clustergroup +# scope: Group 1 +# slug: vlan-group-6 diff --git a/startup_scripts/075_rack_groups.py b/startup_scripts/075_locations.py similarity index 60% rename from startup_scripts/075_rack_groups.py rename to startup_scripts/075_locations.py index 3974b56..d8a2c5a 100644 --- a/startup_scripts/075_rack_groups.py +++ b/startup_scripts/075_locations.py @@ -1,9 +1,9 @@ import sys -from dcim.models import RackGroup, Site +from dcim.models import Location, Site from startup_script_utils import load_yaml -rack_groups = load_yaml("/opt/netbox/initializers/rack_groups.yml") +rack_groups = load_yaml("/opt/netbox/initializers/locations.yml") if rack_groups is None: sys.exit() @@ -17,7 +17,7 @@ for params in rack_groups: query = {field: params.pop(assoc)} params[assoc] = model.objects.get(**query) - rack_group, created = RackGroup.objects.get_or_create(**params) + location, created = Location.objects.get_or_create(**params) if created: - print("🎨 Created rack group", rack_group.name) + print("🎨 Created location", location.name) diff --git a/startup_scripts/080_racks.py b/startup_scripts/080_racks.py index 087b3f9..b2cfc80 100644 --- a/startup_scripts/080_racks.py +++ b/startup_scripts/080_racks.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Rack, RackGroup, RackRole, Site +from dcim.models import Location, Rack, RackRole, Site from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values from tenancy.models import Tenant @@ -14,7 +14,7 @@ required_assocs = {"site": (Site, "name")} optional_assocs = { "role": (RackRole, "name"), "tenant": (Tenant, "name"), - "group": (RackGroup, "name"), + "location": (Location, "name"), } for params in racks: diff --git a/startup_scripts/140_devices.py b/startup_scripts/140_devices.py index 597db0c..423b7c9 100644 --- a/startup_scripts/140_devices.py +++ b/startup_scripts/140_devices.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site +from dcim.models import Device, DeviceRole, DeviceType, Location, Platform, Rack, Site from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values from tenancy.models import Tenant from virtualization.models import Cluster @@ -21,6 +21,7 @@ optional_assocs = { "platform": (Platform, "name"), "rack": (Rack, "name"), "cluster": (Cluster, "name"), + "location": (Location, "name"), } for params in devices: diff --git a/startup_scripts/145_devices.py b/startup_scripts/145_devices.py deleted file mode 100644 index 597db0c..0000000 --- a/startup_scripts/145_devices.py +++ /dev/null @@ -1,51 +0,0 @@ -import sys - -from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site -from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values -from tenancy.models import Tenant -from virtualization.models import Cluster - -devices = load_yaml("/opt/netbox/initializers/devices.yml") - -if devices is None: - sys.exit() - -required_assocs = { - "device_role": (DeviceRole, "name"), - "device_type": (DeviceType, "model"), - "site": (Site, "name"), -} - -optional_assocs = { - "tenant": (Tenant, "name"), - "platform": (Platform, "name"), - "rack": (Rack, "name"), - "cluster": (Cluster, "name"), -} - -for params in devices: - custom_field_data = pop_custom_fields(params) - - # primary ips are handled later in `270_primary_ips.py` - params.pop("primary_ip4", None) - params.pop("primary_ip6", None) - - for assoc, details in required_assocs.items(): - model, field = details - query = {field: params.pop(assoc)} - - params[assoc] = model.objects.get(**query) - - for assoc, details in optional_assocs.items(): - if assoc in params: - model, field = details - query = {field: params.pop(assoc)} - - params[assoc] = model.objects.get(**query) - - device, created = Device.objects.get_or_create(**params) - - if created: - set_custom_fields_values(device, custom_field_data) - - print("🖥️ Created device", device.name) diff --git a/startup_scripts/200_vlan_groups.py b/startup_scripts/200_vlan_groups.py index 7b72b2d..2a4a33d 100644 --- a/startup_scripts/200_vlan_groups.py +++ b/startup_scripts/200_vlan_groups.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Site +from django.contrib.contenttypes.models import ContentType from ipam.models import VLANGroup from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values @@ -9,7 +9,7 @@ vlan_groups = load_yaml("/opt/netbox/initializers/vlan_groups.yml") if vlan_groups is None: sys.exit() -optional_assocs = {"site": (Site, "name")} +optional_assocs = {"scope": (None, "name")} for params in vlan_groups: custom_field_data = pop_custom_fields(params) @@ -18,9 +18,20 @@ for params in vlan_groups: if assoc in params: model, field = details query = {field: params.pop(assoc)} - - params[assoc] = model.objects.get(**query) - + # Get model from Contenttype + scope_type = params.pop("scope_type", None) + if not scope_type: + print(f"VLAN Group '{params['name']}': scope_type is missing from VLAN Group") + continue + app_label, model = str(scope_type).split(".") + ct = ContentType.objects.filter(app_label=app_label, model=model).first() + if not ct: + print( + f"VLAN Group '{params['name']}': ContentType for " + + f"app_label = '{app_label}' and model = '{model}' not found" + ) + continue + params["scope_id"] = ct.model_class().objects.get(**query).id vlan_group, created = VLANGroup.objects.get_or_create(**params) if created: diff --git a/startup_scripts/330_power_panels.py b/startup_scripts/330_power_panels.py index bfde18f..8542435 100644 --- a/startup_scripts/330_power_panels.py +++ b/startup_scripts/330_power_panels.py @@ -1,6 +1,6 @@ import sys -from dcim.models import PowerPanel, RackGroup, Site +from dcim.models import Location, PowerPanel, Site from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values power_panels = load_yaml("/opt/netbox/initializers/power_panels.yml") @@ -10,7 +10,7 @@ if power_panels is None: required_assocs = {"site": (Site, "name")} -optional_assocs = {"rack_group": (RackGroup, "name")} +optional_assocs = {"location": (Location, "name")} for params in power_panels: custom_field_data = pop_custom_fields(params)