2022-01-10 03:49:51 -06:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from ipam.models import ASN, RIR
|
2022-04-05 01:34:08 -05:00
|
|
|
from startup_script_utils import load_yaml, split_params
|
2022-01-10 03:49:51 -06:00
|
|
|
from tenancy.models import Tenant
|
|
|
|
|
|
|
|
asns = load_yaml("/opt/netbox/initializers/asns.yml")
|
|
|
|
|
|
|
|
if asns is None:
|
|
|
|
sys.exit()
|
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
match_params = ["asn", "rir"]
|
2022-01-10 03:49:51 -06:00
|
|
|
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)
|
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
matching_params, defaults = split_params(params, match_params)
|
|
|
|
asn, created = ASN.objects.get_or_create(**matching_params, defaults=defaults)
|
2022-01-10 03:49:51 -06:00
|
|
|
|
|
|
|
if created:
|
|
|
|
print(f"🔡 Created ASN {asn.asn}")
|