Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get agent API and "deleted" agents #10714

Open
ketan opened this issue Aug 19, 2022 · 2 comments
Open

Get agent API and "deleted" agents #10714

ketan opened this issue Aug 19, 2022 · 2 comments
Labels

Comments

@ketan
Copy link
Member

ketan commented Aug 19, 2022

Issue Type
  • Feature proposal
Summary

For background: I'm asking this question in the context of moving the job detail page away from velocity + jstemplate to a spark based SPA.

The get agent API does not return agents that are soft-deleted. However, I realised that the job detail page does contain agent details. On further examiation, it occured to me that the agents are simply soft-deleted from the DB.

Would it be sensible to make this agent information available via the get API? I have taken a stab at this, and am getting the following response. To me, this response seems reasonable, and does clearly indicate that the agent has been "Disabled". We could also possibly add another field, to indicate deleted, if it helps.

$ curl  -H 'Accept: application/vnd.go.cd+json' -u admin:badger http://localhost:8153/go/api/agents/8002f0dc-5f60-4767-a0c9-f6af55076a0a

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8153/go/api/agents/8002f0dc-5f60-4767-a0c9-f6af55076a0a"
    },
    "doc" : {
      "href" : "https://api.gocd.org/22.3.0/#agents"
    },
    "find" : {
      "href" : "http://localhost:8153/go/api/agents/:uuid"
    }
  },
  "uuid" : "8002f0dc-5f60-4767-a0c9-f6af55076a0a",
  "hostname" : "Ketans-MacBook-Pro",
  "ip_address" : "127.0.0.1",
  "sandbox" : "",
  "operating_system" : "",
  "agent_config_state" : "Disabled",
  "agent_state" : "Missing",
  "agent_version" : "UNKNOWN",
  "agent_bootstrapper_version" : "UNKNOWN",
  "environments" : [ ],
  "build_state" : "Unknown",
  "free_space" : "unknown",
  "resources" : [ ]
}

Here is the quick and dirty change I applied locally, to get the above response:

diff --git a/api/api-agents-v7/src/main/java/com/thoughtworks/go/apiv7/agents/AgentsControllerV7.java b/api/api-agents-v7/src/main/java/com/thoughtworks/go/apiv7/agents/AgentsControllerV7.java
index c19f479399..ed93a8663c 100644
--- a/api/api-agents-v7/src/main/java/com/thoughtworks/go/apiv7/agents/AgentsControllerV7.java
+++ b/api/api-agents-v7/src/main/java/com/thoughtworks/go/apiv7/agents/AgentsControllerV7.java
@@ -34,6 +34,7 @@ import com.thoughtworks.go.config.EnvironmentConfig;
 import com.thoughtworks.go.config.EnvironmentsConfig;
 import com.thoughtworks.go.config.exceptions.EntityType;
 import com.thoughtworks.go.config.exceptions.HttpException;
+import com.thoughtworks.go.config.exceptions.RecordNotFoundException;
 import com.thoughtworks.go.domain.AgentInstance;
 import com.thoughtworks.go.domain.NullAgentInstance;
 import com.thoughtworks.go.domain.exception.InvalidAgentInstructionException;
@@ -44,6 +45,7 @@ import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult;
 import com.thoughtworks.go.server.service.result.HttpOperationResult;
 import com.thoughtworks.go.spark.Routes;
 import com.thoughtworks.go.spark.spring.SparkSpringController;
+import com.thoughtworks.go.util.SystemEnvironment;
 import com.thoughtworks.go.util.TriState;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpStatus;
@@ -130,9 +132,21 @@ public class AgentsControllerV7 extends ApiController implements SparkSpringCont
 
     public String show(Request request, Response response) throws IOException {
         String uuid = request.params("uuid");
-        final AgentInstance agentInstance = fetchEntityFromConfig(uuid);
+        AgentInstance agentInstance = doFetchEntityFromConfig(uuid);
 
-        return writerForTopLevelObject(request, response, outputWriter -> toJSON(outputWriter, agentInstance, environmentConfigService.getAgentEnvironments(uuid), securityService, currentUsername()));
+        if (agentInstance == null) {
+            Agent agentByUUID = agentService.findAgentByUUID(uuid);
+            if (agentByUUID != null) {
+                agentInstance = AgentInstance.createFromAgent(agentByUUID, new SystemEnvironment(), null);
+            }
+        }
+
+        if (agentInstance == null) {
+            throw new RecordNotFoundException(getEntityType(), uuid);
+        }
+
+        AgentInstance finalAgentInstance = agentInstance;
+        return writerForTopLevelObject(request, response, outputWriter -> toJSON(outputWriter, finalAgentInstance, environmentConfigService.getAgentEnvironments(uuid), securityService, currentUsername()));
     }
 
     public String update(Request request, Response response) {
@chadlwilson
Copy link
Member

I wonder if this is related to why you get an error if you try to click a link off the popup to an elastic agent since removed: e.g https://build.gocd.org/go/agents/7c736314-cd8b-4b01-898f-2ca6e2817d99/job_run_history

@stale
Copy link

stale bot commented Dec 21, 2022

This issue has been automatically marked as stale because it has not had activity in the last 90 days.
If you can still reproduce this error on the master branch using local development environment or on the latest GoCD Release, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@stale stale bot added the stale label Dec 21, 2022
@chadlwilson chadlwilson added no stalebot Don't mark this stale. and removed stale labels Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants