Merge pull request #685 from tobiasge/asn-initializers
Added ASN initializer script
This commit is contained in:
commit
ff20e4f49c
7
initializers/asns.yml
Normal file
7
initializers/asns.yml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# - asn: 1
|
||||||
|
# rir: RFC1918
|
||||||
|
# tenant: tenant1
|
||||||
|
# - asn: 2
|
||||||
|
# rir: RFC4193 ULA
|
||||||
|
# - asn: 3
|
||||||
|
# rir: RFC3849
|
@ -3,7 +3,6 @@
|
|||||||
# region: Downtown
|
# region: Downtown
|
||||||
# status: active
|
# status: active
|
||||||
# facility: Amsterdam 1
|
# facility: Amsterdam 1
|
||||||
# asn: 12345
|
|
||||||
# custom_field_data:
|
# custom_field_data:
|
||||||
# text_field: Description for AMS1
|
# text_field: Description for AMS1
|
||||||
# - name: AMS 2
|
# - name: AMS 2
|
||||||
@ -11,7 +10,6 @@
|
|||||||
# region: Downtown
|
# region: Downtown
|
||||||
# status: active
|
# status: active
|
||||||
# facility: Amsterdam 2
|
# facility: Amsterdam 2
|
||||||
# asn: 54321
|
|
||||||
# custom_field_data:
|
# custom_field_data:
|
||||||
# text_field: Description for AMS2
|
# text_field: Description for AMS2
|
||||||
# - name: AMS 3
|
# - name: AMS 3
|
||||||
@ -19,7 +17,6 @@
|
|||||||
# region: Suburbs
|
# region: Suburbs
|
||||||
# status: active
|
# status: active
|
||||||
# facility: Amsterdam 3
|
# facility: Amsterdam 3
|
||||||
# asn: 67890
|
|
||||||
# tenant: tenant1
|
# tenant: tenant1
|
||||||
# custom_field_data:
|
# custom_field_data:
|
||||||
# text_field: Description for AMS3
|
# text_field: Description for AMS3
|
||||||
@ -28,7 +25,6 @@
|
|||||||
# region: Singapore
|
# region: Singapore
|
||||||
# status: active
|
# status: active
|
||||||
# facility: Singapore 1
|
# facility: Singapore 1
|
||||||
# asn: 09876
|
|
||||||
# tenant: tenant2
|
# tenant: tenant2
|
||||||
# custom_field_data:
|
# custom_field_data:
|
||||||
# text_field: Description for SING1
|
# text_field: Description for SING1
|
||||||
|
33
startup_scripts/155_asns.py
Normal file
33
startup_scripts/155_asns.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
from ipam.models import ASN, RIR
|
||||||
|
from startup_script_utils import load_yaml
|
||||||
|
from tenancy.models import Tenant
|
||||||
|
|
||||||
|
asns = load_yaml("/opt/netbox/initializers/asns.yml")
|
||||||
|
|
||||||
|
if asns is None:
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
required_assocs = {"rir": (RIR, "name")}
|
||||||
|
|
||||||
|
optional_assocs = {"tenant": (Tenant, "name")}
|
||||||
|
|
||||||
|
for params in asns:
|
||||||
|
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
|
||||||
|
query = {field: params.pop(assoc)}
|
||||||
|
|
||||||
|
params[assoc] = model.objects.get(**query)
|
||||||
|
|
||||||
|
asn, created = ASN.objects.get_or_create(**params)
|
||||||
|
|
||||||
|
if created:
|
||||||
|
print(f"🔡 Created ASN {asn.asn}")
|
Loading…
Reference in New Issue
Block a user