blob: 369a9edc31c9c01c26c608d490916c6a2c283f41 [file] [log] [blame]
Chandan Kumar6f97ca02017-12-07 12:50:34 +05301# 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 Kumarbc03a052018-01-11 15:01:04 +053016import json
17
Takashi Kajinamidc522902023-12-20 21:57:53 +090018from urllib import parse
19
Martin Kopec43e01f52020-12-18 13:14:28 +000020from tempest import clients as tempest_clients
Chandan Kumar6f97ca02017-12-07 12:50:34 +053021from tempest import config
22from tempest.lib.common import rest_client
Martin Kopec43e01f52020-12-18 13:14:28 +000023from tempest.lib.services import clients
24
Chandan Kumar6f97ca02017-12-07 12:50:34 +053025
26CONF = config.CONF
27
28
29class AlarmingClient(rest_client.RestClient):
30
31 version = '2'
32 uri_prefix = "v2"
33
34 def deserialize(self, body):
yatinaf38fe52018-12-26 17:47:41 +053035 return json.loads(body.decode('utf-8').replace("\n", ""))
Chandan Kumar6f97ca02017-12-07 12:50:34 +053036
37 def serialize(self, body):
Chandan Kumarbc03a052018-01-11 15:01:04 +053038 return json.dumps(body)
Chandan Kumar6f97ca02017-12-07 12:50:34 +053039
Erno Kuvaja50072122023-10-19 13:08:39 +010040 def list_alarms(self, query=None, sort=None, limit=None, marker=None):
Chandan Kumar6f97ca02017-12-07 12:50:34 +053041 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 Kuvaja50072122023-10-19 13:08:39 +010047 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 Kumar6f97ca02017-12-07 12:50:34 +053053 if uri_dict:
Takashi Kajinamidc522902023-12-20 21:57:53 +090054 uri += "?%s" % parse.urlencode(uri_dict, doseq=True)
Chandan Kumar6f97ca02017-12-07 12:50:34 +053055 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 Kuvaja50072122023-10-19 13:08:39 +0100113 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 Kumar6f97ca02017-12-07 12:50:34 +0530135
Martin Kopec43e01f52020-12-18 13:14:28 +0000136class Manager(clients.ServiceClients):
Chandan Kumar6f97ca02017-12-07 12:50:34 +0530137
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 Kopec43e01f52020-12-18 13:14:28 +0000153 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 Kumar6f97ca02017-12-07 12:50:34 +0530162 self.set_alarming_client()
163
164 def set_alarming_client(self):
165 self.alarming_client = AlarmingClient(self.auth_provider,
166 **self.alarming_params)