-
Notifications
You must be signed in to change notification settings - Fork 81
NOTIF-512 Notify org admins when a webhook is disabled #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
database/src/main/resources/db/migration/v1.60.0__NOTIF-512_webhook_disabled_event_type.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| INSERT INTO event_type (id, application_id, name, display_name) | ||
| SELECT gen_random_uuid(), a.id , 'integration-disabled', 'Integration disabled' | ||
| FROM applications a, bundles b | ||
| WHERE a.bundle_id = b.id AND a.name = 'notifications' AND b.name = 'console' | ||
| ON CONFLICT DO NOTHING; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
engine/src/main/java/com/redhat/cloud/notifications/events/IntegrationDisabledNotifier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.redhat.cloud.notifications.events; | ||
|
|
||
| import com.redhat.cloud.notifications.ingress.Action; | ||
| import com.redhat.cloud.notifications.ingress.Event; | ||
| import com.redhat.cloud.notifications.ingress.Parser; | ||
| import com.redhat.cloud.notifications.ingress.Payload; | ||
| import com.redhat.cloud.notifications.ingress.Recipient; | ||
| import com.redhat.cloud.notifications.models.Endpoint; | ||
| import org.eclipse.microprofile.reactive.messaging.Channel; | ||
| import org.eclipse.microprofile.reactive.messaging.Emitter; | ||
| import org.eclipse.microprofile.reactive.messaging.Message; | ||
|
|
||
| import javax.enterprise.context.ApplicationScoped; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import static com.redhat.cloud.notifications.events.FromCamelHistoryFiller.EGRESS_CHANNEL; | ||
| import static java.time.ZoneOffset.UTC; | ||
|
|
||
| @ApplicationScoped | ||
| public class IntegrationDisabledNotifier { | ||
|
|
||
| public static final String CLIENT_ERROR_TYPE = "client"; | ||
| public static final String SERVER_ERROR_TYPE = "server"; | ||
| public static final String CONSOLE_BUNDLE = "console"; | ||
| public static final String NOTIFICATIONS_APP = "notifications"; | ||
| public static final String INTEGRATION_DISABLED_EVENT_TYPE = "integration-disabled"; | ||
| public static final String ERROR_TYPE_PROPERTY = "error_type"; | ||
| public static final String ENDPOINT_ID_PROPERTY = "endpoint_id"; | ||
| public static final String ENDPOINT_NAME_PROPERTY = "endpoint_name"; | ||
| public static final String STATUS_CODE_PROPERTY = "status_code"; | ||
| public static final String ERRORS_COUNT_PROPERTY = "errors_count"; | ||
|
|
||
| @Channel(EGRESS_CHANNEL) | ||
| Emitter<String> emitter; | ||
|
|
||
| public void clientError(Endpoint endpoint, int statusCode) { | ||
| notify(endpoint, CLIENT_ERROR_TYPE, statusCode, 1); | ||
| } | ||
|
|
||
| public void tooManyServerErrors(Endpoint endpoint, int errorsCount) { | ||
| notify(endpoint, SERVER_ERROR_TYPE, -1, errorsCount); | ||
| } | ||
|
|
||
| private void notify(Endpoint endpoint, String errorType, int statusCode, int errorsCount) { | ||
| Payload.PayloadBuilderBase payloadBuilder = new Payload.PayloadBuilder() | ||
| .withAdditionalProperty(ERROR_TYPE_PROPERTY, errorType) | ||
| .withAdditionalProperty(ENDPOINT_ID_PROPERTY, endpoint.getId()) | ||
| .withAdditionalProperty(ENDPOINT_NAME_PROPERTY, endpoint.getName()) | ||
| .withAdditionalProperty(ERRORS_COUNT_PROPERTY, errorsCount); | ||
|
|
||
| if (statusCode > 0) { | ||
| payloadBuilder.withAdditionalProperty(STATUS_CODE_PROPERTY, statusCode); | ||
| } | ||
|
|
||
| Event event = new Event.EventBuilder() | ||
| .withPayload(payloadBuilder.build()) | ||
| .build(); | ||
|
|
||
| Recipient recipients = new Recipient.RecipientBuilder() | ||
| .withOnlyAdmins(true) | ||
| .withIgnoreUserPreferences(true) | ||
| .build(); | ||
|
|
||
| Action action = new Action.ActionBuilder() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a side note, the suggested builder syntax has been merged, but not deployed yet! |
||
| .withId(UUID.randomUUID()) | ||
| .withBundle(CONSOLE_BUNDLE) | ||
| .withApplication(NOTIFICATIONS_APP) | ||
| .withEventType(INTEGRATION_DISABLED_EVENT_TYPE) | ||
| .withOrgId(endpoint.getOrgId()) | ||
| .withTimestamp(LocalDateTime.now(UTC)) | ||
| .withEvents(List.of(event)) | ||
| .withRecipients(List.of(recipients)) | ||
| .build(); | ||
| String encodedAction = Parser.encode(action); | ||
|
|
||
| Message<String> message = KafkaMessageWithIdBuilder.build(encodedAction); | ||
| emitter.send(message); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
engine/src/main/java/com/redhat/cloud/notifications/events/KafkaMessageWithIdBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.redhat.cloud.notifications.events; | ||
|
|
||
| import io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata; | ||
| import org.apache.kafka.common.header.internals.RecordHeaders; | ||
| import org.eclipse.microprofile.reactive.messaging.Message; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static com.redhat.cloud.notifications.events.KafkaMessageDeduplicator.MESSAGE_ID_HEADER; | ||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| public class KafkaMessageWithIdBuilder { | ||
|
|
||
| public static Message build(String payload) { | ||
| byte[] messageId = UUID.randomUUID().toString().getBytes(UTF_8); | ||
| OutgoingKafkaRecordMetadata metadata = OutgoingKafkaRecordMetadata.builder() | ||
| .withHeaders(new RecordHeaders().add(MESSAGE_ID_HEADER, messageId)) | ||
| .build(); | ||
| return Message.of(payload).addMetadata(metadata); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be removed in the future, once we have something to have defaults (e.g. all admins subscribed for these notifications and a default behavior group on the account that can be removed) so they can fine-tune this setting (maybe a particular RBAC group would be the one handling these errors and no the admin itself.