Skip to content

Commit

Permalink
Merge pull request #18 from netboxlabs/develop
Browse files Browse the repository at this point in the history
🚚 release
  • Loading branch information
mfiedorowicz authored Aug 2, 2024
2 parents f24705c + 508915e commit e60458e
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ docker-compose-netbox-plugin-down:

.PHONY: docker-compose-netbox-plugin-test
docker-compose-netbox-plugin-test:
-@$(DOCKER_COMPOSE) -f docker/docker-compose.yaml -f docker/docker-compose.test.yaml run -u root --rm netbox ./manage.py test --keepdb netbox_diode_plugin
-@$(DOCKER_COMPOSE) -f docker/docker-compose.yaml run -u root --rm netbox ./manage.py test --keepdb netbox_diode_plugin
@$(MAKE) docker-compose-netbox-plugin-down

.PHONY: docker-compose-netbox-plugin-test-cover
docker-compose-netbox-plugin-test-cover:
-@$(DOCKER_COMPOSE) -f docker/docker-compose.yaml -f docker/docker-compose.test.yaml run --rm -u root -e COVERAGE_FILE=/opt/netbox/netbox/coverage/.coverage netbox sh -c "coverage run --source=netbox_diode_plugin ./manage.py test --keepdb netbox_diode_plugin && coverage xml -o /opt/netbox/netbox/coverage/report.xml && coverage report -m | tee /opt/netbox/netbox/coverage/report.txt"
-@$(DOCKER_COMPOSE) -f docker/docker-compose.yaml run --rm -u root -e COVERAGE_FILE=/opt/netbox/netbox/coverage/.coverage netbox sh -c "coverage run --source=netbox_diode_plugin ./manage.py test --keepdb netbox_diode_plugin && coverage xml -o /opt/netbox/netbox/coverage/report.xml && coverage report -m | tee /opt/netbox/netbox/coverage/report.txt"
@$(MAKE) docker-compose-netbox-plugin-down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export DIODE_API_KEY=$(head -c20 </dev/urandom|xxd -p); env | grep DIODE_API_KEY

**Note:** store these API key strings in a safe place as they will be needed later to configure the Diode server

Configure the plugin to use the previously generated API keys:
Run migrations to create all necessary resources:

```shell
cd /opt/netbox/netbox
./manage.py configurediodeplugin
./manage.py migrate netbox_diode_plugin
```

## Running Tests
Expand Down
2 changes: 0 additions & 2 deletions docker/Dockerfile-diode-netbox-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ COPY ./netbox/configuration/ /etc/netbox/config/
RUN chmod 755 /etc/netbox/config/* && \
chown unit:root /etc/netbox/config/*

COPY ./netbox/enable-diode-plugin.sh /opt/netbox/enable-diode-plugin.sh

COPY ./requirements-diode-netbox-plugin.txt /opt/netbox/
RUN /opt/netbox/venv/bin/pip install --no-warn-script-location -r /opt/netbox/requirements-diode-netbox-plugin.txt
5 changes: 0 additions & 5 deletions docker/docker-compose.test.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions docker/netbox/configuration-test/plugins.py

This file was deleted.

2 changes: 1 addition & 1 deletion docker/netbox/configuration/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# To learn how to build images with your required plugins
# See https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins

# PLUGINS = ["netbox_diode_plugin"]
PLUGINS = ["netbox_diode_plugin"]

# PLUGINS_CONFIG = {
# "netbox_diode_plugin": {
Expand Down
9 changes: 0 additions & 9 deletions docker/netbox/enable-diode-plugin.sh

This file was deleted.

2 changes: 0 additions & 2 deletions docker/netbox/launch-netbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ reload_netbox() {

load_configuration &

sh /opt/netbox/enable-diode-plugin.sh &

reload_netbox &

exec unitd \
Expand Down
3 changes: 0 additions & 3 deletions netbox_diode_plugin/management/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions netbox_diode_plugin/management/commands/__init__.py

This file was deleted.

75 changes: 0 additions & 75 deletions netbox_diode_plugin/management/commands/configurediodeplugin.py

This file was deleted.

110 changes: 110 additions & 0 deletions netbox_diode_plugin/migrations/0001_initial.py
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
),
]
4 changes: 0 additions & 4 deletions netbox_diode_plugin/tests/test_apply_change_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class BaseApplyChangeSet(APITestCase):

def setUp(self):
"""Set up test."""
# Necessary to use with signals.
self.user_netbox_to_diode = User.objects.create_user(username="NETBOX_TO_DIODE")
Token.objects.create(user=self.user_netbox_to_diode)

self.user = User.objects.create_user(username="testcommonuser")
self.add_permissions("netbox_diode_plugin.add_diode")
self.user_token = Token.objects.create(user=self.user)
Expand Down

0 comments on commit e60458e

Please sign in to comment.