diff --git a/netbox_docker_plugin/__init__.py b/netbox_docker_plugin/__init__.py index 2f25f69..e750e69 100644 --- a/netbox_docker_plugin/__init__.py +++ b/netbox_docker_plugin/__init__.py @@ -10,7 +10,7 @@ class NetBoxDockerConfig(PluginConfig): name = "netbox_docker_plugin" verbose_name = " NetBox Docker Plugin" description = "Manage Docker" - version = "2.5.0" + version = "2.6.0" base_url = "docker" min_version = "4.0.0" author= "Vincent Simonin , David Delassus " diff --git a/netbox_docker_plugin/api/serializers.py b/netbox_docker_plugin/api/serializers.py index 9ab92d3..97ca257 100644 --- a/netbox_docker_plugin/api/serializers.py +++ b/netbox_docker_plugin/api/serializers.py @@ -146,6 +146,7 @@ class Meta: "restart_policy", "operation", "hostname", + "cap_add", ) @@ -406,6 +407,7 @@ class Meta: "ContainerID", "hostname", "restart_policy", + "cap_add", "ports", "env", "labels", diff --git a/netbox_docker_plugin/forms/container.py b/netbox_docker_plugin/forms/container.py index 4833daf..ce54156 100644 --- a/netbox_docker_plugin/forms/container.py +++ b/netbox_docker_plugin/forms/container.py @@ -15,7 +15,7 @@ ) from ..models.volume import Volume from ..models.host import Host -from ..models.container import Container, ContainerRestartPolicyChoices +from ..models.container import Container, ContainerRestartPolicyChoices, ContainerCapAddChoices from ..models.image import Image @@ -31,6 +31,7 @@ class ContainerForm(NetBoxModelForm): required=True, query_params={"host_id": "$host"}, ) + cap_add = forms.MultipleChoiceField(choices=ContainerCapAddChoices, required=False) class Meta: """Container form definition Meta class""" @@ -42,6 +43,7 @@ class Meta: "name", "hostname", "restart_policy", + "cap_add", "tags", ) labels = { @@ -50,6 +52,7 @@ class Meta: "image": "Image", "hostname": "Hostname", "restart_policy": "Restart Policy", + "cap_add": "Add Host capabilities", } @@ -62,6 +65,7 @@ class ContainerEditForm(NetBoxModelForm): required=True, query_params={"host_id": "$host"}, ) + cap_add = forms.MultipleChoiceField(choices=ContainerCapAddChoices, required=False) class Meta: """Container form definition Meta class""" @@ -72,6 +76,7 @@ class Meta: "name", "hostname", "restart_policy", + "cap_add", "tags", ) labels = { @@ -79,6 +84,7 @@ class Meta: "image": "Image", "hostname": "Hostname", "restart_policy": "Restart Policy", + "cap_add": "Add Host capabilities", } diff --git a/netbox_docker_plugin/migrations/0031_container_cap_add.py b/netbox_docker_plugin/migrations/0031_container_cap_add.py new file mode 100644 index 0000000..d60239f --- /dev/null +++ b/netbox_docker_plugin/migrations/0031_container_cap_add.py @@ -0,0 +1,29 @@ +# pylint: disable=C0103 +"""Migration file""" + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + """Migration file""" + + dependencies = [ + ( + "netbox_docker_plugin", + "0030_alter_container_containerid_alter_container_hostname_and_more", + ), + ] + + operations = [ + migrations.AddField( + model_name="container", + name="cap_add", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(blank=True, max_length=32, null=True), + blank=True, + null=True, + size=None, + ), + ), + ] diff --git a/netbox_docker_plugin/models/container.py b/netbox_docker_plugin/models/container.py index 5de92c3..3c782f6 100644 --- a/netbox_docker_plugin/models/container.py +++ b/netbox_docker_plugin/models/container.py @@ -2,6 +2,7 @@ # pylint: disable=E1101 +from django.contrib.postgres.fields import ArrayField from django.core.exceptions import ValidationError from django.db import models from django.db.models.functions import Lower @@ -84,6 +85,16 @@ class PortTypeChoices(ChoiceSet): ] +class ContainerCapAddChoices(ChoiceSet): + """cap-add choices definition class""" + + key = "Container.cap_add" + + CHOICES = [ + ("NET_ADMIN", "NET_ADMIN"), + ] + + class Container(NetBoxModel): """Container definition class""" @@ -134,39 +145,48 @@ class Container(NetBoxModel): choices=ContainerRestartPolicyChoices, default=ContainerRestartPolicyChoices.DEFAULT_VALUE, ) + cap_add = ArrayField( + models.CharField( + max_length=32, blank=True, null=True, choices=ContainerCapAddChoices + ), + null=True, + blank=True, + ) @property def can_create(self) -> bool: - """ Check if the container can be created """ + """Check if the container can be created""" return self.state == "none" @property def can_start(self) -> bool: - """ Check if the container can be started """ + """Check if the container can be started""" return self.state in ["created", "exited", "dead"] @property def can_stop(self) -> bool: - """ Check if the container can be stopped """ + """Check if the container can be stopped""" return self.state in ["running"] @property def can_restart(self) -> bool: - """ Check if the container can be restarted """ + """Check if the container can be restarted""" return self.state in ["running"] @property def can_recreate(self) -> bool: - """ Check if the container can be recreated """ + """Check if the container can be recreated""" return self.state in ["created", "running", "exited", "dead"] @property def can_delete(self) -> bool: - """ Check if the container can be deleted """ - return ( - self.host.state == HostStateChoices.STATE_DELETED - or self.state in ["created", "paused", "exited", "dead"] - ) + """Check if the container can be deleted""" + return self.host.state == HostStateChoices.STATE_DELETED or self.state in [ + "created", + "paused", + "exited", + "dead", + ] class Meta: """Image Model Meta Class""" diff --git a/netbox_docker_plugin/tables.py b/netbox_docker_plugin/tables.py index e074043..7fa5d1e 100644 --- a/netbox_docker_plugin/tables.py +++ b/netbox_docker_plugin/tables.py @@ -256,6 +256,7 @@ class Meta(NetBoxTable.Meta): "ContainerID", "hostname", "restart_policy", + "cap_add", "port_count", "mount_count", "bind_count", diff --git a/netbox_docker_plugin/templates/netbox_docker_plugin/container.html b/netbox_docker_plugin/templates/netbox_docker_plugin/container.html index c362a1f..efcd6f6 100644 --- a/netbox_docker_plugin/templates/netbox_docker_plugin/container.html +++ b/netbox_docker_plugin/templates/netbox_docker_plugin/container.html @@ -37,6 +37,10 @@
CONTAINER
Restart Policy {{ object.get_restart_policy_display }} + + Host Capacities added + {{ object.cap_add }} + Status {{ object.status|placeholder }} diff --git a/netbox_docker_plugin/tests/container/test_container_api.py b/netbox_docker_plugin/tests/container/test_container_api.py index c3cddd7..51ad142 100644 --- a/netbox_docker_plugin/tests/container/test_container_api.py +++ b/netbox_docker_plugin/tests/container/test_container_api.py @@ -29,6 +29,7 @@ class ContainerApiTestCase( model = Container brief_fields = [ "ContainerID", + "cap_add", "display", "hostname", "id", @@ -168,6 +169,7 @@ def setUpTestData(cls) -> None: "ports": [], "env": [{"var_name": "ENV", "value": ""}], "labels": [], + "cap_add": ["NET_ADMIN"], }, ] diff --git a/netbox_docker_plugin/tests/container/test_container_views.py b/netbox_docker_plugin/tests/container/test_container_views.py index 4c201b0..846a9f8 100644 --- a/netbox_docker_plugin/tests/container/test_container_views.py +++ b/netbox_docker_plugin/tests/container/test_container_views.py @@ -66,6 +66,7 @@ def setUpTestData(cls): "host": host1.pk, "image": image1.pk, "restart_policy": "unless-stopped", + "cap_add": "NET_ADMIN", } cls.csv_data = ( diff --git a/pyproject.toml b/pyproject.toml index cb06773..7cf9e16 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "netbox-docker-plugin" -version = "2.5.0" +version = "2.6.0" authors = [ { name="Vincent Simonin", email="vincent@saashup.com" }, { name="David Delassus", email="david.jose.delassus@gmail.com" }