Debian Codegraph @

aodhclient/v2/alarm.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 from oslo_serialization import jsonutils
15
16 from aodhclient import utils
17 from aodhclient.v2 import alarm_cli
18 from aodhclient.v2 import base
19
20
21 class AlarmManager(base.Manager):
22
23 url = "v2/alarms"
24
25 @staticmethod
26 def _filtersdict_to_url(filters):
27 urls = []
28 for k, v in sorted(filters.items()):
29 url = f"q.field={k}&q.op=eq&q.value={v}"
30 urls.append(url)
31 return '&'.join(urls)
32
33 def list(self, filters=None, limit=None,
34 marker=None, sorts=None):
35 """List alarms.
36
37 :param filters: A dict includes filters parameters, for example,
38 {'type': 'gnocchi_resources_threshold',
39 'severity': 'low'} represent filters to query alarms
40 with type='gnocchi_resources_threshold' and
41 severity='low'.
42 :type filters: dict
43 :param limit: maximum number of resources to return
44 :type limit: int
45 :param marker: the last item of the previous page; we return the next
46 results after this value.
47 :type marker: str
48 :param sorts: list of resource attributes to order by.
49 :type sorts: list of str
50 """
51 pagination = utils.get_pagination_options(limit, marker, sorts)
52 filter_string = (self._filtersdict_to_url(filters) if
53 filters else "")
54 url = self.url
55 options = []
56 if filter_string:
57 options.append(filter_string)
58 if pagination:
59 options.append(pagination)
60 if options:
61 url += "?" + "&".join(options)
62 return self._get(url).json()
63
64 def query(self, query=None):
65 """Query alarms.
66
67 :param query: A json format complex query expression, like this:
68 '{"=":{"type":"gnocchi_resources_threshold"}}', this
69 expression is used to query all the
70 gnocchi_resources_threshold type alarms.
71 :type query: json
72 """
73 query = {'filter': query}
74 url = "v2/query/alarms"
75 return self._post(url,
76 headers={'Content-Type': "application/json"},
77 data=jsonutils.dumps(query)).json()
78
79 def get(self, alarm_id):
80 """Get an alarm
81
82 :param alarm_id: ID of the alarm
83 :type alarm_id: str
84 """
85 return self._get(self.url + '/' + alarm_id).json()
86
87 @staticmethod
88 def _clean_rules(alarm_type, alarm):
89 for rule in alarm_cli.ALARM_TYPES:
90 if rule != alarm_type:
91 alarm.pop('%s_rule' % rule, None)
92
93 def create(self, alarm):
94 """Create an alarm
95
96 :param alarm: the alarm
97 :type alarm: dict
98 """
99 self._clean_rules(alarm['type'], alarm)
100 return self._post(
101 self.url, headers={'Content-Type': "application/json"},
102 data=jsonutils.dumps(alarm)).json()
103
104 def update(self, alarm_id, alarm_update):
105 """Update an alarm
106
107 :param alarm_id: ID of the alarm
108 :type alarm_id: str
109 :param attributes: Attributes of the alarm
110 :type attributes: dict
111 """
112 alarm = self._get(self.url + '/' + alarm_id).json()
113 if 'type' not in alarm_update:
114 self._clean_rules(alarm['type'], alarm_update)
115 else:
116 self._clean_rules(alarm_update['type'], alarm_update)
117
118 if 'prometheus_rule' in alarm_update:
119 rule = alarm_update.get('prometheus_rule')
120 alarm['prometheus_rule'].update(rule)
121 alarm_update.pop('prometheus_rule')
122 elif 'threshold_rule' in alarm_update:
123 alarm['threshold_rule'].update(alarm_update.get('threshold_rule'))
124 alarm_update.pop('threshold_rule')
125 elif 'event_rule' in alarm_update:
126 if ('type' in alarm_update and
127 alarm_update['type'] != alarm['type']):
128 alarm.pop('%s_rule' % alarm['type'], None)
129 alarm['event_rule'] = alarm_update['event_rule']
130 else:
131 alarm['event_rule'].update(alarm_update.get('event_rule'))
132 alarm_update.pop('event_rule')
133 elif 'gnocchi_resources_threshold_rule' in alarm_update:
134 if ('type' in alarm_update and
135 alarm_update['type'] != alarm['type']):
136 alarm.pop('%s_rule' % alarm['type'], None)
137 alarm['gnocchi_resources_threshold_rule'] = alarm_update[
138 'gnocchi_resources_threshold_rule']
139 else:
140 alarm['gnocchi_resources_threshold_rule'].update(
141 alarm_update.get('gnocchi_resources_threshold_rule'))
142 alarm_update.pop('gnocchi_resources_threshold_rule')
143 elif 'gnocchi_aggregation_by_metrics_threshold_rule' in alarm_update:
144 if ('type' in alarm_update and
145 alarm_update['type'] != alarm['type']):
146 alarm.pop('%s_rule' % alarm['type'], None)
147 alarm['gnocchi_aggregation_by_metrics_threshold_rule'] = \
148 alarm_update[
149 'gnocchi_aggregation_by_metrics_threshold_rule']
150 else:
151 alarm['gnocchi_aggregation_by_metrics_threshold_rule'].update(
152 alarm_update.get(
153 'gnocchi_aggregation_by_metrics_threshold_rule'))
154 alarm_update.pop('gnocchi_aggregation_by_metrics_threshold_rule')
155 elif 'gnocchi_aggregation_by_resources_threshold_rule' in alarm_update:
156 if ('type' in alarm_update and
157 alarm_update['type'] != alarm['type']):
158 alarm.pop('%s_rule' % alarm['type'], None)
159 alarm['gnocchi_aggregation_by_resources_threshold_rule'] = \
160 alarm_update[
161 'gnocchi_aggregation_by_resources_threshold_rule']
162 else:
163 alarm['gnocchi_aggregation_by_resources_threshold_rule'].\
164 update(alarm_update.get(
165 'gnocchi_aggregation_by_resources_threshold_rule'))
166 alarm_update.pop(
167 'gnocchi_aggregation_by_resources_threshold_rule')
168 elif 'composite_rule' in alarm_update:
169 if ('type' in alarm_update and
170 alarm_update['type'] != alarm['type']):
171 alarm.pop('%s_rule' % alarm['type'], None)
172 if alarm_update['composite_rule'] is not None:
173 alarm['composite_rule'] = alarm_update[
174 'composite_rule']
175 alarm_update.pop('composite_rule')
176 elif 'loadbalancer_member_health_rule' in alarm_update:
177 if ('type' in alarm_update and
178 alarm_update['type'] != alarm['type']):
179 alarm.pop('%s_rule' % alarm['type'], None)
180 if alarm_update['loadbalancer_member_health_rule'] is not None:
181 alarm['loadbalancer_member_health_rule'] = alarm_update[
182 'loadbalancer_member_health_rule']
183 alarm_update.pop('loadbalancer_member_health_rule')
184
185 alarm.update(alarm_update)
186
187 return self._put(
188 self.url + '/' + alarm_id,
189 headers={'Content-Type': "application/json"},
190 data=jsonutils.dumps(alarm)).json()
191
192 def delete(self, alarm_id):
193 """Delete an alarm
194
195 :param alarm_id: ID of the alarm
196 :type alarm_id: str
197 """
198 self._delete(self.url + '/' + alarm_id)
199
200 def get_state(self, alarm_id):
201 """Get the state of an alarm
202
203 :param alarm_id: ID of the alarm
204 :type alarm_id: str
205 """
206 return self._get(self.url + '/' + alarm_id + '/state').json()
207
208 def set_state(self, alarm_id, state):
209 """Set the state of an alarm
210
211 :param alarm_id: ID of the alarm
212 :type alarm_id: str
213 :param state: the state to be updated to the alarm
214 :type state: str
215 """
216 return self._put(self.url + '/' + alarm_id + '/state',
217 headers={'Content-Type': "application/json"},
218 data='"%s"' % state
219 ).json()