diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..816b4b87b34eef105f0879ae330538b24110dfd6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.git +.gitignore +.dockerignore +.env +.github +*.sh +__pycache__/ +test +venv +*.egg-info +dist +build +.idea +.DS_Store +Dockerfile +libs/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..30993bc3a1b124b0ed8bd129a1b08f2971c44421 --- /dev/null +++ b/.gitignore @@ -0,0 +1,197 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env.* +!.env.example +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + +## +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..cb4f2bb9a5748c57b4558e6a7d77d8778f0819a5 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,7 @@ +stages: + - build + +build-job: + stage: build + script: + - echo "Build..." diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000000000000000000000000000000000000..4fb69fa29117d68c16fb7f04e9c8903e8ab912a1 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/kreuzwerker/docker" { + version = "3.0.2" + constraints = "3.0.2" + hashes = [ + "h1:XjdpVL61KtTsuPE8swok3GY8A+Bu3TZs8T2DOEpyiXo=", + "zh:15b0a2b2b563d8d40f62f83057d91acb02cd0096f207488d8b4298a59203d64f", + "zh:23d919de139f7cd5ebfd2ff1b94e6d9913f0977fcfc2ca02e1573be53e269f95", + "zh:38081b3fe317c7e9555b2aaad325ad3fa516a886d2dfa8605ae6a809c1072138", + "zh:4a9c5065b178082f79ad8160243369c185214d874ff5048556d48d3edd03c4da", + "zh:5438ef6afe057945f28bce43d76c4401254073de01a774760169ac1058830ac2", + "zh:60b7fadc287166e5c9873dfe53a7976d98244979e0ab66428ea0dea1ebf33e06", + "zh:61c5ec1cb94e4c4a4fb1e4a24576d5f39a955f09afb17dab982de62b70a9bdd1", + "zh:a38fe9016ace5f911ab00c88e64b156ebbbbfb72a51a44da3c13d442cd214710", + "zh:c2c4d2b1fd9ebb291c57f524b3bf9d0994ff3e815c0cd9c9bcb87166dc687005", + "zh:d567bb8ce483ab2cf0602e07eae57027a1a53994aba470fa76095912a505533d", + "zh:e83bf05ab6a19dd8c43547ce9a8a511f8c331a124d11ac64687c764ab9d5a792", + "zh:e90c934b5cd65516fbcc454c89a150bfa726e7cf1fe749790c7480bbeb19d387", + "zh:f05f167d2eaf913045d8e7b88c13757e3cf595dd5cd333057fdafc7c4b7fed62", + "zh:fcc9c1cea5ce85e8bcb593862e699a881bd36dffd29e2e367f82d15368659c3d", + ] +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..50269fd9c0131171bbbb539ef0417fdcd68ea02f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM netboxcommunity/netbox:v3.5-2.6.0 + +USER root +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + gcc \ + libpq-dev \ + python3-dev \ + python3-venv \ + && \ + rm -rf /var/lib/apt/lists/* + +COPY . /opt/netbox/netbox/plugins/netbox_rps_plugin + +RUN pip install /opt/netbox/netbox/plugins/netbox_rps_plugin + +RUN echo "PLUGINS = ['netbox_rps_plugin']" >> /opt/netbox/netbox/netbox/configuration.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000000000000000000000000000000000000..bcccb30277f41e26613c386378db89abd8765966 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +recursive-include src *.html diff --git a/README.md b/README.md index 72c3b28a5b91b07de07fbbaf52fa7badaf3d4412..2347403aa92c86b23be7b98eead5264a6ab2a59d 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,28 @@ -# Netbox Mapping - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://code.europa.eu/digit-c4/netbox-mapping.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://code.europa.eu/digit-c4/netbox-mapping/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +# RPS Plugin for Netbox +A Netbox v3.5 plugin for RPS mapping management + + + +## Quickstart +```shell +$ terraform init +$ terraform apply -var="deploy_service=true" +``` + +And browse [http://localhost:8000](http://localhost:8000). + +* username is `username` +* and password is `password` + +## Build +To build a [`netboxcommunity/netbox:v3.5-2.6.0`](https://hub.docker.com/layers/netboxcommunity/netbox/v3.5-2.6.0/images/sha256-edbb00e74a5cdb4bfeb51abbd9280cc005d1242152a67be7e2cd057e7d568fc2?context=explore) +Docker image including the plugin pre-installed, simply run +```shell +$ docker build . +``` + +Refer to the community [Docker image](https://hub.docker.com/r/netboxcommunity/netbox) +for environment variable configuration. + +## Tests +see [tests directory](./tests/README.md) diff --git a/deployment.tf b/deployment.tf new file mode 100644 index 0000000000000000000000000000000000000000..408d72adbe1617407f34971103f614cf3b4b39e1 --- /dev/null +++ b/deployment.tf @@ -0,0 +1,141 @@ +terraform { + required_providers { + docker = { + source = "kreuzwerker/docker" + version = "3.0.2" + } + } +} + +provider "docker" { + host = "unix:///var/run/docker.sock" +} + +variable "deploy_administration" { + type = bool + default = false +} + +variable "deploy_service" { + type = bool + default = false +} + +resource "docker_network" "network" { + name = "netbox_rps" +} + + +resource "docker_image" "redis" { + name = "redis:7-alpine" +} + +resource "docker_container" "redis" { + image = docker_image.redis.image_id + name = "redis" + + command = [ + "sh", + "-c", # this is to evaluate the $REDIS_PASSWORD from the env + "redis-server --appendonly yes --requirepass \"$REDIS_PASSWORD\"" # $$ is because of Docker + ] + + env = [ + "REDIS_PASSWORD=password" + ] + + ports { + internal = 6379 + external = 6379 + } + + networks_advanced { + name = docker_network.network.name + } +} + +resource "docker_image" "postgres" { + name = "postgres:15-alpine" +} + +resource "docker_container" "postgres" { + image = docker_image.postgres.image_id + name = "postgres" + + env = [ + "POSTGRES_USER=username", + "POSTGRES_PASSWORD=password", + "POSTGRES_DB=netbox" + ] + + ports { + internal = 5432 + external = 5432 + } + + networks_advanced { + name = docker_network.network.name + } +} + +resource "docker_container" "adminer" { + count = var.deploy_administration ? 1 : 0 + image = "adminer" + name = "adminer" + + + ports { + internal = 8080 + external = 8080 + } + + networks_advanced { + name = docker_network.network.name + } +} + +resource "docker_image" "netbox" { + count = var.deploy_service ? 1 : 0 + name = "netbox_rps" + + build { + context = "." + } +} + +resource "docker_container" "netbox" { + count = var.deploy_service ? 1 : 0 + name = "netbox" + image = docker_image.netbox[0].image_id + + ports { + internal = 8080 + external = 8000 + } + + networks_advanced { + name = docker_network.network.name + } + + env = [ + "CORS_ORIGIN_ALLOW_ALL=True", + "DB_HOST=postgres", + "DB_NAME=postgres", + "DB_USER=username", + "DB_PASSWORD=password", + "REDIS_CACHE_DATABASE=1", + "REDIS_CACHE_HOST=redis", + "REDIS_CACHE_PASSWORD=password", + "REDIS_CACHE_SSL=false", + "REDIS_DATABASE=0", + "REDIS_HOST=redis", + "REDIS_PASSWORD=password", + "REDIS_SSL=false", + "SECRET_KEY=r8OwDznj!!dci#ParztZELKezarlkjjlazjkhat9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj", + "SKIP_SUPERUSER=false", + "SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567", + "SUPERUSER_EMAIL=username@global.ntt", + "SUPERUSER_NAME=username", + "SUPERUSER_PASSWORD=password", + ] +} diff --git a/docs/Screenshot 2023-05-14 at 11.38.51.png b/docs/Screenshot 2023-05-14 at 11.38.51.png new file mode 100644 index 0000000000000000000000000000000000000000..6982dd344e1e28d843ca619528f1ac4091420d2a Binary files /dev/null and b/docs/Screenshot 2023-05-14 at 11.38.51.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..0d113763047def4ad5625de9e8c37fb0a6ff1912 --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +from setuptools import find_packages, setup + +setup( + name='netbox-rps-plugin', + version='0.1.0', + description='An example NetBox plugin', + install_requires=[], + packages=find_packages('./src'), + include_package_data=True, + zip_safe=False, + package_dir={'': 'src'} +) diff --git a/src/netbox_rps_plugin/__init__.py b/src/netbox_rps_plugin/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..f40438c1c2453f2a844fce01465674ce8670e096 --- /dev/null +++ b/src/netbox_rps_plugin/__init__.py @@ -0,0 +1,12 @@ +from extras.plugins import PluginConfig + + +class NetBoxRpsConfig(PluginConfig): + name = 'netbox_rps_plugin' + verbose_name = 'NetBox RPS' + description = 'Netbox' + version = '0.1.0' + base_url = 'rps' + + +config = NetBoxRpsConfig diff --git a/src/netbox_rps_plugin/api/__init__.py b/src/netbox_rps_plugin/api/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/netbox_rps_plugin/api/serializers.py b/src/netbox_rps_plugin/api/serializers.py new file mode 100644 index 0000000000000000000000000000000000000000..4dd1c26cfaa5cb956b1df5b222096f7c4586d257 --- /dev/null +++ b/src/netbox_rps_plugin/api/serializers.py @@ -0,0 +1,29 @@ +from rest_framework import serializers +from ..models import Mapping +from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer + + + + +class NestedMappingSerializer(WritableNestedSerializer): + url = serializers.HyperlinkedIdentityField( + view_name='plugins-api:netbox_rps_plugin-api:mapping-detail' + ) + + class Meta: + model = Mapping + fields = ('id', 'url', 'display') + + + +class MappingSerializer(NetBoxModelSerializer): + url = serializers.HyperlinkedIdentityField( + view_name='plugins-api:netbox_rps_plugin-api:mapping-detail' + ) + + class Meta: + model = Mapping + fields = ( + 'id', 'url', 'display', 'source', 'target', 'authentication', 'testingpage', 'webdav', 'Comment', 'custom_fields', 'created', + 'last_updated', + ) diff --git a/src/netbox_rps_plugin/api/urls.py b/src/netbox_rps_plugin/api/urls.py new file mode 100644 index 0000000000000000000000000000000000000000..54eee974e726f19b4c5c7ade9df2873968919fe1 --- /dev/null +++ b/src/netbox_rps_plugin/api/urls.py @@ -0,0 +1,9 @@ +from netbox.api.routers import NetBoxRouter +from . import views + +app_name = 'netbox_rps_plugin' + +router = NetBoxRouter() +router.register('mapping', views.MappingViewSet) + +urlpatterns = router.urls diff --git a/src/netbox_rps_plugin/api/views.py b/src/netbox_rps_plugin/api/views.py new file mode 100644 index 0000000000000000000000000000000000000000..85749030d4ac5f9a01708e194200353f9497384e --- /dev/null +++ b/src/netbox_rps_plugin/api/views.py @@ -0,0 +1,10 @@ +from netbox.api.viewsets import NetBoxModelViewSet +from .. import filtersets, models +from .serializers import MappingSerializer + + +class MappingViewSet(NetBoxModelViewSet): + queryset = models.Mapping.objects.all() + serializer_class = MappingSerializer + filterset_class = filtersets.MappingFilterSet + http_method_names = ['get', 'post', 'patch'] diff --git a/src/netbox_rps_plugin/filtersets.py b/src/netbox_rps_plugin/filtersets.py new file mode 100644 index 0000000000000000000000000000000000000000..19625df3ec8c0c56892f6181cc46bdd67cfdec91 --- /dev/null +++ b/src/netbox_rps_plugin/filtersets.py @@ -0,0 +1,13 @@ +from netbox.filtersets import NetBoxModelFilterSet +from .models import Mapping +from django.db.models import Q + +class MappingFilterSet(NetBoxModelFilterSet): + + class Meta: + model = Mapping + fields = ('id', 'authentication', 'source', 'target', 'Comment') + + def search(self, queryset, name, value): + return queryset.filter( Q(source__icontains=value) | Q(target__icontains=value) + | Q(authentication__icontains=value) | Q(Comment__icontains=value)) diff --git a/src/netbox_rps_plugin/forms.py b/src/netbox_rps_plugin/forms.py new file mode 100644 index 0000000000000000000000000000000000000000..a2fa2ac3a34a47369c41e9f205a203f57953be69 --- /dev/null +++ b/src/netbox_rps_plugin/forms.py @@ -0,0 +1,30 @@ +from django import forms +from netbox.forms import NetBoxModelForm +from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm +from .models import Mapping, AuthenticationChoices + + +class MappingForm(NetBoxModelForm): + + class Meta: + model = Mapping + fields = ( + 'source', 'target', 'authentication', 'webdav', 'testingpage', 'Comment', 'tags', + ) + help_texts = { + 'target': 'URL-friendly unique shorthand' + } + labels = { + 'source': 'Source', + 'target': 'Target', + } + + + +class MappingFilterForm(NetBoxModelFilterSetForm): + model = Mapping + authentication = forms.MultipleChoiceField( + choices=AuthenticationChoices, + required=False, + ) + diff --git a/src/netbox_rps_plugin/migrations/0001_initial.py b/src/netbox_rps_plugin/migrations/0001_initial.py new file mode 100644 index 0000000000000000000000000000000000000000..0343a53ec93f52fa6247f2576ecf6c839ce898ee --- /dev/null +++ b/src/netbox_rps_plugin/migrations/0001_initial.py @@ -0,0 +1,40 @@ +# Generated by Django 4.1.5 on 2023-02-13 13:42 + +import django.contrib.postgres.fields +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers +import utilities.json + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('extras', '0084_staging'), + ('ipam', '0063_standardize_description_comments'), + ] + + operations = [ + migrations.CreateModel( + name='Mapping', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)), + ('source', models.CharField(blank=True, max_length=120)), + ('target', models.CharField(blank=True, max_length=120)), + ('authentication', models.CharField(max_length=30)), + ('testingpage', models.CharField(max_length=120)), + ('webdav', models.BooleanField(default=False)), + ('Comment', models.CharField(blank=True, max_length=500)), + ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), + ], + options={ + 'ordering': ('source'), + }, + ), + ] + diff --git a/src/netbox_rps_plugin/migrations/__init__.py b/src/netbox_rps_plugin/migrations/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/netbox_rps_plugin/models.py b/src/netbox_rps_plugin/models.py new file mode 100644 index 0000000000000000000000000000000000000000..261dec7dc41e95d7578dd11b45dfa4f6464bde32 --- /dev/null +++ b/src/netbox_rps_plugin/models.py @@ -0,0 +1,67 @@ +from django.contrib.postgres.fields import ArrayField +from django.db import models +from netbox.models import NetBoxModel +from utilities.choices import ChoiceSet +from django.urls import reverse +from django.core.validators import URLValidator + +class AuthenticationChoices(ChoiceSet): + key = 'Mapping.authentication' + + DEFAULT_VALUE = 'none' + + CHOICES = [ + ('none', 'None', 'dark'), + ('ldap', 'Ldap', 'blue'), + ('ecas', 'Ecas', 'blue'), + ] + + +class Mapping(NetBoxModel): + + source = models.CharField( + max_length=120, + blank=False, + verbose_name='Name', + validators=[URLValidator(message='It must be a url')], + ) + target = models.CharField( + max_length=120, + blank=False, + verbose_name='Identifier', + validators=[URLValidator(message='It must be a url')], + + ) + authentication = models.CharField( + max_length=30, + choices=AuthenticationChoices, + default=AuthenticationChoices.DEFAULT_VALUE, + blank=False, + verbose_name='Auth', + ) + testingpage = models.CharField( + max_length=120, + blank=False, + validators=[URLValidator(message='It must be a url')], + ) + webdav = models.BooleanField( + default=False, + ) + + Comment = models.CharField( + max_length=500, + blank=True + ) + + + class Meta: + ordering = ('source', 'target') + + def __str__(self): + return f'{self.source}' + + def get_absolute_url(self): + return reverse('plugins:netbox_rps_plugin:mapping', args=[self.pk]) + + def get_authentication_color(self): + return AuthenticationChoices.colors.get(self.authentication) diff --git a/src/netbox_rps_plugin/navigation.py b/src/netbox_rps_plugin/navigation.py new file mode 100644 index 0000000000000000000000000000000000000000..d0894f81f55a61bbeab91a4f4fc339e8e3b503f0 --- /dev/null +++ b/src/netbox_rps_plugin/navigation.py @@ -0,0 +1,34 @@ +from extras.plugins import PluginMenuButton, PluginMenuItem, PluginMenu +from utilities.choices import ButtonColorChoices + +mapping_butons = [ + PluginMenuButton( + link='plugins:netbox_rps_plugin:mapping_add', + title='Add', + icon_class='mdi mdi-plus-thick', + color=ButtonColorChoices.GREEN + ), + PluginMenuButton( + link='plugins:netbox_rps_plugin:mapping_add', + title='Import', + icon_class='mdi mdi-upload', + color=ButtonColorChoices.CYAN + ), +] + +mappingItem = PluginMenuItem( + link='plugins:netbox_rps_plugin:mapping_list', + link_text='Mappings', + buttons=mapping_butons + ), + +menu = ( + PluginMenu( + label='Mappings', + groups=( + ('MAPPINGS', mappingItem), + ), + icon_class='mdi mdi-graph-outline' + ) +) + diff --git a/src/netbox_rps_plugin/search.py b/src/netbox_rps_plugin/search.py new file mode 100644 index 0000000000000000000000000000000000000000..39a99b721afafcf4cd66843c3f2ade702031b812 --- /dev/null +++ b/src/netbox_rps_plugin/search.py @@ -0,0 +1,15 @@ +from netbox.search import SearchIndex, register_search +from .models import Mapping + + + +@register_search +class MappingIndex(SearchIndex): + model = Mapping + fields = ( + ('source', 120), + ('target', 120), + ('authentication', 30), + ('Comment', 500), + ) + diff --git a/src/netbox_rps_plugin/tables.py b/src/netbox_rps_plugin/tables.py new file mode 100644 index 0000000000000000000000000000000000000000..98cf82dcc40b02ffa755c51c43b1b0e9e7c579f7 --- /dev/null +++ b/src/netbox_rps_plugin/tables.py @@ -0,0 +1,18 @@ +import django_tables2 as tables + +from netbox.tables import NetBoxTable, ChoiceFieldColumn +from .models import Mapping + + +class MappingTable(NetBoxTable): + authentication = ChoiceFieldColumn() + + class Meta(NetBoxTable.Meta): + model = Mapping + fields = ( + 'pk', 'id', 'source', 'target', 'authentication', 'testingpage', 'webdav', 'Comment' + ) + default_columns = ( + 'source', 'target', 'authentication', 'Comment' + ) + diff --git a/src/netbox_rps_plugin/templates/netbox_rps_plugin/accesslist.html b/src/netbox_rps_plugin/templates/netbox_rps_plugin/accesslist.html new file mode 100644 index 0000000000000000000000000000000000000000..40df33b02e436b5b126dce384e158c657c78cacd --- /dev/null +++ b/src/netbox_rps_plugin/templates/netbox_rps_plugin/accesslist.html @@ -0,0 +1,44 @@ +{% extends 'generic/object.html' %} +{% load render_table from django_tables2 %} + +{% block content %} + <div class="row mb-3"> + <div class="col col-md-6"> + <div class="card"> + <h5 class="card-header">Access List</h5> + <div class="card-body"> + <table class="table table-hover attr-table"> + <tr> + <th scope="row">Name</th> + <td>{{ object.name }}</td> + </tr> + <tr> + <th scope="row">Default Action</th> + <td>{{ object.get_default_action_display }}</td> + </tr> + <tr> + <th scope="row">Rules</th> + <td>{{ object.rules.count }}</td> + </tr> + </table> + </div> + </div> + {% include 'inc/panels/custom_fields.html' %} + </div> + <div class="col col-md-6"> + {% include 'inc/panels/tags.html' %} + {% include 'inc/panels/comments.html' %} + </div> + </div> + + <div class="row"> + <div class="col col-md-12"> + <div class="card"> + <h5 class="card-header">Rules</h5> + <div class="card-body table-responsive"> + {% render_table rules_table %} + </div> + </div> + </div> + </div> +{% endblock content %} diff --git a/src/netbox_rps_plugin/templates/netbox_rps_plugin/mapping.html b/src/netbox_rps_plugin/templates/netbox_rps_plugin/mapping.html new file mode 100644 index 0000000000000000000000000000000000000000..e90dd64ad583c73b23d19f504f869789ec1acd69 --- /dev/null +++ b/src/netbox_rps_plugin/templates/netbox_rps_plugin/mapping.html @@ -0,0 +1,70 @@ +{% extends 'generic/object.html' %} + +{% block content %} + <div class="row mb-3"> + <div class="col col-md-6"> + <div class="card"> + <h5 class="card-header">MAPPINGS</h5> + <div class="card-body"> + <table class="table table-hover attr-table"> + <tr> + <th scope="row">Name</th> + <td>{{ object.source }}</td> + </tr> + <tr> + <th scope="row">Comment</th> + <td>{{ object.Comment|placeholder }}</td> + </tr> + </table> + </div> + </div> + {% include 'inc/panels/custom_fields.html' %} + {% include 'inc/panels/tags.html' %} + </div> + <div class="col col-md-6"> + <div class="card"> + <h5 class="card-header">Details</h5> + <div class="card-body"> + <table class="table table-hover attr-table"> + <tr> + <th scope="row">Protocol</th> + <td>{{ object.get_protocol_display }}</td> + </tr> + <tr> + <th scope="row">Source Prefix</th> + <td> + {% if object.source_prefix %} + <a href="{{ object.source_prefix.get_absolute_url }}">{{ object.source_prefix }}</a> + {% else %} + {{ ''|placeholder }} + {% endif %} + </td> + </tr> + <tr> + <th scope="row">Source Ports</th> + <td>{{ object.source_ports|join:", "|placeholder }}</td> + </tr> + <tr> + <th scope="row">Destination Prefix</th> + <td> + {% if object.destination_prefix %} + <a href="{{ object.destination_prefix.get_absolute_url }}">{{ object.destination_prefix }}</a> + {% else %} + {{ ''|placeholder }} + {% endif %} + </td> + </tr> + <tr> + <th scope="row">Destination Ports</th> + <td>{{ object.destination_ports|join:", "|placeholder }}</td> + </tr> + <tr> + <th scope="row">Action</th> + <td>{{ object.get_action_display }}</td> + </tr> + </table> + </div> + </div> + </div> + </div> +{% endblock content %} diff --git a/src/netbox_rps_plugin/urls.py b/src/netbox_rps_plugin/urls.py new file mode 100644 index 0000000000000000000000000000000000000000..b2ea124e32585afdb2c9e32738e977d9344b85de --- /dev/null +++ b/src/netbox_rps_plugin/urls.py @@ -0,0 +1,16 @@ +from django.urls import path +from netbox_rps_plugin import views, models +from netbox.views.generic import ObjectChangeLogView + +urlpatterns = ( + + # Mapping + path('mappings/', views.MappingListView.as_view(), name='mapping_list'), + path('mappings/add/', views.MappingEditView.as_view(), name='mapping_add'), + path('mappings/<int:pk>/', views.MappingView.as_view(), name='mapping'), + path('mappings/<int:pk>/edit/', views.MappingEditView.as_view(), name='mapping_edit'), + path('mappings/<int:pk>/delete/', views.MappingDeleteView.as_view(), name='mapping_delete'), + path('mappings/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='mapping_changelog', kwargs={ + 'model': models.Mapping + }), +) diff --git a/src/netbox_rps_plugin/views.py b/src/netbox_rps_plugin/views.py new file mode 100644 index 0000000000000000000000000000000000000000..9a76bab15fb6f5fc4135fbd7610880b0a66f6ad6 --- /dev/null +++ b/src/netbox_rps_plugin/views.py @@ -0,0 +1,23 @@ +from netbox.views import generic +from netbox_rps_plugin import forms, tables, filtersets, models + + +class MappingView(generic.ObjectView): + queryset = models.Mapping.objects.all() + + +class MappingListView(generic.ObjectListView): + queryset = models.Mapping.objects.all() + table = tables.MappingTable + filterset = filtersets.MappingFilterSet + filterset_form = forms.MappingFilterForm + + +class MappingEditView(generic.ObjectEditView): + queryset = models.Mapping.objects.all() + form = forms.MappingForm + + +class MappingDeleteView(generic.ObjectDeleteView): + queryset = models.Mapping.objects.all() + diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e6a6cae4491c8806a7321cb2f445c2968f7cb8e8 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,21 @@ +# Testing the library + +## Demo +``` +TODO +``` + +## End to end +Deploy a full stack +```shell +$ terraform init +$ terraform apply -var="deploy_service=true" +``` +wait `netbox` container to be ready (run `docker logs -f netbox` to watch the logs) +and prepare a python environment to execute the E2E tests suite +```shell +$ python3 -m venv venv +$ source venv/bin/activate +$ pip install -r tests/requirements.e2e.txt +$ python -m unittest discover tests/e2e +``` diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/e2e/__init__.py b/tests/e2e/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/e2e/test_unauthenticated.py b/tests/e2e/test_unauthenticated.py new file mode 100644 index 0000000000000000000000000000000000000000..4ae2d5f9bb725343b6b92cf6e96e571b30a2305c --- /dev/null +++ b/tests/e2e/test_unauthenticated.py @@ -0,0 +1,39 @@ +import unittest +import requests + + +class TestUnauthenticatedMappings(unittest.TestCase): + + def test_mappings_get_unauthenticated(self): + r = requests.get('http://localhost:8000/api/plugins/rps/') + + self.assertEqual(r.status_code, 403) + + def test_mappings_get_unauthenticated(self): + r = requests.get('http://localhost:8000/api/plugins/rps/mapping/') + + self.assertEqual(r.status_code, 403) + + def test_mappings_post_unauthenticated(self): + r = requests.post('http://localhost:8000/api/plugins/rps/mapping/') + + self.assertEqual(r.status_code, 403) + + def test_mappings_patch_unauthenticated(self): + r = requests.patch('http://localhost:8000/api/plugins/rps/mapping/') + + self.assertEqual(r.status_code, 403) + + def test_mappings_delete_unauthenticated(self): + r = requests.delete('http://localhost:8000/api/plugins/rps/mapping/') + + self.assertEqual(r.status_code, 403) + + def test_mappings_put_unauthenticated(self): + r = requests.put('http://localhost:8000/api/plugins/rps/mapping') + + self.assertEqual(r.status_code, 403) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/requirements.e2e.txt b/tests/requirements.e2e.txt new file mode 100644 index 0000000000000000000000000000000000000000..becc27ff2e02f26b19133cd3224759d1e8578b79 --- /dev/null +++ b/tests/requirements.e2e.txt @@ -0,0 +1 @@ +requests==2.30.0