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

Commit

Permalink
refactor tasks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anludke committed Jul 28, 2023
1 parent f0c4c7a commit cdfbb13
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 288 deletions.
5 changes: 5 additions & 0 deletions prebuilt-tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<artifactId>notification-service-sdk</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>dev.parodos</groupId>
<artifactId>workflow-service-sdk</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.redhat.parodos.infrastructure;

import java.util.UUID;

import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.model.AccessRequestDTO;
import com.redhat.parodos.sdk.model.AccessResponseDTO;
import com.redhat.parodos.sdk.model.AccessStatusResponseDTO;

public interface ProjectRequester {

String getBasePath();

AccessResponseDTO createAccess(UUID id, AccessRequestDTO accessRequestDTO) throws ApiException;

AccessStatusResponseDTO getAccessStatus(UUID id) throws ApiException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import java.util.Arrays;
import java.util.UUID;

import com.redhat.parodos.tasks.project.dto.NotificationRequest;
import com.redhat.parodos.utils.RestUtils;
import com.redhat.parodos.infrastructure.Notifier;
import com.redhat.parodos.infrastructure.ProjectRequester;
import com.redhat.parodos.notification.sdk.model.NotificationMessageCreateRequestDTO;
import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.task.BaseWorkFlowTask;
import com.redhat.parodos.workflows.work.DefaultWorkReport;
Expand All @@ -28,9 +29,6 @@
import com.redhat.parodos.workflows.work.WorkStatus;
import lombok.extern.slf4j.Slf4j;

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

import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_APPROVAL_USERNAMES;
import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_ID;
import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.NOTIFICATION_SUBJECT_ACCESS_REQUEST_APPROVAL;
Expand All @@ -44,35 +42,21 @@
@Slf4j
public class ProjectAccessRequestApprovalWorkFlowTask extends BaseWorkFlowTask {

private final String serviceUrl;

private final String servicePort;

private final String notificationServiceUrl;
private final ProjectRequester projectRequester;

private final String notificationServicePort;
private final Notifier notifier;

private final String notificationServiceAccountName;

private final String notificationServiceAccountPassword;

public ProjectAccessRequestApprovalWorkFlowTask(String serviceUrl, String servicePort,
String notificationServiceUrl, String notificationServicePort, String notificationServiceAccountName,
String notificationServiceAccountPassword) {
public ProjectAccessRequestApprovalWorkFlowTask(ProjectRequester projectRequester, Notifier notifier) {
super();
this.serviceUrl = serviceUrl;
this.servicePort = servicePort;
this.notificationServiceUrl = notificationServiceUrl;
this.notificationServicePort = notificationServicePort;
this.notificationServiceAccountName = notificationServiceAccountName;
this.notificationServiceAccountPassword = notificationServiceAccountPassword;
this.projectRequester = projectRequester;
this.notifier = notifier;
}

@Override
public WorkReport execute(WorkContext workContext) {
log.info("Start projectAccessRequestApprovalWorkFlowTask...");
String approvalUsernames;
UUID accessRequestId;
String approvalUsernames;
try {
accessRequestId = UUID.fromString(getRequiredParameterValue(ACCESS_REQUEST_ID));
approvalUsernames = getRequiredParameterValue(ACCESS_REQUEST_APPROVAL_USERNAMES);
Expand All @@ -82,28 +66,13 @@ public WorkReport execute(WorkContext workContext) {
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e);
}

String projectAccessRequestStatusUrl = String.format("%s:%s/api/v1/projects/access/%s", serviceUrl, servicePort,
accessRequestId);
NotificationRequest request = NotificationRequest.builder()
.usernames(Arrays.stream(approvalUsernames.split(",")).toList())
.subject(NOTIFICATION_SUBJECT_ACCESS_REQUEST_APPROVAL).body(getMessage(projectAccessRequestStatusUrl))
.build();
HttpEntity<NotificationRequest> notificationRequestHttpEntity = RestUtils.getRequestWithHeaders(request,
notificationServiceAccountName, notificationServiceAccountPassword);

String url = String.format("%s:%s/api/v1/messages", notificationServiceUrl, notificationServicePort);
ResponseEntity<String> response = RestUtils.executePost(url, notificationRequestHttpEntity);
try {
if (response.getStatusCode().is2xxSuccessful()) {
log.info("Rest call completed: {}", response.getBody());
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
log.error("Call to the API was not successful. Response: {}", response.getStatusCode());
}
catch (Exception e) {
log.error("There was an issue with the REST call: {}", e.getMessage());
}
return new DefaultWorkReport(WorkStatus.FAILED, workContext);
NotificationMessageCreateRequestDTO notificationMessageCreateRequestDTO = new NotificationMessageCreateRequestDTO();
notificationMessageCreateRequestDTO.setSubject(NOTIFICATION_SUBJECT_ACCESS_REQUEST_APPROVAL);
notificationMessageCreateRequestDTO.setUsernames(Arrays.stream(approvalUsernames.split(",")).toList());
notificationMessageCreateRequestDTO.setBody(getMessage(
String.format("%s/api/v1/projects/access/%s", projectRequester.getBasePath(), accessRequestId)));
notifier.send(notificationMessageCreateRequestDTO);
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}

private String getMessage(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.redhat.parodos.project.enums.Role;
import com.redhat.parodos.utils.RestUtils;
import com.redhat.parodos.infrastructure.ProjectRequester;
import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.model.AccessRequestDTO;
import com.redhat.parodos.sdk.model.AccessResponseDTO;
import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.parameter.WorkParameter;
import com.redhat.parodos.workflow.parameter.WorkParameterType;
Expand All @@ -14,13 +15,9 @@
import com.redhat.parodos.workflows.work.WorkContext;
import com.redhat.parodos.workflows.work.WorkReport;
import com.redhat.parodos.workflows.work.WorkStatus;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.ResponseEntity;

import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_APPROVAL_USERNAMES;
import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_ESCALATION_USERNAME;
import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_ID;
Expand All @@ -31,27 +28,15 @@
@Slf4j
public class ProjectAccessRequestWorkFlowTask extends BaseWorkFlowTask {

private final String serviceUrl;

private final String servicePort;

private final String serviceAccountUsername;

private final String serviceAccountPassword;
private final ProjectRequester projectRequester;

public ProjectAccessRequestWorkFlowTask(String serviceUrl, String servicePort, String serviceAccountUsername,
String serviceAccountPassword) {
super();
this.serviceUrl = serviceUrl;
this.servicePort = servicePort;
this.serviceAccountUsername = serviceAccountUsername;
this.serviceAccountPassword = serviceAccountPassword;
public ProjectAccessRequestWorkFlowTask(ProjectRequester projectRequester) {
this.projectRequester = projectRequester;
}

@Override
public WorkReport execute(WorkContext workContext) {
String username;
String role;
String username, role;
try {
username = getRequiredParameterValue(PARAMETER_USERNAME);
role = getOptionalParameterValue(PARAMETER_ROLE, PARAMETER_ROLE_DEFAULT, false);
Expand All @@ -63,37 +48,30 @@ public WorkReport execute(WorkContext workContext) {
}

try {
Role.valueOf(role.toUpperCase());
AccessRequestDTO.RoleEnum.valueOf(role.toUpperCase());
}
catch (IllegalArgumentException e) {
log.error("Exception when trying to convert role requested: {}", e.getMessage());
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e);
}

AccessRequestDTO accessRequestDTO = new AccessRequestDTO();
accessRequestDTO.setUsername(username);
accessRequestDTO.setRole(AccessRequestDTO.RoleEnum.valueOf(role.toUpperCase()));
accessRequestDTO.setUsername(username);
try {
String url = String.format("%s:%s/api/v1/projects/%s/access", serviceUrl, servicePort,
getProjectId(workContext));
log.info("url: {}", url);
AccessRequestDTO requestDTO = AccessRequestDTO.builder().username(username)
.role(Role.valueOf(role.toUpperCase())).build();
ResponseEntity<AccessResponseDTO> responseDTO = RestUtils.executePost(url, requestDTO,
serviceAccountUsername, serviceAccountPassword, AccessResponseDTO.class);
if (responseDTO.getStatusCode().is2xxSuccessful()) {
log.info("Rest call completed with response: {}", responseDTO.getBody());
addParameter(ACCESS_REQUEST_ID,
Objects.requireNonNull(responseDTO.getBody()).accessRequestId.toString());
addParameter(ACCESS_REQUEST_APPROVAL_USERNAMES,
String.join(",", Objects.requireNonNull(responseDTO.getBody()).getApprovalSentTo()));
addParameter(ACCESS_REQUEST_ESCALATION_USERNAME,
String.join(",", Objects.requireNonNull(responseDTO.getBody()).getEscalationSentTo()));
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
log.error("Call to the api was not successful with status code: {}", responseDTO.getStatusCode());
AccessResponseDTO accessResponseDTO = projectRequester.createAccess(getProjectId(workContext),
accessRequestDTO);
addParameter(ACCESS_REQUEST_ID, Objects.requireNonNull(accessResponseDTO.getAccessRequestId()).toString());
addParameter(ACCESS_REQUEST_APPROVAL_USERNAMES,
String.join(",", Objects.requireNonNull(accessResponseDTO.getApprovalSentTo())));
addParameter(ACCESS_REQUEST_ESCALATION_USERNAME, String.join(",", accessResponseDTO.getEscalationSentTo()));
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
catch (Exception e) {
log.error("There was an issue with the REST call: {}", e.getMessage());
catch (ApiException e) {
log.error("There was an issue with the api call: {}", e.getMessage());
return new DefaultWorkReport(WorkStatus.FAILED, workContext);
}
return new DefaultWorkReport(WorkStatus.FAILED, workContext);
}

@Override
Expand All @@ -105,25 +83,4 @@ public WorkReport execute(WorkContext workContext) {
.description("The role to grant to the user").build());
}

@Data
@Builder
private static class AccessRequestDTO {

private String username;

private Role role;

}

@Data
private static class AccessResponseDTO {

private UUID accessRequestId;

private List<String> approvalSentTo;

private String escalationSentTo;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@
import java.util.Objects;
import java.util.UUID;

import com.redhat.parodos.project.enums.ProjectAccessStatus;
import com.redhat.parodos.utils.RestUtils;
import com.redhat.parodos.infrastructure.ProjectRequester;
import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.model.AccessStatusResponseDTO;
import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.task.checker.BaseWorkFlowCheckerTask;
import com.redhat.parodos.workflows.work.DefaultWorkReport;
import com.redhat.parodos.workflows.work.WorkContext;
import com.redhat.parodos.workflows.work.WorkReport;
import com.redhat.parodos.workflows.work.WorkStatus;
import com.redhat.parodos.workflows.workflow.WorkFlow;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.ResponseEntity;

import static com.redhat.parodos.tasks.project.consts.ProjectAccessRequestConstant.ACCESS_REQUEST_ID;

/**
Expand All @@ -46,76 +41,45 @@
@Slf4j
public class ProjectAccessRequestApprovalWorkFlowCheckerTask extends BaseWorkFlowCheckerTask {

private final String serviceUrl;

private final String servicePort;

private final String serviceAccountUsername;

private final String serviceAccountPassword;
private final ProjectRequester projectRequester;

public ProjectAccessRequestApprovalWorkFlowCheckerTask(WorkFlow projectAccessRequestApprovalEscalationWorkFlow,
long sla, String serviceUrl, String servicePort, String serviceAccountUsername,
String serviceAccountPassword) {
long sla, ProjectRequester projectRequester) {
super(projectAccessRequestApprovalEscalationWorkFlow, sla);
this.serviceUrl = serviceUrl;
this.servicePort = servicePort;
this.serviceAccountUsername = serviceAccountUsername;
this.serviceAccountPassword = serviceAccountPassword;
this.projectRequester = projectRequester;
}

@Override
public WorkReport checkWorkFlowStatus(WorkContext workContext) {
log.info("Start ProjectAccessRequestApprovalWorkFlowCheckerTask...");
log.info("Start projectAccessRequestApprovalWorkFlowCheckerTask...");
UUID accessRequestId;
try {
accessRequestId = UUID.fromString(getRequiredParameterValue(ACCESS_REQUEST_ID));
}
catch (MissingParameterException e) {
log.error("Exception when trying to get required parameter(s): {}", e.getMessage());
log.error("Exception when trying to get required parameter: {}", e.getMessage());
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e);
}

try {
String url = String.format("%s:%s/api/v1/projects/access/%s/status", serviceUrl, servicePort,
accessRequestId);
ResponseEntity<AccessStatusResponseDTO> responseDTO = RestUtils.restExchange(url, serviceAccountUsername,
serviceAccountPassword, AccessStatusResponseDTO.class);
if (!responseDTO.getStatusCode().is2xxSuccessful()) {
log.error("Call to the api was not successful: {}", responseDTO.getStatusCode());
}
else {
log.info("Rest call completed with response: {}", responseDTO.getBody());
switch (Objects.requireNonNull(responseDTO.getBody()).getStatus()) {
case APPROVED -> {
log.info("Project access request {} is approved", responseDTO.getBody().getAccessRequestId());
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
case REJECTED -> {
log.info("Project access request {} is rejected", responseDTO.getBody().getAccessRequestId());
return new DefaultWorkReport(WorkStatus.REJECTED, workContext);
}
default -> log.info("Project access request {} is waiting for approval",
responseDTO.getBody().getAccessRequestId());
AccessStatusResponseDTO accessStatusResponseDTO = projectRequester.getAccessStatus(accessRequestId);
switch (Objects.requireNonNull(accessStatusResponseDTO.getStatus())) {
case REJECTED -> {
log.info("Project access request {} is rejected!", accessStatusResponseDTO.getAccessRequestId());
return new DefaultWorkReport(WorkStatus.REJECTED, workContext);
}
case APPROVED -> {
log.info("Project access request {} is completed!", accessStatusResponseDTO.getAccessRequestId());
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
default -> log.info("Project access request {} awaits for approval",
accessStatusResponseDTO.getAccessRequestId());
}
}
catch (Exception e) {
log.error("There was an issue with the REST call: {}", e.getMessage());
catch (ApiException e) {
log.error("There was an issue with the api call: {}", e.getMessage());
}
return new DefaultWorkReport(WorkStatus.FAILED, workContext);
}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
private static class AccessStatusResponseDTO {

private UUID accessRequestId;

private ProjectAccessStatus status;

}

}

This file was deleted.

Loading

0 comments on commit cdfbb13

Please sign in to comment.