Skip to content

Commit

Permalink
Merge branch 'main' into multi_owners
Browse files Browse the repository at this point in the history
  • Loading branch information
karanh37 authored Jul 29, 2024
2 parents 61cc229 + 72fd62a commit 115e25d
Show file tree
Hide file tree
Showing 22 changed files with 624 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public interface Destination<T> {
void sendMessage(T event) throws EventPublisherException;

void sendTestMessage() throws EventPublisherException;

SubscriptionDestination getSubscriptionDestination();

EventSubscription getEventSubscriptionForDestination();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException {
}
}

@Override
public void sendTestMessage() throws EventPublisherException {
try {
Set<String> receivers = emailAlertConfig.getReceivers();
EmailMessage emailMessage =
emailDecorator.buildOutgoingTestMessage(eventSubscription.getFullyQualifiedName());
for (String email : receivers) {
EmailUtil.sendChangeEventMail(
eventSubscription.getFullyQualifiedName(), email, emailMessage);
}
setSuccessStatus(System.currentTimeMillis());
} catch (Exception e) {
setErrorStatus(System.currentTimeMillis(), 500, e.getMessage());
String message = CatalogExceptionMessage.eventPublisherFailedToPublish(EMAIL, e.getMessage());
LOG.error(message);
throw new EventPublisherException(message);
}
}

@Override
public EventSubscription getEventSubscriptionForDestination() {
return eventSubscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void sendMessage(ChangeEvent changeEvent) throws EventPublisherException
}
}

@Override
public void sendTestMessage() throws EventPublisherException {}

@Override
public EventSubscription getEventSubscriptionForDestination() {
return eventSubscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException {
}
}

@Override
public void sendTestMessage() throws EventPublisherException {
try {
GChatMessage gchatMessage =
gChatMessageMessageDecorator.buildOutgoingTestMessage(
eventSubscription.getFullyQualifiedName());

if (target != null) {
postWebhookMessage(this, target, gchatMessage);
}
} catch (Exception e) {
String message =
CatalogExceptionMessage.eventPublisherFailedToPublish(G_CHAT, e.getMessage());
LOG.error(message);
throw new EventPublisherException(message);
}
}

@Override
public EventSubscription getEventSubscriptionForDestination() {
return eventSubscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,39 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException {
}
}

@Override
public void sendTestMessage() throws EventPublisherException {
long attemptTime = System.currentTimeMillis();
try {
// Post Message to default
String json =
"This is a test message from OpenMetadata to confirm your webhook destination is configured correctly.";
if (webhook.getEndpoint() != null) {
if (webhook.getSecretKey() != null && !webhook.getSecretKey().isEmpty()) {
String hmac = "sha256=" + CommonUtil.calculateHMAC(webhook.getSecretKey(), json);
postWebhookMessage(this, getTarget().header(RestUtil.SIGNATURE_HEADER, hmac), json);
} else {
postWebhookMessage(this, getTarget(), json);
}
}
} catch (Exception ex) {
Throwable cause = ex.getCause();
String message = "";
if (cause != null && cause.getClass() == UnknownHostException.class) {
message =
String.format(
"Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s",
subscriptionDestination.getId(), webhook.getEndpoint());
LOG.warn(message);
setErrorStatus(attemptTime, 400, "UnknownHostException");
} else {
message = CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, ex.getMessage());
LOG.error(message);
}
throw new EventPublisherException(message);
}
}

private Invocation.Builder getTarget() {
Map<String, String> authHeaders = SecurityUtil.authHeaders("admin@open-metadata.org");
return SecurityUtil.addHeaders(client.target(webhook.getEndpoint()), authHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException {
}
}

@Override
public void sendTestMessage() throws EventPublisherException {
try {
TeamsMessage teamsMessage =
teamsMessageFormatter.buildOutgoingTestMessage(eventSubscription.getFullyQualifiedName());

if (target != null) {
if (webhook.getSecretKey() != null && !webhook.getSecretKey().isEmpty()) {
String hmac =
"sha256="
+ CommonUtil.calculateHMAC(
webhook.getSecretKey(), JsonUtils.pojoToJson(teamsMessage));
postWebhookMessage(this, target.header(RestUtil.SIGNATURE_HEADER, hmac), teamsMessage);
} else {
postWebhookMessage(this, target, teamsMessage);
}
}
} catch (Exception e) {
String message =
CatalogExceptionMessage.eventPublisherFailedToPublish(MS_TEAMS, e.getMessage());
LOG.error(message);
throw new EventPublisherException(message);
}
}

@Override
public EventSubscription getEventSubscriptionForDestination() {
return eventSubscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException {
}
}

@Override
public void sendTestMessage() throws EventPublisherException {
try {
SlackMessage slackMessage =
slackMessageFormatter.buildOutgoingTestMessage(eventSubscription.getFullyQualifiedName());

if (target != null) {
if (webhook.getSecretKey() != null && !webhook.getSecretKey().isEmpty()) {
String hmac =
"sha256="
+ CommonUtil.calculateHMAC(
webhook.getSecretKey(), JsonUtils.pojoToJson(slackMessage));
postWebhookMessage(this, target.header(RestUtil.SIGNATURE_HEADER, hmac), slackMessage);
} else {
postWebhookMessage(this, target, slackMessage);
}
}
} catch (Exception e) {
String message = CatalogExceptionMessage.eventPublisherFailedToPublish(SLACK, e.getMessage());
LOG.error(message);
throw new EventPublisherException(message);
}
}

@Override
public EventSubscription getEventSubscriptionForDestination() {
return eventSubscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ public static String eventPublisherFailedToPublish(
JsonUtils.pojoToJson(event), type.value(), message);
}

public static String eventPublisherFailedToPublish(
SubscriptionDestination.SubscriptionType type, String message) {
return String.format("Failed to publish event %s due to %s ", type.value(), message);
}

public static String invalidTaskField(EntityLink entityLink, TaskType taskType) {
return String.format(
"The Entity link with no field name - %s is not supported for %s task.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.openmetadata.service.util.EmailUtil.getSmtpSettings;

import java.util.ArrayList;
import java.util.Collections;
import org.openmetadata.schema.type.ChangeEvent;
import org.openmetadata.service.apps.bundles.changeEvent.email.EmailMessage;
import org.openmetadata.service.exception.UnhandledServerException;
Expand Down Expand Up @@ -68,6 +69,11 @@ public EmailMessage buildEntityMessage(String publisherName, ChangeEvent event)
return getEmailMessage(createEntityMessage(publisherName, event));
}

@Override
public EmailMessage buildTestMessage(String publisherName) {
return getEmailTestMessage(publisherName);
}

@Override
public EmailMessage buildThreadMessage(String publisherName, ChangeEvent event) {
return getEmailMessage(createThreadMessage(publisherName, event));
Expand All @@ -84,4 +90,22 @@ public EmailMessage getEmailMessage(OutgoingMessage outgoingMessage) {
}
throw new UnhandledServerException("No messages found for the event");
}

public EmailMessage getEmailTestMessage(String publisherName) {
if (!publisherName.isEmpty()) {
EmailMessage emailMessage = new EmailMessage();
emailMessage.setUserName("test_user");
emailMessage.setUpdatedBy("system");
emailMessage.setChangeMessage(
new ArrayList<>(
Collections.singleton(
"This is a test alert to verify the destination configuration for alerts. "
+ "Publisher: "
+ publisherName
+ ". If you received this message, your alert "
+ "configuration is correct.")));
return emailMessage;
}
throw new UnhandledServerException("Publisher name not found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public FeedMessage buildEntityMessage(String publisherName, ChangeEvent event) {
return null;
}

@Override
public FeedMessage buildTestMessage(String publisherName) {
return null;
}

@Override
public FeedMessage buildThreadMessage(String publisherName, ChangeEvent event) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public GChatMessage buildEntityMessage(String publisherName, ChangeEvent event)
return getGChatMessage(createEntityMessage(publisherName, event));
}

@Override
public GChatMessage buildTestMessage(String publisherName) {
return getGChatTestMessage(publisherName);
}

@Override
public GChatMessage buildThreadMessage(String publisherName, ChangeEvent event) {
return getGChatMessage(createThreadMessage(publisherName, event));
Expand Down Expand Up @@ -101,6 +106,30 @@ private GChatMessage getGChatMessage(OutgoingMessage outgoingMessage) {
throw new UnhandledServerException("No messages found for the event");
}

private GChatMessage getGChatTestMessage(String publisherName) {
if (!publisherName.isEmpty()) {
GChatMessage gChatMessage = new GChatMessage();
GChatMessage.CardsV2 cardsV2 = new GChatMessage.CardsV2();
GChatMessage.Card card = new GChatMessage.Card();
GChatMessage.Section section = new GChatMessage.Section();

// Header
gChatMessage.setText(
"This is a test message from OpenMetadata to confirm your GChat destination is configured correctly.");
GChatMessage.CardHeader cardHeader = new GChatMessage.CardHeader();
cardHeader.setTitle("Alert: " + publisherName);
cardHeader.setSubtitle("GChat destination test successful.");

card.setHeader(cardHeader);
card.setSections(List.of(section));
cardsV2.setCard(card);
gChatMessage.setCardsV2(List.of(cardsV2));

return gChatMessage;
}
throw new UnhandledServerException("Publisher name not found.");
}

private GChatMessage.Widget getGChatWidget(String message) {
GChatMessage.Widget widget = new GChatMessage.Widget();
widget.setTextParagraph(new GChatMessage.TextParagraph(message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public TeamsMessage buildEntityMessage(String publisherName, ChangeEvent event)
return getTeamMessage(createEntityMessage(publisherName, event));
}

@Override
public TeamsMessage buildTestMessage(String publisherName) {
return getTeamTestMessage(publisherName);
}

@Override
public TeamsMessage buildThreadMessage(String publisherName, ChangeEvent event) {
return getTeamMessage(createThreadMessage(publisherName, event));
Expand All @@ -93,6 +98,25 @@ private TeamsMessage getTeamMessage(OutgoingMessage outgoingMessage) {
throw new UnhandledServerException("No messages found for the event");
}

private TeamsMessage getTeamTestMessage(String publisherName) {
if (!publisherName.isEmpty()) {
TeamsMessage teamsMessage = new TeamsMessage();
teamsMessage.setSummary(
"This is a test message from OpenMetadata to confirm your Microsoft Teams destination is configured correctly.");

// Sections
TeamsMessage.Section teamsSection = new TeamsMessage.Section();
teamsSection.setActivityTitle("Alert: " + publisherName);

List<TeamsMessage.Section> sectionList = new ArrayList<>();
sectionList.add(teamsSection);

teamsMessage.setSections(sectionList);
return teamsMessage;
}
throw new UnhandledServerException("Publisher name not found.");
}

private TeamsMessage.Section getTeamsSection(String activityTitle, String message) {
TeamsMessage.Section section = new TeamsMessage.Section();
section.setActivityTitle(activityTitle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ default String httpRemoveMarker() {

T buildEntityMessage(String publisherName, ChangeEvent event);

T buildTestMessage(String publisherName);

T buildThreadMessage(String publisherName, ChangeEvent event);

default String buildEntityUrl(String entityType, EntityInterface entityInterface) {
Expand Down Expand Up @@ -151,6 +153,10 @@ default T buildOutgoingMessage(String publisherName, ChangeEvent event) {
}
}

default T buildOutgoingTestMessage(String publisherName) {
return buildTestMessage(publisherName);
}

default String getPlaintextDiff(String oldValue, String newValue) {
// create a configured DiffRowGenerator
oldValue = oldValue == null ? StringUtils.EMPTY : oldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public SlackMessage buildEntityMessage(String publisherName, ChangeEvent event)
return getSlackMessage(createEntityMessage(publisherName, event));
}

@Override
public SlackMessage buildTestMessage(String publisherName) {
return getSlackTestMessage(publisherName);
}

@Override
public SlackMessage buildThreadMessage(String publisherName, ChangeEvent event) {
return getSlackMessage(createThreadMessage(publisherName, event));
Expand All @@ -88,6 +93,30 @@ private SlackMessage getSlackMessage(OutgoingMessage outgoingMessage) {
throw new UnhandledServerException("No messages found for the event");
}

private SlackMessage getSlackTestMessage(String publisherName) {
if (!publisherName.isEmpty()) {
SlackMessage message = new SlackMessage();
message.setUsername("Slack destination test");
message.setText("Slack has been successfully configured for alerts from: " + publisherName);

SlackAttachment attachment = new SlackAttachment();
attachment.setFallback("Slack destination test successful.");
attachment.setColor("#36a64f"); // Setting a green color to indicate success
attachment.setTitle("Test Successful");
attachment.setText(
"This is a test message from OpenMetadata confirming that your Slack destination is correctly set up to receive alerts.");
attachment.setFooter("OpenMetadata");
attachment.setTs(String.valueOf(System.currentTimeMillis() / 1000)); // Adding timestamp

List<SlackAttachment> attachmentList = new ArrayList<>();
attachmentList.add(attachment);
message.setAttachments(attachmentList.toArray(new SlackAttachment[0]));

return message;
}
throw new UnhandledServerException("Publisher name not found.");
}

private SlackAttachment getSlackAttachment(String message) {
SlackAttachment attachment = new SlackAttachment();
List<String> mark = new ArrayList<>();
Expand Down
Loading

0 comments on commit 115e25d

Please sign in to comment.