Skip to content

Commit

Permalink
Unify api serializers (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkoot authored Apr 19, 2023
1 parent 7f18a3a commit 88edcde
Show file tree
Hide file tree
Showing 29 changed files with 127 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
COMPOSE_PATH_SEPARATOR=:
COMPOSE_FILE=ci/docker-compose.yml:ci/docker-compose.override.yml
COMPOSE_FILE=ci/docker-compose.yml:ci/docker-compose.ci.yml
COMPOSE_PROJECT_NAME=netbox-docker
23 changes: 20 additions & 3 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Docker Image CI

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
Expand All @@ -15,5 +20,17 @@ jobs:
# clearcache is provided by Django extras, which is not loaded if netbox-slm cannot be loaded
run: docker compose run netbox sh -c "/opt/netbox/venv/bin/python manage.py clearcache"

- name: Run Django tests for netbox_slm
run: docker compose run netbox sh -c "/opt/netbox/venv/bin/python manage.py test netbox_slm"
- name: Run Django tests with coverage for netbox_slm
run: |
chmod go+w ci/reports
docker compose run netbox sh -c " \
/opt/netbox/venv/bin/coverage run --source='netbox_slm' manage.py test netbox_slm && \
/opt/netbox/venv/bin/coverage report --fail-under=0 && \
/opt/netbox/venv/bin/coverage xml -o /ci/reports/coverage.xml"
- name: Sonarcloud scan
if: env.SONAR_TOKEN != null
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
**/.vscode
**/dist
**/netbox_slm.egg-info
ci/docker-compose.override.yml
ci/reports
out/production
5 changes: 3 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include ../LICENSE
include ../README.rst
recursive-include netbox_slm/templates *
include ../README.md
include ../SECURITY.md
recursive-include netbox_slm/templates *
4 changes: 4 additions & 0 deletions ci/Dockerfile-CI
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
FROM netboxcommunity/netbox:v3.4.7

RUN mkdir /ci && chmod go+w /ci
COPY ../ci/requirements_ci.txt /ci/
RUN pip install -r /ci/requirements_ci.txt

COPY ../netbox_slm /opt/netbox/netbox/netbox_slm
7 changes: 7 additions & 0 deletions ci/configuration/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
DEBUG = True
SECRET_KEY = 'dummy'
DEVELOPER = True
PLUGINS_CONFIG = {
'netbox_slm': dict(
TEST_RUNNER='xmlrunner.extra.djangotestrunner.XMLTestRunner',
TEST_OUTPUT_DIR='/ci/reports/',
TEST_OUTPUT_FILE_NAME='junit.xml',
)
}
28 changes: 0 additions & 28 deletions ci/configuration/ldap/extra.py

This file was deleted.

90 changes: 0 additions & 90 deletions ci/configuration/ldap/ldap_config.py

This file was deleted.

4 changes: 4 additions & 0 deletions ci/docker-compose.override.yml → ci/docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ services:
context: ..
dockerfile: ci/Dockerfile-CI
image: netbox:slm
environment:
- COVERAGE_FILE=/ci/.coverage
volumes:
- ./reports:/ci/reports
netbox-worker:
image: netbox:slm
netbox-housekeeping:
Expand Down
2 changes: 0 additions & 2 deletions ci/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ services:
user: 'unit:root'
volumes:
- ./configuration:/etc/netbox/config:z,ro
# - ./reports:/etc/netbox/reports:z,ro
# - ./scripts:/etc/netbox/scripts:z,ro
- netbox-media-files:/opt/netbox/netbox/media:z
netbox-worker:
<<: *netbox
Expand Down
Empty file added ci/reports/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions ci/requirements_ci.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage==7.2.2
unittest-xml-reporting==3.2.0
9 changes: 5 additions & 4 deletions netbox_slm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
class SLMConfig(PluginConfig):
name = 'netbox_slm'
verbose_name = 'Software Lifecycle Management'
description = 'Software Lifecycle Management'
version = '1.2'
author = 'Hedde van der Heide'
author_email = 'hedde.vanderheide@ictu.nl'
description = 'Software Lifecycle Management Netbox Plugin.'
version = '1.3'
author = 'ICTU'
author_email = 'open-source-projects@ictu.nl'
base_url = 'slm'
required_settings = []
default_settings = {
'version_info': False
}


config = SLMConfig
10 changes: 4 additions & 6 deletions netbox_slm/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from rest_framework import serializers

from netbox.api.serializers import NetBoxModelSerializer
from netbox_slm.models import (
SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation,
)
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation


class SoftwareProductSerializer(NetBoxModelSerializer):
Expand All @@ -19,7 +17,7 @@ class Meta:
]

def get_display(self, obj):
return f"{obj.manufacturer.name} - {obj.name}"
return f"{obj.manufacturer} - {obj}"


class SoftwareProductVersionSerializer(NetBoxModelSerializer):
Expand All @@ -35,7 +33,7 @@ class Meta:
]

def get_display(self, obj):
return obj.name
return f"{obj}"


class SoftwareProductInstallationSerializer(NetBoxModelSerializer):
Expand All @@ -52,4 +50,4 @@ class Meta:
]

def get_display(self, obj):
return obj
return f"{obj}"
2 changes: 1 addition & 1 deletion netbox_slm/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
router.register("softwareproducts", SoftwareProductViewSet)
router.register("softwareproductversions", SoftwareProductVersionViewSet)
router.register("softwareproductinstallations", SoftwareProductInstallationViewSet)
urlpatterns = router.urls
urlpatterns = router.urls
6 changes: 2 additions & 4 deletions netbox_slm/api/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from netbox.api.viewsets import NetBoxModelViewSet
from rest_framework.routers import APIRootView

from netbox.api.viewsets import NetBoxModelViewSet
from netbox_slm.api.serializers import (
SoftwareProductSerializer, SoftwareProductVersionSerializer, SoftwareProductInstallationSerializer,
)
from netbox_slm.filtersets import (
SoftwareProductFilterSet, SoftwareProductVersionFilterSet, SoftwareProductInstallationFilterSet,
)
from netbox_slm.models import (
SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation,
)
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation


class NetboxSLMRootView(APIRootView):
Expand Down
8 changes: 5 additions & 3 deletions netbox_slm/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.db.models import Q

from netbox.filtersets import NetBoxModelFilterSet
from netbox_slm.models import *
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation


class SoftwareProductFilterSet(NetBoxModelFilterSet):
"""Filter capabilities for SoftwareProduct instances."""

class Meta:
model = SoftwareProduct
fields = tuple()
Expand All @@ -14,13 +15,13 @@ def search(self, queryset, name, value):
"""Perform the filtered search."""
if not value.strip():
return queryset
qs_filter = Q(name__icontains=value) | \
Q(manufacturer__name__icontains=value)
qs_filter = Q(name__icontains=value) | Q(manufacturer__name__icontains=value)
return queryset.filter(qs_filter)


class SoftwareProductVersionFilterSet(NetBoxModelFilterSet):
"""Filter capabilities for SoftwareProductVersion instances."""

class Meta:
model = SoftwareProductVersion
fields = (
Expand All @@ -39,6 +40,7 @@ def search(self, queryset, name, value):

class SoftwareProductInstallationFilterSet(NetBoxModelFilterSet):
"""Filter capabilities for SoftwareProductInstallation instances."""

class Meta:
model = SoftwareProductInstallation
fields = tuple()
Expand Down
11 changes: 2 additions & 9 deletions netbox_slm/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
from django.utils.translation import gettext as _

from dcim.models import Manufacturer, Device
from netbox.forms import (
NetBoxModelForm,
NetBoxModelCSVForm,
NetBoxModelBulkEditForm,
NetBoxModelFilterSetForm,
)
from netbox.forms import NetBoxModelForm, NetBoxModelCSVForm, NetBoxModelBulkEditForm, NetBoxModelFilterSetForm
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation
from utilities.forms import (
DynamicModelChoiceField, APISelect, TagFilterField, ChoiceField
)
from utilities.forms import DynamicModelChoiceField, APISelect, TagFilterField
from virtualization.models import VirtualMachine


Expand Down
4 changes: 2 additions & 2 deletions netbox_slm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class SoftwareProduct(NetBoxModel):
name = models.CharField(max_length=128)
description = models.CharField(max_length=255, null=True, blank=True)

manufacturer = models.ForeignKey(
to='dcim.Manufacturer',
on_delete=models.PROTECT,
Expand Down Expand Up @@ -89,4 +89,4 @@ def get_platform(self):
return self.device or self.virtualmachine

def render_type(self):
return f"{'device' if self.device else 'virtualmachine'}"
return 'device' if self.device else 'virtualmachine'
1 change: 0 additions & 1 deletion netbox_slm/navigation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from extras.plugins import PluginMenuButton, PluginMenuItem
from utilities.choices import ButtonColorChoices


menu_items = (
PluginMenuItem(
link='plugins:netbox_slm:softwareproduct_list',
Expand Down
4 changes: 2 additions & 2 deletions netbox_slm/tables.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import django_tables2 as tables

from django.db.models import Count
from django_tables2.utils import Accessor

from netbox.tables import NetBoxTable, ToggleColumn, columns
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation
from netbox.tables import NetBoxTable, ChoiceFieldColumn, ToggleColumn, columns


class SoftwareProductTable(NetBoxTable):
Expand Down
Loading

0 comments on commit 88edcde

Please sign in to comment.