-
-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle primary IPs on devices and VMs
- Loading branch information
Showing
5 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |