blob: 2bbd88da86cfb87a7598dc734b637e249979cef4 [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
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000019from tempest import config
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080020from tempest.openstack.common import jsonutils as json
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000021
22CONF = config.CONF
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080023
24
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000025class TelemetryClientJSON(service_client.ServiceClient):
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080026
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000027 def __init__(self, auth_provider):
Ken'ichi Ohmichi0690ea42015-01-02 07:03:51 +000028 super(TelemetryClientJSON, self).__init__(
29 auth_provider,
30 CONF.telemetry.catalog_type,
Ken'ichi Ohmichie9f50412015-01-05 04:57:26 +000031 CONF.identity.region,
Ken'ichi Ohmichi0690ea42015-01-02 07:03:51 +000032 endpoint_type=CONF.telemetry.endpoint_type)
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000033 self.version = '2'
34 self.uri_prefix = "v%s" % self.version
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080035
36 def deserialize(self, body):
37 return json.loads(body.replace("\n", ""))
38
39 def serialize(self, body):
40 return json.dumps(body)
41
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080042 def add_sample(self, sample_list, meter_name, meter_unit, volume,
43 sample_type, resource_id, **kwargs):
44 sample = {"counter_name": meter_name, "counter_unit": meter_unit,
45 "counter_volume": volume, "counter_type": sample_type,
46 "resource_id": resource_id}
47 for key in kwargs:
48 sample[key] = kwargs[key]
49
50 sample_list.append(self.serialize(sample))
51 return sample_list
52
53 def create_sample(self, meter_name, sample_list):
54 uri = "%s/meters/%s" % (self.uri_prefix, meter_name)
Ken'ichi Ohmichi553b6672014-12-22 01:37:47 +000055 body = self.serialize(sample_list)
56 resp, body = self.post(uri, body)
57 body = self.deserialize(body)
58 return resp, body
59
60 def helper_list(self, uri, query=None, period=None):
61 uri_dict = {}
62 if query:
63 uri_dict = {'q.field': query[0],
64 'q.op': query[1],
65 'q.value': query[2]}
66 if period:
67 uri_dict['period'] = period
68 if uri_dict:
69 uri += "?%s" % urllib.urlencode(uri_dict)
70 resp, body = self.get(uri)
71 body = self.deserialize(body)
72 return resp, body
73
74 def list_resources(self, query=None):
75 uri = '%s/resources' % self.uri_prefix
76 return self.helper_list(uri, query)
77
78 def list_meters(self, query=None):
79 uri = '%s/meters' % self.uri_prefix
80 return self.helper_list(uri, query)
81
82 def list_alarms(self, query=None):
83 uri = '%s/alarms' % self.uri_prefix
84 return self.helper_list(uri, query)
85
86 def list_statistics(self, meter, period=None, query=None):
87 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
88 return self.helper_list(uri, query, period)
89
90 def list_samples(self, meter_id, query=None):
91 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
92 return self.helper_list(uri, query)
93
94 def get_resource(self, resource_id):
95 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
96 resp, body = self.get(uri)
97 body = self.deserialize(body)
98 return resp, body
99
100 def get_alarm(self, alarm_id):
101 uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
102 resp, body = self.get(uri)
103 body = self.deserialize(body)
104 return resp, body
105
106 def delete_alarm(self, alarm_id):
107 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
108 resp, body = self.delete(uri)
109 if body:
110 body = self.deserialize(body)
111 return resp, body
112
113 def create_alarm(self, **kwargs):
114 uri = "%s/alarms" % self.uri_prefix
115 body = self.serialize(kwargs)
116 resp, body = self.post(uri, body)
117 body = self.deserialize(body)
118 return resp, body
119
120 def update_alarm(self, alarm_id, **kwargs):
121 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
122 body = self.serialize(kwargs)
123 resp, body = self.put(uri, body)
124 body = self.deserialize(body)
125 return resp, body
126
127 def alarm_get_state(self, alarm_id):
128 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
129 resp, body = self.get(uri)
130 body = self.deserialize(body)
131 return resp, body
132
133 def alarm_set_state(self, alarm_id, state):
134 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
135 body = self.serialize(state)
136 resp, body = self.put(uri, body)
137 body = self.deserialize(body)
138 return resp, body