@ -3,6 +3,10 @@ from rest_framework import serializers
|
||||
|
||||
from access.serializers.organization import OrganizationBaseSerializer
|
||||
|
||||
from itam.serializers.device import DeviceBaseSerializer
|
||||
|
||||
from itim.serializers.cluster import ClusterBaseSerializer
|
||||
from itim.serializers.port import PortBaseSerializer
|
||||
from itim.models.services import Service
|
||||
|
||||
|
||||
@ -15,6 +19,10 @@ class ServiceBaseSerializer(serializers.ModelSerializer):
|
||||
|
||||
return str( item )
|
||||
|
||||
url = serializers.HyperlinkedIdentityField(
|
||||
view_name="API:_api_v2_service-detail", format="html"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
||||
model = Service
|
||||
@ -23,10 +31,87 @@ class ServiceBaseSerializer(serializers.ModelSerializer):
|
||||
'id',
|
||||
'display_name',
|
||||
'name',
|
||||
'url',
|
||||
]
|
||||
|
||||
read_only_fields = [
|
||||
'id',
|
||||
'display_name',
|
||||
'name',
|
||||
'url',
|
||||
]
|
||||
|
||||
|
||||
class ServiceModelSerializer(ServiceBaseSerializer):
|
||||
|
||||
_urls = serializers.SerializerMethodField('get_url')
|
||||
|
||||
def get_url(self, item):
|
||||
|
||||
return {
|
||||
'_self': reverse("API:_api_v2_service-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_service_notes-list", request=self._context['view'].request, kwargs={'service_id': item.pk}),
|
||||
'tickets': 'ToDo'
|
||||
}
|
||||
|
||||
|
||||
rendered_config = serializers.JSONField(source='config_variables')
|
||||
|
||||
|
||||
class Meta:
|
||||
|
||||
model = Service
|
||||
|
||||
fields = [
|
||||
'id',
|
||||
'organization',
|
||||
'display_name',
|
||||
'name',
|
||||
'model_notes',
|
||||
'is_template',
|
||||
'template',
|
||||
'device',
|
||||
'cluster',
|
||||
'config',
|
||||
'rendered_config',
|
||||
'config_key_variable',
|
||||
'port',
|
||||
'dependent_service',
|
||||
'is_global',
|
||||
'created',
|
||||
'modified',
|
||||
'_urls',
|
||||
]
|
||||
|
||||
read_only_fields = [
|
||||
'id',
|
||||
'display_name',
|
||||
'rendered_config',
|
||||
'created',
|
||||
'modified',
|
||||
'_urls',
|
||||
]
|
||||
|
||||
|
||||
|
||||
class ServiceViewSerializer(ServiceModelSerializer):
|
||||
|
||||
cluster = ClusterBaseSerializer( many = False, read_only = True )
|
||||
|
||||
device = DeviceBaseSerializer( many = False, read_only = True )
|
||||
|
||||
dependent_service = ServiceBaseSerializer( many = True, read_only = True )
|
||||
|
||||
organization = OrganizationBaseSerializer( many = False, read_only = True )
|
||||
|
||||
port = PortBaseSerializer( many = True, read_only = True )
|
||||
|
||||
template = ServiceBaseSerializer( many = False, read_only = True )
|
||||
|
@ -29,6 +29,6 @@ class Index(CommonViewSet):
|
||||
"cluster": "ToDo",
|
||||
"incident": "ToDo",
|
||||
"problem": "ToDo",
|
||||
"service": "ToDo",
|
||||
"service": reverse('API:_api_v2_service-list', request=request),
|
||||
}
|
||||
)
|
||||
|
89
app/itim/viewsets/service.py
Normal file
89
app/itim/viewsets/service.py
Normal file
@ -0,0 +1,89 @@
|
||||
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse
|
||||
|
||||
from api.viewsets.common import ModelViewSet
|
||||
|
||||
from itim.serializers.service import (
|
||||
Service,
|
||||
ServiceModelSerializer,
|
||||
ServiceViewSerializer
|
||||
)
|
||||
|
||||
|
||||
|
||||
@extend_schema_view(
|
||||
create=extend_schema(
|
||||
summary = 'Create a service',
|
||||
description="""Add a new device to the ITAM database.
|
||||
If you attempt to create a device and a device with a matching name and uuid or name and serial number
|
||||
is found within the database, it will not re-create it. The device will be returned within the message body.
|
||||
""",
|
||||
responses = {
|
||||
201: OpenApiResponse(description='Device created', response=ServiceViewSerializer),
|
||||
400: OpenApiResponse(description='Validation failed.'),
|
||||
403: OpenApiResponse(description='User is missing create permissions'),
|
||||
}
|
||||
),
|
||||
destroy = extend_schema(
|
||||
summary = 'Delete a service',
|
||||
description = '',
|
||||
responses = {
|
||||
204: OpenApiResponse(description=''),
|
||||
403: OpenApiResponse(description='User is missing delete permissions'),
|
||||
}
|
||||
),
|
||||
list = extend_schema(
|
||||
summary = 'Fetch all services',
|
||||
description='',
|
||||
responses = {
|
||||
200: OpenApiResponse(description='', response=ServiceViewSerializer),
|
||||
403: OpenApiResponse(description='User is missing view permissions'),
|
||||
}
|
||||
),
|
||||
retrieve = extend_schema(
|
||||
summary = 'Fetch a single service',
|
||||
description='',
|
||||
responses = {
|
||||
200: OpenApiResponse(description='', response=ServiceViewSerializer),
|
||||
403: OpenApiResponse(description='User is missing view permissions'),
|
||||
}
|
||||
),
|
||||
update = extend_schema(exclude = True),
|
||||
partial_update = extend_schema(
|
||||
summary = 'Update a service',
|
||||
description = '',
|
||||
responses = {
|
||||
200: OpenApiResponse(description='', response=ServiceViewSerializer),
|
||||
403: OpenApiResponse(description='User is missing change permissions'),
|
||||
}
|
||||
),
|
||||
)
|
||||
class ViewSet( ModelViewSet ):
|
||||
|
||||
filterset_fields = [
|
||||
'name',
|
||||
'cluster',
|
||||
'device',
|
||||
'port',
|
||||
]
|
||||
|
||||
search_fields = [
|
||||
'name',
|
||||
]
|
||||
|
||||
model = Service
|
||||
|
||||
documentation: str = 'https://nofusscomputing.com/docs/not_model_docs'
|
||||
|
||||
view_description = 'Physical Devices'
|
||||
|
||||
def get_serializer_class(self):
|
||||
|
||||
if (
|
||||
self.action == 'list'
|
||||
or self.action == 'retrieve'
|
||||
):
|
||||
|
||||
return globals()[str( self.model._meta.verbose_name) + 'ViewSerializer']
|
||||
|
||||
|
||||
return globals()[str( self.model._meta.verbose_name) + 'ModelSerializer']
|
Reference in New Issue
Block a user