Skip to content

Commit 07596ff

Browse files
authored
Merge pull request #102 from FlowCI/feature/api/delete_agent
delete & create agent
2 parents 78bdf13 + 8f6edb9 commit 07596ff

File tree

12 files changed

+190
-4
lines changed

12 files changed

+190
-4
lines changed

platform-api/src/main/java/com/flow/platform/api/controller/AgentController.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,30 @@ public BooleanValue shutDown(@RequestParam String zone,
195195
public void callback(@RequestBody Agent agent) {
196196
this.dispatchEvent(new AgentStatusChangeEvent(this, agent));
197197
}
198+
199+
/**
200+
* @api {Post} /agents/delete Delete
201+
* @apiName Delete Agent
202+
* @apiGroup Agent
203+
* @apiDescription Delete agent by agentPath
204+
*
205+
* @apiParamExample {json} Request-Example:
206+
* {
207+
* zone: xxx,
208+
* name: xxx
209+
* }
210+
*
211+
* @apiSuccessExample {json} Success-Response:
212+
* HTTP/1.1 200 OK
213+
*
214+
* @apiErrorExample {json} Error-Response:
215+
* HTTP/1.1 400
216+
* {
217+
* "message": xxx
218+
* }
219+
*/
220+
@PostMapping(path = "/delete")
221+
public void delete(@RequestBody AgentPath agentPath){
222+
agentService.delete(agentPath);
223+
}
198224
}

platform-api/src/main/java/com/flow/platform/api/controller/FlowController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Node createEmptyFlow() {
114114
}
115115

116116
/**
117-
* @api {delete} /flows/:root/delete Delete
117+
* @api {delete} /flows/:root Delete
118118
* @apiParam {String} root flow node name will be deleted
119119
* @apiDescription Delete flow node by name and return flow node object
120120
* @apiGroup Flows

platform-api/src/main/java/com/flow/platform/api/service/AgentService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public interface AgentService {
4949
*/
5050
AgentSettings settings(String token);
5151

52+
/**
53+
* Delete agent by agentPath from cc
54+
*/
55+
void delete(AgentPath agentPath);
5256
/**
5357
* send sys cmd
5458
* @param agentPath required

platform-api/src/main/java/com/flow/platform/api/service/AgentServiceImpl.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.flow.platform.domain.AgentPath;
2929
import com.flow.platform.domain.AgentPathWithWebhook;
3030
import com.flow.platform.domain.AgentSettings;
31+
import com.flow.platform.domain.AgentStatus;
3132
import com.flow.platform.domain.CmdInfo;
3233
import com.flow.platform.domain.CmdType;
3334
import com.flow.platform.domain.Jsonable;
@@ -170,13 +171,52 @@ public AgentSettings settings(String token) {
170171
return AgentSettings.parse(response.getBody(), AgentSettings.class);
171172
}
172173

173-
private String buildAgentWebhook() {
174-
return domain + "/agents/callback";
174+
@Override
175+
public void delete(AgentPath agentPath){
176+
Agent agent = findAgent(agentPath);
177+
178+
try {
179+
HttpClient.build(platformURL.getAgentDeleteUrl())
180+
.post(agent.toJson())
181+
.withContentType(ContentType.APPLICATION_JSON)
182+
.retry(httpRetryTimes);
183+
184+
} catch (UnsupportedEncodingException e) {
185+
throw new IllegalStatusException(e.getMessage());
186+
}
175187
}
176188

177189
@Override
178190
public void sendSysCmd(AgentPath agentPath) {
179191
CmdInfo cmdInfo = new CmdInfo(agentPath, CmdType.SYSTEM_INFO, "");
180192
cmdService.sendCmd(agentPath, cmdInfo);
181193
}
194+
195+
private String buildAgentWebhook() {
196+
return domain + "/agents/callback";
197+
}
198+
199+
/**
200+
* find agent
201+
*/
202+
private Agent findAgent(AgentPath agentPath){
203+
String url = platformURL.getAgentFindUrl() + "?" + "zone=" + agentPath.getZone() + "&" + "name=" + agentPath.getName();
204+
HttpResponse<String> response = HttpClient.build(url)
205+
.get()
206+
.retry(httpRetryTimes)
207+
.bodyAsString();
208+
209+
if (!response.hasSuccess()) {
210+
throw new HttpException("Unable to delete agent");
211+
}
212+
213+
Agent agent = Agent.parse(response.getBody(), Agent.class);
214+
215+
if (agent.getStatus() == AgentStatus.BUSY){
216+
throw new IllegalStatusException("agent is busy, please wait");
217+
}
218+
219+
return agent;
220+
}
221+
182222
}

platform-api/src/main/java/com/flow/platform/api/util/PlatformURL.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class PlatformURL {
4040

4141
private final String cmdDownloadLogUrl;
4242

43+
private final String agentDeleteUrl;
44+
45+
private final String agentFindUrl;
46+
4347
public PlatformURL(String baseURL) {
4448
queueUrl = HttpURL.build(baseURL).append("cmd/queue/send").toString();
4549
cmdUrl = HttpURL.build(baseURL).append("cmd/send").toString();
@@ -51,6 +55,8 @@ public PlatformURL(String baseURL) {
5155
agentUrl = HttpURL.build(baseURL).append("agents/list").toString();
5256
agentCreateUrl = HttpURL.build(baseURL).append("agents/create").toString();
5357
agentSettingsUrl = HttpURL.build(baseURL).append("agents/settings").toString();
58+
agentDeleteUrl = HttpURL.build(baseURL).append("agents/delete").toString();
59+
agentFindUrl = HttpURL.build(baseURL).append("agents/find").toString();
5460
}
5561

5662
public String getAgentCreateUrl() {
@@ -85,17 +91,26 @@ public String getSysInfoUrl() {
8591
return sysInfoUrl;
8692
}
8793

94+
public String getAgentDeleteUrl() {
95+
return agentDeleteUrl;
96+
}
97+
public String getAgentFindUrl() {
98+
return agentFindUrl;
99+
}
100+
88101
@Override
89102
public String toString() {
90103
return "PlatformURL{" +
91104
"cmdUrl='" + cmdUrl + '\'' +
92105
", queueUrl='" + queueUrl + '\'' +
93106
", agentUrl='" + agentUrl + '\'' +
94107
", agentSettingsUrl='" + agentSettingsUrl + '\'' +
108+
", agentDeleteUrl='" + agentDeleteUrl + '\'' +
109+
", agentFindUrl='" + agentFindUrl + '\'' +
95110
", sysInfoUrl='" + sysInfoUrl + '\'' +
96111
", sysIndexUrl='" + sysIndexUrl + '\'' +
97112
", agentCreateUrl='" + agentCreateUrl + '\'' +
98113
", cmdDownloadLogUrl='" + cmdDownloadLogUrl + '\'' +
99114
'}';
100115
}
101-
}
116+
}

platform-api/src/test/java/com/flow/platform/api/test/service/AgentServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.flow.platform.api.test.TestBase;
3131
import com.flow.platform.api.util.CommonUtil;
3232
import com.flow.platform.domain.Agent;
33+
import com.flow.platform.domain.AgentPath;
3334
import com.flow.platform.domain.Jsonable;
3435
import com.google.common.collect.Lists;
3536
import java.util.List;

platform-control-center/src/main/java/com/flow/platform/cc/controller/AgentController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,9 @@ public void reportStatus(@RequestBody Agent agent) {
9898

9999
agentService.saveWithStatus(agent, agent.getStatus());
100100
}
101+
102+
@PostMapping(path = "/delete")
103+
public void delete(@RequestBody Agent agent){
104+
agentService.delete(agent);
105+
}
101106
}

platform-control-center/src/main/java/com/flow/platform/cc/service/AgentService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,10 @@ public interface AgentService extends WebhookService {
9999
* @return agent setting
100100
*/
101101
AgentSettings settings(String token);
102+
103+
104+
/**
105+
* delete agent
106+
*/
107+
void delete(Agent agent);
102108
}

platform-control-center/src/main/java/com/flow/platform/cc/service/AgentServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.flow.platform.util.Logger;
3939
import com.google.common.base.Strings;
4040
import com.google.gson.annotations.Expose;
41+
import java.sql.SQLDataException;
4142
import java.time.ZonedDateTime;
4243
import java.time.temporal.ChronoUnit;
4344
import java.util.Collection;
@@ -235,4 +236,14 @@ public AgentSettings settings(String token) {
235236
agentSettings.setAgentPath(agent.getPath());
236237
return agentSettings;
237238
}
239+
240+
@Override
241+
public void delete(Agent agent){
242+
try {
243+
agentDao.delete(agent);
244+
} catch (Throwable e){
245+
throw new UnsupportedOperationException("delete agent failure " + e.getMessage());
246+
}
247+
248+
}
238249
}

platform-control-center/src/test/java/com/flow/platform/cc/test/controller/AgentControllerTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,44 @@ public void should_4xx_when_create_agent_with_invalid_zone_name() throws Excepti
163163
.andExpect(status().isBadRequest())
164164
.andReturn();
165165
}
166+
167+
@Test
168+
public void should_delete_agent() throws Exception{
169+
// given:
170+
final String webhook = "http://flow.ci/agent/callback";
171+
AgentPathWithWebhook agentPath = new AgentPathWithWebhook("default", "test", webhook);
172+
173+
// when: create agent
174+
MockHttpServletRequestBuilder content = post("/agents/create")
175+
.contentType(MediaType.APPLICATION_JSON)
176+
.content(agentPath.toJson());
177+
178+
MvcResult result = this.mockMvc.perform(content)
179+
.andExpect(status().isOk())
180+
.andReturn();
181+
182+
// then: verify agent been created with token
183+
String agentJson = result.getResponse().getContentAsString();
184+
Assert.assertNotNull(agentJson);
185+
186+
Agent created = Agent.parse(agentJson, Agent.class);
187+
188+
MockHttpServletRequestBuilder content1 = post("/agents/delete")
189+
.contentType(MediaType.APPLICATION_JSON)
190+
.content(created.toJson());
191+
this.mockMvc.perform(content1);
192+
193+
194+
MvcResult result1 = this.mockMvc.perform(get("/agents/list").param(created.getZone(), created.getName()))
195+
.andDo(print())
196+
.andExpect(status().isOk())
197+
.andReturn();
198+
199+
// then:
200+
String json = result1.getResponse().getContentAsString();
201+
202+
Agent[] agentList = gsonConfig.fromJson(json, Agent[].class);
203+
Assert.assertEquals(0, agentList.length);
204+
205+
}
166206
}

0 commit comments

Comments
 (0)