Skip to content

Commit 13675d9

Browse files
authored
Merge pull request #153 from SaaShup/add_cap_add_field_v4
✨ Add cap_add Container parameter for Netbox V4 env
2 parents 56cf98d + 3be26ff commit 13675d9

File tree

10 files changed

+78
-13
lines changed

10 files changed

+78
-13
lines changed

netbox_docker_plugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NetBoxDockerConfig(PluginConfig):
1010
name = "netbox_docker_plugin"
1111
verbose_name = " NetBox Docker Plugin"
1212
description = "Manage Docker"
13-
version = "2.5.0"
13+
version = "2.6.0"
1414
base_url = "docker"
1515
min_version = "4.0.0"
1616
author= "Vincent Simonin <[email protected]>, David Delassus <[email protected]>"

netbox_docker_plugin/api/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class Meta:
146146
"restart_policy",
147147
"operation",
148148
"hostname",
149+
"cap_add",
149150
)
150151

151152

@@ -406,6 +407,7 @@ class Meta:
406407
"ContainerID",
407408
"hostname",
408409
"restart_policy",
410+
"cap_add",
409411
"ports",
410412
"env",
411413
"labels",

netbox_docker_plugin/forms/container.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from ..models.volume import Volume
1717
from ..models.host import Host
18-
from ..models.container import Container, ContainerRestartPolicyChoices
18+
from ..models.container import Container, ContainerRestartPolicyChoices, ContainerCapAddChoices
1919
from ..models.image import Image
2020

2121

@@ -31,6 +31,7 @@ class ContainerForm(NetBoxModelForm):
3131
required=True,
3232
query_params={"host_id": "$host"},
3333
)
34+
cap_add = forms.MultipleChoiceField(choices=ContainerCapAddChoices, required=False)
3435

3536
class Meta:
3637
"""Container form definition Meta class"""
@@ -42,6 +43,7 @@ class Meta:
4243
"name",
4344
"hostname",
4445
"restart_policy",
46+
"cap_add",
4547
"tags",
4648
)
4749
labels = {
@@ -50,6 +52,7 @@ class Meta:
5052
"image": "Image",
5153
"hostname": "Hostname",
5254
"restart_policy": "Restart Policy",
55+
"cap_add": "Add Host capabilities",
5356
}
5457

5558

@@ -62,6 +65,7 @@ class ContainerEditForm(NetBoxModelForm):
6265
required=True,
6366
query_params={"host_id": "$host"},
6467
)
68+
cap_add = forms.MultipleChoiceField(choices=ContainerCapAddChoices, required=False)
6569

6670
class Meta:
6771
"""Container form definition Meta class"""
@@ -72,13 +76,15 @@ class Meta:
7276
"name",
7377
"hostname",
7478
"restart_policy",
79+
"cap_add",
7580
"tags",
7681
)
7782
labels = {
7883
"name": "Name",
7984
"image": "Image",
8085
"hostname": "Hostname",
8186
"restart_policy": "Restart Policy",
87+
"cap_add": "Add Host capabilities",
8288
}
8389

8490

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# pylint: disable=C0103
2+
"""Migration file"""
3+
4+
import django.contrib.postgres.fields
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
"""Migration file"""
10+
11+
dependencies = [
12+
(
13+
"netbox_docker_plugin",
14+
"0030_alter_container_containerid_alter_container_hostname_and_more",
15+
),
16+
]
17+
18+
operations = [
19+
migrations.AddField(
20+
model_name="container",
21+
name="cap_add",
22+
field=django.contrib.postgres.fields.ArrayField(
23+
base_field=models.CharField(blank=True, max_length=32, null=True),
24+
blank=True,
25+
null=True,
26+
size=None,
27+
),
28+
),
29+
]

netbox_docker_plugin/models/container.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# pylint: disable=E1101
44

5+
from django.contrib.postgres.fields import ArrayField
56
from django.core.exceptions import ValidationError
67
from django.db import models
78
from django.db.models.functions import Lower
@@ -84,6 +85,16 @@ class PortTypeChoices(ChoiceSet):
8485
]
8586

8687

88+
class ContainerCapAddChoices(ChoiceSet):
89+
"""cap-add choices definition class"""
90+
91+
key = "Container.cap_add"
92+
93+
CHOICES = [
94+
("NET_ADMIN", "NET_ADMIN"),
95+
]
96+
97+
8798
class Container(NetBoxModel):
8899
"""Container definition class"""
89100

@@ -134,39 +145,48 @@ class Container(NetBoxModel):
134145
choices=ContainerRestartPolicyChoices,
135146
default=ContainerRestartPolicyChoices.DEFAULT_VALUE,
136147
)
148+
cap_add = ArrayField(
149+
models.CharField(
150+
max_length=32, blank=True, null=True, choices=ContainerCapAddChoices
151+
),
152+
null=True,
153+
blank=True,
154+
)
137155

138156
@property
139157
def can_create(self) -> bool:
140-
""" Check if the container can be created """
158+
"""Check if the container can be created"""
141159
return self.state == "none"
142160

143161
@property
144162
def can_start(self) -> bool:
145-
""" Check if the container can be started """
163+
"""Check if the container can be started"""
146164
return self.state in ["created", "exited", "dead"]
147165

148166
@property
149167
def can_stop(self) -> bool:
150-
""" Check if the container can be stopped """
168+
"""Check if the container can be stopped"""
151169
return self.state in ["running"]
152170

153171
@property
154172
def can_restart(self) -> bool:
155-
""" Check if the container can be restarted """
173+
"""Check if the container can be restarted"""
156174
return self.state in ["running"]
157175

158176
@property
159177
def can_recreate(self) -> bool:
160-
""" Check if the container can be recreated """
178+
"""Check if the container can be recreated"""
161179
return self.state in ["created", "running", "exited", "dead"]
162180

163181
@property
164182
def can_delete(self) -> bool:
165-
""" Check if the container can be deleted """
166-
return (
167-
self.host.state == HostStateChoices.STATE_DELETED
168-
or self.state in ["created", "paused", "exited", "dead"]
169-
)
183+
"""Check if the container can be deleted"""
184+
return self.host.state == HostStateChoices.STATE_DELETED or self.state in [
185+
"created",
186+
"paused",
187+
"exited",
188+
"dead",
189+
]
170190

171191
class Meta:
172192
"""Image Model Meta Class"""

netbox_docker_plugin/tables.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class Meta(NetBoxTable.Meta):
256256
"ContainerID",
257257
"hostname",
258258
"restart_policy",
259+
"cap_add",
259260
"port_count",
260261
"mount_count",
261262
"bind_count",

netbox_docker_plugin/templates/netbox_docker_plugin/container.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ <h5 class="card-header">CONTAINER</h5>
3737
<th scope="row">Restart Policy</th>
3838
<td>{{ object.get_restart_policy_display }}</td>
3939
</tr>
40+
<tr>
41+
<th scope="row">Host Capacities added</th>
42+
<td>{{ object.cap_add }}</td>
43+
</tr>
4044
<tr>
4145
<th scope="row">Status</th>
4246
<td>{{ object.status|placeholder }}</td>

netbox_docker_plugin/tests/container/test_container_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ContainerApiTestCase(
2929
model = Container
3030
brief_fields = [
3131
"ContainerID",
32+
"cap_add",
3233
"display",
3334
"hostname",
3435
"id",
@@ -168,6 +169,7 @@ def setUpTestData(cls) -> None:
168169
"ports": [],
169170
"env": [{"var_name": "ENV", "value": ""}],
170171
"labels": [],
172+
"cap_add": ["NET_ADMIN"],
171173
},
172174
]
173175

netbox_docker_plugin/tests/container/test_container_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def setUpTestData(cls):
6666
"host": host1.pk,
6767
"image": image1.pk,
6868
"restart_policy": "unless-stopped",
69+
"cap_add": "NET_ADMIN",
6970
}
7071

7172
cls.csv_data = (

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "netbox-docker-plugin"
7-
version = "2.5.0"
7+
version = "2.6.0"
88
authors = [
99
{ name="Vincent Simonin", email="[email protected]" },
1010
{ name="David Delassus", email="[email protected]" }

0 commit comments

Comments
 (0)