Change logic to prevent sys.exit from stopping script processing
This commit is contained in:
parent
5769684c98
commit
5292afaae0
28 changed files with 544 additions and 572 deletions
|
@ -5,19 +5,18 @@ from startup_script_utils import load_yaml, set_permissions
|
||||||
from users.models import Token
|
from users.models import Token
|
||||||
|
|
||||||
users = load_yaml('/opt/netbox/initializers/users.yml')
|
users = load_yaml('/opt/netbox/initializers/users.yml')
|
||||||
if users is None:
|
if not users is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for username, user_details in users.items():
|
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,
|
||||||
password = user_details.get('password', 0) or User.objects.make_random_password)
|
password = user_details.get('password', 0) or User.objects.make_random_password)
|
||||||
|
|
||||||
print("👤 Created user",username)
|
print("👤 Created user",username)
|
||||||
|
|
||||||
if user_details.get('api_token', 0):
|
if user_details.get('api_token', 0):
|
||||||
Token.objects.create(user=user, key=user_details['api_token'])
|
Token.objects.create(user=user, key=user_details['api_token'])
|
||||||
|
|
||||||
yaml_permissions = user_details.get('permissions', [])
|
yaml_permissions = user_details.get('permissions', [])
|
||||||
set_permissions(user.user_permissions, yaml_permissions)
|
set_permissions(user.user_permissions, yaml_permissions)
|
||||||
|
|
|
@ -4,20 +4,19 @@ from django.contrib.auth.models import Group, User
|
||||||
from startup_script_utils import load_yaml, set_permissions
|
from startup_script_utils import load_yaml, set_permissions
|
||||||
|
|
||||||
groups = load_yaml('/opt/netbox/initializers/groups.yml')
|
groups = load_yaml('/opt/netbox/initializers/groups.yml')
|
||||||
if groups is None:
|
if not groups is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for groupname, group_details in groups.items():
|
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:
|
||||||
print("👥 Created group", groupname)
|
print("👥 Created group", groupname)
|
||||||
|
|
||||||
for username in group_details.get('users', []):
|
for username in group_details.get('users', []):
|
||||||
user = User.objects.get(username=username)
|
user = User.objects.get(username=username)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
user.groups.add(group)
|
user.groups.add(group)
|
||||||
|
|
||||||
yaml_permissions = group_details.get('permissions', [])
|
yaml_permissions = group_details.get('permissions', [])
|
||||||
set_permissions(group.permissions, yaml_permissions)
|
set_permissions(group.permissions, yaml_permissions)
|
||||||
|
|
|
@ -14,41 +14,40 @@ def get_class_for_class_path(class_path):
|
||||||
|
|
||||||
customfields = load_yaml('/opt/netbox/initializers/custom_fields.yml')
|
customfields = load_yaml('/opt/netbox/initializers/custom_fields.yml')
|
||||||
|
|
||||||
if customfields is None:
|
if not customfields is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for cf_name, cf_details in customfields.items():
|
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:
|
||||||
if cf_details.get('default', 0):
|
if cf_details.get('default', 0):
|
||||||
custom_field.default = cf_details['default']
|
custom_field.default = cf_details['default']
|
||||||
|
|
||||||
if cf_details.get('description', 0):
|
if cf_details.get('description', 0):
|
||||||
custom_field.description = cf_details['description']
|
custom_field.description = cf_details['description']
|
||||||
|
|
||||||
if cf_details.get('label', 0):
|
if cf_details.get('label', 0):
|
||||||
custom_field.label = cf_details['label']
|
custom_field.label = cf_details['label']
|
||||||
|
|
||||||
for object_type in cf_details.get('on_objects', []):
|
for object_type in cf_details.get('on_objects', []):
|
||||||
custom_field.obj_type.add(get_class_for_class_path(object_type))
|
custom_field.obj_type.add(get_class_for_class_path(object_type))
|
||||||
|
|
||||||
if cf_details.get('required', 0):
|
if cf_details.get('required', 0):
|
||||||
custom_field.required = cf_details['required']
|
custom_field.required = cf_details['required']
|
||||||
|
|
||||||
if cf_details.get('type', 0):
|
if cf_details.get('type', 0):
|
||||||
custom_field.type = cf_details['type']
|
custom_field.type = cf_details['type']
|
||||||
|
|
||||||
if cf_details.get('weight', 0):
|
if cf_details.get('weight', 0):
|
||||||
custom_field.weight = cf_details['weight']
|
custom_field.weight = cf_details['weight']
|
||||||
|
|
||||||
custom_field.save()
|
custom_field.save()
|
||||||
|
|
||||||
for idx, choice_details in enumerate(cf_details.get('choices', [])):
|
for idx, choice_details in enumerate(cf_details.get('choices', [])):
|
||||||
choice, _ = CustomFieldChoice.objects.get_or_create(
|
choice, _ = CustomFieldChoice.objects.get_or_create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
value=choice_details['value'],
|
value=choice_details['value'],
|
||||||
defaults={'weight': idx * 10}
|
defaults={'weight': idx * 10}
|
||||||
)
|
)
|
||||||
|
|
||||||
print("🔧 Created custom field", cf_name)
|
print("🔧 Created custom field", cf_name)
|
||||||
|
|
|
@ -4,23 +4,22 @@ import sys
|
||||||
|
|
||||||
regions = load_yaml('/opt/netbox/initializers/regions.yml')
|
regions = load_yaml('/opt/netbox/initializers/regions.yml')
|
||||||
|
|
||||||
if regions is None:
|
if not regions is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'parent': (Region, 'name')
|
'parent': (Region, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
||||||
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)
|
||||||
|
|
||||||
region, created = Region.objects.get_or_create(**params)
|
region, created = Region.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🌐 Created region", region.name)
|
print("🌐 Created region", region.name)
|
||||||
|
|
|
@ -6,36 +6,35 @@ import sys
|
||||||
|
|
||||||
sites = load_yaml('/opt/netbox/initializers/sites.yml')
|
sites = load_yaml('/opt/netbox/initializers/sites.yml')
|
||||||
|
|
||||||
if sites is None:
|
if not sites is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'region': (Region, 'name'),
|
'region': (Region, 'name'),
|
||||||
'tenant': (Tenant, 'name')
|
'tenant': (Tenant, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
site, created = Site.objects.get_or_create(**params)
|
site, created = Site.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=site,
|
obj=site,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
site.custom_field_values.add(custom_field_value)
|
site.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("📍 Created site", site.name)
|
print("📍 Created site", site.name)
|
||||||
|
|
|
@ -4,11 +4,10 @@ import sys
|
||||||
|
|
||||||
manufacturers = load_yaml('/opt/netbox/initializers/manufacturers.yml')
|
manufacturers = load_yaml('/opt/netbox/initializers/manufacturers.yml')
|
||||||
|
|
||||||
if manufacturers is None:
|
if not manufacturers is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in manufacturers:
|
for params in manufacturers:
|
||||||
manufacturer, created = Manufacturer.objects.get_or_create(**params)
|
manufacturer, created = Manufacturer.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🏭 Created Manufacturer", manufacturer.name)
|
print("🏭 Created Manufacturer", manufacturer.name)
|
||||||
|
|
|
@ -6,46 +6,45 @@ import sys
|
||||||
|
|
||||||
device_types = load_yaml('/opt/netbox/initializers/device_types.yml')
|
device_types = load_yaml('/opt/netbox/initializers/device_types.yml')
|
||||||
|
|
||||||
if device_types is None:
|
if not device_types is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'manufacturer': (Manufacturer, 'name')
|
'manufacturer': (Manufacturer, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'region': (Region, 'name'),
|
'region': (Region, 'name'),
|
||||||
'tenant': (Tenant, 'name')
|
'tenant': (Tenant, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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
|
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)
|
||||||
|
|
||||||
device_type, created = DeviceType.objects.get_or_create(**params)
|
for assoc, details in optional_assocs.items():
|
||||||
|
if assoc in params:
|
||||||
|
model, field = details
|
||||||
|
query = { field: params.pop(assoc) }
|
||||||
|
|
||||||
if created:
|
params[assoc] = model.objects.get(**query)
|
||||||
if custom_fields is not None:
|
|
||||||
for cf_name, cf_value in custom_fields.items():
|
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
|
||||||
field=custom_field,
|
|
||||||
obj=device_type,
|
|
||||||
value=cf_value
|
|
||||||
)
|
|
||||||
|
|
||||||
device_type.custom_field_values.add(custom_field_value)
|
device_type, created = DeviceType.objects.get_or_create(**params)
|
||||||
|
|
||||||
print("🔡 Created device type", device_type.manufacturer, device_type.model)
|
if created:
|
||||||
|
if custom_fields is not None:
|
||||||
|
for cf_name, cf_value in custom_fields.items():
|
||||||
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
|
field=custom_field,
|
||||||
|
obj=device_type,
|
||||||
|
value=cf_value
|
||||||
|
)
|
||||||
|
|
||||||
|
device_type.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
|
print("🔡 Created device type", device_type.manufacturer, device_type.model)
|
||||||
|
|
|
@ -6,18 +6,17 @@ import sys
|
||||||
|
|
||||||
rack_roles = load_yaml('/opt/netbox/initializers/rack_roles.yml')
|
rack_roles = load_yaml('/opt/netbox/initializers/rack_roles.yml')
|
||||||
|
|
||||||
if rack_roles is None:
|
if not rack_roles is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in rack_roles:
|
for params in rack_roles:
|
||||||
if 'color' in params:
|
if 'color' in params:
|
||||||
color = params.pop('color')
|
color = params.pop('color')
|
||||||
|
|
||||||
for color_tpl in COLOR_CHOICES:
|
for color_tpl in COLOR_CHOICES:
|
||||||
if color in color_tpl:
|
if color in color_tpl:
|
||||||
params['color'] = color_tpl[0]
|
params['color'] = color_tpl[0]
|
||||||
|
|
||||||
rack_role, created = RackRole.objects.get_or_create(**params)
|
rack_role, created = RackRole.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🎨 Created rack role", rack_role.name)
|
print("🎨 Created rack role", rack_role.name)
|
||||||
|
|
|
@ -4,22 +4,21 @@ import sys
|
||||||
|
|
||||||
rack_groups = load_yaml('/opt/netbox/initializers/rack_groups.yml')
|
rack_groups = load_yaml('/opt/netbox/initializers/rack_groups.yml')
|
||||||
|
|
||||||
if rack_groups is None:
|
if not rack_groups is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'site': (Site, 'name')
|
'site': (Site, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
query = { field: params.pop(assoc) }
|
query = { field: params.pop(assoc) }
|
||||||
params[assoc] = model.objects.get(**query)
|
params[assoc] = model.objects.get(**query)
|
||||||
|
|
||||||
rack_group, created = RackGroup.objects.get_or_create(**params)
|
rack_group, created = RackGroup.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🎨 Created rack group", rack_group.name)
|
print("🎨 Created rack group", rack_group.name)
|
||||||
|
|
||||||
|
|
|
@ -6,47 +6,46 @@ import sys
|
||||||
|
|
||||||
racks = load_yaml('/opt/netbox/initializers/racks.yml')
|
racks = load_yaml('/opt/netbox/initializers/racks.yml')
|
||||||
|
|
||||||
if racks is None:
|
if not racks is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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
|
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)
|
||||||
|
|
||||||
rack, created = Rack.objects.get_or_create(**params)
|
for assoc, details in optional_assocs.items():
|
||||||
|
if assoc in params:
|
||||||
|
model, field = details
|
||||||
|
query = { field: params.pop(assoc) }
|
||||||
|
|
||||||
if created:
|
params[assoc] = model.objects.get(**query)
|
||||||
if custom_fields is not None:
|
|
||||||
for cf_name, cf_value in custom_fields.items():
|
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
|
||||||
field=custom_field,
|
|
||||||
obj=rack,
|
|
||||||
value=cf_value
|
|
||||||
)
|
|
||||||
|
|
||||||
rack.custom_field_values.add(custom_field_value)
|
rack, created = Rack.objects.get_or_create(**params)
|
||||||
|
|
||||||
print("🔳 Created rack", rack.site, rack.name)
|
if created:
|
||||||
|
if custom_fields is not None:
|
||||||
|
for cf_name, cf_value in custom_fields.items():
|
||||||
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
|
field=custom_field,
|
||||||
|
obj=rack,
|
||||||
|
value=cf_value
|
||||||
|
)
|
||||||
|
|
||||||
|
rack.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
|
print("🔳 Created rack", rack.site, rack.name)
|
||||||
|
|
|
@ -5,19 +5,18 @@ import sys
|
||||||
|
|
||||||
device_roles = load_yaml('/opt/netbox/initializers/device_roles.yml')
|
device_roles = load_yaml('/opt/netbox/initializers/device_roles.yml')
|
||||||
|
|
||||||
if device_roles is None:
|
if not device_roles is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in device_roles:
|
for params in device_roles:
|
||||||
|
|
||||||
if 'color' in params:
|
if 'color' in params:
|
||||||
color = params.pop('color')
|
color = params.pop('color')
|
||||||
|
|
||||||
for color_tpl in COLOR_CHOICES:
|
for color_tpl in COLOR_CHOICES:
|
||||||
if color in color_tpl:
|
if color in color_tpl:
|
||||||
params['color'] = color_tpl[0]
|
params['color'] = color_tpl[0]
|
||||||
|
|
||||||
device_role, created = DeviceRole.objects.get_or_create(**params)
|
device_role, created = DeviceRole.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🎨 Created device role", device_role.name)
|
print("🎨 Created device role", device_role.name)
|
||||||
|
|
|
@ -4,23 +4,22 @@ import sys
|
||||||
|
|
||||||
platforms = load_yaml('/opt/netbox/initializers/platforms.yml')
|
platforms = load_yaml('/opt/netbox/initializers/platforms.yml')
|
||||||
|
|
||||||
if platforms is None:
|
if not platforms is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'manufacturer': (Manufacturer, 'name'),
|
'manufacturer': (Manufacturer, 'name'),
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
||||||
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)
|
||||||
|
|
||||||
platform, created = Platform.objects.get_or_create(**params)
|
platform, created = Platform.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("💾 Created platform", platform.name)
|
print("💾 Created platform", platform.name)
|
||||||
|
|
|
@ -4,11 +4,10 @@ import sys
|
||||||
|
|
||||||
tenant_groups = load_yaml('/opt/netbox/initializers/tenant_groups.yml')
|
tenant_groups = load_yaml('/opt/netbox/initializers/tenant_groups.yml')
|
||||||
|
|
||||||
if tenant_groups is None:
|
if not tenant_groups is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in tenant_groups:
|
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:
|
||||||
print("🔳 Created Tenant Group", tenant_group.name)
|
print("🔳 Created Tenant Group", tenant_group.name)
|
||||||
|
|
|
@ -5,35 +5,34 @@ import sys
|
||||||
|
|
||||||
tenants = load_yaml('/opt/netbox/initializers/tenants.yml')
|
tenants = load_yaml('/opt/netbox/initializers/tenants.yml')
|
||||||
|
|
||||||
if tenants is None:
|
if not tenants is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'group': (TenantGroup, 'name')
|
'group': (TenantGroup, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
tenant, created = Tenant.objects.get_or_create(**params)
|
tenant, created = Tenant.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=tenant,
|
obj=tenant,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
tenant.custom_field_values.add(custom_field_value)
|
tenant.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("👩💻 Created Tenant", tenant.name)
|
print("👩💻 Created Tenant", tenant.name)
|
||||||
|
|
|
@ -8,52 +8,51 @@ import sys
|
||||||
|
|
||||||
devices = load_yaml('/opt/netbox/initializers/devices.yml')
|
devices = load_yaml('/opt/netbox/initializers/devices.yml')
|
||||||
|
|
||||||
if devices is None:
|
if not devices is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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
|
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)
|
||||||
|
|
||||||
device, created = Device.objects.get_or_create(**params)
|
for assoc, details in optional_assocs.items():
|
||||||
|
if assoc in params:
|
||||||
|
model, field = details
|
||||||
|
query = { field: params.pop(assoc) }
|
||||||
|
|
||||||
if created:
|
params[assoc] = model.objects.get(**query)
|
||||||
if custom_fields is not None:
|
|
||||||
for cf_name, cf_value in custom_fields.items():
|
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
|
||||||
field=custom_field,
|
|
||||||
obj=device,
|
|
||||||
value=cf_value
|
|
||||||
)
|
|
||||||
|
|
||||||
device.custom_field_values.add(custom_field_value)
|
device, created = Device.objects.get_or_create(**params)
|
||||||
|
|
||||||
print("🖥️ Created device", device.name)
|
if created:
|
||||||
|
if custom_fields is not None:
|
||||||
|
for cf_name, cf_value in custom_fields.items():
|
||||||
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
|
field=custom_field,
|
||||||
|
obj=device,
|
||||||
|
value=cf_value
|
||||||
|
)
|
||||||
|
|
||||||
|
device.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
|
print("🖥️ Created device", device.name)
|
||||||
|
|
|
@ -4,11 +4,10 @@ import sys
|
||||||
|
|
||||||
cluster_types = load_yaml('/opt/netbox/initializers/cluster_types.yml')
|
cluster_types = load_yaml('/opt/netbox/initializers/cluster_types.yml')
|
||||||
|
|
||||||
if cluster_types is None:
|
if not cluster_types is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in cluster_types:
|
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:
|
||||||
print("🧰 Created Cluster Type", cluster_type.name)
|
print("🧰 Created Cluster Type", cluster_type.name)
|
||||||
|
|
|
@ -4,11 +4,10 @@ import sys
|
||||||
|
|
||||||
rirs = load_yaml('/opt/netbox/initializers/rirs.yml')
|
rirs = load_yaml('/opt/netbox/initializers/rirs.yml')
|
||||||
|
|
||||||
if rirs is None:
|
if not rirs is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in rirs:
|
for params in rirs:
|
||||||
rir, created = RIR.objects.get_or_create(**params)
|
rir, created = RIR.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🗺️ Created RIR", rir.name)
|
print("🗺️ Created RIR", rir.name)
|
||||||
|
|
|
@ -8,35 +8,34 @@ import sys
|
||||||
|
|
||||||
aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml')
|
aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml')
|
||||||
|
|
||||||
if aggregates is None:
|
if not aggregates is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'rir': (RIR, 'name')
|
'rir': (RIR, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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'])
|
||||||
|
|
||||||
for assoc, details in required_assocs.items():
|
for assoc, details in required_assocs.items():
|
||||||
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)
|
||||||
|
|
||||||
aggregate, created = Aggregate.objects.get_or_create(**params)
|
aggregate, created = Aggregate.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=aggregate,
|
obj=aggregate,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
aggregate.custom_field_values.add(custom_field_value)
|
aggregate.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🗞️ Created Aggregate", aggregate.prefix)
|
print("🗞️ Created Aggregate", aggregate.prefix)
|
||||||
|
|
|
@ -6,46 +6,45 @@ import sys
|
||||||
|
|
||||||
clusters = load_yaml('/opt/netbox/initializers/clusters.yml')
|
clusters = load_yaml('/opt/netbox/initializers/clusters.yml')
|
||||||
|
|
||||||
if clusters is None:
|
if not clusters is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'type': (ClusterType, 'name')
|
'type': (ClusterType, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'site': (Site, 'name'),
|
'site': (Site, 'name'),
|
||||||
'group': (ClusterGroup, 'name')
|
'group': (ClusterGroup, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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
|
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)
|
||||||
|
|
||||||
cluster, created = Cluster.objects.get_or_create(**params)
|
for assoc, details in optional_assocs.items():
|
||||||
|
if assoc in params:
|
||||||
|
model, field = details
|
||||||
|
query = { field: params.pop(assoc) }
|
||||||
|
|
||||||
if created:
|
params[assoc] = model.objects.get(**query)
|
||||||
if custom_fields is not None:
|
|
||||||
for cf_name, cf_value in custom_fields.items():
|
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
|
||||||
field=custom_field,
|
|
||||||
obj=cluster,
|
|
||||||
value=cf_value
|
|
||||||
)
|
|
||||||
|
|
||||||
cluster.custom_field_values.add(custom_field_value)
|
cluster, created = Cluster.objects.get_or_create(**params)
|
||||||
|
|
||||||
print("🗄️ Created cluster", cluster.name)
|
if created:
|
||||||
|
if custom_fields is not None:
|
||||||
|
for cf_name, cf_value in custom_fields.items():
|
||||||
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
|
field=custom_field,
|
||||||
|
obj=cluster,
|
||||||
|
value=cf_value
|
||||||
|
)
|
||||||
|
|
||||||
|
cluster.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
|
print("🗄️ Created cluster", cluster.name)
|
||||||
|
|
|
@ -8,35 +8,34 @@ import sys
|
||||||
|
|
||||||
vrfs = load_yaml('/opt/netbox/initializers/vrfs.yml')
|
vrfs = load_yaml('/opt/netbox/initializers/vrfs.yml')
|
||||||
|
|
||||||
if vrfs is None:
|
if not vrfs is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'tenant': (Tenant, 'name')
|
'tenant': (Tenant, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
vrf, created = VRF.objects.get_or_create(**params)
|
vrf, created = VRF.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=vrf,
|
obj=vrf,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
vrf.custom_field_values.add(custom_field_value)
|
vrf.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("📦 Created VRF", vrf.name)
|
print("📦 Created VRF", vrf.name)
|
||||||
|
|
|
@ -4,11 +4,10 @@ import sys
|
||||||
|
|
||||||
roles = load_yaml('/opt/netbox/initializers/prefix_vlan_roles.yml')
|
roles = load_yaml('/opt/netbox/initializers/prefix_vlan_roles.yml')
|
||||||
|
|
||||||
if roles is None:
|
if not roles is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
for params in roles:
|
for params in roles:
|
||||||
role, created = Role.objects.get_or_create(**params)
|
role, created = Role.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("⛹️ Created Prefix/VLAN Role", role.name)
|
print("⛹️ Created Prefix/VLAN Role", role.name)
|
||||||
|
|
|
@ -6,35 +6,34 @@ import sys
|
||||||
|
|
||||||
vlan_groups = load_yaml('/opt/netbox/initializers/vlan_groups.yml')
|
vlan_groups = load_yaml('/opt/netbox/initializers/vlan_groups.yml')
|
||||||
|
|
||||||
if vlan_groups is None:
|
if not vlan_groups is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'site': (Site, 'name')
|
'site': (Site, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
vlan_group, created = VLANGroup.objects.get_or_create(**params)
|
vlan_group, created = VLANGroup.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=vlan_group,
|
obj=vlan_group,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
vlan_group.custom_field_values.add(custom_field_value)
|
vlan_group.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🏘️ Created VLAN Group", vlan_group.name)
|
print("🏘️ Created VLAN Group", vlan_group.name)
|
||||||
|
|
|
@ -7,39 +7,38 @@ import sys
|
||||||
|
|
||||||
vlans = load_yaml('/opt/netbox/initializers/vlans.yml')
|
vlans = load_yaml('/opt/netbox/initializers/vlans.yml')
|
||||||
|
|
||||||
if vlans is None:
|
if not vlans is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
vlan, created = VLAN.objects.get_or_create(**params)
|
vlan, created = VLAN.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=vlan,
|
obj=vlan,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
vlan.custom_field_values.add(custom_field_value)
|
vlan.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🏠 Created VLAN", vlan.name)
|
print("🏠 Created VLAN", vlan.name)
|
||||||
|
|
|
@ -8,39 +8,38 @@ import sys
|
||||||
|
|
||||||
prefixes = load_yaml('/opt/netbox/initializers/prefixes.yml')
|
prefixes = load_yaml('/opt/netbox/initializers/prefixes.yml')
|
||||||
|
|
||||||
if prefixes is None:
|
if not prefixes is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
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'])
|
||||||
|
|
||||||
for assoc, details in optional_assocs.items():
|
for assoc, details in optional_assocs.items():
|
||||||
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)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
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)
|
||||||
|
|
|
@ -7,47 +7,46 @@ import sys
|
||||||
|
|
||||||
virtual_machines = load_yaml('/opt/netbox/initializers/virtual_machines.yml')
|
virtual_machines = load_yaml('/opt/netbox/initializers/virtual_machines.yml')
|
||||||
|
|
||||||
if virtual_machines is None:
|
if not virtual_machines is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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
|
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)
|
||||||
|
|
||||||
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
|
for assoc, details in optional_assocs.items():
|
||||||
|
if assoc in params:
|
||||||
|
model, field = details
|
||||||
|
query = { field: params.pop(assoc) }
|
||||||
|
|
||||||
if created:
|
params[assoc] = model.objects.get(**query)
|
||||||
if custom_fields is not None:
|
|
||||||
for cf_name, cf_value in custom_fields.items():
|
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
|
||||||
field=custom_field,
|
|
||||||
obj=virtual_machine,
|
|
||||||
value=cf_value
|
|
||||||
)
|
|
||||||
|
|
||||||
virtual_machine.custom_field_values.add(custom_field_value)
|
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
|
||||||
|
|
||||||
print("🖥️ Created virtual machine", virtual_machine.name)
|
if created:
|
||||||
|
if custom_fields is not None:
|
||||||
|
for cf_name, cf_value in custom_fields.items():
|
||||||
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
|
field=custom_field,
|
||||||
|
obj=virtual_machine,
|
||||||
|
value=cf_value
|
||||||
|
)
|
||||||
|
|
||||||
|
virtual_machine.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
|
print("🖥️ Created virtual machine", virtual_machine.name)
|
||||||
|
|
|
@ -6,34 +6,33 @@ import sys
|
||||||
|
|
||||||
interfaces = load_yaml('/opt/netbox/initializers/virtualization_interfaces.yml')
|
interfaces = load_yaml('/opt/netbox/initializers/virtualization_interfaces.yml')
|
||||||
|
|
||||||
if interfaces is None:
|
if not interfaces is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'virtual_machine': (VirtualMachine, 'name')
|
'virtual_machine': (VirtualMachine, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
interface, created = Interface.objects.get_or_create(**params)
|
interface, created = Interface.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=interface,
|
obj=interface,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
interface.custom_field_values.add(custom_field_value)
|
interface.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
|
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
|
||||||
|
|
|
@ -5,34 +5,33 @@ import sys
|
||||||
|
|
||||||
interfaces= load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')
|
interfaces= load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')
|
||||||
|
|
||||||
if interfaces is None:
|
if not interfaces is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
required_assocs = {
|
required_assocs = {
|
||||||
'device': (Device, 'name')
|
'device': (Device, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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():
|
||||||
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)
|
||||||
|
|
||||||
interface, created = Interface.objects.get_or_create(**params)
|
interface, created = Interface.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=interface,
|
obj=interface,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
interface.custom_field_values.add(custom_field_value)
|
interface.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🧷 Created interface", interface.name, interface.device.name)
|
print("🧷 Created interface", interface.name, interface.device.name)
|
||||||
|
|
|
@ -10,51 +10,50 @@ import sys
|
||||||
|
|
||||||
ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
|
ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
|
||||||
|
|
||||||
if ip_addresses is None:
|
if not ip_addresses is None:
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
'tenant': (Tenant, 'name'),
|
'tenant': (Tenant, 'name'),
|
||||||
'vrf': (VRF, 'name'),
|
'vrf': (VRF, 'name'),
|
||||||
'interface': (Interface, 'name')
|
'interface': (Interface, 'name')
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
params['address'] = IPNetwork(params['address'])
|
params['address'] = IPNetwork(params['address'])
|
||||||
|
|
||||||
if vm and device:
|
if vm and device:
|
||||||
print("IP Address can only specify one of the following: virtual_machine or device.")
|
print("IP Address can only specify one of the following: virtual_machine or device.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
for assoc, details in optional_assocs.items():
|
for assoc, details in optional_assocs.items():
|
||||||
if assoc in params:
|
if assoc in params:
|
||||||
model, field = details
|
model, field = details
|
||||||
if assoc == 'interface':
|
if assoc == 'interface':
|
||||||
if vm:
|
if vm:
|
||||||
vm_id = VirtualMachine.objects.get(name=vm).id
|
vm_id = VirtualMachine.objects.get(name=vm).id
|
||||||
query = { field: params.pop(assoc), "virtual_machine_id": vm_id }
|
query = { field: params.pop(assoc), "virtual_machine_id": vm_id }
|
||||||
elif device:
|
elif device:
|
||||||
dev_id = Device.objects.get(name=device).id
|
dev_id = Device.objects.get(name=device).id
|
||||||
query = { field: params.pop(assoc), "device_id": dev_id }
|
query = { field: params.pop(assoc), "device_id": dev_id }
|
||||||
else:
|
else:
|
||||||
query = { field: params.pop(assoc) }
|
query = { field: params.pop(assoc) }
|
||||||
params[assoc] = model.objects.get(**query)
|
params[assoc] = model.objects.get(**query)
|
||||||
|
|
||||||
ip_address, created = IPAddress.objects.get_or_create(**params)
|
ip_address, created = IPAddress.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
if custom_fields is not None:
|
if custom_fields is not None:
|
||||||
for cf_name, cf_value in custom_fields.items():
|
for cf_name, cf_value in custom_fields.items():
|
||||||
custom_field = CustomField.objects.get(name=cf_name)
|
custom_field = CustomField.objects.get(name=cf_name)
|
||||||
custom_field_value = CustomFieldValue.objects.create(
|
custom_field_value = CustomFieldValue.objects.create(
|
||||||
field=custom_field,
|
field=custom_field,
|
||||||
obj=ip_address,
|
obj=ip_address,
|
||||||
value=cf_value
|
value=cf_value
|
||||||
)
|
)
|
||||||
|
|
||||||
ip_address.custom_field_values.add(custom_field_value)
|
ip_address.custom_field_values.add(custom_field_value)
|
||||||
|
|
||||||
print("🧬 Created IP Address", ip_address.address)
|
print("🧬 Created IP Address", ip_address.address)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue