Nikolay Pliashechnikov | b053aab | 2013-11-05 06:06:44 -0800 | [diff] [blame] | 1 | # 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 | |
| 16 | import abc |
| 17 | import six |
| 18 | import urllib |
| 19 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
| 21 | |
| 22 | CONF = config.CONF |
| 23 | |
Nikolay Pliashechnikov | b053aab | 2013-11-05 06:06:44 -0800 | [diff] [blame] | 24 | |
| 25 | @six.add_metaclass(abc.ABCMeta) |
| 26 | class 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 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 38 | def __init__(self, username, password, auth_url, tenant_name=None): |
| 39 | self.rest_client = self.get_rest_client(username, password, |
Nikolay Pliashechnikov | b053aab | 2013-11-05 06:06:44 -0800 | [diff] [blame] | 40 | auth_url, tenant_name) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 41 | self.rest_client.service = CONF.telemetry.catalog_type |
Nikolay Pliashechnikov | b053aab | 2013-11-05 06:06:44 -0800 | [diff] [blame] | 42 | self.headers = self.rest_client.headers |
| 43 | self.version = '2' |
| 44 | self.uri_prefix = "v%s" % self.version |
| 45 | |
| 46 | @abc.abstractmethod |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 47 | def get_rest_client(self, username, password, |
Nikolay Pliashechnikov | b053aab | 2013-11-05 06:06:44 -0800 | [diff] [blame] | 48 | auth_url, tenant_name): |
| 49 | """ |
| 50 | :param config: |
| 51 | :param username: |
| 52 | :param password: |
| 53 | :param auth_url: |
| 54 | :param tenant_name: |
| 55 | :return: RestClient |
| 56 | """ |
| 57 | |
| 58 | @abc.abstractmethod |
| 59 | def deserialize(self, body): |
| 60 | """ |
| 61 | :param body: |
| 62 | :return: Deserialize body |
| 63 | """ |
| 64 | |
| 65 | @abc.abstractmethod |
| 66 | def serialize(self, body): |
| 67 | """ |
| 68 | :param body: |
| 69 | :return: Serialize body |
| 70 | """ |
| 71 | |
| 72 | def post(self, uri, body): |
| 73 | body = self.serialize(body) |
| 74 | resp, body = self.rest_client.post(uri, body, self.headers) |
| 75 | body = self.deserialize(body) |
| 76 | return resp, body |
| 77 | |
| 78 | def put(self, uri, body): |
| 79 | return self.rest_client.put(uri, body, self.headers) |
| 80 | |
| 81 | def get(self, uri): |
| 82 | resp, body = self.rest_client.get(uri) |
| 83 | 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): |
| 129 | uri = '%s/meter/%s' % (self.uri_prefix, alarm_id) |
| 130 | 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) |