Skip to content

Commit

Permalink
Remove environment_url, db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicarod7 committed Jan 10, 2025
1 parent 8874555 commit 7ffe725
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class PagerDutyTransformer implements Processor {
public static final String CONTEXT = "context";
public static final String CUSTOM_DETAILS = "custom_details";
public static final String DISPLAY_NAME = "display_name";
public static final String ENVIRONMENT_URL = "environment_url";
public static final String EVENT_ACTION = "event_action";
public static final String EVENT_TYPE = "event_type";
public static final String EVENTS = "events";
Expand All @@ -57,7 +56,6 @@ public void process(Exchange exchange) {

JsonObject message = new JsonObject();
message.put(EVENT_ACTION, PagerDutyEventAction.TRIGGER);
message.mergeIn(getClientLink(cloudEventPayload, cloudEventPayload.getString(ENVIRONMENT_URL)));
message.mergeIn(getClientLinks(cloudEventPayload));

JsonObject messagePayload = new JsonObject();
Expand Down Expand Up @@ -124,45 +122,6 @@ private void validatePayload(final JsonObject cloudEventPayload) {
}
}

/**
* Adapted from CamelProcessor template for Teams, with some changes to more gracefully handle missing fields
* <br>
* TODO update to work more consistently and with other platforms
*
* @return {@link #CLIENT} and {@link #CLIENT_URL}
*/
private JsonObject getClientLink(final JsonObject cloudEventPayload, String environmentUrl) {
JsonObject clientLink = new JsonObject();

String contextName = cloudEventPayload.containsKey(CONTEXT)
? cloudEventPayload.getJsonObject(CONTEXT).getString(DISPLAY_NAME)
: null;

if (contextName != null) {
clientLink.put(CLIENT, contextName);

String inventoryId = cloudEventPayload.getJsonObject(CONTEXT).getString("inventory_id");
if (environmentUrl != null && !environmentUrl.isEmpty() && inventoryId != null && !inventoryId.isEmpty()) {
clientLink.put(CLIENT_URL, String.format("%s/insights/inventory/%s",
environmentUrl,
cloudEventPayload.getJsonObject(CONTEXT).getString("inventory_id")
));
}
} else {
if (environmentUrl != null && !environmentUrl.isEmpty()) {
clientLink.put(CLIENT, String.format("Open %s", cloudEventPayload.getString(APPLICATION)));
clientLink.put(CLIENT_URL, String.format("%s/insights/%s",
environmentUrl,
cloudEventPayload.getString(APPLICATION)
));
} else {
clientLink.put(CLIENT, cloudEventPayload.getString(APPLICATION));
}
}

return clientLink;
}

/**
* Performs the following link conversions:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.APPLICATION;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.APPLICATION_URL;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.BUNDLE;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.CLIENT;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.CLIENT_URL;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.CONTEXT;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.CUSTOM_DETAILS;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.DISPLAY_NAME;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.ENVIRONMENT_URL;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.EVENTS;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.EVENT_ACTION;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.EVENT_TYPE;
Expand Down Expand Up @@ -86,7 +83,6 @@ static JsonObject createIncomingPayload(JsonObject cloudEventData) {
);

payload.put(SOURCE, source);
payload.put(ENVIRONMENT_URL, "https://console.redhat.com");
payload.put(INVENTORY_URL, "https://console.redhat.com/insights/inventory/8a4a4f75-5319-4255-9eb5-1ee5a92efd7f");
payload.put(APPLICATION_URL, "https://console.redhat.com/insights/default-application");
payload.put(SEVERITY, PagerDutySeverity.WARNING);
Expand All @@ -102,7 +98,6 @@ static JsonObject buildExpectedOutgoingPayload(final JsonObject incoming) {

JsonObject oldInnerPayload = expected.getJsonObject(PAYLOAD);
expected.put(EVENT_ACTION, PagerDutyEventAction.TRIGGER);
expected.mergeIn(getClientLink(oldInnerPayload, oldInnerPayload.getString(ENVIRONMENT_URL)));
expected.mergeIn(getClientLinks(oldInnerPayload));

JsonObject newInnerPayload = new JsonObject();
Expand Down Expand Up @@ -140,36 +135,4 @@ static JsonObject buildExpectedOutgoingPayload(final JsonObject incoming) {
expected.put(PAYLOAD, newInnerPayload);
return expected;
}

private static JsonObject getClientLink(final JsonObject oldInnerPayload, String environmentUrl) {
JsonObject clientLink = new JsonObject();

String contextName = oldInnerPayload.containsKey(CONTEXT)
? oldInnerPayload.getJsonObject(CONTEXT).getString(DISPLAY_NAME)
: null;

if (contextName != null) {
clientLink.put(CLIENT, contextName);

String inventoryId = oldInnerPayload.getJsonObject(CONTEXT).getString("inventory_id");
if (environmentUrl != null && !environmentUrl.isEmpty() && inventoryId != null && !inventoryId.isEmpty()) {
clientLink.put(CLIENT_URL, String.format("%s/insights/inventory/%s",
environmentUrl,
oldInnerPayload.getJsonObject(CONTEXT).getString("inventory_id")
));
}
} else {
if (environmentUrl != null && !environmentUrl.isEmpty()) {
clientLink.put(CLIENT, String.format("Open %s", oldInnerPayload.getString(APPLICATION)));
clientLink.put(CLIENT_URL, String.format("%s/insights/%s",
environmentUrl,
oldInnerPayload.getString(APPLICATION)
));
} else {
clientLink.put(CLIENT, oldInnerPayload.getString(APPLICATION));
}
}

return clientLink;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.APPLICATION;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.CONTEXT;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.DISPLAY_NAME;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.ENVIRONMENT_URL;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.EVENTS;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.EVENT_TYPE;
import static com.redhat.cloud.notifications.connector.pagerduty.PagerDutyTransformer.INVENTORY_URL;
Expand Down Expand Up @@ -112,7 +111,6 @@ void testSuccessfulTestMessage() {
)
);
cloudEventPayload.put("recipients", JsonArray.of());
cloudEventPayload.put("environment_url", "https://console.redhat.com");
// No inventory_url generated
cloudEventPayload.put("application_url", "https://console.redhat.com/settings/integrations");
cloudEventPayload.put("severity", "warning");
Expand Down Expand Up @@ -175,20 +173,6 @@ void testMissingEvents() {
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testWithClientDisplayName() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
JsonObject cloudPayload = cloudEventData.getJsonObject(PAYLOAD);
cloudPayload.put(CONTEXT, JsonObject.of(
DISPLAY_NAME, "console",
"inventory_id", "8a4a4f75-5319-4255-9eb5-1ee5a92efd7f"
));
cloudEventData.put(PAYLOAD, cloudPayload);

JsonObject expectedPayload = buildExpectedOutgoingPayload(cloudEventData);
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testWithHostUrl() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
Expand All @@ -211,17 +195,6 @@ void testWithMissingClientDisplayName() {
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testWithClientDisplayNameAndInventoryId() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
JsonObject cloudPayload = cloudEventData.getJsonObject(PAYLOAD);
cloudPayload.put(CONTEXT, JsonObject.of(
DISPLAY_NAME, "console",
"inventory_id", "8a4a4f75-5319-4255-9eb5-1ee5a92efd7f"
));
cloudEventData.put(PAYLOAD, cloudPayload);
}

@Test
void testWithMissingInventoryId() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
Expand All @@ -233,16 +206,6 @@ void testWithMissingInventoryId() {
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testMissingEnvironmentUrl() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
JsonObject cloudPayload = cloudEventData.getJsonObject(PAYLOAD);
cloudPayload.remove(ENVIRONMENT_URL);

JsonObject expectedPayload = buildExpectedOutgoingPayload(cloudEventData);
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testMissingInventoryUrl() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
UPDATE template
SET data = '{#if data.context.display_name??}<{data.inventory_url}|{data.context.display_name}> triggered {data.events.size()} event{#if data.events.size() > 1}s{/if}{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} triggered{/if} from {data.source.application.display_name} - {data.source.bundle.display_name}. <{data.application_url}|Open {data.source.application.display_name}>'
WHERE name = 'generic-slack';

UPDATE template
SET data = '{"text":"{#if data.context.display_name??}[{data.context.display_name}]({data.inventory_url}) triggered {data.events.size()} event{#if data.events.size() > 1}s{/if}{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} triggered{/if} from {data.source.application.display_name} - {data.source.bundle.display_name}. [Open {data.source.application.display_name}]({data.application_url})"}'
WHERE name = 'generic-teams';

UPDATE template
SET data = '{"text":"{#if data.context.display_name??}<{data.inventory_url}|{data.context.display_name}> triggered {data.events.size()} event{#if data.events.size() > 1}s{/if}{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} triggered{/if} from {data.source.application.display_name} - {data.source.bundle.display_name}. <{data.application_url}|Open {data.source.application.display_name}>"}'
WHERE name = 'generic-google-chat';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.redhat.cloud.notifications.db.repositories.TemplateRepository;
import com.redhat.cloud.notifications.models.CamelProperties;
import com.redhat.cloud.notifications.models.Endpoint;
import com.redhat.cloud.notifications.models.Environment;
import com.redhat.cloud.notifications.models.Event;
import com.redhat.cloud.notifications.models.IntegrationTemplate;
import com.redhat.cloud.notifications.processors.ConnectorSender;
Expand All @@ -34,9 +33,6 @@ public abstract class CamelProcessor extends EndpointTypeProcessor {
@Inject
BaseTransformer baseTransformer;

@Inject
Environment environment;

@Inject
InsightsUrlsBuilder insightsUrlsBuilder;

Expand Down Expand Up @@ -79,7 +75,6 @@ private void process(Event event, Endpoint endpoint) {

protected String buildNotificationMessage(Event event) {
JsonObject data = baseTransformer.toJsonObject(event);
data.put("environment_url", environment.url());
insightsUrlsBuilder.buildInventoryUrl(data).ifPresent(url -> data.put("inventory_url", url));
data.put("application_url", insightsUrlsBuilder.buildApplicationUrl(data));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.redhat.cloud.notifications.DelayedThrower;
import com.redhat.cloud.notifications.config.EngineConfig;
import com.redhat.cloud.notifications.models.Endpoint;
import com.redhat.cloud.notifications.models.Environment;
import com.redhat.cloud.notifications.models.Event;
import com.redhat.cloud.notifications.models.PagerDutyProperties;
import com.redhat.cloud.notifications.processors.ConnectorSender;
Expand Down Expand Up @@ -35,9 +34,6 @@ public class PagerDutyProcessor extends EndpointTypeProcessor {
@Inject
EngineConfig engineConfig;

@Inject
Environment environment;

@Inject
InsightsUrlsBuilder insightsUrlsBuilder;

Expand Down Expand Up @@ -77,7 +73,6 @@ private void process(Event event, Endpoint endpoint) {

JsonObject connectorData = new JsonObject();
JsonObject transformedEvent = transformer.toJsonObject(event);
transformedEvent.put("environment_url", environment.url());
insightsUrlsBuilder.buildInventoryUrl(transformedEvent).ifPresent(url -> transformedEvent.put("inventory_url", url));
transformedEvent.put("application_url", insightsUrlsBuilder.buildApplicationUrl(transformedEvent));
transformedEvent.put("severity", properties.getSeverity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ protected static Event buildEvent(boolean withHostUrl) {
event.setId(UUID.randomUUID());
event.setOrgId(DEFAULT_ORG_ID);
event.setEventWrapper(new EventWrapperAction(action));
event.setApplicationDisplayName("policies");
event.setBundleDisplayName("Red Hat Enterprise Linux");
event.setApplicationDisplayName("Policies");
Application application = new Application();
application.setName("policies");
EventType eventType = new EventType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
public class GoogleChatProcessorTest extends CamelProcessorTest {

private static final String GOOGLE_CHAT_TEMPLATE = "{\"text\":\"{#if data.context.display_name??}" +
"<{data.environment_url}/insights/inventory/{data.context.inventory_id}|{data.context.display_name}> " +
"<{data.inventory_url}|{data.context.display_name}> " +
"triggered {data.events.size()} event{#if data.events.size() > 1}s{/if}" +
"{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} triggered{/if} " +
"from {data.bundle}/{data.application}. " +
"<{data.environment_url}/insights/{data.application}|Open {data.application}>\"}";
"{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} " +
"triggered{/if} from {data.source.application.display_name} - {data.source.bundle.display_name}. " +
"<{data.application_url}|Open {data.source.application.display_name}>\"}";

private static final String GOOGLE_CHAT_EXPECTED_MSG = "{\"text\":\"<" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/inventory/6ad30f3e-0497-4e74-99f1-b3f9a6120a6f|my-computer> " +
"triggered 1 event from rhel/policies. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open policies>\"}";
"triggered 1 event from Policies - Red Hat Enterprise Linux. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open Policies>\"}";

private static final String GOOGLE_CHAT_EXPECTED_MSG_WITH_HOST_URL = "{\"text\":\"<" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/inventory/6ad30f3e-0497-4e74-99f1-b3f9a6120a6f|my-computer> " +
"triggered 1 event from rhel/policies. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open policies>\"}";
private static final String GOOGLE_CHAT_EXPECTED_MSG_WITH_HOST_URL = "{\"text\":\"<" + CONTEXT_HOST_URL + "|my-computer> " +
"triggered 1 event from Policies - Red Hat Enterprise Linux. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open Policies>\"}";

@Inject
GoogleChatProcessor googleSpacesProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class SlackProcessorTest extends CamelProcessorTest {
private static final String WEBHOOK_URL = "https://foo.bar";
private static final String CHANNEL = "#notifications";
private static final String SLACK_TEMPLATE = "{#if data.context.display_name??}" +
"<{data.environment_url}/insights/inventory/{data.context.inventory_id}|{data.context.display_name}> " +
"<{data.inventory_url}|{data.context.display_name}> " +
"triggered {data.events.size()} event{#if data.events.size() > 1}s{/if}" +
"{#else}{data.events.size()} event{#if data.events.size() > 1}s{/if} triggered{/if} " +
"from {data.bundle}/{data.application}. " +
"<{data.environment_url}/insights/{data.application}|Open {data.application}>";
"from {data.source.application.display_name} - {data.source.bundle.display_name}. " +
"<{data.application_url}|Open {data.source.application.display_name}>";
private static final String SLACK_EXPECTED_MSG = "<" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/inventory/6ad30f3e-0497-4e74-99f1-b3f9a6120a6f|my-computer> " +
"triggered 1 event from rhel/policies. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open policies>";
private static final String SLACK_EXPECTED_MSG_WITH_HOST_URL = "<" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/inventory/6ad30f3e-0497-4e74-99f1-b3f9a6120a6f|my-computer> " +
"triggered 1 event from rhel/policies. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open policies>";
"triggered 1 event from Policies - Red Hat Enterprise Linux. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open Policies>";
private static final String SLACK_EXPECTED_MSG_WITH_HOST_URL = "<" + CONTEXT_HOST_URL + "|my-computer> " +
"triggered 1 event from Policies - Red Hat Enterprise Linux. <" + EnvironmentTest.expectedTestEnvUrlValue + "/insights/policies|Open Policies>";

@Inject
SlackProcessor slackProcessor;
Expand Down
Loading

0 comments on commit 7ffe725

Please sign in to comment.