Chandan Kumar | 6f97ca0 | 2017-12-07 12:50:34 +0530 | [diff] [blame] | 1 | # 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 | |
Chandan Kumar | bc03a05 | 2018-01-11 15:01:04 +0530 | [diff] [blame] | 16 | import json |
| 17 | |
Chandan Kumar | 6f97ca0 | 2017-12-07 12:50:34 +0530 | [diff] [blame] | 18 | from six.moves.urllib import parse as urllib |
| 19 | from tempest import config |
| 20 | from tempest.lib.common import rest_client |
| 21 | from tempest import manager |
Chandan Kumar | 6f97ca0 | 2017-12-07 12:50:34 +0530 | [diff] [blame] | 22 | |
| 23 | CONF = config.CONF |
| 24 | |
| 25 | |
| 26 | class AlarmingClient(rest_client.RestClient): |
| 27 | |
| 28 | version = '2' |
| 29 | uri_prefix = "v2" |
| 30 | |
| 31 | def deserialize(self, body): |
yatin | af38fe5 | 2018-12-26 17:47:41 +0530 | [diff] [blame] | 32 | return json.loads(body.decode('utf-8').replace("\n", "")) |
Chandan Kumar | 6f97ca0 | 2017-12-07 12:50:34 +0530 | [diff] [blame] | 33 | |
| 34 | def serialize(self, body): |
Chandan Kumar | bc03a05 | 2018-01-11 15:01:04 +0530 | [diff] [blame] | 35 | return json.dumps(body) |
Chandan Kumar | 6f97ca0 | 2017-12-07 12:50:34 +0530 | [diff] [blame] | 36 | |
| 37 | def list_alarms(self, query=None): |
| 38 | uri = '%s/alarms' % self.uri_prefix |
| 39 | uri_dict = {} |
| 40 | if query: |
| 41 | uri_dict = {'q.field': query[0], |
| 42 | 'q.op': query[1], |
| 43 | 'q.value': query[2]} |
| 44 | if uri_dict: |
| 45 | uri += "?%s" % urllib.urlencode(uri_dict) |
| 46 | resp, body = self.get(uri) |
| 47 | self.expected_success(200, resp.status) |
| 48 | body = self.deserialize(body) |
| 49 | return rest_client.ResponseBodyList(resp, body) |
| 50 | |
| 51 | def show_alarm(self, alarm_id): |
| 52 | uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id) |
| 53 | resp, body = self.get(uri) |
| 54 | self.expected_success(200, resp.status) |
| 55 | body = self.deserialize(body) |
| 56 | return rest_client.ResponseBody(resp, body) |
| 57 | |
| 58 | def show_alarm_history(self, alarm_id): |
| 59 | uri = "%s/alarms/%s/history" % (self.uri_prefix, alarm_id) |
| 60 | resp, body = self.get(uri) |
| 61 | self.expected_success(200, resp.status) |
| 62 | body = self.deserialize(body) |
| 63 | return rest_client.ResponseBodyList(resp, body) |
| 64 | |
| 65 | def delete_alarm(self, alarm_id): |
| 66 | uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id) |
| 67 | resp, body = self.delete(uri) |
| 68 | self.expected_success(204, resp.status) |
| 69 | if body: |
| 70 | body = self.deserialize(body) |
| 71 | return rest_client.ResponseBody(resp, body) |
| 72 | |
| 73 | def create_alarm(self, **kwargs): |
| 74 | uri = "%s/alarms" % self.uri_prefix |
| 75 | body = self.serialize(kwargs) |
| 76 | resp, body = self.post(uri, body) |
| 77 | self.expected_success(201, resp.status) |
| 78 | body = self.deserialize(body) |
| 79 | return rest_client.ResponseBody(resp, body) |
| 80 | |
| 81 | def update_alarm(self, alarm_id, **kwargs): |
| 82 | uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id) |
| 83 | body = self.serialize(kwargs) |
| 84 | resp, body = self.put(uri, body) |
| 85 | self.expected_success(200, resp.status) |
| 86 | body = self.deserialize(body) |
| 87 | return rest_client.ResponseBody(resp, body) |
| 88 | |
| 89 | def show_alarm_state(self, alarm_id): |
| 90 | uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id) |
| 91 | resp, body = self.get(uri) |
| 92 | self.expected_success(200, resp.status) |
| 93 | body = self.deserialize(body) |
| 94 | return rest_client.ResponseBodyData(resp, body) |
| 95 | |
| 96 | def alarm_set_state(self, alarm_id, state): |
| 97 | uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id) |
| 98 | body = self.serialize(state) |
| 99 | resp, body = self.put(uri, body) |
| 100 | self.expected_success(200, resp.status) |
| 101 | body = self.deserialize(body) |
| 102 | return rest_client.ResponseBodyData(resp, body) |
| 103 | |
| 104 | |
| 105 | class Manager(manager.Manager): |
| 106 | |
| 107 | default_params = { |
| 108 | 'disable_ssl_certificate_validation': |
| 109 | CONF.identity.disable_ssl_certificate_validation, |
| 110 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 111 | 'trace_requests': CONF.debug.trace_requests |
| 112 | } |
| 113 | |
| 114 | alarming_params = { |
| 115 | 'service': CONF.alarming_plugin.catalog_type, |
| 116 | 'region': CONF.identity.region, |
| 117 | 'endpoint_type': CONF.alarming_plugin.endpoint_type, |
| 118 | } |
| 119 | alarming_params.update(default_params) |
| 120 | |
| 121 | def __init__(self, credentials=None, service=None): |
| 122 | super(Manager, self).__init__(credentials) |
| 123 | self.set_alarming_client() |
| 124 | |
| 125 | def set_alarming_client(self): |
| 126 | self.alarming_client = AlarmingClient(self.auth_provider, |
| 127 | **self.alarming_params) |