blob: bab15bd3a6d6bac799ebb67a8f04d40ce57cc6a1 [file] [log] [blame]
Matteo Scandoloeb0d11c2017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker31acc652016-06-23 15:47:56 -070017from rest_framework.decorators import api_view
18from rest_framework.response import Response
19from rest_framework.reverse import reverse
20from rest_framework import serializers
21from rest_framework import generics
22from rest_framework import status
23from core.models import *
24from django.forms import widgets
25from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
26from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
27
Srikanth Vavilapallid84b7b72016-06-28 00:19:07 +000028from services.monitoring.models import MonitoringChannel, CeilometerService
Scott Baker31acc652016-06-23 15:47:56 -070029
30def get_default_ceilometer_service():
31 ceilometer_services = CeilometerService.get_service_objects().all()
32 if ceilometer_services:
33 return ceilometer_services[0].id
34 return None
35
36class MonitoringChannelForAPI(MonitoringChannel):
37 class Meta:
38 proxy = True
39 app_label = "ceilometer"
40
41 @property
42 def related(self):
43 related = {}
44 if self.creator:
45 related["creator"] = self.creator.username
46 if self.instance:
47 related["instance_id"] = self.instance.id
48 related["instance_name"] = self.instance.name
49 if self.instance.node:
50 related["compute_node_name"] = self.instance.node.name
51 return related
52
53class MonitoringChannelSerializer(PlusModelSerializer):
54 id = ReadOnlyField()
Srikanth Vavilapalli1dfc9c82016-12-20 02:58:32 +000055 backend_status = ReadOnlyField()
Scott Baker31acc652016-06-23 15:47:56 -070056 service_specific_attribute = ReadOnlyField()
57 ceilometer_url = ReadOnlyField()
Srikanth Vavilapalli71aa28d2017-01-31 00:43:13 +000058 ceilometer_ssh_proxy_url = ReadOnlyField()
59 kafka_url = ReadOnlyField()
Scott Baker31acc652016-06-23 15:47:56 -070060 tenant_list_str = ReadOnlyField()
61 #creator = ReadOnlyField()
62 #instance = ReadOnlyField()
63 provider_service = serializers.PrimaryKeyRelatedField(queryset=CeilometerService.get_service_objects().all(), default=get_default_ceilometer_service)
64
65 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
66 related = serializers.DictField(required=False)
67
68 #computeNodeName = serializers.SerializerMethodField("getComputeNodeName")
69
70 class Meta:
71 model = MonitoringChannelForAPI
Srikanth Vavilapalli71aa28d2017-01-31 00:43:13 +000072 fields = ('humanReadableName', 'id', 'backend_status', 'provider_service', 'service_specific_attribute', 'ceilometer_url', 'ceilometer_ssh_proxy_url', 'kafka_url', 'tenant_list_str', 'related' )
Scott Baker31acc652016-06-23 15:47:56 -070073
74 def getHumanReadableName(self, obj):
75 return obj.__unicode__()
76
77 #def getComputeNodeName(self, obj):
78 # instance = obj.instance
79 # if not instance:
80 # return None
81 # return instance.node.name
82
83class MonitoringChannelSet(XOSViewSet):
84 base_name = "monitoringchannel"
85 method_name = "monitoringchannel"
86 method_kind = "viewset"
87 queryset = MonitoringChannelForAPI.get_tenant_objects().all()
88 serializer_class = MonitoringChannelSerializer
89
90 def get_queryset(self):
91 queryset = MonitoringChannelForAPI.get_tenant_objects().all()
92
93 current_user = self.request.user.username
94 if current_user is not None:
95 ids = [x.id for x in queryset if x.creator.username==current_user]
96 queryset = queryset.filter(id__in=ids)
97
98 return queryset
99
100 def create(self, request):
101 current_user = request.user.username
102 existing_obj = None
103 for obj in MonitoringChannelForAPI.get_tenant_objects().all():
104 if (obj.creator.username == current_user):
105 existing_obj = obj
106 break
107
108 if existing_obj:
109 serializer = MonitoringChannelSerializer(existing_obj)
110 headers = self.get_success_headers(serializer.data)
111 return Response( serializer.data, status=status.HTTP_200_OK )
112
113 return super(MonitoringChannelSet, self).create(request)