From cdfbb13e2ffd826368b7e442e2e4baca391cdc10 Mon Sep 17 00:00:00 2001 From: Annel Ketcha Date: Fri, 28 Jul 2023 16:59:08 -0400 Subject: [PATCH] refactor tasks and tests --- prebuilt-tasks/pom.xml | 5 + .../infrastructure/ProjectRequester.java | 18 ++++ ...jectAccessRequestApprovalWorkFlowTask.java | 63 ++++--------- .../ProjectAccessRequestWorkFlowTask.java | 89 +++++------------- ...essRequestApprovalWorkFlowCheckerTask.java | 78 +++++----------- .../tasks/project/dto/MessageRequest.java | 4 - .../project/dto/NotificationRequest.java | 18 ---- ...ctAccessRequestEscalationWorkFlowTask.java | 63 +++---------- .../ansible/AapLaunchJobWorkFlowTaskTest.java | 4 - ...AccessRequestApprovalWorkFlowTaskTest.java | 5 + .../ProjectAccessRequestWorkFlowTaskTest.java | 92 +++++++++++++++++++ ...equestApprovalWorkFlowCheckerTaskTest.java | 5 + ...cessRequestEscalationWorkFlowTaskTest.java | 5 + ...ectAccessRequestWorkFlowConfiguration.java | 35 ++----- .../VmOnboardingWorkFlowConfiguration.java | 9 +- .../task/NotificationWorkFlowTask.java | 10 +- .../task/NotificationWorkFlowTaskTest.java | 26 ++++-- .../client/ProjectRequesterClient.java | 52 +++++++++++ .../src/main/resources/application-local.yml | 7 ++ 19 files changed, 300 insertions(+), 288 deletions(-) create mode 100644 prebuilt-tasks/src/main/java/com/redhat/parodos/infrastructure/ProjectRequester.java delete mode 100644 prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/MessageRequest.java delete mode 100644 prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/NotificationRequest.java create mode 100644 prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java create mode 100644 prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java create mode 100644 prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java create mode 100644 prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java create mode 100644 workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java diff --git a/prebuilt-tasks/pom.xml b/prebuilt-tasks/pom.xml index e6a037f3e..1e18d0ad7 100644 --- a/prebuilt-tasks/pom.xml +++ b/prebuilt-tasks/pom.xml @@ -90,6 +90,11 @@ notification-service-sdk ${revision} + + dev.parodos + workflow-service-sdk + ${revision} + org.springframework spring-web diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/infrastructure/ProjectRequester.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/infrastructure/ProjectRequester.java new file mode 100644 index 000000000..03333e4a7 --- /dev/null +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/infrastructure/ProjectRequester.java @@ -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; + +} diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java index 6efb10e95..1642e9cd9 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java @@ -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; @@ -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; @@ -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); @@ -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 notificationRequestHttpEntity = RestUtils.getRequestWithHeaders(request, - notificationServiceAccountName, notificationServiceAccountPassword); - - String url = String.format("%s:%s/api/v1/messages", notificationServiceUrl, notificationServicePort); - ResponseEntity 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) { diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java index 3482fa2e9..cb1898ada 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java @@ -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; @@ -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; @@ -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); @@ -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 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 @@ -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 approvalSentTo; - - private String escalationSentTo; - - } - } diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTask.java index a66744731..08076583d 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTask.java @@ -18,8 +18,9 @@ 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; @@ -27,14 +28,8 @@ 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; /** @@ -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 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; - - } - } diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/MessageRequest.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/MessageRequest.java deleted file mode 100644 index c64d55183..000000000 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/MessageRequest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.redhat.parodos.tasks.project.dto; - -public record MessageRequest(String username, String subject, String[] recipients, String message) { -} diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/NotificationRequest.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/NotificationRequest.java deleted file mode 100644 index 59b11b69c..000000000 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/dto/NotificationRequest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.redhat.parodos.tasks.project.dto; - -import java.util.List; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class NotificationRequest { - - private List usernames; - - private String subject; - - private String body; - -} diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java index 93cbe214a..7b698fddc 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java @@ -15,11 +15,11 @@ */ package com.redhat.parodos.tasks.project.escalation; -import java.util.List; 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; @@ -28,9 +28,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_ESCALATION_USERNAME; 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_ESCALATION; @@ -44,28 +41,14 @@ @Slf4j public class ProjectAccessRequestEscalationWorkFlowTask extends BaseWorkFlowTask { - private final String serviceUrl; - - private final String servicePort; - - private final String notificationServiceUrl; - - private final String notificationServicePort; - - private final String notificationServiceAccountName; + private final ProjectRequester projectRequester; - private final String notificationServiceAccountPassword; + private final Notifier notifier; - public ProjectAccessRequestEscalationWorkFlowTask(String serviceUrl, String servicePort, - String notificationServiceUrl, String notificationServicePort, String notificationServiceAccountName, - String notificationServiceAccountPassword) { + public ProjectAccessRequestEscalationWorkFlowTask(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 @@ -81,29 +64,13 @@ public WorkReport execute(WorkContext workContext) { log.error("Exception when trying to get required parameter(s): {}", e.getMessage()); return new DefaultWorkReport(WorkStatus.FAILED, workContext, e); } - - String projectAccessRequestStatusUrl = String.format("%s:%s/api/v1/projects/access/%s/status", serviceUrl, - servicePort, accessRequestId); - NotificationRequest request = NotificationRequest.builder().usernames(List.of(escalationUsername)) - .subject(NOTIFICATION_SUBJECT_ACCESS_REQUEST_ESCALATION).body(getMessage(projectAccessRequestStatusUrl)) - .build(); - HttpEntity notificationRequestHttpEntity = RestUtils.getRequestWithHeaders(request, - notificationServiceAccountName, notificationServiceAccountPassword); - - ResponseEntity response = RestUtils.executePost( - String.format("%s:%s/api/v1/messages", notificationServiceUrl, notificationServicePort), - 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_ESCALATION); + notificationMessageCreateRequestDTO.addUsernamesItem(escalationUsername); + notificationMessageCreateRequestDTO.setBody(getMessage( + String.format("%s/api/v1/projects/access/%s/status", projectRequester.getBasePath(), accessRequestId))); + notifier.send(notificationMessageCreateRequestDTO); + return new DefaultWorkReport(WorkStatus.COMPLETED, workContext); } private String getMessage(String url) { diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/ansible/AapLaunchJobWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/ansible/AapLaunchJobWorkFlowTaskTest.java index 1c435ed1d..807fe056a 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/ansible/AapLaunchJobWorkFlowTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/ansible/AapLaunchJobWorkFlowTaskTest.java @@ -27,10 +27,6 @@ public class AapLaunchJobWorkFlowTaskTest { private static final String AAP_URL_TEST = "aap-url-test"; - private static final String WINDOWS_JOB_TEMPLATE_ID_TEST = "windows-job-template-id-test"; - - private static final String RHEL_JOB_TEMPLATE_ID_TEST = "rhel-job-template-id-test"; - private static final String USERNAME_TEST = "username-test"; private static final String PASSWORD_TEST = "password-test"; diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java new file mode 100644 index 000000000..1b6f5f922 --- /dev/null +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java @@ -0,0 +1,5 @@ +package com.redhat.parodos.tasks.project; + +public class ProjectAccessRequestApprovalWorkFlowTaskTest { + +} diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java new file mode 100644 index 000000000..c6684ec4f --- /dev/null +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java @@ -0,0 +1,92 @@ +package com.redhat.parodos.tasks.project; + +import java.util.UUID; + +import com.redhat.parodos.infrastructure.ProjectRequester; +import com.redhat.parodos.sdk.model.AccessResponseDTO; +import com.redhat.parodos.workflow.utils.WorkContextUtils; +import com.redhat.parodos.workflows.work.WorkContext; +import com.redhat.parodos.workflows.work.WorkReport; +import com.redhat.parodos.workflows.work.WorkStatus; +import lombok.SneakyThrows; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class ProjectAccessRequestWorkFlowTaskTest { + + private static final String USERNAME_PARAMETER_NAME = "USERNAME"; + + private static final String USERNAME_VALUE_TEST = "username-test"; + + private static final String ROLE_PARAMETER_NAME = "ROLE"; + + private static final String ROLE_DEFAULT_VALUE = "DEVELOPER"; + + private static final String ROLE_VALUE_TEST = "ADMIN"; + + private static final String INVALID_ROLE_VALUE_TEST = "INVALID_ROLE"; + + private static final String APPROVAL_USERNAME_TEST = "test"; + + private static final String ESCALATION_USERNAME_TEST = "test"; + + @Mock + private ProjectRequester projectRequester; + + private WorkContext workContext; + + private ProjectAccessRequestWorkFlowTask projectAccessRequestWorkFlowTask; + + @Before + public void setUp() { + this.projectAccessRequestWorkFlowTask = spy(new ProjectAccessRequestWorkFlowTask(projectRequester)); + this.projectAccessRequestWorkFlowTask.setBeanName("projectAccessRequestWorkFlowTask"); + workContext = new WorkContext(); + WorkContextUtils.setProjectId(workContext, UUID.randomUUID()); + } + + @Test + @SneakyThrows + public void executeSuccess() { + doReturn(USERNAME_VALUE_TEST).when(this.projectAccessRequestWorkFlowTask) + .getRequiredParameterValue(eq(USERNAME_PARAMETER_NAME)); + doReturn(ROLE_VALUE_TEST).when(this.projectAccessRequestWorkFlowTask) + .getOptionalParameterValue(eq(ROLE_PARAMETER_NAME), eq(ROLE_DEFAULT_VALUE), eq(false)); + + AccessResponseDTO accessResponseDTO = new AccessResponseDTO(); + accessResponseDTO.setAccessRequestId(UUID.randomUUID()); + accessResponseDTO.addApprovalSentToItem(APPROVAL_USERNAME_TEST); + accessResponseDTO.setEscalationSentTo(ESCALATION_USERNAME_TEST); + + when(projectRequester.createAccess(any(), any())).thenReturn(accessResponseDTO); + + doNothing().when(projectAccessRequestWorkFlowTask).addParameter(any(), any()); + + WorkReport workReport = projectAccessRequestWorkFlowTask.execute(workContext); + assertEquals(WorkStatus.COMPLETED, workReport.getStatus()); + } + + @Test + @SneakyThrows + public void executeFailForInvalidRole() { + doReturn(USERNAME_VALUE_TEST).when(this.projectAccessRequestWorkFlowTask) + .getRequiredParameterValue(eq(USERNAME_PARAMETER_NAME)); + doReturn(INVALID_ROLE_VALUE_TEST).when(this.projectAccessRequestWorkFlowTask) + .getOptionalParameterValue(eq(ROLE_PARAMETER_NAME), eq(ROLE_DEFAULT_VALUE), eq(false)); + WorkReport workReport = projectAccessRequestWorkFlowTask.execute(workContext); + assertEquals(WorkStatus.FAILED, workReport.getStatus()); + } + +} diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java new file mode 100644 index 000000000..73f0d29b7 --- /dev/null +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java @@ -0,0 +1,5 @@ +package com.redhat.parodos.tasks.project.checker; + +public class ProjectAccessRequestApprovalWorkFlowCheckerTaskTest { + +} diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java new file mode 100644 index 000000000..b3609bc28 --- /dev/null +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java @@ -0,0 +1,5 @@ +package com.redhat.parodos.tasks.project.escalation; + +public class ProjectAccessRequestEscalationWorkFlowTaskTest { + +} diff --git a/workflow-examples/src/main/java/com/redhat/parodos/examples/prebuilt/project/ProjectAccessRequestWorkFlowConfiguration.java b/workflow-examples/src/main/java/com/redhat/parodos/examples/prebuilt/project/ProjectAccessRequestWorkFlowConfiguration.java index 6c0b57f9c..7d36fa15f 100644 --- a/workflow-examples/src/main/java/com/redhat/parodos/examples/prebuilt/project/ProjectAccessRequestWorkFlowConfiguration.java +++ b/workflow-examples/src/main/java/com/redhat/parodos/examples/prebuilt/project/ProjectAccessRequestWorkFlowConfiguration.java @@ -3,6 +3,8 @@ import java.util.Date; import java.util.List; +import com.redhat.parodos.infrastructure.Notifier; +import com.redhat.parodos.infrastructure.ProjectRequester; import com.redhat.parodos.tasks.project.ProjectAccessRequestApprovalWorkFlowTask; import com.redhat.parodos.tasks.project.ProjectAccessRequestWorkFlowTask; import com.redhat.parodos.tasks.project.checker.ProjectAccessRequestApprovalWorkFlowCheckerTask; @@ -15,7 +17,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -25,13 +26,8 @@ public class ProjectAccessRequestWorkFlowConfiguration { @Bean ProjectAccessRequestEscalationWorkFlowTask projectAccessRequestEscalationWorkFlowTask( - @Value("${SERVICE_URL:localhost}") String serviceUrl, @Value("${SERVICE_PORT:8080}") String servicePort, - @Value("${NOTIFICATION_SERVICE_URL:localhost}") String notificationServiceUrl, - @Value("${NOTIFICATION_SERVICE_PORT:8081}") String notificationServicePort, - @Value("${NOTIFICATION_SERVICE_ACCOUNT_NAME:test}") String notificationServiceAccountName, - @Value("${NOTIFICATION_SERVICE_ACCOUNT_PASSWORD:test}") String notificationServiceAccountPassword) { - return new ProjectAccessRequestEscalationWorkFlowTask(serviceUrl, servicePort, notificationServiceUrl, - notificationServicePort, notificationServiceAccountName, notificationServiceAccountPassword); + ProjectRequester projectRequester, Notifier notifier) { + return new ProjectAccessRequestEscalationWorkFlowTask(projectRequester, notifier); } @Bean(name = "projectAccessRequestEscalationWorkFlow") @@ -45,11 +41,9 @@ WorkFlow projectAccessRequestEscalationWorkFlow( @Bean ProjectAccessRequestApprovalWorkFlowCheckerTask projectAccessRequestApprovalWorkFlowCheckerTask( @Qualifier("projectAccessRequestEscalationWorkFlow") WorkFlow projectAccessRequestEscalationWorkFlow, - @Value("${SERVICE_URL:localhost}") String serviceUrl, @Value("${SERVICE_PORT:8080}") String servicePort, - @Value("${SERVICE_ACCOUNT_NAME:test}") String serviceAccountName, - @Value("${SERVICE_ACCOUNT_PASSWORD:test}") String serviceAccountPassword) { + ProjectRequester projectRequester) { return new ProjectAccessRequestApprovalWorkFlowCheckerTask(projectAccessRequestEscalationWorkFlow, - new Date().getTime() / 1000 + 30, serviceUrl, servicePort, serviceAccountName, serviceAccountPassword); + new Date().getTime() / 1000 + 30, projectRequester); } @Bean(name = "projectAccessRequestApprovalWorkFlowChecker") @@ -61,25 +55,16 @@ WorkFlow projectAccessRequestApprovalWorkFlowChecker( } @Bean - ProjectAccessRequestWorkFlowTask projectAccessRequestWorkFlowTask( - @Value("${SERVICE_URL:localhost}") String serviceUrl, @Value("${SERVICE_PORT:8080}") String servicePort, - @Value("${SERVICE_ACCOUNT_NAME:test}") String serviceAccountName, - @Value("${SERVICE_ACCOUNT_PASSWORD:test}") String serviceAccountPassword) { - return new ProjectAccessRequestWorkFlowTask(serviceUrl, servicePort, serviceAccountName, - serviceAccountPassword); + ProjectAccessRequestWorkFlowTask projectAccessRequestWorkFlowTask(ProjectRequester projectRequester) { + return new ProjectAccessRequestWorkFlowTask(projectRequester); } @Bean ProjectAccessRequestApprovalWorkFlowTask projectAccessRequestApprovalWorkFlowTask( @Qualifier("projectAccessRequestApprovalWorkFlowChecker") WorkFlow projectAccessRequestApprovalWorkFlowChecker, - @Value("${SERVICE_URL:localhost}") String serviceUrl, @Value("${SERVICE_PORT:8080}") String servicePort, - @Value("${NOTIFICATION_SERVICE_URL:localhost}") String notificationServiceUrl, - @Value("${NOTIFICATION_SERVICE_PORT:8081}") String notificationServicePort, - @Value("${NOTIFICATION_SERVICE_ACCOUNT_NAME:test}") String notificationServiceAccountName, - @Value("${NOTIFICATION_SERVICE_ACCOUNT_PASSWORD:test}") String notificationServiceAccountPassword) { + ProjectRequester projectRequester, Notifier notifier) { ProjectAccessRequestApprovalWorkFlowTask projectAccessRequestApprovalWorkFlowTask = new ProjectAccessRequestApprovalWorkFlowTask( - serviceUrl, servicePort, notificationServiceUrl, notificationServicePort, - notificationServiceAccountName, notificationServiceAccountPassword); + projectRequester, notifier); projectAccessRequestApprovalWorkFlowTask .setWorkFlowCheckers(List.of(projectAccessRequestApprovalWorkFlowChecker)); return projectAccessRequestApprovalWorkFlowTask; diff --git a/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/VmOnboardingWorkFlowConfiguration.java b/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/VmOnboardingWorkFlowConfiguration.java index 1a555eb5a..521603ada 100644 --- a/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/VmOnboardingWorkFlowConfiguration.java +++ b/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/VmOnboardingWorkFlowConfiguration.java @@ -9,6 +9,7 @@ import com.redhat.parodos.examples.vmonboarding.task.AapInstallToolsWorkFlowTask; import com.redhat.parodos.examples.vmonboarding.task.NotificationWorkFlowTask; import com.redhat.parodos.examples.vmonboarding.task.ServiceNowTicketCreationWorkFlowTask; +import com.redhat.parodos.infrastructure.Notifier; import com.redhat.parodos.workflow.annotation.Checker; import com.redhat.parodos.workflow.annotation.Infrastructure; import com.redhat.parodos.workflow.annotation.Parameter; @@ -73,13 +74,13 @@ ServiceNowTicketCreationWorkFlowTask serviceNowTicketCreationWorkFlowTask( } @Bean(name = "notificationTicketApprovalWorkFlowTask") - NotificationWorkFlowTask notificationTicketApprovalWorkFlowTask() { - return new NotificationWorkFlowTask(NOTIFICATION_SERVICE_NOW_TICKET_APPROVED); + NotificationWorkFlowTask notificationTicketApprovalWorkFlowTask(Notifier notifier) { + return new NotificationWorkFlowTask(notifier, NOTIFICATION_SERVICE_NOW_TICKET_APPROVED); } @Bean(name = "notificationVmCreatedWorkFlowTask") - NotificationWorkFlowTask notificationVmCreatedWorkFlowTask() { - return new NotificationWorkFlowTask(NOTIFICATION_SERVICE_NOW_VM_CREATED); + NotificationWorkFlowTask notificationVmCreatedWorkFlowTask(Notifier notifier) { + return new NotificationWorkFlowTask(notifier, NOTIFICATION_SERVICE_NOW_VM_CREATED); } @Bean(name = "ansibleVMCreationWorkFlowCheckerTask") diff --git a/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTask.java b/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTask.java index d09e51f21..c6917e3ab 100644 --- a/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTask.java +++ b/workflow-examples/src/main/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTask.java @@ -15,8 +15,6 @@ */ package com.redhat.parodos.examples.vmonboarding.task; -import javax.inject.Inject; - import com.redhat.parodos.infrastructure.Notifier; import com.redhat.parodos.workflow.task.infrastructure.BaseInfrastructureWorkFlowTask; import com.redhat.parodos.workflows.work.DefaultWorkReport; @@ -35,13 +33,13 @@ @Slf4j public class NotificationWorkFlowTask extends BaseInfrastructureWorkFlowTask { - private final String subject; + private final Notifier notifier; - @Inject - private Notifier notifier; + private final String subject; - public NotificationWorkFlowTask(String subject) { + public NotificationWorkFlowTask(Notifier notifier, String subject) { super(); + this.notifier = notifier; this.subject = subject; } diff --git a/workflow-examples/src/test/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTaskTest.java b/workflow-examples/src/test/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTaskTest.java index 982eecf3d..dcfc5739f 100644 --- a/workflow-examples/src/test/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTaskTest.java +++ b/workflow-examples/src/test/java/com/redhat/parodos/examples/vmonboarding/task/NotificationWorkFlowTaskTest.java @@ -2,7 +2,9 @@ import java.util.UUID; +import com.redhat.parodos.examples.base.BaseInfrastructureWorkFlowTaskTest; import com.redhat.parodos.infrastructure.Notifier; +import com.redhat.parodos.workflow.task.infrastructure.BaseInfrastructureWorkFlowTask; import com.redhat.parodos.workflow.task.log.service.WorkFlowLogService; import com.redhat.parodos.workflow.utils.WorkContextUtils; import com.redhat.parodos.workflows.work.WorkContext; @@ -11,9 +13,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.test.util.ReflectionTestUtils; @@ -22,32 +22,40 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; @RunWith(MockitoJUnitRunner.class) -public class NotificationWorkFlowTaskTest { +public class NotificationWorkFlowTaskTest extends BaseInfrastructureWorkFlowTaskTest { - @InjectMocks - private NotificationWorkFlowTask notificationWorkFlowTask; - - private WorkContext workContext; + private static final String NOTIFICATION_SUBJECT_TEST = "notification-subject-test"; @Mock private Notifier notifier; + private WorkContext workContext; + + private NotificationWorkFlowTask notificationWorkFlowTask; + @Before public void setUp() { - MockitoAnnotations.openMocks(this); + this.notificationWorkFlowTask = spy((NotificationWorkFlowTask) getTaskUnderTest()); + this.notificationWorkFlowTask.setBeanName("test"); workContext = new WorkContext(); WorkFlowLogService workFlowLogService = mock(WorkFlowLogService.class); doNothing().when(workFlowLogService).writeLog(any(), any(), any()); ReflectionTestUtils.setField(notificationWorkFlowTask, "workFlowLogService", workFlowLogService); } + @Override + protected BaseInfrastructureWorkFlowTask getTaskUnderTest() { + return new NotificationWorkFlowTask(notifier, NOTIFICATION_SUBJECT_TEST); + } + @Test public void executeSuccess() { WorkContextUtils.setMainExecutionId(workContext, UUID.randomUUID()); - notificationWorkFlowTask.setBeanName("test"); notificationWorkFlowTask.preExecute(workContext); + doNothing().when(notifier).send(any(), any()); WorkReport workReport = notificationWorkFlowTask.execute(workContext); assertEquals(WorkStatus.COMPLETED, workReport.getStatus()); } diff --git a/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java b/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java new file mode 100644 index 000000000..24dc32614 --- /dev/null +++ b/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java @@ -0,0 +1,52 @@ +package com.redhat.parodos.client; + +import java.util.Base64; +import java.util.UUID; + +import com.redhat.parodos.infrastructure.ProjectRequester; +import com.redhat.parodos.sdk.api.ProjectAccessApi; +import com.redhat.parodos.sdk.api.ProjectApi; +import com.redhat.parodos.sdk.invoker.ApiClient; +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; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class ProjectRequesterClient implements ProjectRequester { + + private final ProjectApi projectApi; + + private final ProjectAccessApi projectAccessApi; + + public ProjectRequesterClient(@Value("${workflow.url}") String url, + @Value("${workflow.auth.basic.user}") String user, + @Value("${workflow.auth.basic.password}") String password) { + ApiClient apiClient = new ApiClient().setBasePath(url).addDefaultHeader(HttpHeaders.AUTHORIZATION, + "Basic " + Base64.getEncoder().encodeToString((user + ":" + password).getBytes())); + projectApi = new ProjectApi(apiClient); + projectAccessApi = new ProjectAccessApi(apiClient); + } + + @Override + public String getBasePath() { + return projectApi.getApiClient().getBasePath(); + } + + @Override + public AccessResponseDTO createAccess(UUID id, AccessRequestDTO accessRequestDTO) throws ApiException { + return projectApi.createAccessRequestToProject(id, accessRequestDTO); + } + + @Override + public AccessStatusResponseDTO getAccessStatus(UUID id) throws ApiException { + return projectAccessApi.getProjectAccessStatus(id); + } + +} diff --git a/workflow-service/src/main/resources/application-local.yml b/workflow-service/src/main/resources/application-local.yml index 89f1cdbc5..663ddab8e 100644 --- a/workflow-service/src/main/resources/application-local.yml +++ b/workflow-service/src/main/resources/application-local.yml @@ -40,6 +40,13 @@ notification: user: test password: test +workflow: + url: "${WORKFLOW_SERVER_URL:http://localhost:8080}" + auth: + basic: + user: test + password: test + workflows: mta: url: "${MTA_URL:https://mta-openshift-mta.apps.parodos-dev.projects.ecosystem.sysdeseng.com}"