from dcim.models import Site, Platform, DeviceRole
from virtualization.models import Cluster, VirtualMachine
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys

virtual_machines = load_yaml('/opt/netbox/initializers/virtualization_virtual_machines.yml')

if virtual_machines is None:
  sys.exit()

required_assocs = {
  'cluster': (Cluster, 'name')
}

optional_assocs = {
  'tenant': (Tenant, 'name'),
  'platform': (Platform, 'name'),
  'role': (DeviceRole, 'name')
}

for params in virtual_machines:
  custom_fields = params.pop('custom_fields', None)

  for assoc, details in required_assocs.items():
    model, field = details
    query = { field: params.pop(assoc) }

    params[assoc] = model.objects.get(**query)
from virtualization.models import VirtualMachine, VMInterface
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys

interfaces = load_yaml(
    '/opt/netbox/initializers/virtualization_interfaces.yml')

if interfaces is None:
    sys.exit()

required_assocs = {'virtual_machine': (VirtualMachine, 'name')}

for params in interfaces:
    custom_fields = params.pop('custom_fields', None)

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}

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

    interface, created = VMInterface.objects.get_or_create(**params)

    if created:
        if custom_fields is not None:
            for cf_name, cf_value in custom_fields.items():
                custom_field = CustomField.objects.get(name=cf_name)
                custom_field_value = CustomFieldValue.objects.create(
                    field=custom_field, obj=interface, value=cf_value)
Example #3
0
import sys

from circuits.models import Provider
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values

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

if providers is None:
    sys.exit()

for params in providers:
    custom_field_data = pop_custom_fields(params)

    provider, created = Provider.objects.get_or_create(**params)

    if created:
        set_custom_fields_values(provider, custom_field_data)

        print("📡 Created provider", provider.name)
Example #4
0
import sys

from dcim.models import Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from tenancy.models import Tenant
from virtualization.models import Cluster, ClusterGroup, ClusterType

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

if clusters is None:
    sys.exit()

required_assocs = {"type": (ClusterType, "name")}

optional_assocs = {
    "site": (Site, "name"),
    "group": (ClusterGroup, "name"),
    "tenant": (Tenant, "name"),
}

for params in clusters:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}

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

    for assoc, details in optional_assocs.items():
        if assoc in params:
Example #5
0
import sys

from dcim.models import PowerPanel, RackGroup, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values

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

if power_panels is None:
    sys.exit()

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

optional_assocs = {"rack_group": (RackGroup, "name")}

for params in power_panels:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}

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

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

    power_panel, created = PowerPanel.objects.get_or_create(**params)
from dcim.models import Interface, Device
from ipam.models import VLAN
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys

interfaces= load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')

if interfaces is None:
  sys.exit()

required_assocs = {
  'device': (Device, 'name')
}

optional_assocs = {
  'untagged_vlan': (VLAN, 'name')
}

for params in interfaces:
  custom_fields = params.pop('custom_fields', None)

  for assoc, details in required_assocs.items():
    model, field = details
    query = { field: params.pop(assoc) }

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

  for assoc, details in optional_assocs.items():
    if assoc in params:
      model, field = details
from ipam.models import Aggregate, RIR

from extras.models import CustomField, CustomFieldValue

from netaddr import IPNetwork
from startup_script_utils import load_yaml
import sys

aggregates = load_yaml('/opt/netbox/initializers/ipam_aggregates.yml')

if aggregates is None:
    sys.exit()

required_assocs = {'rir': (RIR, 'name')}

for params in aggregates:
    custom_fields = params.pop('custom_fields', None)
    params['prefix'] = IPNetwork(params['prefix'])

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}

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

    aggregate, created = Aggregate.objects.get_or_create(**params)

    if created:
        if custom_fields is not None:
            for cf_name, cf_value in custom_fields.items():
                custom_field = CustomField.objects.get(name=cf_name)
Example #8
0
import sys

from dcim.models import Site
from ipam.models import VLAN, Role, VLANGroup
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from tenancy.models import Tenant, TenantGroup

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

if vlans is None:
    sys.exit()

optional_assocs = {
    "site": (Site, "name"),
    "tenant": (Tenant, "name"),
    "tenant_group": (TenantGroup, "name"),
    "group": (VLANGroup, "name"),
    "role": (Role, "name"),
}

for params in vlans:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

    vlan, created = VLAN.objects.get_or_create(**params)
Example #9
0
import sys

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

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

if webhooks is None:
    sys.exit()


def get_content_type_id(hook_name, content_type):
    try:
        return ContentType.objects.get(model=content_type).id
    except ContentType.DoesNotExist as ex:
        print("тЪая╕П Webhook '{0}': The object_type '{1}' is unknown.".format(
            hook_name, content_type))
        raise ex


for hook in webhooks:
    obj_types = hook.pop("object_types")

    try:
        obj_type_ids = [
            get_content_type_id(hook["name"], obj) for obj in obj_types
        ]
    except ContentType.DoesNotExist:
        continue
from dcim.models import Manufacturer
from startup_script_utils import load_yaml
import sys

manufacturers = load_yaml('/opt/netbox/initializers/manufacturers.yml')

if manufacturers is None:
    sys.exit()

for params in manufacturers:
    manufacturer, created = Manufacturer.objects.get_or_create(**params)

    if created:
        print("🏭 Created Manufacturer", manufacturer.name)
Example #11
0
import sys

from dcim.models import Region
from startup_script_utils import load_yaml

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

if regions is None:
    sys.exit()

optional_assocs = {"parent": (Region, "name")}

for params in regions:

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

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

    if created:
        print("🌐 Created region", region.name)
Example #12
0
import sys

from django.contrib.auth.models import Group, User
from startup_script_utils import load_yaml, set_permissions

groups = load_yaml("/opt/netbox/initializers/groups.yml")
if groups is None:
    sys.exit()

for groupname, group_details in groups.items():
    group, created = Group.objects.get_or_create(name=groupname)

    if created:
        print("👥 Created group", groupname)

    for username in group_details.get("users", []):
        user = User.objects.get(username=username)

        if user:
            user.groups.add(group)

    yaml_permissions = group_details.get("permissions", [])
    set_permissions(group.permissions, yaml_permissions)
from ipam.models import RIR
from startup_script_utils import load_yaml
import sys

rirs = load_yaml('/opt/netbox/initializers/ipam_rirs.yml')

if rirs is None:
    sys.exit()

for params in rirs:
    rir, created = RIR.objects.get_or_create(**params)

    if created:
        print("🗺️ Created RIR", rir.name)
Example #14
0
from dcim.models import Site
from ipam.models import VLANGroup
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys

vlan_groups = load_yaml('/opt/netbox/initializers/vlan_groups.yml')

if vlan_groups is None:
    sys.exit()

optional_assocs = {'site': (Site, 'name')}

for params in vlan_groups:
    custom_fields = params.pop('custom_fields', None)

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

    vlan_group, created = VLANGroup.objects.get_or_create(**params)

    if created:
        if custom_fields is not None:
            for cf_name, cf_value in custom_fields.items():
                custom_field = CustomField.objects.get(name=cf_name)
                custom_field_value = CustomFieldValue.objects.create(
                    field=custom_field, obj=vlan_group, value=cf_value)
Example #15
0
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 tenancy.models import Tenant

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

if racks is None:
    sys.exit()

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

optional_assocs = {
    "role": (RackRole, "name"),
    "tenant": (Tenant, "name"),
    "location": (Location, "name"),
}

for params in racks:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}

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

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
Example #16
0
import sys

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

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

if custom_links is None:
    sys.exit()


def get_content_type_id(content_type):
    try:
        return ContentType.objects.get(model=content_type).id
    except ContentType.DoesNotExist:
        pass


for link in custom_links:
    content_type = link.pop("content_type")
    link["content_type_id"] = get_content_type_id(content_type)
    if link["content_type_id"] is None:
        print(
            "тЪая╕П Unable to create Custom Link '{0}': The content_type '{1}' is unknown"
            .format(link.name, content_type))
        continue

    custom_link, created = CustomLink.objects.get_or_create(**link)
    if created:
        print("ЁЯФЧ Created Custom Link '{0}'".format(custom_link.name))
Example #17
0
from ipam.models import IPAddress, VRF
from dcim.models import Device, Interface
from virtualization.models import VirtualMachine
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue

from netaddr import IPNetwork
from startup_script_utils import load_yaml
import sys

ip_addresses = load_yaml('/opt/netbox/initializers/ipam_ip_addresses.yml')

if ip_addresses is None:
    sys.exit()

optional_assocs = {
    'tenant': (Tenant, 'name'),
    'vrf': (VRF, 'name'),
    'interface': (Interface, 'name')
}

for params in ip_addresses:
    vm = params.pop('virtual_machine', None)
    device = params.pop('device', None)
    custom_fields = params.pop('custom_fields', None)
    params['address'] = IPNetwork(params['address'])

    if vm and device:
        print(
            "IP Address can only specify one of the following: virtual_machine or device."
        )
Example #18
0
from dcim.models import Site, Rack, DeviceRole, DeviceType, Device, Platform
from ipam.models import IPAddress
from virtualization.models import Cluster
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys

devices = load_yaml('/opt/netbox/initializers/devices.yml')

if devices is None:
    sys.exit()

required_assocs = {
    'device_role': (DeviceRole, 'name'),
    'device_type': (DeviceType, 'model'),
    'site': (Site, 'name')
}

optional_assocs = {
    'tenant': (Tenant, 'name'),
    'platform': (Platform, 'name'),
    'rack': (Rack, 'name'),
    'cluster': (Cluster, 'name'),
    'primary_ip4': (IPAddress, 'address'),
    'primary_ip6': (IPAddress, 'address')
}

for params in devices:
    custom_fields = params.pop('custom_fields', None)
from ipam.models import Role
from startup_script_utils import load_yaml
import sys

roles = load_yaml('/opt/netbox/initializers/ipam_vlan_roles.yml')

if roles is None:
  sys.exit()

for params in roles:
  role, created = Role.objects.get_or_create(**params)

  if created:
    print("⛹️‍ Created Prefix/VLAN Role", role.name)
from extras.models.tags import Tag
from utilities.choices import ColorChoices

from startup_script_utils import load_yaml
import sys

tags = load_yaml('/opt/netbox/initializers/tags.yml')

if tags is None:
    sys.exit()

for params in tags:
    if 'color' in params:
        color = params.pop('color')

        for color_tpl in ColorChoices:
            if color in color_tpl:
                params['color'] = color_tpl[0]

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

    if created:
        print("🏷 Created tag", tag.name)
Example #21
0
from extras.models import CustomField
from startup_script_utils import load_yaml


def get_class_for_class_path(class_path):
    import importlib
    from django.contrib.contenttypes.models import ContentType

    module_name, class_name = class_path.rsplit(".", 1)
    module = importlib.import_module(module_name)
    clazz = getattr(module, class_name)
    return ContentType.objects.get_for_model(clazz)


customfields = load_yaml('/opt/netbox/initializers/custom_fields.yml')

if customfields is None:
    sys.exit()

for cf_name, cf_details in customfields.items():
    custom_field, created = CustomField.objects.get_or_create(name=cf_name)

    if created:
        if cf_details.get('default', False):
            custom_field.default = cf_details['default']

        if cf_details.get('description', False):
            custom_field.description = cf_details['description']

        if cf_details.get('label', False):
Example #22
0
                try:
                    params[assoc] = model.objects.get(**query)
                except model.DoesNotExist:
                    primary_ip_fields -= {assoc}
                    print(f"тЪая╕П IP Address '{query[field]}' not found")

        asset = asset_model.objects.get(name=params['name'])
        for field in primary_ip_fields:
            if getattr(asset, field) != params[field]:
                setattr(asset, field, params[field])
                print(
                    f"ЁЯФЧ Define primary IP '{params[field].address}' on '{asset.name}'"
                )
        asset.save()


devices = load_yaml('/opt/netbox/initializers/devices.yml')
virtual_machines = load_yaml('/opt/netbox/initializers/virtual_machines.yml')

optional_assocs = {
    'primary_ip4': (IPAddress, 'address'),
    'primary_ip6': (IPAddress, 'address')
}

if devices is None and virtual_machines is None:
    sys.exit()
if devices is not None:
    link_primary_ip(devices, Device)
if virtual_machines is not None:
    link_primary_ip(virtual_machines, VirtualMachine)
from dcim.models import Site
from ipam.models import Prefix, VLAN, Role, VRF
from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue
from netaddr import IPNetwork
from startup_script_utils import load_yaml
import sys

prefixes = load_yaml('/opt/netbox/initializers/ipam_prefixes.yml')

if prefixes is None:
    sys.exit()

optional_assocs = {
    'site': (Site, 'name'),
    'tenant': (Tenant, 'name'),
    'tenant_group': (TenantGroup, 'name'),
    'vlan': (VLAN, 'name'),
    'role': (Role, 'name'),
    'vrf': (VRF, 'name')
}

for params in prefixes:
    custom_fields = params.pop('custom_fields', None)
    params['prefix'] = IPNetwork(params['prefix'])

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}
            params[assoc] = model.objects.get(**query)
import sys

from django.contrib.contenttypes.models import ContentType
from startup_script_utils import load_yaml
from users.models import AdminGroup, AdminUser, ObjectPermission

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

if object_permissions is None:
    sys.exit()

for permission_name, permission_details in object_permissions.items():

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

    if permission_details.get("object_types", 0):
        object_types = permission_details["object_types"]

        if object_types == "all":
            object_permission.object_types.set(ContentType.objects.all())

        else:
            for app_label, models in object_types.items():
                if models == "all":
                    app_models = ContentType.objects.filter(
Example #25
0
import sys

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

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

if tenants is None:
    sys.exit()

optional_assocs = {"group": (TenantGroup, "name")}

for params in tenants:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

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

    if created:
        set_custom_fields_values(tenant, custom_field_data)

        print("👩‍💻 Created Tenant", tenant.name)
Example #26
0
import sys

from dcim.models import DeviceRole, Platform
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from tenancy.models import Tenant
from virtualization.models import Cluster, VirtualMachine

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

if virtual_machines is None:
    sys.exit()

required_assocs = {"cluster": (Cluster, "name")}

optional_assocs = {
    "tenant": (Tenant, "name"),
    "platform": (Platform, "name"),
    "role": (DeviceRole, "name"),
}

for params in virtual_machines:
    custom_field_data = pop_custom_fields(params)

    # primary ips are handled later in `270_primary_ips.py`
    params.pop("primary_ip4", None)
    params.pop("primary_ip6", None)

    for assoc, details in required_assocs.items():
        model, field = details
        query = {field: params.pop(assoc)}
from dcim.models import RackRole
from utilities.choices import ColorChoices

from startup_script_utils import load_yaml
import sys

rack_roles = load_yaml('/opt/netbox/initializers/rack_roles.yml')

if rack_roles is None:
    sys.exit()

for params in rack_roles:
    if 'color' in params:
        color = params.pop('color')

        for color_tpl in ColorChoices:
            if color in color_tpl:
                params['color'] = color_tpl[0]

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

    if created:
        print("🎨 Created rack role", rack_role.name)
Example #28
0
from secrets.models import SecretRole
from startup_script_utils import load_yaml
import sys

secret_roles = load_yaml('/opt/netbox/initializers/secret_roles.yml')

if secret_roles is None:
    sys.exit()

for params in secret_roles:
    secret_role, created = SecretRole.objects.get_or_create(**params)

    if created:
        print("🔑 Created Secret Role", secret_role.name)
Example #29
0
import sys

from django.contrib.auth.models import Group, User
from startup_script_utils import load_yaml, set_permissions
from users.models import Token

users = load_yaml('/opt/netbox/initializers/users.yml')
if users is None:
    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)

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

        yaml_permissions = user_details.get('permissions', [])
        set_permissions(user.user_permissions, yaml_permissions)
Example #30
0
import sys

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

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

if sites is None:
    sys.exit()

optional_assocs = {"region": (Region, "name"), "tenant": (Tenant, "name")}

for params in sites:
    custom_field_data = pop_custom_fields(params)

    for assoc, details in optional_assocs.items():
        if assoc in params:
            model, field = details
            query = {field: params.pop(assoc)}

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

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

    if created:
        set_custom_fields_values(site, custom_field_data)

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