blob: 59c585e7fe4da790cb4a15f330bf056ec065af82 [file] [log] [blame]
Ryota MIBU08ff8672015-12-10 15:54:38 +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
Andrea Frittolid15c5162017-10-12 22:10:04 +010019from tempest import clients
Ryota MIBU08ff8672015-12-10 15:54:38 +090020from tempest import config
21from tempest.lib.common import rest_client
Ryota MIBU08ff8672015-12-10 15:54:38 +090022
23
24CONF = config.CONF
25
26
27class TelemetryClient(rest_client.RestClient):
28
29 version = '2'
30 uri_prefix = "v2"
31
32 def deserialize(self, body):
33 return json.loads(body.replace("\n", ""))
34
35 def serialize(self, body):
36 return json.dumps(body)
37
38 def create_sample(self, meter_name, sample_list):
39 uri = "%s/meters/%s" % (self.uri_prefix, meter_name)
40 body = self.serialize(sample_list)
41 resp, body = self.post(uri, body)
42 self.expected_success(200, resp.status)
43 body = self.deserialize(body)
44 return rest_client.ResponseBody(resp, body)
45
46 def _helper_list(self, uri, query=None, period=None):
47 uri_dict = {}
48 if query:
49 uri_dict = {'q.field': query[0],
50 'q.op': query[1],
51 'q.value': query[2]}
52 if period:
53 uri_dict['period'] = period
54 if uri_dict:
55 uri += "?%s" % urllib.urlencode(uri_dict)
56 resp, body = self.get(uri)
57 self.expected_success(200, resp.status)
58 body = self.deserialize(body)
59 return rest_client.ResponseBodyList(resp, body)
60
61 def list_resources(self, query=None):
62 uri = '%s/resources' % self.uri_prefix
63 return self._helper_list(uri, query)
64
65 def list_meters(self, query=None):
66 uri = '%s/meters' % self.uri_prefix
67 return self._helper_list(uri, query)
68
69 def list_statistics(self, meter, period=None, query=None):
70 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
71 return self._helper_list(uri, query, period)
72
73 def list_samples(self, meter_id, query=None):
74 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
75 return self._helper_list(uri, query)
76
Ryota MIBU08ff8672015-12-10 15:54:38 +090077 def show_resource(self, resource_id):
78 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
79 resp, body = self.get(uri)
80 self.expected_success(200, resp.status)
81 body = self.deserialize(body)
82 return rest_client.ResponseBody(resp, body)
83
84
Andrea Frittolid15c5162017-10-12 22:10:04 +010085class Manager(clients.Manager):
Ryota MIBU08ff8672015-12-10 15:54:38 +090086
Andrea Frittolid15c5162017-10-12 22:10:04 +010087 default_params = config.service_client_config()
Ryota MIBU08ff8672015-12-10 15:54:38 +090088
89 telemetry_params = {
ghanshyam905e0812016-04-14 11:45:56 +090090 'service': CONF.telemetry.catalog_type,
Ryota MIBU08ff8672015-12-10 15:54:38 +090091 'region': CONF.identity.region,
ghanshyam905e0812016-04-14 11:45:56 +090092 'endpoint_type': CONF.telemetry.endpoint_type,
Ryota MIBU08ff8672015-12-10 15:54:38 +090093 }
94 telemetry_params.update(default_params)
95
Andrea Frittolid15c5162017-10-12 22:10:04 +010096 def __init__(self, credentials):
97 # TODO(andreaf) Overriding Manager is a workaround. The "proper" way
98 # would it to expose the ceilometer service client via the plugin
99 # interface, use tempest.lib.clients and tempest master.
100 # Then ceilometer service client would be loaded and configured
101 # automatically into ServiceClients.
102 # In any case we're about to declare clients.Manager a stable
103 # interface for plugins and we won't change it, so this code won't
104 # break.
105 super(Manager, self).__init__(credentials=credentials)
106 self.set_telemetry_client()
Ryota MIBU08ff8672015-12-10 15:54:38 +0900107
108 def set_telemetry_client(self):
109 self.telemetry_client = TelemetryClient(self.auth_provider,
110 **self.telemetry_params)