Merge pull request #685 from tobiasge/asn-initializers

Added ASN initializer script
This commit is contained in:
Tobias Genannt 2022-01-10 11:14:55 +01:00 committed by GitHub
commit ff20e4f49c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

7
initializers/asns.yml Normal file
View File

@ -0,0 +1,7 @@
# - asn: 1
# rir: RFC1918
# tenant: tenant1
# - asn: 2
# rir: RFC4193 ULA
# - asn: 3
# rir: RFC3849

View File

@ -3,7 +3,6 @@
# region: Downtown
# status: active
# facility: Amsterdam 1
# asn: 12345
# custom_field_data:
# text_field: Description for AMS1
# - name: AMS 2
@ -11,7 +10,6 @@
# region: Downtown
# status: active
# facility: Amsterdam 2
# asn: 54321
# custom_field_data:
# text_field: Description for AMS2
# - name: AMS 3
@ -19,7 +17,6 @@
# region: Suburbs
# status: active
# facility: Amsterdam 3
# asn: 67890
# tenant: tenant1
# custom_field_data:
# text_field: Description for AMS3
@ -28,7 +25,6 @@
# region: Singapore
# status: active
# facility: Singapore 1
# asn: 09876
# tenant: tenant2
# custom_field_data:
# text_field: Description for SING1

View 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}")