Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split zone account #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions netbox_gcore_plugin/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@

from rest_framework import serializers
from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer
from ..models import ZoneAccount, DnsRecord
from ..models import ZoneAccount, DnsRecord, ZoneZones


class NestedZoneZonesSerializer(WritableNestedSerializer):
"""Nested Zones Serializer class"""

url = serializers.HyperlinkedIdentityField(
view_name="plugins-api:netbox_gcore_plugin-api:zonezones-detail"
)

class Meta:
"""Nested Zone Serializer Meta class"""

model = ZoneZones
fields = (
"id",
"url",
"display",
"zone_name",
)

class NestedZoneAccountSerializer(WritableNestedSerializer):
"""Nested ZoneAccount Serializer class"""
"""Nested Account Serializer class"""

url = serializers.HyperlinkedIdentityField(
view_name="plugins-api:netbox_gcore_plugin-api:zoneaccount-detail"
Expand All @@ -20,7 +38,6 @@ class Meta:
"id",
"url",
"display",
"zone_name",
"token",
)

Expand Down Expand Up @@ -93,11 +110,33 @@ class Meta:
"id",
"url",
"display",
"zone_name",
"token",
"custom_fields",
"created",
"last_updated",
"tags",
)
brief_fields = NestedZoneAccountSerializer.Meta.fields

class ZoneZonesSerializer(NetBoxModelSerializer):
"""ZoneZones Serializer class"""

url = serializers.HyperlinkedIdentityField(
view_name="plugins-api:netbox_gcore_plugin-api:zonezones-detail"
)

class Meta:
"""ZoneZones Serializer Meta class"""

model = ZoneZones
fields = (
"id",
"url",
"display",
"zone_name",
"custom_fields",
"created",
"last_updated",
"tags",
)
brief_fields = NestedZoneZonesSerializer.Meta.fields
3 changes: 2 additions & 1 deletion netbox_gcore_plugin/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""API URLs definition"""

from netbox.api.routers import NetBoxRouter
from .views import ZoneAccountViewSet, DnsRecordViewSet
from .views import ZoneAccountViewSet, DnsRecordViewSet, ZoneZonesViewSet


APP_NAME = "netbox_gcore_plugin"

router = NetBoxRouter()
router.register("dns/accounts", ZoneAccountViewSet)
router.register("dns/zones", ZoneZonesViewSet)
router.register("dns/records", DnsRecordViewSet)

urlpatterns = router.urls
10 changes: 8 additions & 2 deletions netbox_gcore_plugin/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""API views definitions"""

from netbox.api.viewsets import NetBoxModelViewSet
from .serializers import ZoneAccountSerializer, DnsRecordSerializer
from ..models import ZoneAccount, DnsRecord
from .serializers import ZoneAccountSerializer, DnsRecordSerializer, ZoneZonesSerializer
from ..models import ZoneAccount, DnsRecord, ZoneZones

class ZoneZonesViewSet(NetBoxModelViewSet):
"""ZoneZones view set class"""

queryset = ZoneZones.objects.all()
serializer_class = ZoneZonesSerializer
http_method_names = ["get", "post", "delete", "options"]

class ZoneAccountViewSet(NetBoxModelViewSet):
"""ZoneAccount view set class"""
Expand Down
26 changes: 23 additions & 3 deletions netbox_gcore_plugin/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
from django.db.models import Q
from django_filters import ModelMultipleChoiceFilter
from netbox.filtersets import NetBoxModelFilterSet
from .models import ZoneAccount, DnsRecord
from .models import ZoneAccount, DnsRecord, ZoneZones

class ZoneZonesFilterSet(NetBoxModelFilterSet):
"""ZoneZones filterset definition class"""

account_id = ModelMultipleChoiceFilter(
field_name="account_id",
queryset=ZoneAccount.objects.all(),
label="Account (ID)",
)

class Meta:
"""DnsRecord filterset definition meta class"""

model = ZoneZones
fields = ("id",)

def search(self, queryset, name, value):
"""override"""
if not value.strip():
return queryset
return queryset.filter(Q(name__icontains=value))

class DnsRecordFilterSet(NetBoxModelFilterSet):
"""DnsRecord filterset definition class"""

zone_id = ModelMultipleChoiceFilter(
field_name="zone_id",
queryset=ZoneAccount.objects.all(),
label="Account (ID)",
queryset=ZoneZones.objects.all(),
label="Zone (ID)",
)

class Meta:
Expand Down
27 changes: 22 additions & 5 deletions netbox_gcore_plugin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@
DynamicModelChoiceField,
)
from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm
from .models import ZoneAccount, DnsRecord
from .models import ZoneAccount, DnsRecord, ZoneZones

class ZoneZonesForm(NetBoxModelForm):
"""ZoneZones form definition class"""

account = DynamicModelChoiceField(
label="Account", queryset=ZoneAccount.objects.all(), required=True
)

class Meta:
"""ZoneZones form definition Meta class"""

model = ZoneZones
fields = (
"account",
"zone_name",
"tags",
)


class ZoneAccountForm(NetBoxModelForm):
Expand All @@ -18,7 +35,7 @@ class Meta:

model = ZoneAccount
fields = (
"zone_name",
"name",
"token",
"tags",
)
Expand All @@ -28,7 +45,7 @@ class DnsRecordForm(NetBoxModelForm):
"""DnsRecord form definition class"""

zone = DynamicModelChoiceField(
label="Zone Account", queryset=ZoneAccount.objects.all(), required=True
label="Zone", queryset=ZoneZones.objects.all(), required=True
)

class Meta:
Expand All @@ -51,9 +68,9 @@ class DnsRecordFilterForm(NetBoxModelFilterSetForm):
model = DnsRecord

zone_name = DynamicModelMultipleChoiceField(
queryset=ZoneAccount.objects.all(),
queryset=ZoneZones.objects.all(),
required=False,
label="Account",
label="Zones",
)
type = forms.ChoiceField(
label="Type", choices=DnsRecord.TYPE_CHOICE, required=False
Expand Down
55 changes: 52 additions & 3 deletions netbox_gcore_plugin/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class Migration(migrations.Migration):
),
),
(
"zone_name",
"name",
models.CharField(
max_length=255,
unique=True,
validators=[
django.core.validators.MinLengthValidator(limit_value=1),
django.core.validators.MaxLengthValidator(limit_value=255),
Expand All @@ -65,6 +64,56 @@ class Migration(migrations.Migration):
),
),
],
options={
"ordering": ("token",),
},
),

migrations.CreateModel(
name="ZoneZones",
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,
),
),
(
"zone_name",
models.CharField(
max_length=255,
unique=True,
validators=[
django.core.validators.MinLengthValidator(limit_value=1),
django.core.validators.MaxLengthValidator(limit_value=255),
],
),
),
(
"tags",
taggit.managers.TaggableManager(
through="extras.TaggedItem", to="extras.Tag"
),
),
(
"account",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="zones",
to="netbox_gcore_plugin.zoneaccount",
),
),
],
options={
"ordering": ("zone_name",),
},
Expand Down Expand Up @@ -141,7 +190,7 @@ class Migration(migrations.Migration):
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="records",
to="netbox_gcore_plugin.zoneaccount",
to="netbox_gcore_plugin.zonezones",
),
),
],
Expand Down
48 changes: 41 additions & 7 deletions netbox_gcore_plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
from taggit.managers import TaggableManager
from netbox.models import NetBoxModel


class ZoneAccount(NetBoxModel):
"""GCore DNS zone account definition class"""
"""GCore account definition class"""

zone_name = models.CharField(
unique=True,
name = models.CharField(
max_length=255,
null=False,
blank=False,
Expand All @@ -25,6 +23,7 @@ class ZoneAccount(NetBoxModel):
MaxLengthValidator(limit_value=255),
],
)

token = models.CharField(
max_length=255,
null=False,
Expand All @@ -40,7 +39,42 @@ class ZoneAccount(NetBoxModel):
)

class Meta:
"""GCore DNS zone account Model Meta Class"""
"""GCore zone account Model Meta Class"""

ordering = ("name",)

def __str__(self):
return f"{self.name}"

def get_absolute_url(self):
"""override"""
return reverse("plugins:netbox_gcore_plugin:zoneaccount", args=[self.pk])


class ZoneZones(NetBoxModel):
"""GCore DNS zone definition class"""

account = models.ForeignKey(
ZoneAccount, on_delete=models.CASCADE, related_name="zones"
)

zone_name = models.CharField(
unique=True,
max_length=255,
null=False,
blank=False,
validators=[
MinLengthValidator(limit_value=1),
MaxLengthValidator(limit_value=255),
],
)

tags = TaggableManager(
through="extras.TaggedItem", related_name="netbox_gcore_plugin_zonezones_set"
)

class Meta:
"""GCore DNS zone Model Meta Class"""

ordering = ("zone_name",)

Expand All @@ -49,7 +83,7 @@ def __str__(self):

def get_absolute_url(self):
"""override"""
return reverse("plugins:netbox_gcore_plugin:zoneaccount", args=[self.pk])
return reverse("plugins:netbox_gcore_plugin:zonezones", args=[self.pk])


class DnsRecord(NetBoxModel):
Expand All @@ -64,7 +98,7 @@ class DnsRecord(NetBoxModel):
)

zone = models.ForeignKey(
ZoneAccount, on_delete=models.CASCADE, related_name="records"
ZoneZones, on_delete=models.CASCADE, related_name="records"
)
record_id = name = models.CharField(
max_length=32,
Expand Down
Loading