Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional primary_ip on virtual machines initializer #301

Merged
merged 2 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions initializers/devices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# rack: rack-02
# face: front
# position: 2
# primary_ip4: 10.1.1.2/24
# primary_ip6: 2001:db8:a000:1::2/64
# custom_fields:
# text_field: Description
# - name: server03
Expand Down
2 changes: 2 additions & 0 deletions initializers/virtual_machines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# memory: 2048
# name: virtual machine 2
# platform: Platform 2
# primary_ip4: 10.1.1.10/24
# primary_ip6: 2001:db8:a000:1::10/64
# status: active
# tenant: tenant1
# vcpus: 8
8 changes: 4 additions & 4 deletions startup_scripts/130_devices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dcim.models import Site, Rack, DeviceRole, DeviceType, Device, Platform
from ipam.models import IPAddress
from virtualization.models import Cluster
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
Expand All @@ -21,13 +20,14 @@
'tenant': (Tenant, 'name'),
'platform': (Platform, 'name'),
'rack': (Rack, 'name'),
'cluster': (Cluster, 'name'),
'primary_ip4': (IPAddress, 'address'),
'primary_ip6': (IPAddress, 'address')
'cluster': (Cluster, 'name')
}

for params in devices:
custom_fields = params.pop('custom_fields', None)
# 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
Expand Down
3 changes: 3 additions & 0 deletions startup_scripts/230_virtual_machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

for params in virtual_machines:
custom_fields = params.pop('custom_fields', None)
# 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
Expand Down
43 changes: 43 additions & 0 deletions startup_scripts/270_primary_ips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dcim.models import Device
from ipam.models import IPAddress
from virtualization.models import VirtualMachine
from startup_script_utils import load_yaml
import sys

def link_primary_ip(assets, asset_model):
for params in assets:
primary_ip_fields = set(params) & {'primary_ip4', 'primary_ip6'}
if not primary_ip_fields:
continue

for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }

try:
params[assoc] = model.objects.get(**query)
except model.DoesNotExist:
primary_ip_fields -= {assoc}
print(f"⚠️ IP Address {query[field]} not found")

asset = asset_model.objects.get(name=params['name'])
for field in primary_ip_fields:
if getattr(asset, field) != params[field]:
setattr(asset, field, params[field])
print(f"🧬 Linked primary IP {params[field].address} on {asset.name}")
asset.save()

devices = load_yaml('/opt/netbox/initializers/devices.yml')
virtual_machines = load_yaml('/opt/netbox/initializers/virtual_machines.yml')

if devices is None and virtual_machines is None:
sys.exit()

optional_assocs = {
'primary_ip4': (IPAddress, 'address'),
'primary_ip6': (IPAddress, 'address')
}

link_primary_ip(devices, Device)
link_primary_ip(virtual_machines, VirtualMachine)