Skip to content

Commit 880cd54

Browse files
Notification Channel Name Should Be Unique (#292)
* Notification Channel Name Should Be Unique
1 parent 37859de commit 880cd54

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

notification-channel-config-service-impl/src/main/java/org/hypertrace/notification/config/service/NotificationChannelConfigServiceImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public void createNotificationChannel(
5151
StreamObserver<CreateNotificationChannelResponse> responseObserver) {
5252
try {
5353
RequestContext requestContext = RequestContext.CURRENT.get();
54+
List<NotificationChannel> existingNotificationChannels =
55+
notificationChannelStore.getAllConfigData(requestContext);
5456
validator.validateCreateNotificationChannelRequest(
55-
requestContext, request, notificationChannelConfig);
57+
requestContext, request, notificationChannelConfig, existingNotificationChannels);
5658
NotificationChannel.Builder builder =
5759
NotificationChannel.newBuilder()
5860
.setId(UUID.randomUUID().toString())
@@ -75,8 +77,10 @@ public void updateNotificationChannel(
7577
StreamObserver<UpdateNotificationChannelResponse> responseObserver) {
7678
try {
7779
RequestContext requestContext = RequestContext.CURRENT.get();
80+
List<NotificationChannel> existingNotificationChannels =
81+
notificationChannelStore.getAllConfigData(requestContext);
7882
validator.validateUpdateNotificationChannelRequest(
79-
requestContext, request, notificationChannelConfig);
83+
requestContext, request, notificationChannelConfig, existingNotificationChannels);
8084
responseObserver.onNext(
8185
UpdateNotificationChannelResponse.newBuilder()
8286
.setNotificationChannel(

notification-channel-config-service-impl/src/main/java/org/hypertrace/notification/config/service/NotificationChannelConfigServiceRequestValidator.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.MalformedURLException;
99
import java.net.URL;
1010
import java.util.List;
11+
import javax.annotation.Nullable;
1112
import org.hypertrace.core.grpcutils.context.RequestContext;
1213
import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig;
1314
import org.hypertrace.notification.config.service.v1.AwsS3BucketChannelConfig.WebIdentityAuthenticationCredential;
@@ -16,6 +17,7 @@
1617
import org.hypertrace.notification.config.service.v1.EmailChannelConfig;
1718
import org.hypertrace.notification.config.service.v1.GetAllNotificationChannelsRequest;
1819
import org.hypertrace.notification.config.service.v1.GetNotificationChannelRequest;
20+
import org.hypertrace.notification.config.service.v1.NotificationChannel;
1921
import org.hypertrace.notification.config.service.v1.NotificationChannelMutableData;
2022
import org.hypertrace.notification.config.service.v1.SplunkIntegrationChannelConfig;
2123
import org.hypertrace.notification.config.service.v1.SyslogIntegrationChannelConfig;
@@ -31,11 +33,16 @@ public class NotificationChannelConfigServiceRequestValidator {
3133
public void validateCreateNotificationChannelRequest(
3234
RequestContext requestContext,
3335
CreateNotificationChannelRequest request,
34-
Config notificationChannelConfig) {
36+
Config notificationChannelConfig,
37+
List<NotificationChannel> existingNotificationChannels) {
3538
validateRequestContextOrThrow(requestContext);
3639
validateNotificationChannelMutableData(request.getNotificationChannelMutableData());
3740
validateWebhookConfigExclusionDomains(
3841
request.getNotificationChannelMutableData(), notificationChannelConfig);
42+
validateNonDuplicateNotificationChannelOrThrow(
43+
null,
44+
request.getNotificationChannelMutableData().getChannelName(),
45+
existingNotificationChannels);
3946
validateWebhookHttpSupport(
4047
request.getNotificationChannelMutableData(), notificationChannelConfig);
4148
}
@@ -99,12 +106,17 @@ void validateWebhookConfigExclusionDomains(
99106
public void validateUpdateNotificationChannelRequest(
100107
RequestContext requestContext,
101108
UpdateNotificationChannelRequest request,
102-
Config notificationChannelConfig) {
109+
Config notificationChannelConfig,
110+
List<NotificationChannel> existingNotificationChannels) {
103111
validateRequestContextOrThrow(requestContext);
104112
validateNonDefaultPresenceOrThrow(request, UpdateNotificationChannelRequest.ID_FIELD_NUMBER);
105113
validateNotificationChannelMutableData(request.getNotificationChannelMutableData());
106114
validateWebhookConfigExclusionDomains(
107115
request.getNotificationChannelMutableData(), notificationChannelConfig);
116+
validateNonDuplicateNotificationChannelOrThrow(
117+
request.getId(),
118+
request.getNotificationChannelMutableData().getChannelName(),
119+
existingNotificationChannels);
108120
validateWebhookHttpSupport(
109121
request.getNotificationChannelMutableData(), notificationChannelConfig);
110122
}
@@ -193,4 +205,21 @@ private void validateSyslogIntegrationChannelConfig(
193205
syslogIntegrationChannelConfig,
194206
SyslogIntegrationChannelConfig.SYSLOG_SERVER_INTEGRATION_ID_FIELD_NUMBER);
195207
}
208+
209+
private void validateNonDuplicateNotificationChannelOrThrow(
210+
@Nullable String id,
211+
String channelName,
212+
List<NotificationChannel> existingNotificationChannels) {
213+
for (NotificationChannel existingNotificationChannel : existingNotificationChannels) {
214+
if (!existingNotificationChannel.getId().equals(id)
215+
&& existingNotificationChannel
216+
.getNotificationChannelMutableData()
217+
.getChannelName()
218+
.equals(channelName)) {
219+
throw Status.ALREADY_EXISTS
220+
.withDescription("Notification Channel with the same name already exists.")
221+
.asRuntimeException();
222+
}
223+
}
224+
}
196225
}

notification-channel-config-service-impl/src/test/java/org/hypertrace/notification/config/service/NotificationChannelConfigServiceRequestValidatorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.typesafe.config.ConfigFactory;
88
import com.typesafe.config.ConfigValueFactory;
99
import java.io.File;
10+
import java.util.List;
1011
import org.hypertrace.core.grpcutils.context.RequestContext;
1112
import org.hypertrace.notification.config.service.v1.NotificationChannelMutableData;
1213
import org.hypertrace.notification.config.service.v1.UpdateNotificationChannelRequest;
@@ -83,7 +84,8 @@ public void testValidateWebhookHttpsSupport() {
8384
notificationChannelConfigServiceRequestValidator.validateUpdateNotificationChannelRequest(
8485
RequestContext.forTenantId("tenant1"),
8586
getUpdateNotificationChannelRequestWithHttpUrl(),
86-
notificationChannelConfig);
87+
notificationChannelConfig,
88+
List.of());
8789
},
8890
"RuntimeException was expected");
8991

@@ -103,7 +105,8 @@ public void testValidateWebhookHttpsSupport() {
103105
.setId("id1")
104106
.setNotificationChannelMutableData(notificationChannelMutableDataWithHttpUrl)
105107
.build(),
106-
updatedNotificationChannelConfig);
108+
updatedNotificationChannelConfig,
109+
List.of());
107110
}
108111

109112
private static UpdateNotificationChannelRequest getUpdateNotificationChannelRequestWithHttpUrl() {

0 commit comments

Comments
 (0)