netbox-docker/startup_scripts/100_platforms.py
Tobias Genannt 50ade7bce1 Add function to load YAML files
This commit starts to remove some code redundancy from the startup
scripts for easier maintenance.
2020-02-10 07:47:17 +01:00

27 lines
618 B
Python

from dcim.models import Manufacturer, Platform
from startup_script_utils import load_yaml
import sys
platforms = load_yaml('/opt/netbox/initializers/platforms.yml')
if platforms is None:
sys.exit()
optional_assocs = {
'manufacturer': (Manufacturer, 'name'),
}
for params in platforms:
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)
platform, created = Platform.objects.get_or_create(**params)
if created:
print("💾 Created platform", platform.name)