Skip to content

Commit

Permalink
Merge pull request #3 from Lon1/develop
Browse files Browse the repository at this point in the history
Rebase develop branch
  • Loading branch information
Lon1 authored Apr 18, 2022
2 parents 9ad4a3c + e794a32 commit ca03689
Show file tree
Hide file tree
Showing 46 changed files with 529 additions and 140 deletions.
5 changes: 5 additions & 0 deletions configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def _read_secret(secret_name, default = None):
CORS_ORIGIN_WHITELIST = list(filter(None, environ.get('CORS_ORIGIN_WHITELIST', 'https://localhost').split(' ')))
CORS_ORIGIN_REGEX_WHITELIST = [re.compile(r) for r in list(filter(None, environ.get('CORS_ORIGIN_REGEX_WHITELIST', '').split(' ')))]

# Cross-Site-Request-Forgery-Attack settings. If Netbox is sitting behind a reverse proxy, you might need to set the CSRF_TRUSTED_ORIGINS flag.
# Django 4.0 requires to specify the URL Scheme in this setting. An example environment variable could be specified like:
# CSRF_TRUSTED_ORIGINS=https://demo.netbox.dev http://demo.netbox.dev
CSRF_TRUSTED_ORIGINS = list(filter(None, environ.get('CSRF_TRUSTED_ORIGINS', '').split(' ')))

# Set to True to enable server debugging. WARNING: Debugging introduces a substantial performance penalty and may reveal
# sensitive information about your installation. Only enable debugging while performing testing. Never enable debugging
# on a production system.
Expand Down
9 changes: 9 additions & 0 deletions initializers/dcim_interfaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
##
## Examples:

# - device: server01
# name: ath0
# type: 1000base-t
# lag: ae0
# bridge: br0
# - device: server01
# name: ath1
# type: 1000base-t
# parent: ath0
# - device: server01
# enabled: true
# type: virtual
Expand Down
33 changes: 33 additions & 0 deletions initializers/device_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,36 @@
# slug: other
# custom_field_data:
# text_field: Description
# interfaces:
# - name: eth0
# type: 1000base-t
# mgmt_only: True
# - name: eth1
# type: 1000base-t
# console_server_ports:
# - name_template: ttyS[1-48]
# type: rj-45
# power_ports:
# - name: psu0 # both non-template and template field specified; non-template field takes precedence
# name_template: psu[0,1]
# type: iec-60320-c14
# maximum_draw: 35
# allocated_draw: 35
# front_ports:
# - name_template: front[1,2]
# type: 8p8c
# rear_port_template: rear[0,1]
# rear_port_position_template: "[1,2]"
# rear_ports:
# - name_template: rear[0,1]
# type: 8p8c
# positions_template: "[3,2]"
# device_bays:
# - name_template: bay[0-9]
# label_template: test[0-5,9,6-8]
# description: Test description
# power_outlets:
# - name_template: outlet[0,1]
# type: iec-60320-c5
# power_port: psu0
# feed_leg: B
1 change: 1 addition & 0 deletions initializers/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# password: reader
# writer:
# password: writer
# api_token: "" # a token is generated automatically unless the value is explicity set to empty
# jdoe:
# first_name: John
# last_name: Doe
Expand Down
20 changes: 12 additions & 8 deletions startup_scripts/000_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
sys.exit()

for username, user_details in users.items():
if not User.objects.filter(username=username):
user = User.objects.create_user(
username=username,
password=user_details.get("password", 0) or User.objects.make_random_password(),
)

print("👤 Created user", username)
api_token = user_details.pop("api_token", Token.generate_key())
password = user_details.pop("password", User.objects.make_random_password())

user, created = User.objects.get_or_create(username=username, defaults=user_details)

if created:
user.set_password(password)
user.save()

if user_details.get("api_token", 0):
Token.objects.create(user=user, key=user_details["api_token"])
if api_token:
Token.objects.get_or_create(user=user, key=api_token)

print("👤 Created user", username)
8 changes: 5 additions & 3 deletions startup_scripts/020_object_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

object_permission, created = ObjectPermission.objects.get_or_create(
name=permission_name,
description=permission_details["description"],
enabled=permission_details["enabled"],
actions=permission_details["actions"],
defaults={
"description": permission_details["description"],
"enabled": permission_details["enabled"],
"actions": permission_details["actions"],
},
)

if permission_details.get("object_types", 0):
Expand Down
6 changes: 4 additions & 2 deletions startup_scripts/040_custom_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.models import ContentType
from extras.models import CustomLink
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

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

Expand All @@ -28,6 +28,8 @@ def get_content_type_id(content_type):
)
continue

custom_link, created = CustomLink.objects.get_or_create(**link)
matching_params, defaults = split_params(link)
custom_link, created = CustomLink.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🔗 Created Custom Link '{0}'".format(custom_link.name))
5 changes: 3 additions & 2 deletions startup_scripts/050_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from extras.models import Tag
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from utilities.choices import ColorChoices

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

tag, created = Tag.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tag, created = Tag.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created Tag", tag.name)
6 changes: 4 additions & 2 deletions startup_scripts/060_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.models import ContentType
from extras.models import Webhook
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

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

Expand All @@ -26,7 +26,9 @@ def get_content_type_id(hook_name, content_type):
except ContentType.DoesNotExist:
continue

webhook, created = Webhook.objects.get_or_create(**hook)
matching_params, defaults = split_params(hook)
webhook, created = Webhook.objects.get_or_create(**matching_params, defaults=defaults)

if created:
webhook.content_types.set(obj_type_ids)
webhook.save()
Expand Down
5 changes: 3 additions & 2 deletions startup_scripts/070_tenant_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from tenancy.models import TenantGroup

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

for params in tenant_groups:
tenant_group, created = TenantGroup.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tenant_group, created = TenantGroup.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🔳 Created Tenant Group", tenant_group.name)
14 changes: 10 additions & 4 deletions startup_scripts/080_tenants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys

from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant, TenantGroup

tenants = load_yaml("/opt/netbox/initializers/tenants.yml")
Expand All @@ -20,9 +25,10 @@

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

tenant, created = Tenant.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tenant, created = Tenant.objects.get_or_create(**matching_params, defaults=defaults)

if created:
set_custom_fields_values(tenant, custom_field_data)

print("👩‍💻 Created Tenant", tenant.name)

set_custom_fields_values(tenant, custom_field_data)
5 changes: 3 additions & 2 deletions startup_scripts/090_regions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from dcim.models import Region
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

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

Expand All @@ -19,7 +19,8 @@

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

region, created = Region.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
region, created = Region.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🌐 Created region", region.name)
14 changes: 10 additions & 4 deletions startup_scripts/110_sites.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import sys

from dcim.models import Region, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant

sites = load_yaml("/opt/netbox/initializers/sites.yml")
Expand All @@ -21,9 +26,10 @@

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

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

if created:
set_custom_fields_values(site, custom_field_data)

print("📍 Created site", site.name)

set_custom_fields_values(site, custom_field_data)
6 changes: 4 additions & 2 deletions startup_scripts/120_locations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys

from dcim.models import Location, Site
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")

if rack_groups is None:
sys.exit()

match_params = ["name", "slug", "site"]
required_assocs = {"site": (Site, "name")}

for params in rack_groups:
Expand All @@ -17,7 +18,8 @@
query = {field: params.pop(assoc)}
params[assoc] = model.objects.get(**query)

location, created = Location.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created location", location.name)
5 changes: 3 additions & 2 deletions startup_scripts/130_rack_roles.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from dcim.models import RackRole
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from utilities.choices import ColorChoices

rack_roles = load_yaml("/opt/netbox/initializers/rack_roles.yml")
Expand All @@ -17,7 +17,8 @@
if color in color_tpl:
params["color"] = color_tpl[0]

rack_role, created = RackRole.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
rack_role, created = RackRole.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created rack role", rack_role.name)
16 changes: 11 additions & 5 deletions startup_scripts/140_racks.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import sys

from dcim.models import Location, Rack, RackRole, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant

racks = load_yaml("/opt/netbox/initializers/racks.yml")

if racks is None:
sys.exit()

match_params = ["name", "site"]
required_assocs = {"site": (Site, "name")}

optional_assocs = {
"role": (RackRole, "name"),
"tenant": (Tenant, "name"),
Expand All @@ -33,9 +38,10 @@

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

rack, created = Rack.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
rack, created = Rack.objects.get_or_create(**matching_params, defaults=defaults)

if created:
set_custom_fields_values(rack, custom_field_data)

print("🔳 Created rack", rack.site, rack.name)

set_custom_fields_values(rack, custom_field_data)
16 changes: 11 additions & 5 deletions startup_scripts/150_power_panels.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import sys

from dcim.models import Location, PowerPanel, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)

power_panels = load_yaml("/opt/netbox/initializers/power_panels.yml")

if power_panels is None:
sys.exit()

match_params = ["name", "site"]
required_assocs = {"site": (Site, "name")}

optional_assocs = {"location": (Location, "name")}

for params in power_panels:
Expand All @@ -28,9 +33,10 @@

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

power_panel, created = PowerPanel.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
power_panel, created = PowerPanel.objects.get_or_create(**matching_params, defaults=defaults)

if created:
set_custom_fields_values(power_panel, custom_field_data)

print("⚡ Created Power Panel", power_panel.site, power_panel.name)

set_custom_fields_values(power_panel, custom_field_data)
Loading

0 comments on commit ca03689

Please sign in to comment.