Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Fix: changes in how we send notifications from move2kube workflow exa…
Browse files Browse the repository at this point in the history
…mples

Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto committed Jun 13, 2023
1 parent 2455d67 commit 96294cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.redhat.parodos.workflow.annotation.Checker;
import com.redhat.parodos.workflow.annotation.Infrastructure;
import com.redhat.parodos.workflow.consts.WorkFlowConstants;
import com.redhat.parodos.workflow.task.infrastructure.Notifier;
import com.redhat.parodos.workflows.workflow.ParallelFlow;
import com.redhat.parodos.workflows.workflow.SequentialFlow;
import com.redhat.parodos.workflows.workflow.WorkFlow;
Expand Down Expand Up @@ -79,8 +80,9 @@ WorkFlow transformWorkFlowChecker(@Qualifier("transformChecker") TransformChecke
}

@Bean
Move2KubeTransform move2KubeTransform(@Qualifier("transformWorkFlowChecker") WorkFlow transformWorkFlowChecker) {
Move2KubeTransform move2KubeTransform = new Move2KubeTransform(m2kURL);
Move2KubeTransform move2KubeTransform(@Qualifier("transformWorkFlowChecker") WorkFlow transformWorkFlowChecker,
Notifier notifier) {
Move2KubeTransform move2KubeTransform = new Move2KubeTransform(m2kURL, notifier);
move2KubeTransform.setWorkFlowCheckers(List.of(transformWorkFlowChecker));
return move2KubeTransform;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.redhat.parodos.examples.move2kube.task;

import java.io.IOException;
import java.util.List;
import java.net.URI;
import java.net.URISyntaxException;

import com.redhat.parodos.examples.ocponboarding.task.dto.notification.NotificationRequest;
import com.redhat.parodos.utils.RestUtils;
import com.redhat.parodos.workflow.task.infrastructure.Notifier;
import com.redhat.parodos.workflow.utils.WorkContextUtils;
import com.redhat.parodos.workflows.work.DefaultWorkReport;
import com.redhat.parodos.workflows.work.WorkContext;
Expand All @@ -18,34 +18,40 @@
import dev.parodos.move2kube.client.model.StartTransformationRequest;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;

@Slf4j
public class Move2KubeTransform extends Move2KubeBase {

private PlanApi planApi;

private ProjectOutputsApi projectOutputsApi;

private Notifier notifierBus;

private String plan;

public Move2KubeTransform(String server) {
private String server;

public Move2KubeTransform(String serverUrl, Notifier notifier) {
super();
this.setClient(server);
planApi = new PlanApi(client);
projectOutputsApi = new ProjectOutputsApi(client);
notifierBus = notifier;
server = serverUrl;
}

public Move2KubeTransform(String server, PlanApi plan, ProjectOutputsApi projectOutputs) {
new Move2KubeTransform(server);
public Move2KubeTransform(String serverUrl, Notifier notifier, PlanApi plan, ProjectOutputsApi projectOutputs) {
new Move2KubeTransform(serverUrl, notifier);
planApi = plan;
projectOutputsApi = projectOutputs;
notifierBus = notifier;
server = serverUrl;
}

public WorkReport execute(WorkContext workContext) {
String workspaceId = (String) workContext.get(getWorkspaceContextKey());
String projectId = (String) workContext.get(getProjectContextKey());
String transformID = null;
try {
isPlanCreated(workspaceId, projectId);
}
Expand All @@ -67,30 +73,29 @@ public WorkReport execute(WorkContext workContext) {
}

String userId = String.valueOf(WorkContextUtils.getUserId(workContext));
if (!sendNotification(userId, workspaceId, projectId)) {
if (!sendNotification(userId, workspaceId, projectId, transformID)) {
return new DefaultWorkReport(WorkStatus.FAILED, workContext,
new RuntimeException("Cannot notify user about the transformation status"));
}
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}

private boolean sendNotification(String userID, String workspaceID, String projectID) {

String url = String.format("http://localhost:8081/workspaces/%s/projects/%s", workspaceID, projectID);
String message = String.format(
"You need to complete some information for your transformation in the following url <a href=\"%s\"> %s</a>",
url, url);

// @TODO userID is the ID, but we need the username, so hardcode it here for now.
NotificationRequest request = NotificationRequest.builder().usernames(List.of("test"))
.subject("Complete the Move2Kube transformation steps").body(message).build();
private boolean sendNotification(String userID, String workspaceID, String projectID, String outputID) {
String path = String.format("/workspaces/%s/projects/%s/output/%s", workspaceID, projectID, outputID);
try {
URI baseUri = new URI(server);
URI url = new URI(baseUri.getScheme(), baseUri.getAuthority(), path, null, null);

HttpEntity<NotificationRequest> notificationRequestHttpEntity = RestUtils.getRequestWithHeaders(request, "test",
"test");
ResponseEntity<String> response = RestUtils.executePost("http://localhost:8082/api/v1/messages",
notificationRequestHttpEntity);
String message = String.format(
"You need to complete some information for your transformation in the following [url](%s)", url);

return response.getStatusCode().is2xxSuccessful();
notifierBus.send("Move2kube workflow approval needed", message);
}
catch (URISyntaxException e) {
log.error("Cannot parse move2kube url {}", server);
return false;
}
return true;
}

private String transform(String workspaceID, String projectID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.UUID;

import com.redhat.parodos.utils.RestUtils;
import com.redhat.parodos.workflow.task.infrastructure.Notifier;
import com.redhat.parodos.workflow.utils.WorkContextUtils;
import com.redhat.parodos.workflows.work.WorkContext;
import com.redhat.parodos.workflows.work.WorkReport;
Expand Down Expand Up @@ -38,13 +39,16 @@ public class Move2KubeTransformTest {

private PlanApi planApi;

private Notifier notifierBus;

private ProjectOutputsApi projectOutputsApi;

@Before
public void setup() throws Exception {
planApi = mock(PlanApi.class);
projectOutputsApi = mock(ProjectOutputsApi.class);
task = new Move2KubeTransform("http://localhost:8080", planApi, projectOutputsApi);
notifierBus = mock(Notifier.class);
task = new Move2KubeTransform("http://localhost:8080", notifierBus, planApi, projectOutputsApi);
}

@Test
Expand Down

0 comments on commit 96294cb

Please sign in to comment.