-
Notifications
You must be signed in to change notification settings - Fork 120
/
OpenRestController.java
168 lines (143 loc) · 6.26 KB
/
OpenRestController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2019 flow.ci
*
* Licensed 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 com.flowci.core.api;
import com.flowci.core.agent.service.AgentService;
import com.flowci.core.api.domain.AddStatsItem;
import com.flowci.core.api.domain.CreateJobArtifact;
import com.flowci.core.api.domain.CreateJobReport;
import com.flowci.core.api.service.OpenRestService;
import com.flowci.core.config.domain.Config;
import com.flowci.core.flow.domain.StatsCounter;
import com.flowci.core.job.domain.JobCache;
import com.flowci.core.job.service.CacheService;
import com.flowci.core.job.service.LoggingService;
import com.flowci.core.secret.domain.RSASecret;
import com.flowci.core.secret.domain.Secret;
import com.flowci.core.user.domain.User;
import com.flowci.exception.ArgumentException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Provides API which calling from agent plugin
*/
@Log4j2
@RestController
@RequestMapping("/api")
public class OpenRestController {
@Autowired
private OpenRestService openRestService;
@Autowired
private AgentService agentService;
@Autowired
private LoggingService loggingService;
@Autowired
private CacheService cacheService;
@GetMapping("/secret/{name}")
public Secret getSecret(@PathVariable String name) {
Secret secret = openRestService.getSecret(name);
if (secret instanceof RSASecret) {
RSASecret rsa = (RSASecret) secret;
rsa.setPrivateKey(null);
}
return secret;
}
@GetMapping("/secret/{name}/download/{file:.+}")
public ResponseEntity<Resource> downloadSecretFile(@PathVariable String name,
@PathVariable String file) {
Secret secret = openRestService.getSecret(name);
Resource resource = openRestService.getResource(secret, file);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file + "\"")
.body(resource);
}
@GetMapping("/config/{name}")
public Config getConfig(@PathVariable String name) {
return openRestService.getConfig(name);
}
@GetMapping("/flow/{name}/users")
public List<User> listFlowUserEmail(@PathVariable String name) {
return openRestService.users(name);
}
@PostMapping("/flow/{name}/stats")
public void addStatsItem(@PathVariable String name,
@Validated @RequestBody AddStatsItem body) {
openRestService.saveStatsForFlow(name, body.getType(), StatsCounter.from(body.getData()));
}
@PostMapping("/flow/{name}/job/{number}/context")
public void addJobContext(@PathVariable String name,
@PathVariable long number,
@RequestBody Map<String, String> vars) {
openRestService.addToJobContext(name, number, vars);
}
@PostMapping("/flow/{name}/job/{number}/report")
public void uploadJobReport(@PathVariable String name,
@PathVariable long number,
@Validated @RequestPart("body") CreateJobReport meta,
@RequestPart("file") MultipartFile file) {
openRestService.saveJobReport(name, number, meta, file);
}
@PostMapping("/flow/{name}/job/{number}/artifact")
public void uploadJobArtifact(@PathVariable String name,
@PathVariable long number,
@Validated @RequestPart("body") CreateJobArtifact meta,
@RequestPart("file") MultipartFile file) {
openRestService.saveJobArtifact(name, number, meta, file);
}
@PostMapping("/logs/upload")
public void upload(@RequestPart("file") MultipartFile file) {
try (InputStream stream = file.getInputStream()) {
loggingService.save(file.getOriginalFilename(), stream);
} catch (IOException e) {
log.warn("Unable to save log, cause {}", e.getMessage());
}
}
@PostMapping("/cache/{jobId}/{key}/{os}")
public void putCache(@PathVariable String jobId,
@PathVariable String key,
@PathVariable String os,
@RequestParam("files") MultipartFile[] files) {
if (files.length == 0) {
throw new ArgumentException("the cached files are empty");
}
cacheService.put(jobId, key, os, files);
}
@GetMapping("/cache/{jobId}/{key}")
public JobCache getCache(@PathVariable String jobId, @PathVariable String key) {
return cacheService.get(jobId, key);
}
@GetMapping("/cache/{cacheId}")
public ResponseEntity<Resource> downloadCache(@PathVariable String cacheId, @RequestParam String file) {
InputStream stream = cacheService.fetch(cacheId, file);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file + "\"")
.body(new InputStreamResource(stream));
}
}