Skip to content

Commit f8b0a90

Browse files
committed
delete & create agent
1 parent b06c3b3 commit f8b0a90

File tree

12 files changed

+183
-2
lines changed

12 files changed

+183
-2
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
@@ -181,4 +181,30 @@ public BooleanValue shutDown(@RequestParam String zone,
181181
public void callback(@RequestBody Agent agent) {
182182
this.dispatchEvent(new AgentStatusChangeEvent(this, agent));
183183
}
184+
185+
/**
186+
* @api {Post} /agents/delete Delete
187+
* @apiName Delete Agent
188+
* @apiGroup Agent
189+
* @apiDescription Delete agent by agentPath
190+
*
191+
* @apiParamExample {json} Request-Example:
192+
* {
193+
* zone: xxx,
194+
* name: xxx
195+
* }
196+
*
197+
* @apiSuccessExample {json} Success-Response:
198+
* HTTP/1.1 200 OK
199+
*
200+
* @apiErrorExample {json} Error-Response:
201+
* HTTP/1.1 400
202+
* {
203+
* "message": xxx
204+
* }
205+
*/
206+
@PostMapping(path = "/delete")
207+
public void delete(@RequestBody AgentPath agentPath){
208+
agentService.delete(agentPath);
209+
}
184210
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public interface AgentService {
4848
* Get agent setting by token from cc
4949
*/
5050
AgentSettings settings(String token);
51+
52+
/**
53+
* Delete agent by agentPath from cc
54+
*/
55+
void delete(AgentPath agentPath);
5156
}

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

Lines changed: 40 additions & 0 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.Jsonable;
3233
import com.flow.platform.util.CollectionUtil;
3334
import com.flow.platform.util.Logger;
@@ -168,7 +169,46 @@ public AgentSettings settings(String token) {
168169
return AgentSettings.parse(response.getBody(), AgentSettings.class);
169170
}
170171

172+
@Override
173+
public void delete(AgentPath agentPath){
174+
Agent agent = findAgent(agentPath);
175+
176+
try {
177+
HttpClient.build(platformURL.getAgentDeleteUrl())
178+
.post(agent.toJson())
179+
.withContentType(ContentType.APPLICATION_JSON)
180+
.retry(httpRetryTimes)
181+
.bodyAsString().getBody();
182+
183+
} catch (UnsupportedEncodingException e) {
184+
throw new IllegalStatusException(e.getMessage());
185+
}
186+
}
187+
171188
private String buildAgentWebhook() {
172189
return domain + "/agents/callback";
173190
}
191+
192+
/**
193+
* find agent
194+
*/
195+
private Agent findAgent(AgentPath agentPath){
196+
String url = platformURL.getAgentFindUrl() + "?" + "zone=" + agentPath.getZone() + "&" + "name=" + agentPath.getName();
197+
String res = HttpClient.build(url)
198+
.get()
199+
.retry(httpRetryTimes)
200+
.bodyAsString().getBody();
201+
202+
if (Strings.isNullOrEmpty(res)) {
203+
throw new HttpException("Unable to delete agent");
204+
}
205+
Agent agent = Agent.parse(res, Agent.class);
206+
if (agent.getStatus() == AgentStatus.BUSY){
207+
throw new IllegalStatusException("agent is busy, please wait");
208+
}
209+
210+
return agent;
211+
}
212+
213+
174214
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,9 @@ public AgentSettings settings(String token) {
224224
agentSettings.setAgentPath(agent.getPath());
225225
return agentSettings;
226226
}
227+
228+
@Override
229+
public void delete(Agent agent){
230+
agentDao.delete(agent);
231+
}
227232
}

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)