Skip to content

Commit 5e56ff5

Browse files
authored
Merge pull request #60 from yanndegat/fixsitesasns
fix sites asns
2 parents d240672 + 31e0ec9 commit 5e56ff5

File tree

1 file changed

+18
-7
lines changed
  • src/netbox_initializers/initializers

1 file changed

+18
-7
lines changed

src/netbox_initializers/initializers/sites.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,20 @@ def load_data(self):
2626

2727
matching_params, defaults = self.split_params(params)
2828

29+
asnFounds = []
2930
if defaults.get("asns", 0):
31+
for asn in defaults["asns"]:
32+
found = ASN.objects.filter(asn=asn).first()
33+
if found:
34+
asnFounds += [found]
35+
36+
if len(defaults["asns"]) != len(asnFounds):
37+
print(
38+
"⚠️ Unable to create Site '{0}': all ASNs could not be found".format(
39+
params.get("name")
40+
)
41+
)
42+
3043
# asns will be assosciated below
3144
del defaults["asns"]
3245

@@ -35,15 +48,13 @@ def load_data(self):
3548
if created:
3649
print("📍 Created site", site.name)
3750

38-
if params.get("asns", 0):
39-
for asn in params["asns"]:
40-
found = ASN.objects.filter(asn=asn).first()
51+
self.set_custom_fields_values(site, custom_field_data)
4152

42-
if found:
43-
site.asns.add(found)
44-
print(" 👤 Assigned site %s asn %s" % (site.name, asn))
53+
for asn in asnFounds:
54+
site.asns.add(asn)
55+
print(" 👤 Assigned asn %s to site %s" % (asn, site.name))
4556

46-
self.set_custom_fields_values(site, custom_field_data)
57+
site.save()
4758

4859

4960
register_initializer("sites", SiteInitializer)

0 commit comments

Comments
 (0)