Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ASN initializer script #685

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions initializers/asns.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# - asn: 1
# rir: RFC1918
# tenant: tenant1
# - asn: 2
# rir: RFC4193 ULA
# - asn: 3
# rir: RFC3849
4 changes: 0 additions & 4 deletions initializers/sites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
# region: Downtown
# status: active
# facility: Amsterdam 1
# asn: 12345
# custom_field_data:
# text_field: Description for AMS1
# - name: AMS 2
# slug: ams2
# region: Downtown
# status: active
# facility: Amsterdam 2
# asn: 54321
# custom_field_data:
# text_field: Description for AMS2
# - name: AMS 3
# slug: ams3
# region: Suburbs
# status: active
# facility: Amsterdam 3
# asn: 67890
# tenant: tenant1
# custom_field_data:
# text_field: Description for AMS3
Expand All @@ -28,7 +25,6 @@
# region: Singapore
# status: active
# facility: Singapore 1
# asn: 09876
# tenant: tenant2
# custom_field_data:
# text_field: Description for SING1
33 changes: 33 additions & 0 deletions startup_scripts/155_asns.py
Original file line number Diff line number Diff line change
@@ -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}")