Add function to load YAML files

This commit starts to remove some code redundancy from the startup
scripts for easier maintenance.
This commit is contained in:
Tobias Genannt 2020-02-05 15:31:01 +01:00
parent 3717b7469a
commit 50ade7bce1
30 changed files with 669 additions and 811 deletions

View File

@ -1,20 +1,15 @@
from django.contrib.auth.models import Permission, Group, User from django.contrib.auth.models import Permission, Group, User
from users.models import Token from users.models import Token
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/users.yml') users = load_yaml('/opt/netbox/initializers/users.yml')
if not file.is_file():
if users is None:
sys.exit() sys.exit()
with file.open('r') as stream: for username, user_details in users.items():
yaml=YAML(typ='safe')
users = yaml.load(stream)
if users is not None:
for username, user_details in users.items():
if not User.objects.filter(username=username): if not User.objects.filter(username=username):
user = User.objects.create_user( user = User.objects.create_user(
username = username, username = username,

View File

@ -1,18 +1,12 @@
from django.contrib.auth.models import Permission, Group, User from django.contrib.auth.models import Permission, Group, User
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/groups.yml') groups = load_yaml('/opt/netbox/initializers/groups.yml')
if not file.is_file(): if groups is None:
sys.exit() sys.exit()
with file.open('r') as stream: for groupname, group_details in groups.items():
yaml=YAML(typ='safe')
groups = yaml.load(stream)
if groups is not None:
for groupname, group_details in groups.items():
group, created = Group.objects.get_or_create(name=groupname) group, created = Group.objects.get_or_create(name=groupname)
if created: if created:

View File

@ -1,7 +1,6 @@
from extras.models import CustomField, CustomFieldChoice from extras.models import CustomField, CustomFieldChoice
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
def get_class_for_class_path(class_path): def get_class_for_class_path(class_path):
@ -13,16 +12,12 @@ def get_class_for_class_path(class_path):
clazz = getattr(module, class_name) clazz = getattr(module, class_name)
return ContentType.objects.get_for_model(clazz) return ContentType.objects.get_for_model(clazz)
file = Path('/opt/netbox/initializers/custom_fields.yml') customfields = load_yaml('/opt/netbox/initializers/custom_fields.yml')
if not file.is_file():
if customfields is None:
sys.exit() sys.exit()
with file.open('r') as stream: for cf_name, cf_details in customfields.items():
yaml = YAML(typ='safe')
customfields = yaml.load(stream)
if customfields is not None:
for cf_name, cf_details in customfields.items():
custom_field, created = CustomField.objects.get_or_create(name = cf_name) custom_field, created = CustomField.objects.get_or_create(name = cf_name)
if created: if created:

View File

@ -1,22 +1,17 @@
from dcim.models import Region from dcim.models import Region
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/regions.yml') regions = load_yaml('/opt/netbox/initializers/regions.yml')
if not file.is_file():
if regions is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml=YAML(typ='safe')
regions = yaml.load(stream)
optional_assocs = {
'parent': (Region, 'name') 'parent': (Region, 'name')
} }
if regions is not None: for params in regions:
for params in regions:
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():
if assoc in params: if assoc in params:

View File

@ -1,25 +1,20 @@
from dcim.models import Region, Site from dcim.models import Region, Site
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from tenancy.models import Tenant from tenancy.models import Tenant
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/sites.yml') sites = load_yaml('/opt/netbox/initializers/sites.yml')
if not file.is_file():
if sites is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
sites = yaml.load(stream)
optional_assocs = {
'region': (Region, 'name'), 'region': (Region, 'name'),
'tenant': (Tenant, 'name') 'tenant': (Tenant, 'name')
} }
if sites is not None: for params in sites:
for params in sites:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():

View File

@ -1,18 +1,13 @@
from dcim.models import Manufacturer from dcim.models import Manufacturer
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/manufacturers.yml') manufacturers = load_yaml('/opt/netbox/initializers/manufacturers.yml')
if not file.is_file():
if manufacturers is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in manufacturers:
yaml = YAML(typ='safe')
manufacturers = yaml.load(stream)
if manufacturers is not None:
for params in manufacturers:
manufacturer, created = Manufacturer.objects.get_or_create(**params) manufacturer, created = Manufacturer.objects.get_or_create(**params)
if created: if created:

View File

@ -1,29 +1,24 @@
from dcim.models import DeviceType, Manufacturer, Region from dcim.models import DeviceType, Manufacturer, Region
from tenancy.models import Tenant from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/device_types.yml') device_types = load_yaml('/opt/netbox/initializers/device_types.yml')
if not file.is_file():
if device_types is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
device_types = yaml.load(stream)
required_assocs = {
'manufacturer': (Manufacturer, 'name') 'manufacturer': (Manufacturer, 'name')
} }
optional_assocs = { optional_assocs = {
'region': (Region, 'name'), 'region': (Region, 'name'),
'tenant': (Tenant, 'name') 'tenant': (Tenant, 'name')
} }
if device_types is not None: for params in device_types:
for params in device_types:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,20 +1,15 @@
from dcim.models import RackRole from dcim.models import RackRole
from ruamel.yaml import YAML
from utilities.forms import COLOR_CHOICES from utilities.forms import COLOR_CHOICES
from pathlib import Path from startup_script_utils import load_yaml
import sys import sys
file = Path('/opt/netbox/initializers/rack_roles.yml') rack_roles = load_yaml('/opt/netbox/initializers/rack_roles.yml')
if not file.is_file():
if rack_roles is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in rack_roles:
yaml=YAML(typ='safe')
rack_roles = yaml.load(stream)
if rack_roles is not None:
for params in rack_roles:
if 'color' in params: if 'color' in params:
color = params.pop('color') color = params.pop('color')

View File

@ -1,23 +1,17 @@
from dcim.models import Site,RackGroup from dcim.models import Site,RackGroup
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/rack_groups.yml') rack_groups = load_yaml('/opt/netbox/initializers/rack_groups.yml')
if not file.is_file():
if rack_groups is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml=YAML(typ='safe')
rack_groups= yaml.load(stream)
required_assocs = {
'site': (Site, 'name') 'site': (Site, 'name')
} }
if rack_groups is not None: for params in rack_groups:
for params in rack_groups:
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():
model, field = details model, field = details

View File

@ -1,30 +1,25 @@
from dcim.models import Site, RackRole, Rack, RackGroup from dcim.models import Site, RackRole, Rack, RackGroup
from tenancy.models import Tenant from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/racks.yml') racks = load_yaml('/opt/netbox/initializers/racks.yml')
if not file.is_file():
if racks is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
racks = yaml.load(stream)
required_assocs = {
'site': (Site, 'name') 'site': (Site, 'name')
} }
optional_assocs = { optional_assocs = {
'role': (RackRole, 'name'), 'role': (RackRole, 'name'),
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'group': (RackGroup, 'name') 'group': (RackGroup, 'name')
} }
if racks is not None: for params in racks:
for params in racks:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,20 +1,14 @@
from dcim.models import DeviceRole from dcim.models import DeviceRole
from ruamel.yaml import YAML
from utilities.forms import COLOR_CHOICES from utilities.forms import COLOR_CHOICES
from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/device_roles.yml') device_roles = load_yaml('/opt/netbox/initializers/device_roles.yml')
if not file.is_file():
if device_roles is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in device_roles:
yaml=YAML(typ='safe')
device_roles = yaml.load(stream)
if device_roles is not None:
for params in device_roles:
if 'color' in params: if 'color' in params:
color = params.pop('color') color = params.pop('color')

View File

@ -1,23 +1,17 @@
from dcim.models import Manufacturer, Platform from dcim.models import Manufacturer, Platform
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/platforms.yml') platforms = load_yaml('/opt/netbox/initializers/platforms.yml')
if not file.is_file():
if platforms is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
platforms = yaml.load(stream)
optional_assocs = {
'manufacturer': (Manufacturer, 'name'), 'manufacturer': (Manufacturer, 'name'),
} }
if platforms is not None: for params in platforms:
for params in platforms:
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():
if assoc in params: if assoc in params:

View File

@ -1,18 +1,13 @@
from tenancy.models import TenantGroup from tenancy.models import TenantGroup
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/tenant_groups.yml') tenant_groups = load_yaml('/opt/netbox/initializers/tenant_groups.yml')
if not file.is_file():
if tenant_groups is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in tenant_groups:
yaml = YAML(typ='safe')
tenant_groups = yaml.load(stream)
if tenant_groups is not None:
for params in tenant_groups:
tenant_group, created = TenantGroup.objects.get_or_create(**params) tenant_group, created = TenantGroup.objects.get_or_create(**params)
if created: if created:

View File

@ -1,24 +1,18 @@
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/tenants.yml') tenants = load_yaml('/opt/netbox/initializers/tenants.yml')
if not file.is_file():
if tenants is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
tenants = yaml.load(stream)
optional_assocs = {
'group': (TenantGroup, 'name') 'group': (TenantGroup, 'name')
} }
if tenants is not None: for params in tenants:
for params in tenants:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():

View File

@ -3,36 +3,30 @@ from ipam.models import IPAddress
from virtualization.models import Cluster from virtualization.models import Cluster
from tenancy.models import Tenant from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/devices.yml') devices = load_yaml('/opt/netbox/initializers/devices.yml')
if not file.is_file():
if devices is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
devices = yaml.load(stream)
required_assocs = {
'device_role': (DeviceRole, 'name'), 'device_role': (DeviceRole, 'name'),
'device_type': (DeviceType, 'model'), 'device_type': (DeviceType, 'model'),
'site': (Site, 'name') 'site': (Site, 'name')
} }
optional_assocs = { optional_assocs = {
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'platform': (Platform, 'name'), 'platform': (Platform, 'name'),
'rack': (Rack, 'name'), 'rack': (Rack, 'name'),
'cluster': (Cluster, 'name'), 'cluster': (Cluster, 'name'),
'primary_ip4': (IPAddress, 'address'), 'primary_ip4': (IPAddress, 'address'),
'primary_ip6': (IPAddress, 'address') 'primary_ip6': (IPAddress, 'address')
} }
if devices is not None: for params in devices:
for params in devices:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,18 +1,13 @@
from virtualization.models import ClusterType from virtualization.models import ClusterType
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/cluster_types.yml') cluster_types = load_yaml('/opt/netbox/initializers/cluster_types.yml')
if not file.is_file():
if cluster_types is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in cluster_types:
yaml = YAML(typ='safe')
cluster_types = yaml.load(stream)
if cluster_types is not None:
for params in cluster_types:
cluster_type, created = ClusterType.objects.get_or_create(**params) cluster_type, created = ClusterType.objects.get_or_create(**params)
if created: if created:

View File

@ -1,18 +1,13 @@
from ipam.models import RIR from ipam.models import RIR
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/rirs.yml') rirs = load_yaml('/opt/netbox/initializers/rirs.yml')
if not file.is_file():
if rirs is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in rirs:
yaml = YAML(typ='safe')
rirs = yaml.load(stream)
if rirs is not None:
for params in rirs:
rir, created = RIR.objects.get_or_create(**params) rir, created = RIR.objects.get_or_create(**params)
if created: if created:

View File

@ -1,25 +1,21 @@
from ipam.models import Aggregate, RIR from ipam.models import Aggregate, RIR
from ruamel.yaml import YAML
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from netaddr import IPNetwork from netaddr import IPNetwork
from pathlib import Path from startup_script_utils import load_yaml
import sys import sys
file = Path('/opt/netbox/initializers/aggregates.yml') aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml')
if not file.is_file():
if aggregates is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
aggregates = yaml.load(stream)
required_assocs = {
'rir': (RIR, 'name') 'rir': (RIR, 'name')
} }
if aggregates is not None: for params in aggregates:
for params in aggregates:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
params['prefix'] = IPNetwork(params['prefix']) params['prefix'] = IPNetwork(params['prefix'])

View File

@ -1,30 +1,24 @@
from dcim.models import Site from dcim.models import Site
from virtualization.models import Cluster, ClusterType, ClusterGroup from virtualization.models import Cluster, ClusterType, ClusterGroup
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/clusters.yml') clusters = load_yaml('/opt/netbox/initializers/clusters.yml')
if not file.is_file():
if clusters is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
clusters = yaml.load(stream)
required_assocs = {
'type': (ClusterType, 'name') 'type': (ClusterType, 'name')
} }
optional_assocs = { optional_assocs = {
'site': (Site, 'name'), 'site': (Site, 'name'),
'group': (ClusterGroup, 'name') 'group': (ClusterGroup, 'name')
} }
if clusters is not None: for params in clusters:
for params in clusters:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,25 +1,21 @@
from ipam.models import VRF from ipam.models import VRF
from tenancy.models import Tenant from tenancy.models import Tenant
from ruamel.yaml import YAML
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from pathlib import Path from startup_script_utils import load_yaml
import sys import sys
file = Path('/opt/netbox/initializers/vrfs.yml') vrfs = load_yaml('/opt/netbox/initializers/vrfs.yml')
if not file.is_file():
if vrfs is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
vrfs = yaml.load(stream)
optional_assocs = {
'tenant': (Tenant, 'name') 'tenant': (Tenant, 'name')
} }
if vrfs is not None: for params in vrfs:
for params in vrfs:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():

View File

@ -1,18 +1,13 @@
from ipam.models import Role from ipam.models import Role
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/prefix_vlan_roles.yml') roles = load_yaml('/opt/netbox/initializers/prefix_vlan_roles.yml')
if not file.is_file():
if roles is None:
sys.exit() sys.exit()
with file.open('r') as stream: for params in roles:
yaml = YAML(typ='safe')
roles = yaml.load(stream)
if roles is not None:
for params in roles:
role, created = Role.objects.get_or_create(**params) role, created = Role.objects.get_or_create(**params)
if created: if created:

View File

@ -1,25 +1,19 @@
from dcim.models import Site from dcim.models import Site
from ipam.models import VLANGroup from ipam.models import VLANGroup
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/vlan_groups.yml') vlan_groups = load_yaml('/opt/netbox/initializers/vlan_groups.yml')
if not file.is_file():
if vlan_groups is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
vlan_groups = yaml.load(stream)
optional_assocs = {
'site': (Site, 'name') 'site': (Site, 'name')
} }
if vlan_groups is not None: for params in vlan_groups:
for params in vlan_groups:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():

View File

@ -2,29 +2,23 @@ from dcim.models import Site
from ipam.models import VLAN, VLANGroup, Role from ipam.models import VLAN, VLANGroup, Role
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/vlans.yml') vlans = load_yaml('/opt/netbox/initializers/vlans.yml')
if not file.is_file():
if vlans is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
vlans = yaml.load(stream)
optional_assocs = {
'site': (Site, 'name'), 'site': (Site, 'name'),
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'tenant_group': (TenantGroup, 'name'), 'tenant_group': (TenantGroup, 'name'),
'group': (VLANGroup, 'name'), 'group': (VLANGroup, 'name'),
'role': (Role, 'name') 'role': (Role, 'name')
} }
if vlans is not None: for params in vlans:
for params in vlans:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items(): for assoc, details in optional_assocs.items():

View File

@ -2,31 +2,25 @@ from dcim.models import Site
from ipam.models import Prefix, VLAN, Role, VRF from ipam.models import Prefix, VLAN, Role, VRF
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
from netaddr import IPNetwork from netaddr import IPNetwork
from pathlib import Path from startup_script_utils import load_yaml
import sys import sys
file = Path('/opt/netbox/initializers/prefixes.yml') prefixes = load_yaml('/opt/netbox/initializers/prefixes.yml')
if not file.is_file():
if prefixes is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
prefixes = yaml.load(stream)
optional_assocs = {
'site': (Site, 'name'), 'site': (Site, 'name'),
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'tenant_group': (TenantGroup, 'name'), 'tenant_group': (TenantGroup, 'name'),
'vlan': (VLAN, 'name'), 'vlan': (VLAN, 'name'),
'role': (Role, 'name'), 'role': (Role, 'name'),
'vrf': (VRF, 'name') 'vrf': (VRF, 'name')
} }
if prefixes is not None: for params in prefixes:
for params in prefixes:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
params['prefix'] = IPNetwork(params['prefix']) params['prefix'] = IPNetwork(params['prefix'])
@ -34,7 +28,6 @@ with file.open('r') as stream:
if assoc in params: if assoc in params:
model, field = details model, field = details
query = { field: params.pop(assoc) } query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query) params[assoc] = model.objects.get(**query)
prefix, created = Prefix.objects.get_or_create(**params) prefix, created = Prefix.objects.get_or_create(**params)
@ -48,7 +41,6 @@ with file.open('r') as stream:
obj=prefix, obj=prefix,
value=cf_value value=cf_value
) )
prefix.custom_field_values.add(custom_field_value) prefix.custom_field_values.add(custom_field_value)
print("📌 Created Prefix", prefix.prefix) print("📌 Created Prefix", prefix.prefix)

View File

@ -2,31 +2,25 @@ from dcim.models import Site, Platform, DeviceRole
from virtualization.models import Cluster, VirtualMachine from virtualization.models import Cluster, VirtualMachine
from tenancy.models import Tenant from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/virtual_machines.yml') virtual_machines = load_yaml('/opt/netbox/initializers/virtual_machines.yml')
if not file.is_file():
if virtual_machines is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
virtual_machines = yaml.load(stream)
required_assocs = {
'cluster': (Cluster, 'name') 'cluster': (Cluster, 'name')
} }
optional_assocs = { optional_assocs = {
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'platform': (Platform, 'name'), 'platform': (Platform, 'name'),
'role': (DeviceRole, 'name') 'role': (DeviceRole, 'name')
} }
if virtual_machines is not None: for params in virtual_machines:
for params in virtual_machines:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,25 +1,19 @@
from dcim.models import Interface from dcim.models import Interface
from virtualization.models import VirtualMachine from virtualization.models import VirtualMachine
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/virtualization_interfaces.yml') interfaces = load_yaml('/opt/netbox/initializers/virtualization_interfaces.yml')
if not file.is_file():
if interfaces is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
interfaces = yaml.load(stream)
required_assocs = {
'virtual_machine': (VirtualMachine, 'name') 'virtual_machine': (VirtualMachine, 'name')
} }
if interfaces is not None: for params in interfaces:
for params in interfaces:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -1,24 +1,18 @@
from dcim.models import Interface, Device from dcim.models import Interface, Device
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML from startup_script_utils import load_yaml
from pathlib import Path
import sys import sys
file = Path('/opt/netbox/initializers/dcim_interfaces.yml') interfaces= load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')
if not file.is_file():
if interfaces is None:
sys.exit() sys.exit()
with file.open('r') as stream: required_assocs = {
yaml = YAML(typ='safe')
interfaces = yaml.load(stream)
required_assocs = {
'device': (Device, 'name') 'device': (Device, 'name')
} }
if interfaces is not None: for params in interfaces:
for params in interfaces:
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)
for assoc, details in required_assocs.items(): for assoc, details in required_assocs.items():

View File

@ -3,28 +3,23 @@ from dcim.models import Device, Interface
from virtualization.models import VirtualMachine from virtualization.models import VirtualMachine
from tenancy.models import Tenant from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
from netaddr import IPNetwork from netaddr import IPNetwork
from pathlib import Path from startup_script_utils import load_yaml
import sys import sys
file = Path('/opt/netbox/initializers/ip_addresses.yml') ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
if not file.is_file():
if ip_addresses is None:
sys.exit() sys.exit()
with file.open('r') as stream: optional_assocs = {
yaml = YAML(typ='safe')
ip_addresses = yaml.load(stream)
optional_assocs = {
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'vrf': (VRF, 'name'), 'vrf': (VRF, 'name'),
'interface': (Interface, 'name') 'interface': (Interface, 'name')
} }
if ip_addresses is not None: for params in ip_addresses:
for params in ip_addresses:
vm = params.pop('virtual_machine', None) vm = params.pop('virtual_machine', None)
device = params.pop('device', None) device = params.pop('device', None)
custom_fields = params.pop('custom_fields', None) custom_fields = params.pop('custom_fields', None)

View File

@ -0,0 +1 @@
from .load_yaml import load_yaml

View File

@ -0,0 +1,10 @@
from ruamel.yaml import YAML
from pathlib import Path
def load_yaml(yaml_file: str):
yf = Path(yaml_file)
if not yf.is_file():
return None
with yf.open("r") as stream:
yaml = YAML(typ="safe")
return yaml.load(stream)