Skip to content

Commit 5551a35

Browse files
authored
Refactor triggers to its own class (#51)
* Refactor triggers to .triggers() Implement status() method assertItemsEqual to avoid order errors * Change triggers() to triggers.
1 parent 114da01 commit 5551a35

7 files changed

Lines changed: 183 additions & 66 deletions

File tree

hawkular/__init__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
from .metrics import HawkularMetricsClient, MetricType, Availability
2-
from .alerts import HawkularAlertsClient, Trigger, FullTrigger, Condition, Dampening, FullTrigger, GroupMemberInfo
3-
from .alerts import GroupConditionsInfo, TriggerType, TriggerMode, DampeningType, ConditionType, Operator, Severity
1+
"""
2+
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
3+
and other contributors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
from hawkular.metrics import HawkularMetricsClient, MetricType, Availability
18+
from hawkular.alerts import HawkularAlertsClient, Trigger, FullTrigger, Condition, Dampening, FullTrigger, GroupMemberInfo
19+
from hawkular.alerts import GroupConditionsInfo, TriggerType, TriggerMode, DampeningType, ConditionType, Operator, Severity, Status
420

521
__all__ = ['HawkularMetricsClient',
622
'MetricType',
@@ -17,4 +33,5 @@
1733
'DampeningType',
1834
'ConditionType',
1935
'Operator',
20-
'Severity']
36+
'Severity',
37+
'Status']

hawkular/alerts/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
3+
and other contributors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
from hawkular.client import ApiObject, HawkularBaseClient
18+
from hawkular.alerts.common import *
19+
from hawkular.alerts.triggers import *
20+
21+
__all__ = ['HawkularAlertsClient',
22+
'Trigger',
23+
'Condition',
24+
'Dampening',
25+
'FullTrigger',
26+
'GroupMemberInfo',
27+
'GroupConditionsInfo',
28+
'TriggerType',
29+
'TriggerMode',
30+
'DampeningType',
31+
'ConditionType',
32+
'Operator',
33+
'Severity',
34+
'Status',
35+
]
36+
# 'AlertsTriggerClient']

hawkular/alerts/common.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from hawkular.client import ApiObject, HawkularBaseClient
2+
from hawkular.alerts.triggers import AlertsTriggerClient
3+
4+
class Status(ApiObject):
5+
__slots__ = [
6+
'status', 'implementation_version', 'built_from_git_sha1', 'distributed', 'members'
7+
]
8+
9+
def isup(self):
10+
return self.status == 'STARTED'
11+
12+
def isdistributed(self):
13+
return self.distributed == 'true'
14+
15+
class HawkularAlertsClient(HawkularBaseClient):
16+
17+
def __init__(self, **opts):
18+
"""
19+
Available parameters:
20+
21+
tenant_id,
22+
host='localhost',
23+
port=8080,
24+
path=None,
25+
scheme='http',
26+
cafile=None,
27+
context=None,
28+
token=None,
29+
username=None,
30+
password=None,
31+
auto_set_legacy_api=True,
32+
authtoken=None
33+
"""
34+
prop_defaults = {
35+
"tenant_id": 'hawkular',
36+
"host": 'localhost',
37+
"port": 8080,
38+
"scheme": 'http',
39+
"path": None,
40+
"cafile": None,
41+
"context": None,
42+
"token": None,
43+
"username": None,
44+
"password": None,
45+
"authtoken": None,
46+
}
47+
48+
for (prop, default) in prop_defaults.items():
49+
setattr(self, prop, opts.get(prop, default))
50+
51+
super(HawkularAlertsClient, self)._setup_path()
52+
53+
self.triggers = AlertsTriggerClient(self)
54+
55+
# def triggers(self):
56+
# """
57+
# Returns triggers methods
58+
# """
59+
# return AlertsTriggerClient(self)
60+
61+
def status(self):
62+
orig_dict = self._get(self._service_url('status'))
63+
orig_dict['implementation_version'] = orig_dict.pop('Implementation-Version')
64+
orig_dict['built_from_git_sha1'] = orig_dict.pop('Built-From-Git-SHA1')
65+
return Status(orig_dict)
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from hawkular.client import ApiObject, HawkularBaseClient
17+
from hawkular.client import ApiObject
1818

1919
class Trigger(ApiObject):
2020
__slots__ = [
@@ -34,7 +34,6 @@ class Condition(ApiObject):
3434
'in_range', 'alerter_id', 'expression', 'direction', 'period', 'interval'
3535
]
3636

37-
3837
class Dampening(ApiObject):
3938
__slots__ = [
4039
'trigger_id', 'trigger_mode', 'type', 'eval_true_setting',
@@ -56,14 +55,12 @@ def __init__(self, dictionary=dict()):
5655
self.dampenings = Dampening.list_to_object_list(udict.get('dampenings'))
5756
self.conditions = Condition.list_to_object_list(udict.get('conditions'))
5857

59-
6058
class GroupMemberInfo(ApiObject):
6159
__slots__ = [
6260
'group_id', 'member_id', 'member_name', 'member_description', 'member_context',
6361
'member_tags', 'data_id_map'
6462
]
6563

66-
6764
class GroupConditionsInfo(ApiObject):
6865
__slots__ = [
6966
'conditions', 'data_id_member_map'
@@ -77,28 +74,24 @@ def __init__(self, dictionary=dict()):
7774
def addCondition(self, c):
7875
self.conditions.append(c)
7976

80-
8177
class TriggerType:
8278
STANDARD = 'STANDARD'
8379
GROUP = 'GROUP'
8480
DATA_DRIVEN_GROUP = 'DATA_DRIVEN_GROUP'
8581
MEMBER = 'MEMBER'
8682
ORPHAN = 'ORPHAN'
8783

88-
8984
class TriggerMode:
9085
FIRING = 'FIRING'
9186
AUTORESOLVE = 'AUTORESOLVE'
9287

93-
9488
class DampeningType:
9589
STRICT = 'STRICT'
9690
RELAXED_COUNT = 'RELAXED_COUNT'
9791
RELAXED_TIME = 'RELAXED_TIME'
9892
STRICT_TIME = 'STRICT_TIME'
9993
STRICT_TIMEOUT = 'STRICT_TIMEOUT'
10094

101-
10295
class ConditionType:
10396
AVAILABILITY = 'AVAILABILITY'
10497
COMPARE = 'COMPARE'
@@ -110,22 +103,27 @@ class ConditionType:
110103
RATE = 'RATE'
111104
MISSING = 'MISSING'
112105

113-
114106
class Operator:
115107
LT = 'LT'
116108
GT = 'GT'
117109
LTE = 'LTE'
118110
GTE = 'GTE'
119111

120-
121112
class Severity:
122113
LOW = 'LOW'
123114
MEDIUM = 'MEDIUM'
124115
HIGH = 'HIGH'
125116
CRITICAL = 'CRITICAL'
126117

118+
class AlertsTriggerClient(object):
119+
120+
def __init__(self, alerts_client):
121+
self.__client = alerts_client
122+
pass
123+
124+
def __getattr__(self, name):
125+
return getattr(self.__client, name)
127126

128-
class HawkularAlertsClient(HawkularBaseClient):
129127
def list_triggers(self, tags=[]):
130128
tags = ','.join(tags)
131129
url = self._service_url('triggers', {'tags': tags})
@@ -189,6 +187,7 @@ def create_group_member(self, member):
189187
data = self._serialize_object(member)
190188
return Trigger(self._post(self._service_url(['triggers', 'groups', 'members']), data))
191189

190+
# TODO The API defines two, PUT which updates conditions and PUT which updates trigger mode also
192191
def put_trigger_conditions(self, trigger_id, trigger_mode, conditions):
193192
data = self._serialize_object(conditions)
194193
url = self._service_url(['triggers', trigger_id, 'conditions', trigger_mode])
@@ -243,3 +242,7 @@ def delete_group_dampening(self, group_id, dampening_id):
243242
:param dampening_id: id of the Dampening to be deleted
244243
"""
245244
self._delete(self._service_url(['triggers', 'groups', group_id, 'dampenings', dampening_id]))
245+
246+
# def enable/disable_trigger (or should it be in the update?)
247+
# def orphan / deorphan trigger
248+
#

hawkular/client.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(self,
132132
auto_set_legacy_api=True,
133133
authtoken=None):
134134
"""
135-
A new instance of HawkularMetricsClient is created with the following defaults:
135+
A new instance of HawkularClient is created with the following defaults:
136136
137137
host = localhost
138138
port = 8080
@@ -157,24 +157,25 @@ def __init__(self,
157157
self.legacy_api = False
158158
self.authtoken = authtoken
159159

160+
self._setup_path()
161+
162+
# Call the server status endpoint to get the version number,
163+
# Use the return sematic version to set the value of legacy_api
164+
if auto_set_legacy_api:
165+
major, minor = self.query_semantic_version()
166+
self.legacy_api = (major == 0 and minor < 16)
167+
168+
def _setup_path(self):
160169
opener = build_opener(HawkularHTTPErrorProcessor())
161170
install_opener(opener)
162171

163-
if path is None:
172+
if self.path is None:
164173
class_name = self.__class__.__name__
165174
path_components = ''.join(["_" + c.lower() if c.isupper() else c for c in class_name]).split('_')
166175
path_components.pop()
167-
self.path = '/'.join(path_components);
168-
else:
169-
self.path = path
176+
self.path = '/'.join(path_components)
170177
self.path = self.path.strip('/')
171178

172-
# Call the server status endpoint to get the version number,
173-
# Use the return sematic version to set the value of legacy_api
174-
if auto_set_legacy_api:
175-
major, minor = self.query_semantic_version()
176-
self.legacy_api = (major == 0 and minor < 16)
177-
178179
def _get_base_url(self):
179180
return "{0}://{1}:{2}/{3}/".format(self.scheme, self.host, str(self.port), self.path)
180181

hawkular/metrics.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@
1616
"""
1717
from __future__ import unicode_literals
1818

19-
import codecs
2019
import time
2120
import collections
22-
import base64
23-
import ssl
2421
from datetime import datetime, timedelta
2522

2623
try:
2724
import simplejson as json
2825
except ImportError:
2926
import json
3027

31-
from hawkular.client import ApiObject, HawkularBaseClient, HawkularError
32-
from hawkular.client import HawkularConnectionError, HawkularStatusError
28+
from hawkular.client import HawkularBaseClient, HawkularError
3329

3430
class MetricType:
3531
Gauge = 'gauges'

0 commit comments

Comments
 (0)