Skip to content

Commit

Permalink
Merge pull request #151 from SaaShup/add_cap_add_field
Browse files Browse the repository at this point in the history
✨ Add cap_add Container parameter
  • Loading branch information
fanshan committed Aug 2, 2024
2 parents ca99c49 + cb1a7f8 commit 1d1bb09
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion netbox_docker_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NetBoxDockerConfig(PluginConfig):
name = "netbox_docker_plugin"
verbose_name = " NetBox Docker Plugin"
description = "Manage Docker"
version = "1.15.0"
version = "1.16.0"
max_version = "3.7.8"
base_url = "docker"
author= "Vincent Simonin <[email protected]>, David Delassus <[email protected]>"
Expand Down
2 changes: 2 additions & 0 deletions netbox_docker_plugin/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Meta:
"restart_policy",
"operation",
"hostname",
"cap_add",
)


Expand Down Expand Up @@ -403,6 +404,7 @@ class Meta:
"ContainerID",
"hostname",
"restart_policy",
"cap_add",
"ports",
"env",
"labels",
Expand Down
13 changes: 12 additions & 1 deletion netbox_docker_plugin/forms/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
)
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


Expand All @@ -30,6 +34,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"""
Expand All @@ -41,6 +46,7 @@ class Meta:
"name",
"hostname",
"restart_policy",
"cap_add",
"tags",
)
labels = {
Expand All @@ -49,8 +55,10 @@ class Meta:
"image": "Image",
"hostname": "Hostname",
"restart_policy": "Restart Policy",
"cap_add": "Add Host capabilities",
}


class ContainerEditForm(NetBoxModelForm):
"""Container form definition class"""

Expand All @@ -60,6 +68,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"""
Expand All @@ -70,13 +79,15 @@ class Meta:
"name",
"hostname",
"restart_policy",
"cap_add",
"tags",
)
labels = {
"name": "Name",
"image": "Image",
"hostname": "Hostname",
"restart_policy": "Restart Policy",
"cap_add": "Add Host capabilities",
}


Expand Down
26 changes: 26 additions & 0 deletions netbox_docker_plugin/migrations/0030_container_cap_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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", "0029_alter_container_state"),
]

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,
),
),
]
40 changes: 30 additions & 10 deletions netbox_docker_plugin/models/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -132,39 +143,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"""
Expand Down
1 change: 1 addition & 0 deletions netbox_docker_plugin/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class Meta(NetBoxTable.Meta):
"ContainerID",
"hostname",
"restart_policy",
"cap_add",
"port_count",
"mount_count",
"bind_count",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ <h5 class="card-header">CONTAINER</h5>
<th scope="row">Restart Policy</th>
<td>{{ object.get_restart_policy_display }}</td>
</tr>
<tr>
<th scope="row">Host Capacities added</th>
<td>{{ object.cap_add }}</td>
</tr>
<tr>
<th scope="row">Status</th>
<td>{{ object.status|placeholder }}</td>
Expand Down
2 changes: 2 additions & 0 deletions netbox_docker_plugin/tests/container/test_container_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ContainerApiTestCase(
model = Container
brief_fields = [
"ContainerID",
"cap_add",
"display",
"hostname",
"id",
Expand Down Expand Up @@ -168,6 +169,7 @@ def setUpTestData(cls) -> None:
"ports": [],
"env": [{"var_name": "ENV", "value": ""}],
"labels": [],
"cap_add": ["NET_ADMIN"]
},
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def setUpTestData(cls):
"host": host1.pk,
"image": image1.pk,
"restart_policy": "unless-stopped",
"cap_add": "NET_ADMIN",
}

cls.csv_data = (
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "netbox-docker-plugin"
version = "1.15.0"
version = "1.16.0"
authors = [
{ name="Vincent Simonin", email="[email protected]" },
{ name="David Delassus", email="[email protected]" }
Expand Down

0 comments on commit 1d1bb09

Please sign in to comment.