-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from netboxlabs/develop
🚚 release
- Loading branch information
Showing
13 changed files
with
115 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
75 changes: 0 additions & 75 deletions
75
netbox_diode_plugin/management/commands/configurediodeplugin.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2024 NetBox Labs Inc | ||
"""Diode Netbox Plugin - Database migrations.""" | ||
|
||
import os | ||
|
||
from django.apps import apps as django_apps | ||
from django.conf import settings | ||
from django.contrib.contenttypes.management import create_contenttypes | ||
from django.db import migrations, models | ||
from users.models import Token as NetBoxToken | ||
|
||
|
||
def _create_user_with_token(apps, username, group, is_superuser: bool = False): | ||
User = apps.get_model(settings.AUTH_USER_MODEL) | ||
"""Create a user with the given username and API key if it does not exist.""" | ||
try: | ||
user = User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
if is_superuser: | ||
user = User.objects.create_superuser(username=username, is_active=True) | ||
else: | ||
user = User.objects.create(username=username, is_active=True) | ||
|
||
user.groups.add(*[group.id]) | ||
|
||
Token = apps.get_model("users", "Token") | ||
|
||
if not Token.objects.filter(user=user).exists(): | ||
api_key = os.getenv(f"{username}_API_KEY") | ||
if api_key is None: | ||
api_key = NetBoxToken.generate_key() | ||
Token.objects.create(user=user, key=api_key) | ||
|
||
return user | ||
|
||
|
||
def configure_plugin(apps, schema_editor): | ||
"""Configure the plugin.""" | ||
diode_to_netbox_username = "DIODE_TO_NETBOX" | ||
netbox_to_diode_username = "NETBOX_TO_DIODE" | ||
diode_username = "DIODE" | ||
|
||
Group = apps.get_model("users", "Group") | ||
group, _ = Group.objects.get_or_create(name="diode") | ||
|
||
diode_to_netbox_user = _create_user_with_token( | ||
apps, diode_to_netbox_username, group | ||
) | ||
_ = _create_user_with_token(apps, netbox_to_diode_username, group, True) | ||
_ = _create_user_with_token(apps, diode_username, group) | ||
|
||
app_config = django_apps.get_app_config("netbox_diode_plugin") | ||
|
||
create_contenttypes(app_config, verbosity=0) | ||
|
||
ContentType = apps.get_model("contenttypes", "ContentType") | ||
|
||
diode_plugin_object_type = ContentType.objects.get( | ||
app_label="netbox_diode_plugin", model="diode" | ||
) | ||
|
||
ObjectPermission = apps.get_model("users", "ObjectPermission") | ||
permission, _ = ObjectPermission.objects.get_or_create( | ||
name="Diode", | ||
actions=["add", "view"], | ||
) | ||
|
||
permission.groups.set([group.id]) | ||
permission.users.set([diode_to_netbox_user.id]) | ||
permission.object_types.set([diode_plugin_object_type.id]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""Initial migration.""" | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
("contenttypes", "0001_initial"), | ||
("users", "0006_custom_group_model"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
# Does not create any table / fields in the database | ||
# Registers the Diode model as migrated | ||
# This model is used to generate permissions for the Diode NetBox Plugin | ||
name="Diode", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False | ||
), | ||
), | ||
], | ||
options={ | ||
"permissions": ( | ||
("view_diode", "Can view Diode"), | ||
("add_diode", "Can apply change sets from Diode"), | ||
), | ||
"managed": False, | ||
"default_permissions": (), | ||
}, | ||
), | ||
migrations.RunPython( | ||
code=configure_plugin, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters