Skip to content

Commit 8146b0c

Browse files
committed
add flow group service
1 parent 406c128 commit 8146b0c

File tree

9 files changed

+134
-12
lines changed

9 files changed

+134
-12
lines changed

core/src/main/java/com/flowci/core/common/config/WebConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void addInterceptors(InterceptorRegistry registry) {
7171
.addPathPatterns("/users/**")
7272
.excludePathPatterns("/users/default")
7373
.addPathPatterns("/flows/**")
74+
.addPathPatterns("/flow_groups/**")
7475
.addPathPatterns("/jobs/**")
7576
.addPathPatterns("/agents/**")
7677
.addPathPatterns("/hosts/**")

core/src/main/java/com/flowci/core/flow/controller/FlowController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public Flow confirm(@PathVariable String name, @RequestBody ConfirmOption option
9292
@DeleteMapping("/{name}")
9393
@Action(FlowAction.DELETE)
9494
public Flow delete(@PathVariable String name) {
95-
return flowService.delete(name);
95+
var flow = flowService.get(name);
96+
flowService.delete(flow);
97+
return flow;
9698
}
9799

98100
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.flowci.core.flow.controller;
2+
3+
import com.flowci.core.auth.annotation.Action;
4+
import com.flowci.core.flow.domain.FlowAction;
5+
import com.flowci.core.flow.domain.FlowGroup;
6+
import com.flowci.core.flow.service.FlowGroupService;
7+
import com.flowci.core.flow.service.FlowService;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
import java.util.Set;
13+
14+
@RestController
15+
@RequestMapping("/flow_groups")
16+
public class FlowGroupController {
17+
18+
private final FlowService flowService;
19+
private final FlowGroupService flowGroupService;
20+
21+
@Autowired
22+
public FlowGroupController(FlowService flowService, FlowGroupService flowGroupService) {
23+
this.flowService = flowService;
24+
this.flowGroupService = flowGroupService;
25+
}
26+
27+
@PostMapping("/{name}")
28+
@Action(FlowAction.GROUP_UPDATE)
29+
public FlowGroup create(@PathVariable String name) {
30+
return flowGroupService.create(name);
31+
}
32+
33+
@PostMapping
34+
@Action(FlowAction.GROUP_READ)
35+
public List<FlowGroup> list(@RequestBody Set<String> ids) {
36+
return flowGroupService.list(ids);
37+
}
38+
39+
@PostMapping("/{groupName}/{flowName}")
40+
@Action(FlowAction.GROUP_UPDATE)
41+
public void addToGroup(@PathVariable String groupName, @PathVariable String flowName) {
42+
flowGroupService.addToGroup(flowName, groupName);
43+
}
44+
45+
@DeleteMapping("/{groupName}/{flowName}")
46+
@Action(FlowAction.GROUP_UPDATE)
47+
public void removeFromGroup(@PathVariable("groupName") String ignore, @PathVariable String flowName) {
48+
flowGroupService.removeFromGroup(flowName);
49+
}
50+
51+
@DeleteMapping("/{name}")
52+
@Action(FlowAction.GROUP_UPDATE)
53+
public void delete(@PathVariable String name, @RequestParam(required = false) boolean deleteFlow) {
54+
var group = flowGroupService.get(name);
55+
56+
if (deleteFlow) {
57+
var flowList = flowGroupService.flows(group.getId());
58+
for (var flow : flowList) {
59+
flowService.delete(flow);
60+
}
61+
}
62+
63+
flowGroupService.delete(group.getName());
64+
}
65+
}

core/src/main/java/com/flowci/core/flow/dao/FlowDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public interface FlowDao extends MongoRepository<Flow, String> {
3535

3636
List<Flow> findAllByStatus(Status status);
3737

38+
List<Flow> findAllByGroupId(String groupId);
39+
3840
List<Flow> findAllByStatusAndCreatedBy(Status status, String createdBy);
3941

4042
List<Flow> findAllByIdInAndStatus(Collection<String> id, Status status);

core/src/main/java/com/flowci/core/flow/domain/FlowAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public abstract class FlowAction {
5757

5858
public static final String LIST_PLUGINS = "list_plugins";
5959

60+
public static final String GROUP_READ = "group_read";
61+
public static final String GROUP_UPDATE = "group_operation";
62+
6063
public static final List<String> ALL = ImmutableList.of(
6164
CREATE,
6265
CHECK_NAME,
@@ -74,7 +77,8 @@ public abstract class FlowAction {
7477
ADD_USER,
7578
REMOVE_USER,
7679
LIST_USER,
77-
LIST_PLUGINS
80+
LIST_PLUGINS,
81+
GROUP_READ,
82+
GROUP_UPDATE
7883
);
79-
8084
}

core/src/main/java/com/flowci/core/flow/service/FlowGroupService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.flowci.core.flow.service;
22

3+
import com.flowci.core.flow.domain.Flow;
34
import com.flowci.core.flow.domain.FlowGroup;
45

56
import java.util.Collection;
@@ -11,7 +12,19 @@ public interface FlowGroupService {
1112

1213
List<FlowGroup> list(Collection<String> ids);
1314

15+
List<Flow> flows(String id);
16+
1417
FlowGroup create(String name);
1518

19+
/**
20+
* Add flow to a group
21+
*/
22+
void addToGroup(String flowName, String groupName);
23+
24+
/**
25+
* Remove flow from a group
26+
*/
27+
void removeFromGroup(String flowName);
28+
1629
void delete(String name);
1730
}

core/src/main/java/com/flowci/core/flow/service/FlowGroupServiceImpl.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.flowci.core.flow.service;
22

3+
import com.flowci.core.flow.dao.FlowDao;
34
import com.flowci.core.flow.dao.FlowGroupDao;
5+
import com.flowci.core.flow.domain.Flow;
46
import com.flowci.core.flow.domain.FlowGroup;
57
import com.flowci.exception.DuplicateException;
68
import com.flowci.exception.NotFoundException;
@@ -15,9 +17,13 @@
1517
@Service
1618
public class FlowGroupServiceImpl implements FlowGroupService {
1719

20+
private final FlowDao flowDao;
21+
1822
private final FlowGroupDao flowGroupDao;
1923

20-
public FlowGroupServiceImpl(FlowGroupDao flowGroupDao) {
24+
@Autowired
25+
public FlowGroupServiceImpl(FlowDao flowDao, FlowGroupDao flowGroupDao) {
26+
this.flowDao = flowDao;
2127
this.flowGroupDao = flowGroupDao;
2228
}
2329

@@ -46,8 +52,36 @@ public FlowGroup create(String name) {
4652
}
4753
}
4854

55+
@Override
56+
public void addToGroup(String flowName, String groupName) {
57+
var group = get(groupName);
58+
var flow = getFlow(flowName);
59+
flow.setGroupId(group.getId());
60+
flowDao.save(flow);
61+
}
62+
63+
@Override
64+
public void removeFromGroup(String flowName) {
65+
var flow = getFlow(flowName);
66+
flow.setGroupId(null);
67+
flowDao.save(flow);
68+
}
69+
4970
@Override
5071
public void delete(String name) {
5172
flowGroupDao.deleteByName(name);
5273
}
74+
75+
@Override
76+
public List<Flow> flows(String id) {
77+
return flowDao.findAllByGroupId(id);
78+
}
79+
80+
private Flow getFlow(String flowName) {
81+
var flow = flowDao.findByName(flowName);
82+
if (flow.isEmpty()) {
83+
throw new NotFoundException("the flow {0} not found", flowName);
84+
}
85+
return flow.get();
86+
}
5387
}

core/src/main/java/com/flowci/core/flow/service/FlowService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public interface FlowService {
7272
/**
7373
* Delete flow and yml
7474
*/
75-
Flow delete(String name);
75+
void delete(Flow flow);
7676

7777
/**
7878
* Create ssh-rsa credential

core/src/main/java/com/flowci/core/flow/service/FlowServiceImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,17 @@ public Flow create(String name) {
157157
}
158158

159159
@Override
160-
public Flow create(String name, String groupId) {
160+
public Flow create(String name, String groupName) {
161161
Flow.validateName(name);
162162
String email = sessionManager.getUserEmail();
163163

164-
if (groupId != null) {
165-
if (!flowGroupDao.existsById(groupId)) {
166-
throw new NotFoundException("The group id {0} not found", groupId);
164+
String groupId = null;
165+
if (groupName != null) {
166+
var optional = flowGroupDao.findByName(groupName);
167+
if (optional.isEmpty()) {
168+
throw new NotFoundException("group {0} not found", groupName);
167169
}
170+
groupId = optional.get().getId();
168171
}
169172

170173
// reuse from pending list
@@ -260,16 +263,14 @@ public Flow getById(String id) {
260263
}
261264

262265
@Override
263-
public Flow delete(String name) {
264-
Flow flow = get(name);
266+
public void delete(Flow flow) {
265267
flowDao.delete(flow);
266268
flowUserDao.delete(flow.getId());
267269

268270
ymlService.delete(flow.getId());
269271
cronService.cancel(flow);
270272

271273
eventManager.publish(new FlowDeletedEvent(this, flow));
272-
return flow;
273274
}
274275

275276
@Override

0 commit comments

Comments
 (0)