blob: 610f07b1b24dee9b73acf2ceb878b86e475bd645 [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):
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020076 return self.rest_client.put(uri, body)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080077
78 def get(self, uri):
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020079 resp, body = self.rest_client.get(uri)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -080080 body = self.deserialize(body)
81 return resp, body
82
83 def delete(self, uri):
84 resp, body = self.rest_client.delete(uri)
85 if body:
86 body = self.deserialize(body)
87 return resp, body
88
89 def helper_list(self, uri, query=None, period=None):
90 uri_dict = {}
91 if query:
92 uri_dict = {'q.field': query[0],
93 'q.op': query[1],
94 'q.value': query[2]}
95 if period:
96 uri_dict['period'] = period
97 if uri_dict:
98 uri += "?%s" % urllib.urlencode(uri_dict)
99 return self.get(uri)
100
101 def list_resources(self):
102 uri = '%s/resources' % self.uri_prefix
103 return self.get(uri)
104
105 def list_meters(self):
106 uri = '%s/meters' % self.uri_prefix
107 return self.get(uri)
108
109 def list_alarms(self):
110 uri = '%s/alarms' % self.uri_prefix
111 return self.get(uri)
112
113 def list_statistics(self, meter, period=None, query=None):
114 uri = "%s/meters/%s/statistics" % (self.uri_prefix, meter)
115 return self.helper_list(uri, query, period)
116
117 def list_samples(self, meter_id, query=None):
118 uri = '%s/meters/%s' % (self.uri_prefix, meter_id)
119 return self.helper_list(uri, query)
120
121 def get_resource(self, resource_id):
122 uri = '%s/resources/%s' % (self.uri_prefix, resource_id)
123 return self.get(uri)
124
125 def get_alarm(self, alarm_id):
vrovachev95a16cc2014-02-04 15:29:48 +0400126 uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
Nikolay Pliashechnikovb053aab2013-11-05 06:06:44 -0800127 return self.get(uri)
128
129 def delete_alarm(self, alarm_id):
130 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
131 return self.delete(uri)
vrovachev95a16cc2014-02-04 15:29:48 +0400132
133 def create_alarm(self, **kwargs):
134 uri = "%s/alarms" % self.uri_prefix
135 return self.post(uri, kwargs)