Skip to content

The Unified and Autonomous Development Platform

Update agent restful resource

Robin Shenb0d9104872ec
diff --git a/server-core/src/main/java/io/onedev/server/rest/AgentResource.java b/server-core/src/main/java/io/onedev/server/rest/AgentResource.java
index 8430f03bcb..3251e0b801 100644
--- a/server-core/src/main/java/io/onedev/server/rest/AgentResource.java
+++ b/server-core/src/main/java/io/onedev/server/rest/AgentResource.java
@@ -1,21 +1,30 @@
package io.onedev.server.rest;
+import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
import org.apache.shiro.authz.UnauthorizedException;
+import io.onedev.commons.utils.ExplicitException;
+import io.onedev.server.entitymanager.AgentAttributeManager;
import io.onedev.server.entitymanager.AgentManager;
import io.onedev.server.model.Agent;
import io.onedev.server.rest.annotation.Api;
+import io.onedev.server.rest.jersey.InvalidParamException;
+import io.onedev.server.rest.support.RestConstants;
+import io.onedev.server.search.entity.agent.AgentQuery;
import io.onedev.server.security.SecurityUtils;
@Api(order=10000)
@@ -27,9 +36,12 @@ public class AgentResource {
private final AgentManager agentManager;
+ private final AgentAttributeManager agentAttributeManager;
+
@Inject
- public AgentResource(AgentManager agentManager) {
+ public AgentResource(AgentManager agentManager, AgentAttributeManager agentAttributeManager) {
this.agentManager = agentManager;
+ this.agentAttributeManager = agentAttributeManager;
}
@Api(order=100)
@@ -50,4 +62,40 @@ public class AgentResource {
return agentManager.load(agentId).getAttributeMap();
}
+ @Api(order=300)
+ @GET
+ public List<Agent> queryBasicInfo(
+ @QueryParam("query") @Api(description="Syntax of this query is the same as query box in <a href='/administration/agents'>agent management page</a>", example="\"Name\" is \"agentName\"") String query,
+ @QueryParam("offset") @Api(example="0") int offset,
+ @QueryParam("count") @Api(example="100") int count) {
+ if (!SecurityUtils.isAdministrator())
+ throw new UnauthorizedException();
+
+ if (count > RestConstants.MAX_PAGE_SIZE)
+ throw new InvalidParamException("Count should not be greater than " + RestConstants.MAX_PAGE_SIZE);
+
+ AgentQuery parsedQuery;
+ try {
+ parsedQuery = AgentQuery.parse(query, false);
+ } catch (Exception e) {
+ throw new InvalidParamException("Error parsing query", e);
+ }
+
+ return agentManager.query(parsedQuery, offset, count);
+ }
+
+ @Api(order=400)
+ @Path("/{agentId}/attributes")
+ @POST
+ public Response updateAttributes(@PathParam("agentId") Long agentId, Map<String, String> attributes) {
+ if (!SecurityUtils.isAdministrator())
+ throw new UnauthorizedException();
+ Agent agent = agentManager.load(agentId);
+ if (!agent.isOnline())
+ throw new ExplicitException("Unable to update attributes as agent is offline");
+ agentAttributeManager.syncAttributes(agent, attributes);
+ agentManager.attributesUpdated(agent);
+ return Response.ok().build();
+ }
+
}