Skip to content

Commit 2ed6285

Browse files
authored
Merge pull request #48 from gbaufake/alerts-enhacenment
[WIP] New Methods for Hawkular Alerts
2 parents 0f60262 + fd1decb commit 2ed6285

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

hawkular/alerts.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from hawkular.client import ApiOject, HawkularBaseClient
17+
from hawkular.client import ApiObject, HawkularBaseClient
1818

19-
class Trigger(ApiOject):
19+
class Trigger(ApiObject):
2020
__slots__ = [
2121
'id', 'name', 'description', 'type', 'event_type', 'event_category',
2222
'event_text', 'event_category', 'event_text', 'severity', 'context',
@@ -25,8 +25,7 @@ class Trigger(ApiOject):
2525
'enabled', 'firing_match', 'source'
2626
]
2727

28-
29-
class Condition(ApiOject):
28+
class Condition(ApiObject):
3029
__slots__ = [
3130
'trigger_id', 'trigger_mode', 'type', 'condition_set_size',
3231
'condition_set_index', 'condition_id', 'context', 'data_id',
@@ -36,14 +35,13 @@ class Condition(ApiOject):
3635
]
3736

3837

39-
class Dampening(ApiOject):
38+
class Dampening(ApiObject):
4039
__slots__ = [
4140
'trigger_id', 'trigger_mode', 'type', 'eval_true_setting',
4241
'eval_total_setting', 'eval_time_setting', 'dampening_id'
4342
]
4443

45-
46-
class FullTrigger(ApiOject):
44+
class FullTrigger(ApiObject):
4745
defaults = {
4846
'conditions': [],
4947
'dampenings': []
@@ -59,20 +57,20 @@ def __init__(self, dictionary=dict()):
5957
self.conditions = Condition.list_to_object_list(udict.get('conditions'))
6058

6159

62-
class GroupMemberInfo(ApiOject):
60+
class GroupMemberInfo(ApiObject):
6361
__slots__ = [
6462
'group_id', 'member_id', 'member_name', 'member_description', 'member_context',
6563
'member_tags', 'data_id_map'
6664
]
6765

6866

69-
class GroupConditionsInfo(ApiOject):
67+
class GroupConditionsInfo(ApiObject):
7068
__slots__ = [
7169
'conditions', 'data_id_member_map'
7270
]
7371

7472
def __init__(self, dictionary=dict()):
75-
ApiOject.__init__(self, dictionary)
73+
ApiObject.__init__(self, dictionary)
7674
udict = self.transform_dict_to_underscore(dictionary)
7775
self.conditions = Condition.list_to_object_list(udict.get('conditions'))
7876

@@ -192,6 +190,12 @@ def create_group_member(self, member):
192190
data = self._serialize_object(member)
193191
return Trigger(self._post(self._service_url(['triggers', 'groups', 'members']), data))
194192

193+
def put_trigger_conditions(self, trigger_id, trigger_mode, conditions):
194+
data = self._serialize_object(conditions)
195+
url = self._service_url(['triggers', trigger_id, 'conditions', trigger_mode])
196+
response = self._put(url, data)
197+
return Condition.list_to_object_list(response)
198+
195199
def get_trigger_conditions(self, trigger_id):
196200
"""
197201
Get all conditions for a specific trigger

hawkular/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
class ApiJsonEncoder(json.JSONEncoder):
4242
def default(self, obj):
43-
if isinstance(obj, ApiOject):
43+
if isinstance(obj, ApiObject):
4444
return obj.to_json_object()
4545
else:
4646
return json.JSONEncoder.default(self, obj)
@@ -71,12 +71,12 @@ def http_response(self, request, response):
7171
https_response = http_response
7272

7373

74-
class ApiOject:
74+
class ApiObject:
7575

7676
defaults = dict()
7777

7878
def __init__(self, dictionary=dict()):
79-
udict = ApiOject.transform_dict_to_underscore(dictionary)
79+
udict = ApiObject.transform_dict_to_underscore(dictionary)
8080
for k in self.__slots__:
8181
setattr(self, k, udict.get(k,self.defaults.get(k)))
8282

@@ -85,7 +85,7 @@ def to_json_object(self):
8585
for attribute in self.__slots__:
8686
if hasattr(self,attribute):
8787
dictionary[attribute] = getattr(self,attribute)
88-
return ApiOject.transform_dict_to_camelcase(dictionary)
88+
return ApiObject.transform_dict_to_camelcase(dictionary)
8989

9090
@staticmethod
9191
def _to_camelcase(word):
@@ -100,13 +100,13 @@ def _to_underscore(word):
100100
def transform_dict_to_camelcase(dictionary):
101101
if dictionary is None:
102102
return dict()
103-
return dict((ApiOject._to_camelcase(k), v) for k, v in dictionary.items() if v is not None)
103+
return dict((ApiObject._to_camelcase(k), v) for k, v in dictionary.items() if v is not None)
104104

105105
@staticmethod
106106
def transform_dict_to_underscore(dictionary):
107107
if dictionary is None:
108108
return dict()
109-
return dict((ApiOject._to_underscore(k), v) for k, v in dictionary.items() if v is not None)
109+
return dict((ApiObject._to_underscore(k), v) for k, v in dictionary.items() if v is not None)
110110

111111
@classmethod
112112
def list_to_object_list(cls, o):

hawkular/metrics.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
except ImportError:
2929
import json
3030

31-
from hawkular.client import ApiOject, HawkularBaseClient, HawkularMetricsError
31+
from hawkular.client import ApiObject, HawkularBaseClient, HawkularMetricsError
3232
from hawkular.client import HawkularMetricsConnectionError, HawkularMetricsStatusError
3333

3434
class MetricType:
@@ -91,25 +91,25 @@ def _get_single_id_url(self, previous_url, id):
9191
@staticmethod
9292
def _transform_tags(**tags):
9393
return ','.join("%s:%s" % (key,val) for (key,val) in tags.items())
94-
94+
9595
def _isfloat(value):
9696
try:
9797
float(value)
9898
return True
9999
except ValueError:
100100
return False
101-
101+
102102
"""
103103
External methods
104-
"""
104+
"""
105105

106106
def tenant(self, tenant_id):
107107
self.tenant_id = tenant_id
108108

109109
"""
110110
Instance methods
111111
"""
112-
112+
113113
def put(self, data):
114114
"""
115115
Send multiple different metric_ids to the server in a single batch. Metrics can be a mixture
@@ -219,7 +219,7 @@ def query_metric_definition(self, metric_type, metric_id):
219219
:param metric_id: Exact string matching metric id
220220
"""
221221
return self._get(self._get_metrics_single_url(metric_type, metric_id))
222-
222+
223223
def query_metric_definitions(self, metric_type=None, id_filter=None, **tags):
224224
"""
225225
Query available metric definitions.
@@ -280,7 +280,7 @@ def create_metric_definition(self, metric_type, metric_id, **tags):
280280
raise e
281281

282282
return True
283-
283+
284284
def query_metric_tags(self, metric_type, metric_id):
285285
"""
286286
Returns a list of tags in the metric definition.
@@ -303,7 +303,7 @@ def update_metric_tags(self, metric_type, metric_id, **tags):
303303

304304
def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
305305
"""
306-
Delete one or more tags from the metric definition.
306+
Delete one or more tags from the metric definition.
307307
308308
:param metric_type: MetricType to be matched (required)
309309
:param metric_id: Exact string matching metric id
@@ -312,12 +312,12 @@ def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
312312
tags = self._transform_tags(**deleted_tags)
313313
tags_url = self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)) + '/{0}'.format(tags)
314314

315-
self._delete(tags_url)
316-
315+
self._delete(tags_url)
316+
317317
"""
318318
Tenant related queries
319319
"""
320-
320+
321321
def query_tenants(self):
322322
"""
323323
Query available tenants and their information.
@@ -330,7 +330,7 @@ def create_tenant(self, tenant_id, retentions=None):
330330
version of Hawkular-Metrics has fixed implementation.
331331
332332
:param retentions: A set of retention settings, see Hawkular-Metrics documentation for more info
333-
"""
333+
"""
334334
item = { 'id': tenant_id }
335335
if retentions is not None:
336336
item['retentions'] = retentions
@@ -391,15 +391,15 @@ def create_metric(metric_type, metric_id, data):
391391
"""
392392
if not isinstance(data, list):
393393
data = [data]
394-
394+
395395
return { 'type': metric_type,'id': metric_id, 'data': data }
396396

397397
def create_percentiles_filter(*percentiles):
398398
"""
399399
Create percentiles filter from a list of float64 percentile values
400400
"""
401401
return ','.join("%s" % p for p in percentiles)
402-
402+
403403
def create_tags_filter(**tags):
404404
"""
405405
Transform a set of parameters to a tag query language filter

0 commit comments

Comments
 (0)