Skip to content

Commit

Permalink
Merge pull request #153 from SaaShup/add_cap_add_field_v4
Browse files Browse the repository at this point in the history
✨ Add cap_add Container parameter for Netbox V4 env
  • Loading branch information
fanshan committed Aug 5, 2024
2 parents 56cf98d + 3be26ff commit 13675d9
Show file tree
Hide file tree
Showing 10 changed files with 78 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 = "2.5.0"
version = "2.6.0"
base_url = "docker"
min_version = "4.0.0"
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 @@ -406,6 +407,7 @@ class Meta:
"ContainerID",
"hostname",
"restart_policy",
"cap_add",
"ports",
"env",
"labels",
Expand Down
8 changes: 7 additions & 1 deletion netbox_docker_plugin/forms/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"""
Expand All @@ -42,6 +43,7 @@ class Meta:
"name",
"hostname",
"restart_policy",
"cap_add",
"tags",
)
labels = {
Expand All @@ -50,6 +52,7 @@ class Meta:
"image": "Image",
"hostname": "Hostname",
"restart_policy": "Restart Policy",
"cap_add": "Add Host capabilities",
}


Expand All @@ -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"""
Expand All @@ -72,13 +76,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
29 changes: 29 additions & 0 deletions netbox_docker_plugin/migrations/0031_container_cap_add.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
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 @@ -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"""
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 = "2.5.0"
version = "2.6.0"
authors = [
{ name="Vincent Simonin", email="[email protected]" },
{ name="David Delassus", email="[email protected]" }
Expand Down

0 comments on commit 13675d9

Please sign in to comment.