| 1 |
# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP |
| 2 |
# Copyright 2017 FUJITSU LIMITED |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain 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, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 |
# implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
from urllib import parse |
| 18 |
|
| 19 |
from monascaclient.common import monasca_manager |
| 20 |
|
| 21 |
|
| 22 |
class AlarmsManager(monasca_manager.MonascaManager): |
| 23 |
base_url = '/alarms' |
| 24 |
|
| 25 |
def get(self, **kwargs): |
| 26 |
"""Get the details for a specific alarm.""" |
| 27 |
|
| 28 |
# NOTE(trebskit) should actually be find_one, but |
| 29 |
# monasca does not support expected response format |
| 30 |
|
| 31 |
url = '%s/%s' % (self.base_url, kwargs['alarm_id']) |
| 32 |
resp = self.client.list(path=url) |
| 33 |
return resp |
| 34 |
|
| 35 |
def list(self, **kwargs): |
| 36 |
"""Get a list of alarms.""" |
| 37 |
return self._list('', 'metric_dimensions', **kwargs) |
| 38 |
|
| 39 |
def delete(self, **kwargs): |
| 40 |
"""Delete a specific alarm.""" |
| 41 |
url_str = self.base_url + '/%s' % kwargs['alarm_id'] |
| 42 |
resp = self.client.delete(url_str) |
| 43 |
return resp |
| 44 |
|
| 45 |
def update(self, **kwargs): |
| 46 |
"""Update a specific alarm.""" |
| 47 |
url_str = self.base_url + '/%s' % kwargs['alarm_id'] |
| 48 |
del kwargs['alarm_id'] |
| 49 |
|
| 50 |
body = self.client.create(url=url_str, |
| 51 |
method='PUT', |
| 52 |
json=kwargs) |
| 53 |
|
| 54 |
return body |
| 55 |
|
| 56 |
def patch(self, **kwargs): |
| 57 |
"""Patch a specific alarm.""" |
| 58 |
url_str = self.base_url + '/%s' % kwargs['alarm_id'] |
| 59 |
del kwargs['alarm_id'] |
| 60 |
|
| 61 |
resp = self.client.create(url=url_str, |
| 62 |
method='PATCH', |
| 63 |
json=kwargs) |
| 64 |
|
| 65 |
return resp |
| 66 |
|
| 67 |
def count(self, **kwargs): |
| 68 |
url_str = self.base_url + '/count' |
| 69 |
if 'metric_dimensions' in kwargs: |
| 70 |
dimstr = self.get_dimensions_url_string( |
| 71 |
kwargs['metric_dimensions']) |
| 72 |
kwargs['metric_dimensions'] = dimstr |
| 73 |
|
| 74 |
if kwargs: |
| 75 |
url_str = url_str + '?%s' % parse.urlencode(kwargs, True) |
| 76 |
body = self.client.list(url_str) |
| 77 |
return body |
| 78 |
|
| 79 |
def history(self, **kwargs): |
| 80 |
"""History of a specific alarm.""" |
| 81 |
url_str = self.base_url + '/%s/state-history' % kwargs['alarm_id'] |
| 82 |
del kwargs['alarm_id'] |
| 83 |
if kwargs: |
| 84 |
url_str = url_str + '?%s' % parse.urlencode(kwargs, True) |
| 85 |
resp = self.client.list(url_str) |
| 86 |
return resp['elements'] if type(resp) is dict else resp |
| 87 |
|
| 88 |
def history_list(self, **kwargs): |
| 89 |
"""History list of alarm state.""" |
| 90 |
url_str = self.base_url + '/state-history/' |
| 91 |
if 'dimensions' in kwargs: |
| 92 |
dimstr = self.get_dimensions_url_string(kwargs['dimensions']) |
| 93 |
kwargs['dimensions'] = dimstr |
| 94 |
if kwargs: |
| 95 |
url_str = url_str + '?%s' % parse.urlencode(kwargs, True) |
| 96 |
resp = self.client.list(url_str) |
| 97 |
return resp['elements'] if type(resp) is dict else resp |