blob: 071bfffd7ffcae36b1f277e9a7cafba546e816a2 [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
Chandan Kumar6f97ca02017-12-07 12:50:34 +053018from six.moves.urllib import parse as urllib
Martin Kopec43e01f52020-12-18 13:14:28 +000019from tempest import clients as tempest_clients
Chandan Kumar6f97ca02017-12-07 12:50:34 +053020from tempest import config
21from tempest.lib.common import rest_client
Martin Kopec43e01f52020-12-18 13:14:28 +000022from tempest.lib.services import clients
23
Chandan Kumar6f97ca02017-12-07 12:50:34 +053024
25CONF = config.CONF
26
27
28class AlarmingClient(rest_client.RestClient):
29
30 version = '2'
31 uri_prefix = "v2"
32
33 def deserialize(self, body):
yatinaf38fe52018-12-26 17:47:41 +053034 return json.loads(body.decode('utf-8').replace("\n", ""))
Chandan Kumar6f97ca02017-12-07 12:50:34 +053035
36 def serialize(self, body):
Chandan Kumarbc03a052018-01-11 15:01:04 +053037 return json.dumps(body)
Chandan Kumar6f97ca02017-12-07 12:50:34 +053038
Erno Kuvaja50072122023-10-19 13:08:39 +010039 def list_alarms(self, query=None, sort=None, limit=None, marker=None):
Chandan Kumar6f97ca02017-12-07 12:50:34 +053040 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]}
Erno Kuvaja50072122023-10-19 13:08:39 +010046 if sort:
47 uri_dict.update({'sort': sort})
48 if limit is not None:
49 uri_dict.update({'limit': int(limit)})
50 if marker:
51 uri_dict.update({'marker': marker})
Chandan Kumar6f97ca02017-12-07 12:50:34 +053052 if uri_dict:
Erno Kuvaja50072122023-10-19 13:08:39 +010053 uri += "?%s" % urllib.urlencode(uri_dict, doseq=True)
Chandan Kumar6f97ca02017-12-07 12:50:34 +053054 resp, body = self.get(uri)
55 self.expected_success(200, resp.status)
56 body = self.deserialize(body)
57 return rest_client.ResponseBodyList(resp, body)
58
59 def show_alarm(self, alarm_id):
60 uri = '%s/alarms/%s' % (self.uri_prefix, alarm_id)
61 resp, body = self.get(uri)
62 self.expected_success(200, resp.status)
63 body = self.deserialize(body)
64 return rest_client.ResponseBody(resp, body)
65
66 def show_alarm_history(self, alarm_id):
67 uri = "%s/alarms/%s/history" % (self.uri_prefix, alarm_id)
68 resp, body = self.get(uri)
69 self.expected_success(200, resp.status)
70 body = self.deserialize(body)
71 return rest_client.ResponseBodyList(resp, body)
72
73 def delete_alarm(self, alarm_id):
74 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
75 resp, body = self.delete(uri)
76 self.expected_success(204, resp.status)
77 if body:
78 body = self.deserialize(body)
79 return rest_client.ResponseBody(resp, body)
80
81 def create_alarm(self, **kwargs):
82 uri = "%s/alarms" % self.uri_prefix
83 body = self.serialize(kwargs)
84 resp, body = self.post(uri, body)
85 self.expected_success(201, resp.status)
86 body = self.deserialize(body)
87 return rest_client.ResponseBody(resp, body)
88
89 def update_alarm(self, alarm_id, **kwargs):
90 uri = "%s/alarms/%s" % (self.uri_prefix, alarm_id)
91 body = self.serialize(kwargs)
92 resp, body = self.put(uri, body)
93 self.expected_success(200, resp.status)
94 body = self.deserialize(body)
95 return rest_client.ResponseBody(resp, body)
96
97 def show_alarm_state(self, alarm_id):
98 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
99 resp, body = self.get(uri)
100 self.expected_success(200, resp.status)
101 body = self.deserialize(body)
102 return rest_client.ResponseBodyData(resp, body)
103
104 def alarm_set_state(self, alarm_id, state):
105 uri = "%s/alarms/%s/state" % (self.uri_prefix, alarm_id)
106 body = self.serialize(state)
107 resp, body = self.put(uri, body)
108 self.expected_success(200, resp.status)
109 body = self.deserialize(body)
110 return rest_client.ResponseBodyData(resp, body)
111
Erno Kuvaja50072122023-10-19 13:08:39 +0100112 def invalid_path(self, headers=None):
113 uri = "invalid_path"
114 extra_headers = headers is not None
115 resp, body = self.get(uri, headers, extra_headers)
116 self.expected_success(404, resp.status)
117 body = self.deserialize(body)
118 return rest_client.ResponseBodyData(resp, body)
119
120 def show_capabilities(self):
121 uri = "%s/capabilities" % (self.uri_prefix)
122 resp, body = self.get(uri)
123 self.expected_success(200, resp.status)
124 body = self.deserialize(body)
125 return rest_client.ResponseBody(resp, body)
126
127 def show_version(self):
128 uri = '/'
129 resp, body = self.get(uri)
130 self.expected_success(200, resp.status)
131 body = self.deserialize(body)
132 return rest_client.ResponseBody(resp, body)
133
Chandan Kumar6f97ca02017-12-07 12:50:34 +0530134
Martin Kopec43e01f52020-12-18 13:14:28 +0000135class Manager(clients.ServiceClients):
Chandan Kumar6f97ca02017-12-07 12:50:34 +0530136
137 default_params = {
138 'disable_ssl_certificate_validation':
139 CONF.identity.disable_ssl_certificate_validation,
140 'ca_certs': CONF.identity.ca_certificates_file,
141 'trace_requests': CONF.debug.trace_requests
142 }
143
144 alarming_params = {
145 'service': CONF.alarming_plugin.catalog_type,
146 'region': CONF.identity.region,
147 'endpoint_type': CONF.alarming_plugin.endpoint_type,
148 }
149 alarming_params.update(default_params)
150
151 def __init__(self, credentials=None, service=None):
Martin Kopec43e01f52020-12-18 13:14:28 +0000152 dscv = CONF.identity.disable_ssl_certificate_validation
153 _, uri = tempest_clients.get_auth_provider_class(credentials)
154 super(Manager, self).__init__(
155 credentials=credentials,
156 identity_uri=uri,
157 scope='project',
158 disable_ssl_certificate_validation=dscv,
159 ca_certs=CONF.identity.ca_certificates_file,
160 trace_requests=CONF.debug.trace_requests)
Chandan Kumar6f97ca02017-12-07 12:50:34 +0530161 self.set_alarming_client()
162
163 def set_alarming_client(self):
164 self.alarming_client = AlarmingClient(self.auth_provider,
165 **self.alarming_params)