insist777
2 years ago
committed by
GitHub
9 changed files with 594 additions and 47 deletions
@ -0,0 +1,154 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.controller; |
||||
|
||||
import static org.apache.dolphinscheduler.api.enums.Status.*; |
||||
|
||||
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation; |
||||
import org.apache.dolphinscheduler.api.dto.workflowInstance.WorkflowInstanceQueryRequest; |
||||
import org.apache.dolphinscheduler.api.enums.ExecuteType; |
||||
import org.apache.dolphinscheduler.api.exceptions.ApiException; |
||||
import org.apache.dolphinscheduler.api.service.ExecutorService; |
||||
import org.apache.dolphinscheduler.api.service.ProcessInstanceService; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.apache.dolphinscheduler.common.constants.Constants; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.web.bind.annotation.DeleteMapping; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PathVariable; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestAttribute; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.ResponseStatus; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import io.swagger.v3.oas.annotations.Operation; |
||||
import io.swagger.v3.oas.annotations.Parameter; |
||||
import io.swagger.v3.oas.annotations.Parameters; |
||||
import io.swagger.v3.oas.annotations.media.Schema; |
||||
import io.swagger.v3.oas.annotations.tags.Tag; |
||||
|
||||
/** |
||||
* workflow instance controller |
||||
*/ |
||||
@Tag(name = "WORKFLOW_INSTANCE_TAG_V2") |
||||
@RestController |
||||
@RequestMapping("/v2/workflow-instances") |
||||
public class WorkflowInstanceV2Controller extends BaseController { |
||||
|
||||
@Autowired |
||||
private ProcessInstanceService processInstanceService; |
||||
|
||||
@Autowired |
||||
private ExecutorService execService; |
||||
|
||||
/** |
||||
* query workflow instance list paging |
||||
* @param loginUser login user |
||||
* @param workflowInstanceQueryRequest workflowInstanceQueryRequest |
||||
* @return workflow instance list |
||||
*/ |
||||
@Operation(summary = "queryWorkflowInstanceListPaging", description = "QUERY_PROCESS_INSTANCE_LIST_NOTES") |
||||
@GetMapping(consumes = {"application/json"}) |
||||
@ResponseStatus(HttpStatus.OK) |
||||
@ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR) |
||||
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") |
||||
public Result queryWorkflowInstanceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestBody WorkflowInstanceQueryRequest workflowInstanceQueryRequest) { |
||||
Result result = |
||||
checkPageParams(workflowInstanceQueryRequest.getPageNo(), workflowInstanceQueryRequest.getPageSize()); |
||||
if (!result.checkResult()) { |
||||
return result; |
||||
} |
||||
|
||||
result = processInstanceService.queryProcessInstanceList(loginUser, workflowInstanceQueryRequest); |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* Query workflowInstance by id |
||||
* |
||||
* @param loginUser login user |
||||
* @param workflowInstanceId workflow instance id |
||||
* @return Result result object query |
||||
*/ |
||||
@Operation(summary = "queryWorkflowInstanceById", description = "QUERY_WORKFLOW_INSTANCE_BY_ID") |
||||
@Parameters({ |
||||
@Parameter(name = "workflowInstanceId", description = "WORKFLOW_INSTANCE_ID", schema = @Schema(implementation = Integer.class, example = "123456", required = true)) |
||||
}) |
||||
@GetMapping(value = "/{workflowInstanceId}") |
||||
@ResponseStatus(HttpStatus.OK) |
||||
@ApiException(QUERY_PROCESS_INSTANCE_BY_ID_ERROR) |
||||
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") |
||||
public Result queryWorkflowInstanceById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@PathVariable("workflowInstanceId") Integer workflowInstanceId) { |
||||
Map<String, Object> result = processInstanceService.queryProcessInstanceById(loginUser, workflowInstanceId); |
||||
return returnDataList(result); |
||||
} |
||||
|
||||
/** |
||||
* Delete workflowInstance by id |
||||
* |
||||
* @param loginUser login user |
||||
* @param workflowInstanceId workflow instance code |
||||
* @return Result result object delete |
||||
*/ |
||||
@Operation(summary = "delete", description = "DELETE_WORKFLOWS_INSTANCE_NOTES") |
||||
@Parameters({ |
||||
@Parameter(name = "workflowInstanceId", description = "WORKFLOW_INSTANCE_ID", schema = @Schema(implementation = Integer.class, example = "123456", required = true)) |
||||
}) |
||||
@DeleteMapping(value = "/{workflowInstanceId}") |
||||
@ResponseStatus(HttpStatus.OK) |
||||
@ApiException(DELETE_PROCESS_DEFINE_BY_CODE_ERROR) |
||||
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") |
||||
public Result deleteWorkflowInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@PathVariable("workflowInstanceId") Integer workflowInstanceId) { |
||||
processInstanceService.deleteProcessInstanceById(loginUser, workflowInstanceId); |
||||
return Result.success(); |
||||
} |
||||
|
||||
/** |
||||
* do action to workflow instance: pause, stop, repeat, recover from pause, recover from stop |
||||
* |
||||
* @param loginUser login user |
||||
* @param workflowInstanceId workflow instance id |
||||
* @param executeType execute type |
||||
* @return execute result code |
||||
*/ |
||||
@Operation(summary = "execute", description = "EXECUTE_ACTION_TO_WORKFLOW_INSTANCE_NOTES") |
||||
@Parameters({ |
||||
@Parameter(name = "workflowInstanceId", description = "WORKFLOW_INSTANCE_ID", required = true, schema = @Schema(implementation = int.class, example = "100")), |
||||
@Parameter(name = "executeType", description = "EXECUTE_TYPE", required = true, schema = @Schema(implementation = ExecuteType.class)) |
||||
}) |
||||
@PostMapping(value = "/{workflowInstanceId}/execute/{executeType}") |
||||
@ResponseStatus(HttpStatus.OK) |
||||
@ApiException(EXECUTE_PROCESS_INSTANCE_ERROR) |
||||
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") |
||||
public Result execute(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@PathVariable("workflowInstanceId") Integer workflowInstanceId, |
||||
@PathVariable("executeType") ExecuteType executeType) { |
||||
Map<String, Object> result = execService.execute(loginUser, workflowInstanceId, executeType); |
||||
return returnDataList(result); |
||||
} |
||||
} |
@ -0,0 +1,65 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.dto.workflowInstance; |
||||
|
||||
import org.apache.dolphinscheduler.api.dto.PageQueryDto; |
||||
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
import io.swagger.v3.oas.annotations.media.Schema; |
||||
|
||||
/** |
||||
* workflow instance request |
||||
*/ |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
@Data |
||||
public class WorkflowInstanceQueryRequest extends PageQueryDto { |
||||
|
||||
@Schema(name = "projectName", example = "PROJECT-NAME") |
||||
String projectName; |
||||
|
||||
@Schema(name = "workflowName", example = "WORKFLOW-NAME") |
||||
String workflowName; |
||||
|
||||
@Schema(name = "host", example = "HOST") |
||||
String host; |
||||
|
||||
@Schema(name = "startDate", example = "START-TIME") |
||||
String startTime; |
||||
|
||||
@Schema(name = "endDate", example = "END-DATE") |
||||
String endTime; |
||||
|
||||
@Schema(name = "state", example = "STATE") |
||||
Integer state; |
||||
|
||||
public ProcessInstance convert2ProcessInstance() { |
||||
ProcessInstance processInstance = new ProcessInstance(); |
||||
if (this.workflowName != null) { |
||||
processInstance.setName(this.workflowName); |
||||
} |
||||
if (this.host != null) { |
||||
processInstance.setHost(this.host); |
||||
} |
||||
return processInstance; |
||||
} |
||||
} |
@ -0,0 +1,121 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.controller; |
||||
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.DATA_LIST; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
|
||||
import org.apache.dolphinscheduler.api.dto.workflowInstance.WorkflowInstanceQueryRequest; |
||||
import org.apache.dolphinscheduler.api.enums.ExecuteType; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.service.ExecutorService; |
||||
import org.apache.dolphinscheduler.api.service.ProcessInstanceService; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
|
||||
public class WorkflowInstanceV2ControllerTest extends AbstractControllerTest { |
||||
|
||||
@InjectMocks |
||||
private WorkflowInstanceV2Controller workflowInstanceV2Controller; |
||||
|
||||
@Mock |
||||
private ProcessInstanceService processInstanceService; |
||||
|
||||
@Mock |
||||
private ExecutorService execService; |
||||
|
||||
@Test |
||||
public void testQueryWorkFlowInstanceListPaging() { |
||||
User loginUser = getLoginUser(); |
||||
|
||||
WorkflowInstanceQueryRequest workflowInstanceQueryRequest = new WorkflowInstanceQueryRequest(); |
||||
workflowInstanceQueryRequest.setProjectName("test"); |
||||
workflowInstanceQueryRequest.setWorkflowName("shell"); |
||||
workflowInstanceQueryRequest.setPageNo(1); |
||||
workflowInstanceQueryRequest.setPageSize(10); |
||||
|
||||
Result result = new Result(); |
||||
PageInfo<ProcessInstance> pageInfo = |
||||
new PageInfo<>(workflowInstanceQueryRequest.getPageNo(), workflowInstanceQueryRequest.getPageSize()); |
||||
pageInfo.setTotalList(Collections.singletonList(new ProcessInstance())); |
||||
result.setData(pageInfo); |
||||
putMsg(result, Status.SUCCESS); |
||||
|
||||
Mockito.when(processInstanceService.queryProcessInstanceList(any(), |
||||
any(WorkflowInstanceQueryRequest.class))).thenReturn(result); |
||||
|
||||
Result result1 = |
||||
workflowInstanceV2Controller.queryWorkflowInstanceListPaging(loginUser, workflowInstanceQueryRequest); |
||||
Assertions.assertTrue(result1.isSuccess()); |
||||
} |
||||
|
||||
@Test |
||||
public void testQueryWorkflowInstanceById() { |
||||
User loginUser = getLoginUser(); |
||||
|
||||
Map<String, Object> result = new HashMap<>(); |
||||
result.put(DATA_LIST, new ProcessInstance()); |
||||
putMsg(result, Status.SUCCESS); |
||||
|
||||
Mockito.when(processInstanceService.queryProcessInstanceById(any(), eq(1))).thenReturn(result); |
||||
Result result1 = workflowInstanceV2Controller.queryWorkflowInstanceById(loginUser, 1); |
||||
Assertions.assertTrue(result1.isSuccess()); |
||||
} |
||||
|
||||
@Test |
||||
public void testDeleteWorkflowInstanceById() { |
||||
User loginUser = getLoginUser(); |
||||
|
||||
Mockito.when(processInstanceService.deleteProcessInstanceById(any(), eq(1))).thenReturn(null); |
||||
Result result = workflowInstanceV2Controller.deleteWorkflowInstance(loginUser, 1); |
||||
Assertions.assertTrue(result.isSuccess()); |
||||
} |
||||
|
||||
@Test |
||||
public void testExecuteWorkflowInstance() { |
||||
User loginUser = getLoginUser(); |
||||
|
||||
Map<String, Object> result = new HashMap<>(); |
||||
putMsg(result, Status.SUCCESS); |
||||
|
||||
Mockito.when(execService.execute(any(), eq(1), any(ExecuteType.class))).thenReturn(result); |
||||
|
||||
Result result1 = workflowInstanceV2Controller.execute(loginUser, 1, ExecuteType.STOP); |
||||
Assertions.assertTrue(result1.isSuccess()); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User user = new User(); |
||||
user.setId(1); |
||||
user.setUserName("admin"); |
||||
return user; |
||||
} |
||||
} |
Loading…
Reference in new issue