feat: Make startup scripts idempotent
This commit is contained in:
parent
a6eb4fef00
commit
9be7b0e109
42 changed files with 275 additions and 95 deletions
|
@ -1,7 +1,7 @@
|
|||
import sys
|
||||
|
||||
from ipam.models import ASN, RIR
|
||||
from startup_script_utils import load_yaml
|
||||
from startup_script_utils import load_yaml, split_params
|
||||
from tenancy.models import Tenant
|
||||
|
||||
asns = load_yaml("/opt/netbox/initializers/asns.yml")
|
||||
|
@ -9,8 +9,8 @@ asns = load_yaml("/opt/netbox/initializers/asns.yml")
|
|||
if asns is None:
|
||||
sys.exit()
|
||||
|
||||
match_params = ["asn", "rir"]
|
||||
required_assocs = {"rir": (RIR, "name")}
|
||||
|
||||
optional_assocs = {"tenant": (Tenant, "name")}
|
||||
|
||||
for params in asns:
|
||||
|
@ -27,7 +27,8 @@ for params in asns:
|
|||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
asn, created = ASN.objects.get_or_create(**params)
|
||||
matching_params, defaults = split_params(params, match_params)
|
||||
asn, created = ASN.objects.get_or_create(**matching_params, defaults=defaults)
|
||||
|
||||
if created:
|
||||
print(f"🔡 Created ASN {asn.asn}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue