Skip to content

Commit 5c4a1cc

Browse files
authored
Merge pull request #729 from kr3ator/feature/separate_default_params
feat: Make startup scripts idempotent
2 parents a6eb4fe + a63af05 commit 5c4a1cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+275
-95
lines changed

initializers/users.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# password: reader
55
# writer:
66
# password: writer
7+
# api_token: "" # a token is generated automatically unless the value is explicity set to empty
78
# jdoe:
89
# first_name: John
910
# last_name: Doe

startup_scripts/000_users.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
sys.exit()
1010

1111
for username, user_details in users.items():
12-
if not User.objects.filter(username=username):
13-
user = User.objects.create_user(
14-
username=username,
15-
password=user_details.get("password", 0) or User.objects.make_random_password(),
16-
)
1712

18-
print("👤 Created user", username)
13+
api_token = user_details.pop("api_token", Token.generate_key())
14+
password = user_details.pop("password", User.objects.make_random_password())
15+
16+
user, created = User.objects.get_or_create(username=username, defaults=user_details)
17+
18+
if created:
19+
user.set_password(password)
20+
user.save()
1921

20-
if user_details.get("api_token", 0):
21-
Token.objects.create(user=user, key=user_details["api_token"])
22+
if api_token:
23+
Token.objects.get_or_create(user=user, key=api_token)
24+
25+
print("👤 Created user", username)

startup_scripts/020_object_permissions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
object_permission, created = ObjectPermission.objects.get_or_create(
1616
name=permission_name,
17-
description=permission_details["description"],
18-
enabled=permission_details["enabled"],
19-
actions=permission_details["actions"],
17+
defaults={
18+
"description": permission_details["description"],
19+
"enabled": permission_details["enabled"],
20+
"actions": permission_details["actions"],
21+
},
2022
)
2123

2224
if permission_details.get("object_types", 0):

startup_scripts/040_custom_links.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.contrib.contenttypes.models import ContentType
44
from extras.models import CustomLink
5-
from startup_script_utils import load_yaml
5+
from startup_script_utils import load_yaml, split_params
66

77
custom_links = load_yaml("/opt/netbox/initializers/custom_links.yml")
88

@@ -28,6 +28,8 @@ def get_content_type_id(content_type):
2828
)
2929
continue
3030

31-
custom_link, created = CustomLink.objects.get_or_create(**link)
31+
matching_params, defaults = split_params(link)
32+
custom_link, created = CustomLink.objects.get_or_create(**matching_params, defaults=defaults)
33+
3234
if created:
3335
print("🔗 Created Custom Link '{0}'".format(custom_link.name))

startup_scripts/050_tags.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
from extras.models import Tag
4-
from startup_script_utils import load_yaml
4+
from startup_script_utils import load_yaml, split_params
55
from utilities.choices import ColorChoices
66

77
tags = load_yaml("/opt/netbox/initializers/tags.yml")
@@ -17,7 +17,8 @@
1717
if color in color_tpl:
1818
params["color"] = color_tpl[0]
1919

20-
tag, created = Tag.objects.get_or_create(**params)
20+
matching_params, defaults = split_params(params)
21+
tag, created = Tag.objects.get_or_create(**matching_params, defaults=defaults)
2122

2223
if created:
2324
print("🎨 Created Tag", tag.name)

startup_scripts/060_webhooks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.contrib.contenttypes.models import ContentType
44
from extras.models import Webhook
5-
from startup_script_utils import load_yaml
5+
from startup_script_utils import load_yaml, split_params
66

77
webhooks = load_yaml("/opt/netbox/initializers/webhooks.yml")
88

@@ -26,7 +26,9 @@ def get_content_type_id(hook_name, content_type):
2626
except ContentType.DoesNotExist:
2727
continue
2828

29-
webhook, created = Webhook.objects.get_or_create(**hook)
29+
matching_params, defaults = split_params(hook)
30+
webhook, created = Webhook.objects.get_or_create(**matching_params, defaults=defaults)
31+
3032
if created:
3133
webhook.content_types.set(obj_type_ids)
3234
webhook.save()

startup_scripts/070_tenant_groups.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from startup_script_utils import load_yaml
3+
from startup_script_utils import load_yaml, split_params
44
from tenancy.models import TenantGroup
55

66
tenant_groups = load_yaml("/opt/netbox/initializers/tenant_groups.yml")
@@ -9,7 +9,8 @@
99
sys.exit()
1010

1111
for params in tenant_groups:
12-
tenant_group, created = TenantGroup.objects.get_or_create(**params)
12+
matching_params, defaults = split_params(params)
13+
tenant_group, created = TenantGroup.objects.get_or_create(**matching_params, defaults=defaults)
1314

1415
if created:
1516
print("🔳 Created Tenant Group", tenant_group.name)

startup_scripts/080_tenants.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import sys
22

3-
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
3+
from startup_script_utils import (
4+
load_yaml,
5+
pop_custom_fields,
6+
set_custom_fields_values,
7+
split_params,
8+
)
49
from tenancy.models import Tenant, TenantGroup
510

611
tenants = load_yaml("/opt/netbox/initializers/tenants.yml")
@@ -20,7 +25,8 @@
2025

2126
params[assoc] = model.objects.get(**query)
2227

23-
tenant, created = Tenant.objects.get_or_create(**params)
28+
matching_params, defaults = split_params(params)
29+
tenant, created = Tenant.objects.get_or_create(**matching_params, defaults=defaults)
2430

2531
if created:
2632
print("👩‍💻 Created Tenant", tenant.name)

startup_scripts/090_regions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
from dcim.models import Region
4-
from startup_script_utils import load_yaml
4+
from startup_script_utils import load_yaml, split_params
55

66
regions = load_yaml("/opt/netbox/initializers/regions.yml")
77

@@ -19,7 +19,8 @@
1919

2020
params[assoc] = model.objects.get(**query)
2121

22-
region, created = Region.objects.get_or_create(**params)
22+
matching_params, defaults = split_params(params)
23+
region, created = Region.objects.get_or_create(**matching_params, defaults=defaults)
2324

2425
if created:
2526
print("🌐 Created region", region.name)

startup_scripts/110_sites.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import sys
22

33
from dcim.models import Region, Site
4-
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
4+
from startup_script_utils import (
5+
load_yaml,
6+
pop_custom_fields,
7+
set_custom_fields_values,
8+
split_params,
9+
)
510
from tenancy.models import Tenant
611

712
sites = load_yaml("/opt/netbox/initializers/sites.yml")
@@ -21,7 +26,8 @@
2126

2227
params[assoc] = model.objects.get(**query)
2328

24-
site, created = Site.objects.get_or_create(**params)
29+
matching_params, defaults = split_params(params)
30+
site, created = Site.objects.get_or_create(**matching_params, defaults=defaults)
2531

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

0 commit comments

Comments
 (0)