blob: 2967cfad6c6dd3b330a55146126391a9bf944057 [file] [log] [blame]
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -08001# Copyright 2014 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000016import urllib
17
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000018from tempest.common import service_client
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080019from tempest.openstack.common import jsonutils as json
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000020
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080021
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000022class TelemetryClientJSON(service_client.ServiceClient):
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080023
Ken'ichi Ohmichid5dba1c2015-01-23 02:23:22 +000024 version = '2'
25 uri_prefix = "v2"
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080026
27 def deserialize(self, body):
28 return json.loads(body.replace("\n", ""))
29
30 def serialize(self, body):
31 return json.dumps(body)
32
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080033 def add_sample(self, sample_list, meter_name, meter_unit, volume,
34 sample_type, resource_id, **kwargs):
35 sample = {"counter_name": meter_name, "counter_unit": meter_unit,
36 "counter_volume": volume, "counter_type": sample_type,
37 "resource_id": resource_id}
38 for key in kwargs:
39 sample[key] = kwargs[key]
40
41 sample_list.append(self.serialize(sample))
42 return sample_list
43
44 def create_sample(self, meter_name, sample_list):
45 uri = "%s/meters/%s" % (self.uri_prefix, meter_name)
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000046 body = self.serialize(sample_list)
47 resp, body = self.post(uri, body)
48 body = self.deserialize(body)
49 return resp, body
50
51 def helper_list(self, uri, query=None, period=None):
52 uri_dict = {}
53 if query:
54 uri_dict = {'q.field': query[0],
55 'q.op': query[1],
56 'q.value': query[2]}
57 if period:
58 uri_dict['period'] = period
59 if uri_dict:
60 uri += "?%s" % urllib.urlencode(uri_dict)
61 resp, body = self.get(uri)
62 body = self.deserialize(body)
63 return resp, body
64
65 def list_resources(self, query=None):
66 uri = '%s/resources' % self.uri_prefix
67 return self.helper_list(uri, query)
68
69 def list_meters(self, query=None):
70 uri = '%s/meters' % self.uri_prefix
71 return self.helper_list(uri, query)
72
73 def list_alarms(self, query=None):
74 uri = '%s/alarms' % self.uri_prefix
75 return self.helper_list(uri, query)
76
77 def list_statistics(self, meter, period=None, query=None):
78 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
79 return self.helper_list(uri, query, period)
80
81 def list_samples(self, meter_id, query=None):
82 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
83 return self.helper_list(uri, query)
84
85 def get_resource(self, resource_id):
86 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
87 resp, body = self.get(uri)
88 body = self.deserialize(body)
89 return resp, body
90
91 def get_alarm(self, alarm_id):
92 uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
93 resp, body = self.get(uri)
94 body = self.deserialize(body)
95 return resp, body
96
97 def delete_alarm(self, alarm_id):
98 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
99 resp, body = self.delete(uri)
100 if body:
101 body = self.deserialize(body)
102 return resp, body
103
104 def create_alarm(self, **kwargs):
105 uri = "%s/alarms" % self.uri_prefix
106 body = self.serialize(kwargs)
107 resp, body = self.post(uri, body)
108 body = self.deserialize(body)
109 return resp, body
110
111 def update_alarm(self, alarm_id, **kwargs):
112 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
113 body = self.serialize(kwargs)
114 resp, body = self.put(uri, body)
115 body = self.deserialize(body)
116 return resp, body
117
118 def alarm_get_state(self, alarm_id):
119 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
120 resp, body = self.get(uri)
121 body = self.deserialize(body)
122 return resp, body
123
124 def alarm_set_state(self, alarm_id, state):
125 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
126 body = self.serialize(state)
127 resp, body = self.put(uri, body)
128 body = self.deserialize(body)
129 return resp, body