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