blob: a073f54ffdd94b62a9f08eb69f2859cc9d29fabe [file] [log] [blame]
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -08001# Copyright 2013 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
16import abc
17import six
18import urllib
19
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
21
22CONF = config.CONF
23
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080024
25@six.add_metaclass(abc.ABCMeta)
26class TelemetryClientBase(object):
27
28 """
29 Tempest REST client for Ceilometer V2 API.
30 Implements the following basic Ceilometer abstractions:
31 resources
32 meters
33 alarms
34 queries
35 statistics
36 """
37
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000038 def __init__(self, auth_provider):
39 self.rest_client = self.get_rest_client(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000040 self.rest_client.service = CONF.telemetry.catalog_type
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080041 self.version = '2'
42 self.uri_prefix = "v%s" % self.version
43
44 @abc.abstractmethod
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000045 def get_rest_client(self, auth_provider):
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080046 """
47 :param config:
48 :param username:
49 :param password:
50 :param auth_url:
51 :param tenant_name:
52 :return: RestClient
53 """
54
55 @abc.abstractmethod
56 def deserialize(self, body):
57 """
58 :param body:
59 :return: Deserialize body
60 """
61
62 @abc.abstractmethod
63 def serialize(self, body):
64 """
65 :param body:
66 :return: Serialize body
67 """
68
69 def post(self, uri, body):
70 body = self.serialize(body)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020071 resp, body = self.rest_client.post(uri, body)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080072 body = self.deserialize(body)
73 return resp, body
74
75 def put(self, uri, body):
nayna-patela4c56c92014-03-24 12:16:52 +000076 body = self.serialize(body)
77 resp, body = self.rest_client.put(uri, body)
78 body = self.deserialize(body)
79 return resp, body
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080080
81 def get(self, uri):
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020082 resp, body = self.rest_client.get(uri)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080083 body = self.deserialize(body)
84 return resp, body
85
86 def delete(self, uri):
87 resp, body = self.rest_client.delete(uri)
88 if body:
89 body = self.deserialize(body)
90 return resp, body
91
92 def helper_list(self, uri, query=None, period=None):
93 uri_dict = {}
94 if query:
95 uri_dict = {'q.field': query[0],
96 'q.op': query[1],
97 'q.value': query[2]}
98 if period:
99 uri_dict['period'] = period
100 if uri_dict:
101 uri += "?%s" % urllib.urlencode(uri_dict)
102 return self.get(uri)
103
104 def list_resources(self):
105 uri = '%s/resources' % self.uri_prefix
106 return self.get(uri)
107
108 def list_meters(self):
109 uri = '%s/meters' % self.uri_prefix
110 return self.get(uri)
111
112 def list_alarms(self):
113 uri = '%s/alarms' % self.uri_prefix
114 return self.get(uri)
115
116 def list_statistics(self, meter, period=None, query=None):
117 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
118 return self.helper_list(uri, query, period)
119
120 def list_samples(self, meter_id, query=None):
121 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
122 return self.helper_list(uri, query)
123
124 def get_resource(self, resource_id):
125 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
126 return self.get(uri)
127
128 def get_alarm(self, alarm_id):
vrovachev95a16cc2014-02-04 15:29:48 +0400129 uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -0800130 return self.get(uri)
131
132 def delete_alarm(self, alarm_id):
133 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
134 return self.delete(uri)
vrovachev95a16cc2014-02-04 15:29:48 +0400135
136 def create_alarm(self, **kwargs):
137 uri = "%s/alarms" % self.uri_prefix
138 return self.post(uri, kwargs)
nayna-patela4c56c92014-03-24 12:16:52 +0000139
140 def update_alarm(self, alarm_id, **kwargs):
141 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
142 return self.put(uri, kwargs)
143
144 def alarm_get_state(self, alarm_id):
145 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
146 return self.get(uri)
147
148 def alarm_set_state(self, alarm_id, state):
149 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
150 return self.put(uri, state)