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