-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from nofusscomputing/itim-api-v2
- Loading branch information
Showing
30 changed files
with
3,589 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/itim/migrations/0006_alter_port_options_alter_port_protocol.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.1.2 on 2024-10-21 11:34 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('itim', '0005_alter_cluster_cluster_type_alter_cluster_id_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='port', | ||
options={'ordering': ['number', 'protocol'], 'verbose_name': 'Port', 'verbose_name_plural': 'Ports'}, | ||
), | ||
migrations.AlterField( | ||
model_name='port', | ||
name='protocol', | ||
field=models.CharField(choices=[('TCP', 'TCP'), ('UDP', 'UDP')], help_text='Layer 4 Network Protocol', max_length=3, verbose_name='Protocol'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from rest_framework.reverse import reverse | ||
from rest_framework import serializers | ||
|
||
from access.serializers.organization import OrganizationBaseSerializer | ||
|
||
from itam.serializers.device import DeviceBaseSerializer | ||
|
||
from itim.serializers.cluster_type import ClusterTypeBaseSerializer | ||
from itim.models.clusters import Cluster | ||
|
||
|
||
|
||
class ClusterBaseSerializer(serializers.ModelSerializer): | ||
|
||
display_name = serializers.SerializerMethodField('get_display_name') | ||
|
||
def get_display_name(self, item): | ||
|
||
return str( item ) | ||
|
||
url = serializers.HyperlinkedIdentityField( | ||
view_name="API:_api_v2_cluster-detail", format="html" | ||
) | ||
|
||
class Meta: | ||
|
||
model = Cluster | ||
|
||
fields = [ | ||
'id', | ||
'display_name', | ||
'name', | ||
'url', | ||
] | ||
|
||
read_only_fields = [ | ||
'id', | ||
'display_name', | ||
'name', | ||
'url', | ||
] | ||
|
||
|
||
class ClusterModelSerializer(ClusterBaseSerializer): | ||
|
||
_urls = serializers.SerializerMethodField('get_url') | ||
|
||
def get_url(self, item): | ||
|
||
return { | ||
'_self': reverse("API:_api_v2_cluster-detail", request=self._context['view'].request, kwargs={'pk': item.pk}), | ||
'history': reverse( | ||
"API:_api_v2_model_history-list", | ||
request=self._context['view'].request, | ||
kwargs={ | ||
'model_class': self.Meta.model._meta.model_name, | ||
'model_id': item.pk | ||
} | ||
), | ||
'notes': reverse("API:_api_v2_cluster_notes-list", request=self._context['view'].request, kwargs={'cluster_id': item.pk}), | ||
'tickets': 'ToDo' | ||
} | ||
|
||
|
||
rendered_config = serializers.JSONField( read_only = True) | ||
|
||
resources = serializers.CharField( | ||
label = 'Available Resources', | ||
read_only = True, | ||
initial = 'xx/yy CPU, xx/yy RAM, xx/yy Storage', | ||
default = 'xx/yy CPU, xx/yy RAM, xx/yy Storage', | ||
) | ||
|
||
|
||
class Meta: | ||
|
||
model = Cluster | ||
|
||
fields = [ | ||
'id', | ||
'organization', | ||
'display_name', | ||
'name', | ||
'model_notes', | ||
'parent_cluster', | ||
'cluster_type', | ||
'resources', | ||
'config', | ||
'rendered_config', | ||
'nodes', | ||
'devices', | ||
'is_global', | ||
'created', | ||
'modified', | ||
'_urls', | ||
] | ||
|
||
read_only_fields = [ | ||
'id', | ||
'display_name', | ||
'rendered_config', | ||
'resources', | ||
'created', | ||
'modified', | ||
'_urls', | ||
] | ||
|
||
|
||
def is_valid(self, *, raise_exception=False): | ||
|
||
is_valid = super().is_valid(raise_exception=raise_exception) | ||
|
||
|
||
if 'parent_cluster' in self.validated_data: | ||
|
||
if hasattr(self.instance, 'id') and self.validated_data['parent_cluster']: | ||
|
||
if self.validated_data['parent_cluster'].id == self.instance.id: | ||
|
||
is_valid = False | ||
|
||
raise serializers.ValidationError( | ||
detail = { | ||
"parent_cluster": "Cluster can't have itself as its parent cluster" | ||
}, | ||
code = 'parent_not_self' | ||
) | ||
|
||
return is_valid | ||
|
||
|
||
|
||
class ClusterViewSerializer(ClusterModelSerializer): | ||
|
||
cluster_type = ClusterTypeBaseSerializer( many = False, read_only = True ) | ||
|
||
devices = DeviceBaseSerializer( many = True, read_only = True ) | ||
|
||
nodes = DeviceBaseSerializer( many = True, read_only = True ) | ||
|
||
organization = OrganizationBaseSerializer( many = False, read_only = True ) | ||
|
||
parent_cluster = ClusterBaseSerializer( many = False, read_only = True ) |
Oops, something went wrong.