From ee47ba04bc2363595080781f61824b9de87b4bae Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 10 Jan 2022 10:49:51 +0100 Subject: [PATCH] Added ASN initializer script --- initializers/asns.yml | 7 +++++++ initializers/sites.yml | 4 ---- startup_scripts/155_asns.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 initializers/asns.yml create mode 100644 startup_scripts/155_asns.py diff --git a/initializers/asns.yml b/initializers/asns.yml new file mode 100644 index 0000000..f75408b --- /dev/null +++ b/initializers/asns.yml @@ -0,0 +1,7 @@ +# - asn: 1 +# rir: RFC1918 +# tenant: tenant1 +# - asn: 2 +# rir: RFC4193 ULA +# - asn: 3 +# rir: RFC3849 diff --git a/initializers/sites.yml b/initializers/sites.yml index 7ace3aa..700c853 100644 --- a/initializers/sites.yml +++ b/initializers/sites.yml @@ -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 diff --git a/startup_scripts/155_asns.py b/startup_scripts/155_asns.py new file mode 100644 index 0000000..893f3ba --- /dev/null +++ b/startup_scripts/155_asns.py @@ -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}")