Skip to content

Commit 455d645

Browse files
author
Tadej Borovšak
committed
Remove event module result
Sensu Go backend processes events in asynchronous fashion, which makes it almost impossible to retrieve the processed event shortly after it has been sent to the backend. Currently, after we submit an event, we query the backend for the last event. What we get back is one of the following three options: 1. nothing, if our event is the first event ever and the backend did not manage to process it yet; 2. previous event, if there is at least one preexisting event on the backend and our event has not been processed yet; 3. our actual event, if the backend processed our event fast enough. In the real world setting, the third scenario almost never happens, which means that our event module is returning bogus results most of the time. To rectify the situation, we removed the result from the event module. NOTE: This change breaks the API. Major version bump is needed when releasing the version of collection that contains changes from this commit.
1 parent 4e72371 commit 455d645

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

plugins/modules/event.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333
seealso:
3434
- module: event_info
3535
notes:
36-
- Metric events bypass the store and are sent off to the event pipeline and corresponding event
37-
handlers. Read more about this at
38-
U(https://docs.sensu.io/sensu-go/latest/reference/events/#metric-only-events)
36+
- Metric events bypass the store and are sent off to the event pipeline and
37+
corresponding event handlers. Read more about this at
38+
U(https://docs.sensu.io/sensu-go/latest/reference/events/#metric-only-events).
39+
- This module is not enforcing a state of any resource in particular. This
40+
is why it ALWAYS sends an event (it is not idempotent).
41+
- Module also does not return an event representation back because Sensu Go
42+
backend processes events asynchronously.
3943
options:
4044
timestamp:
4145
description:
@@ -152,12 +156,7 @@
152156
value: 0.004
153157
'''
154158

155-
RETURN = '''
156-
object:
157-
description: object representing Sensu event
158-
returned: success
159-
type: dict
160-
'''
159+
RETURN = ''' # '''
161160

162161
from ansible.module_utils.basic import AnsibleModule
163162

@@ -220,13 +219,6 @@ def _build_api_payload(client, params):
220219
return payload
221220

222221

223-
def send_event(client, path, payload, check_mode):
224-
if check_mode:
225-
return True, payload
226-
utils.put(client, path, payload)
227-
return True, utils.get(client, path)
228-
229-
230222
def main():
231223
module = AnsibleModule(
232224
supports_check_mode=True,
@@ -281,6 +273,9 @@ def main():
281273
)
282274
)
283275

276+
if module.check_mode:
277+
module.exit_json(changed=True)
278+
284279
client = arguments.get_sensu_client(module.params['auth'])
285280
path = utils.build_core_v2_path(
286281
module.params['namespace'], 'events', module.params['entity'],
@@ -289,8 +284,8 @@ def main():
289284

290285
try:
291286
payload = _build_api_payload(client, module.params)
292-
changed, event = send_event(client, path, payload, module.check_mode)
293-
module.exit_json(changed=changed, object=event)
287+
utils.put(client, path, payload)
288+
module.exit_json(changed=True)
294289
except errors.Error as e:
295290
module.fail_json(msg=str(e))
296291

tests/integration/molecule/module_event/converge.yml

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@
8989
- assert:
9090
that:
9191
- result is changed
92-
- result.object.check.metadata.name == 'awesome_check'
93-
- result.object.check.command == '/bin/true'
94-
- result.object.entity.metadata.name == 'awesome_entity'
9592

9693
- name: Get last event
9794
event_info:
@@ -100,6 +97,11 @@
10097
entity: awesome_entity
10198
check: awesome_check
10299
register: result
100+
# Give backend a few seconds to process the event (out CI is ofter under
101+
# a considerate stress when testing).
102+
retries: 3
103+
delay: 3
104+
until: result.objects | length > 0
103105

104106
- assert:
105107
that:
@@ -181,30 +183,35 @@
181183
- assert:
182184
that:
183185
- result is changed
184-
- result.object.entity.metadata.name == 'entity2'
185-
- result.object.check.metadata.name == 'check2'
186-
- result.object.check.command == '/bin/false'
187-
- result.object.check.duration == 1.945
188-
- result.object.check.executed == 1522100915
189-
- "result.object.check.history.0 == {'executed': 1552505193, 'status': 1}"
190-
- result.object.check.issued == 1552506034
191-
- result.object.check.last_ok == 1552506033
192-
- result.object.check.output == '10'
193-
- result.object.check.state == 'passing'
194-
- result.object.check.status == 0
195-
- result.object.check.total_state_change == 0
196186

197187
- name: Get events matching entity2
198188
event_info:
199189
auth:
200190
url: http://localhost:8080
201191
entity: entity2
202192
register: result
193+
# Give backend a few seconds to process the event (out CI is ofter under
194+
# a considerate stress when testing).
195+
retries: 3
196+
delay: 3
197+
until: result.objects | length > 0
203198

204199
- assert:
205200
that:
206201
- result.objects | length == 1
207202
- result.objects.0.check.metadata.name == 'check2'
203+
- result.objects.0.entity.metadata.name == 'entity2'
204+
- result.objects.0.check.metadata.name == 'check2'
205+
- result.objects.0.check.command == '/bin/false'
206+
- result.objects.0.check.duration == 1.945
207+
- result.objects.0.check.executed == 1522100915
208+
- "result.objects.0.check.history.0 == {'executed': 1552505193, 'status': 1}"
209+
- result.objects.0.check.issued == 1552506034
210+
- result.objects.0.check.last_ok == 1552506033
211+
- result.objects.0.check.output == '10'
212+
- result.objects.0.check.state == 'passing'
213+
- result.objects.0.check.status == 0
214+
- result.objects.0.check.total_state_change == 0
208215

209216
- name: Get all events
210217
event_info:

tests/unit/modules/test_event.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from ansible_collections.sensu.sensu_go.plugins.module_utils import (
7-
errors, http,
7+
errors, http, utils,
88
)
99
from ansible_collections.sensu.sensu_go.plugins.modules import event
1010

@@ -72,8 +72,7 @@ def test_missing_check_on_remote(self, mocker):
7272
event.main()
7373

7474
def test_minimal_event_parameters(self, mocker):
75-
send_event_mock = mocker.patch.object(event, 'send_event')
76-
send_event_mock.return_value = True, {}
75+
put_mock = mocker.patch.object(utils, 'put')
7776
get_entity_mock = mocker.patch.object(event, 'get_entity')
7877
get_entity_mock.return_value = dict(
7978
metadata=dict(
@@ -98,7 +97,7 @@ def test_minimal_event_parameters(self, mocker):
9897
with pytest.raises(AnsibleExitJson):
9998
event.main()
10099

101-
_client, path, payload, check_mode = send_event_mock.call_args[0]
100+
_client, path, payload = put_mock.call_args[0]
102101
assert path == '/api/core/v2/namespaces/default/events/awesome_entity/awesome_check'
103102
assert payload == dict(
104103
metadata=dict(
@@ -118,7 +117,6 @@ def test_minimal_event_parameters(self, mocker):
118117
)
119118
)
120119
)
121-
assert check_mode is False
122120

123121
def test_all_event_parameters(self, mocker):
124122
entity_object = dict(
@@ -139,8 +137,7 @@ def test_all_event_parameters(self, mocker):
139137
publish=True,
140138
subscriptions=["linux"],
141139
)
142-
send_event_mock = mocker.patch.object(event, 'send_event')
143-
send_event_mock.return_value = True, {}
140+
put_mock = mocker.patch.object(utils, 'put')
144141
get_entity_mock = mocker.patch.object(event, 'get_entity')
145142
get_entity_mock.return_value = entity_object
146143
get_check_mock = mocker.patch.object(event, 'get_check')
@@ -198,7 +195,7 @@ def test_all_event_parameters(self, mocker):
198195
with pytest.raises(AnsibleExitJson):
199196
event.main()
200197

201-
_client, path, payload, check_mode = send_event_mock.call_args[0]
198+
_client, path, payload = put_mock.call_args[0]
202199
assert path == '/api/core/v2/namespaces/my/events/awesome_entity/awesome_check'
203200
assert payload == dict(
204201
metadata=dict(
@@ -264,11 +261,10 @@ def test_all_event_parameters(self, mocker):
264261
}]
265262
)
266263
)
267-
assert check_mode is False
268264

269265
def test_failure(self, mocker):
270-
send_event_mock = mocker.patch.object(event, 'send_event')
271-
send_event_mock.side_effect = errors.Error('Bad error')
266+
put_mock = mocker.patch.object(utils, 'put')
267+
put_mock.side_effect = errors.Error('Bad error')
272268
set_module_args(
273269
entity='awesome_entity',
274270
check=dict(

0 commit comments

Comments
 (0)