Skip to content

Commit a3e0878

Browse files
authored
Post EventDefinitionUpdated and EventDefinitionDeleted events (#23143)
* Post EventDefinitionUpdated and EventDefinitionDeleted events * Adjust tests
1 parent b9498f1 commit a3e0878

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2020 Graylog, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*/
17+
package org.graylog.events.processor;
18+
19+
public record EventDefinitionDeleted(String eventDefinitionId) {
20+
}

graylog2-server/src/main/java/org/graylog/events/processor/EventDefinitionHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.graylog.security.shares.EntitySharesService;
3333
import org.graylog2.database.entities.DefaultEntityScope;
3434
import org.graylog2.database.entities.NonDeletableSystemScope;
35+
import org.graylog2.events.ClusterEventBus;
3536
import org.graylog2.plugin.database.users.User;
3637
import org.joda.time.DateTime;
3738
import org.slf4j.Logger;
@@ -59,6 +60,7 @@ public class EventDefinitionHandler {
5960
private final DBEventDefinitionService eventDefinitionService;
6061
private final DBJobDefinitionService jobDefinitionService;
6162
private final DBJobTriggerService jobTriggerService;
63+
private final ClusterEventBus clusterEventBus;
6264

6365
// Provider to avoid circular dependency
6466
private final Provider<EntitySharesService> entitySharesServiceProvider;
@@ -70,12 +72,14 @@ public EventDefinitionHandler(DBEventDefinitionService eventDefinitionService,
7072
DBJobDefinitionService jobDefinitionService,
7173
DBJobTriggerService jobTriggerService,
7274
Provider<EntitySharesService> entitySharesServiceProvider,
73-
JobSchedulerClock clock) {
75+
JobSchedulerClock clock,
76+
ClusterEventBus clusterEventBus) {
7477
this.eventDefinitionService = eventDefinitionService;
7578
this.jobDefinitionService = jobDefinitionService;
7679
this.jobTriggerService = jobTriggerService;
7780
this.entitySharesServiceProvider = entitySharesServiceProvider;
7881
this.clock = clock;
82+
this.clusterEventBus = clusterEventBus;
7983
}
8084

8185
/**
@@ -167,6 +171,7 @@ public EventDefinitionDto update(EventDefinitionDto updatedEventDefinition, bool
167171
throw e;
168172
}
169173

174+
clusterEventBus.post(new EventDefinitionUpdated(updatedEventDefinition.id()));
170175
return eventDefinition;
171176
}
172177

@@ -227,6 +232,7 @@ private boolean doDelete(String eventDefinitionId, Supplier<Boolean> deleteSuppl
227232
getJobDefinition(eventDefinition)
228233
.ifPresent(jobDefinition -> deleteJobDefinitionAndTrigger(jobDefinition, eventDefinition));
229234

235+
clusterEventBus.post(new EventDefinitionDeleted(eventDefinitionId));
230236
LOG.debug("Deleting event definition <{}/{}>", eventDefinition.id(), eventDefinition.title());
231237
return deleteSupplier.get();
232238
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2020 Graylog, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*/
17+
package org.graylog.events.processor;
18+
19+
public record EventDefinitionUpdated(String eventDefinitionId) {
20+
}

graylog2-server/src/test/java/org/graylog/events/contentpack/facade/EventDefinitionFacadeTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.graylog2.database.entities.DefaultEntityScope;
7171
import org.graylog2.database.entities.EntityScope;
7272
import org.graylog2.database.entities.EntityScopeService;
73+
import org.graylog2.events.ClusterEventBus;
7374
import org.graylog2.plugin.PluginMetaData;
7475
import org.graylog2.plugin.cluster.ClusterConfigService;
7576
import org.graylog2.security.PasswordAlgorithmFactory;
@@ -137,6 +138,8 @@ public class EventDefinitionFacadeTest {
137138
private EntityRegistrar entityRegistrar;
138139
@Mock
139140
private EventProcessorConfig mockEventProcessorConfig;
141+
@Mock
142+
private ClusterEventBus clusterEventBus;
140143

141144
@Before
142145
@SuppressForbidden("Using Executors.newSingleThreadExecutor() is okay in tests")
@@ -155,7 +158,7 @@ public void setUp() throws Exception {
155158
jobSchedulerClock = mock(JobSchedulerClock.class);
156159
final MongoCollections mongoCollections = new MongoCollections(mapperProvider, mongodb.mongoConnection());
157160
eventDefinitionService = new DBEventDefinitionService(mongoCollections, stateService, entityRegistrar, new EntityScopeService(ENTITY_SCOPES), new IgnoreSearchFilters());
158-
eventDefinitionHandler = new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, mock(Provider.class), jobSchedulerClock);
161+
eventDefinitionHandler = new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, mock(Provider.class), jobSchedulerClock, clusterEventBus);
159162
Set<PluginMetaData> pluginMetaData = new HashSet<>();
160163
facade = new EventDefinitionFacade(objectMapper, eventDefinitionHandler, pluginMetaData, jobDefinitionService, eventDefinitionService, userService, entityRegistrar);
161164
}

graylog2-server/src/test/java/org/graylog/events/legacy/LegacyAlertConditionMigratorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.graylog2.database.entities.DefaultEntityScope;
5959
import org.graylog2.database.entities.EntityScope;
6060
import org.graylog2.database.entities.EntityScopeService;
61+
import org.graylog2.events.ClusterEventBus;
6162
import org.graylog2.plugin.system.SimpleNodeId;
6263
import org.graylog2.shared.bindings.providers.ObjectMapperProvider;
6364
import org.graylog2.shared.users.UserService;
@@ -137,7 +138,7 @@ public void setUp() throws Exception {
137138
final DBJobTriggerService jobTriggerService = new DBJobTriggerService(mongoCollections, new SimpleNodeId("5ca1ab1e-0000-4000-a000-000000000000"), clock, schedulerCapabilitiesService, Duration.minutes(5));
138139
notificationService = new DBNotificationService(mongoCollections, mock(EntityRegistrar.class));
139140
this.eventDefinitionService = new DBEventDefinitionService(mongoCollections, mock(DBEventProcessorStateService.class), mock(EntityRegistrar.class), new EntityScopeService(ENTITY_SCOPES), new IgnoreSearchFilters());
140-
this.eventDefinitionHandler = spy(new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, mock(Provider.class), clock));
141+
this.eventDefinitionHandler = spy(new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, mock(Provider.class), clock, mock(ClusterEventBus.class)));
141142
this.notificationResourceHandler = spy(new NotificationResourceHandler(notificationService, jobDefinitionService, eventDefinitionService, eventNotificationFactories));
142143
this.userService = mock(UserService.class);
143144
when(userService.getRootUser()).thenReturn(Optional.empty());

graylog2-server/src/test/java/org/graylog/events/processor/EventDefinitionHandlerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.graylog2.database.entities.DefaultEntityScope;
4545
import org.graylog2.database.entities.EntityScope;
4646
import org.graylog2.database.entities.EntityScopeService;
47+
import org.graylog2.events.ClusterEventBus;
4748
import org.graylog2.plugin.database.users.User;
4849
import org.graylog2.plugin.indexer.searches.timeranges.TimeRange;
4950
import org.graylog2.plugin.system.NodeId;
@@ -71,6 +72,8 @@
7172
import static org.mockito.Mockito.mock;
7273
import static org.mockito.Mockito.reset;
7374
import static org.mockito.Mockito.spy;
75+
import static org.mockito.Mockito.times;
76+
import static org.mockito.Mockito.verify;
7477
import static org.mockito.Mockito.when;
7578

7679
public class EventDefinitionHandlerTest {
@@ -89,6 +92,8 @@ public class EventDefinitionHandlerTest {
8992

9093
@Mock
9194
private SchedulerCapabilitiesService schedulerCapabilitiesService;
95+
@Mock
96+
private ClusterEventBus clusterEventBus;
9297

9398
@Mock
9499
Provider<EntitySharesService> entitySharesServiceProvider;
@@ -119,7 +124,7 @@ public void setUp() throws Exception {
119124
this.jobDefinitionService = spy(new DBJobDefinitionService(new MongoCollections(mapperProvider, mongodb.mongoConnection()), mapperProvider));
120125
this.jobTriggerService = spy(new DBJobTriggerService(mongoCollections, nodeId, clock, schedulerCapabilitiesService, Duration.minutes(5)));
121126

122-
this.handler = new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, entitySharesServiceProvider, clock);
127+
this.handler = new EventDefinitionHandler(eventDefinitionService, jobDefinitionService, jobTriggerService, entitySharesServiceProvider, clock, clusterEventBus);
123128
}
124129

125130
@Test
@@ -285,6 +290,7 @@ public void update() {
285290
assertThat(trigger.data()).isEmpty();
286291
assertThat(trigger.nextTime()).isEqualTo(clock.nowUTC());
287292
});
293+
verify(clusterEventBus, times(1)).post(new EventDefinitionUpdated(existingDto.id()));
288294
}
289295

290296
@Test
@@ -430,6 +436,7 @@ public void updateWithErrors() {
430436
assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
431437
assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
432438
});
439+
verify(clusterEventBus, times(0)).post(new EventDefinitionUpdated(existingDto.id()));
433440
}
434441

435442
@Test
@@ -440,6 +447,7 @@ public void delete() {
440447
assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isPresent();
441448

442449
assertThat(handler.delete("54e3deadbeefdeadbeef0000")).isTrue();
450+
verify(clusterEventBus, times(1)).post(new EventDefinitionDeleted("54e3deadbeefdeadbeef0000"));
443451

444452
assertThat(eventDefinitionService.get("54e3deadbeefdeadbeef0000")).isNotPresent();
445453
assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isNotPresent();

0 commit comments

Comments
 (0)