blob: 05530b15d6df11bdf425fe88a899e91acb89356b [file] [log] [blame]
Ryota MIBU9a3a74b2015-12-10 15:17:11 +09001# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.common import service_client
20
21
22class TelemetryClient(service_client.ServiceClient):
23
24 version = '2'
25 uri_prefix = "v2"
26
27 def deserialize(self, body):
28 return json.loads(body.replace("\n", ""))
29
30 def serialize(self, body):
31 return json.dumps(body)
32
33 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)
46 body = self.serialize(sample_list)
47 resp, body = self.post(uri, body)
48 self.expected_success(200, resp.status)
49 body = self.deserialize(body)
50 return service_client.ResponseBody(resp, body)
51
52 def _helper_list(self, uri, query=None, period=None):
53 uri_dict = {}
54 if query:
55 uri_dict = {'q.field': query[0],
56 'q.op': query[1],
57 'q.value': query[2]}
58 if period:
59 uri_dict['period'] = period
60 if uri_dict:
61 uri += "?%s" % urllib.urlencode(uri_dict)
62 resp, body = self.get(uri)
63 self.expected_success(200, resp.status)
64 body = self.deserialize(body)
65 return service_client.ResponseBodyList(resp, body)
66
67 def list_resources(self, query=None):
68 uri = '%s/resources' % self.uri_prefix
69 return self._helper_list(uri, query)
70
71 def list_meters(self, query=None):
72 uri = '%s/meters' % self.uri_prefix
73 return self._helper_list(uri, query)
74
75 def list_statistics(self, meter, period=None, query=None):
76 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
77 return self._helper_list(uri, query, period)
78
79 def list_samples(self, meter_id, query=None):
80 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
81 return self._helper_list(uri, query)
82
83 def list_events(self, query=None):
84 uri = '%s/events' % self.uri_prefix
85 return self._helper_list(uri, query)
86
87 def show_resource(self, resource_id):
88 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
89 resp, body = self.get(uri)
90 self.expected_success(200, resp.status)
91 body = self.deserialize(body)
92 return service_client.ResponseBody(resp, body)