Skip to content

Commit 25aa12e

Browse files
committed
Add support for getting all group conditions
fixes 24
1 parent c29d801 commit 25aa12e

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

hawkular/alerts.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ class GroupConditionsInfo(ApiOject):
7171
'conditions', 'data_id_member_map'
7272
]
7373

74-
defaults = {
75-
'conditions': []
76-
}
74+
def __init__(self, dictionary=dict()):
75+
ApiOject.__init__(self, dictionary)
76+
udict = self.transform_dict_to_underscore(dictionary)
77+
self.conditions = Condition.list_to_object_list(udict.get('conditions'))
7778

7879
def addCondition(self, c):
7980
self.conditions.append(c)
@@ -159,6 +160,15 @@ def create_group_member(self, member):
159160
data = self._serialize_object(member)
160161
return Trigger(self._post(self._service_url(['triggers', 'groups', 'members']), data))
161162

163+
def get_trigger_conditions(self, trigger_id):
164+
"""
165+
Get all conditions for a specific trigger
166+
:param trigger_id: Trigger definition id to be retrieved
167+
:return: list of condition objects
168+
"""
169+
response = self._get(self._service_url(['triggers', trigger_id, 'conditions']))
170+
return Condition.list_to_object_list(response)
171+
162172
def create_group_conditions(self, group_id, trigger_mode, conditions):
163173
data = self._serialize_object(conditions)
164174
url = self._service_url(['triggers', 'groups', group_id, 'conditions', trigger_mode])

tests/test_alerts.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,46 @@ def test_create_group_trigger(self):
125125
self.assertEqual(created_group_trigger.id, trigger.id)
126126
self.assertEqual(created_group_trigger.name, trigger.name)
127127

128+
def test_get_trigger_conditions(self):
129+
# Create group trigger object
130+
trigger = Trigger()
131+
trigger.id = 'group_trigger_01'
132+
trigger.name = 'group_trigger'
133+
self.client.create_group_trigger(trigger)
134+
135+
# Create condition objects
136+
condition1 = Condition()
137+
condition1.trigger_mode = TriggerMode.AUTORESOLVE
138+
condition1.type = ConditionType.THRESHOLD
139+
condition1.data_id = 'did1'
140+
condition1.threshold = 5
141+
condition1.operator = Operator.LT
142+
143+
condition2 = Condition()
144+
condition2.trigger_mode = TriggerMode.AUTORESOLVE
145+
condition2.type = ConditionType.THRESHOLD
146+
condition2.data_id = 'did2'
147+
condition2.threshold = 5
148+
condition2.operator = Operator.GT
149+
150+
condition3 = Condition()
151+
condition3.trigger_mode = TriggerMode.AUTORESOLVE
152+
condition3.type = ConditionType.THRESHOLD
153+
condition3.data_id = 'did3'
154+
condition3.threshold = 5
155+
condition3.operator = Operator.GTE
156+
157+
gc = GroupConditionsInfo()
158+
gc.addCondition(condition1)
159+
gc.addCondition(condition2)
160+
gc.addCondition(condition3)
161+
self.client.create_group_conditions(trigger.id, TriggerMode.AUTORESOLVE, gc)
162+
163+
gc = self.client.get_trigger_conditions(trigger.id)
164+
self.assertEqual(len(gc), 3)
165+
gc_dids = [c.data_id for c in gc]
166+
self.assertEqual(gc_dids, ['did1', 'did2', 'did3'])
167+
128168
def test_create_groups(self):
129169
# Create a group trigger
130170
t = Trigger()

0 commit comments

Comments
 (0)