Skip to content

Commit

Permalink
Add asn association on site initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
yanndegat committed Jun 29, 2023
1 parent 4fff72e commit a969fe7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/netbox_initializers/initializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"tenant_groups",
"tenants",
"regions",
"rirs",
"asns",
"sites",
"locations",
"rack_roles",
Expand All @@ -35,8 +37,6 @@
"interfaces",
"route_targets",
"vrfs",
"rirs",
"asns",
"aggregates",
"prefix_vlan_roles",
"vlan_groups",
Expand Down
14 changes: 14 additions & 0 deletions src/netbox_initializers/initializers/sites.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dcim.models import Region, Site
from ipam.models import ASN
from tenancy.models import Tenant

from . import BaseInitializer, register_initializer
Expand All @@ -24,11 +25,24 @@ def load_data(self):
params[assoc] = model.objects.get(**query)

matching_params, defaults = self.split_params(params)

if defaults.get("asns", 0):
# asns will be assosciated below
del defaults["asns"]

site, created = Site.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("📍 Created site", site.name)

if params.get("asns", 0):
for asn in params["asns"]:
found = ASN.objects.filter(asn=asn).first()

if found:
site.asns.add(found)
print(" 👤 Assigned site %s asn %s" % (site.name, asn))

self.set_custom_fields_values(site, custom_field_data)


Expand Down
2 changes: 2 additions & 0 deletions src/netbox_initializers/initializers/yaml/sites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# text_field: Description for AMS1
# - name: AMS 2
# slug: ams2
# asns:
# - 2
# region: Downtown
# status: active
# facility: Amsterdam 2
Expand Down

0 comments on commit a969fe7

Please sign in to comment.