Allow for creation of interfaces and tenants in startup / initializers

This commit is contained in:
Marco Ceppi 2019-11-25 11:27:24 -05:00
parent fff05b3154
commit 64de66e2d5
4 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1 @@
device_role: 'customer-network-locker'

View File

@ -0,0 +1,11 @@
- slug: e9622cff-0286-4e98-9662-01f91ab907ff
name: vaportest
- slug: 5ec5a570-4b7e-4151-b9df-2d6f9379776c
name: VaporProvider
- slug: c26a3f7c-a854-4d4f-93cc-4fb4d6e34c08
name: Test Org1
- slug: 3cd715b1-42a5-4c6f-ba4d-2fb20f6f06c4
name: Test Org2

View File

@ -0,0 +1,20 @@
from tenancy.models import Tenant
from ruamel.yaml import YAML
from pathlib import Path
import sys
file = Path('/opt/netbox/initializers/tenants.yml')
if not file.is_file():
sys.exit()
with file.open('r') as stream:
yaml = YAML(typ='safe')
tenants = yaml.load(stream)
if tenants is not None:
for params in tenants:
tenant, created = Tenant.objects.get_or_create(**params)
if created:
print("🏠 Created tenant", tenant.name)

View File

@ -0,0 +1,36 @@
from dcim.models import Interface, Device, DeviceRole
from dcim.constants import IFACE_TYPE_KEYSTONE
from ruamel.yaml import YAML
from pathlib import Path
import sys
file = Path('/opt/netbox/initializers/interfaces.yml')
if not file.is_file():
sys.exit()
with file.open('r') as stream:
yaml = YAML(typ='safe')
config = yaml.load(stream)
device_role = DeviceRole.objects.get(slug=config.get('device_role'))
if not device_role:
sys.exit()
network_lockers = Device.objects.all().filter(device_role=device_role.id)
for locker in network_lockers:
i = 1
while i < 24:
strand0 = i
strand1 = i + 1
name = '{}-{}'.format(strand0, strand1)
i += 2
interface, created = Interface.objects.get_or_create(name=name, device=locker, type=IFACE_TYPE_KEYSTONE)
if created:
print("🔗 Created interface {} for {}".format(interface.name, locler.name))