22
33package com.coder.gateway
44
5- import com.coder.gateway.models.TokenSource
6- import com.coder.gateway.models.WorkspaceAgentModel
75import com.coder.gateway.cli.CoderCLIManager
6+ import com.coder.gateway.cli.ensureCLI
7+ import com.coder.gateway.models.TokenSource
8+ import com.coder.gateway.models.WorkspaceAndAgentStatus
89import com.coder.gateway.sdk.BaseCoderRestClient
910import com.coder.gateway.sdk.CoderRestClient
10- import com.coder.gateway.cli.ensureCLI
1111import com.coder.gateway.sdk.ex.AuthenticationResponseException
12- import com.coder.gateway.util.toURL
1312import com.coder.gateway.sdk.v2.models.Workspace
13+ import com.coder.gateway.sdk.v2.models.WorkspaceAgent
1414import com.coder.gateway.sdk.v2.models.WorkspaceStatus
15- import com.coder.gateway.sdk.v2.models.toAgentModels
1615import com.coder.gateway.services.CoderSettingsService
16+ import com.coder.gateway.util.toURL
1717import com.coder.gateway.util.withPath
1818import com.intellij.openapi.components.service
1919import com.intellij.openapi.diagnostic.Logger
@@ -73,12 +73,13 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
7373
7474 // TODO: Show a dropdown and ask for an agent if missing.
7575 val agent = getMatchingAgent(parameters, workspace)
76+ val status = WorkspaceAndAgentStatus .from(workspace, agent)
7677
77- if (agent.agentStatus .pending()) {
78+ if (status .pending()) {
7879 // TODO: Wait for the agent to be ready.
79- throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${agent.agentStatus .toString().lowercase()} ; please wait then try again" )
80- } else if (! agent.agentStatus .ready()) {
81- throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${agent.agentStatus .toString().lowercase()} ; unable to connect" )
80+ throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${status .toString().lowercase()} ; please wait then try again" )
81+ } else if (! status .ready()) {
82+ throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${status .toString().lowercase()} ; unable to connect" )
8283 }
8384
8485 val cli = ensureCLI(
@@ -92,7 +93,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
9293 cli.login(client.token)
9394
9495 indicator.text = " Configuring Coder CLI..."
95- cli.configSsh(client.agents(workspaces).map { it.name } )
96+ cli.configSsh(client.agentNames() )
9697
9798 // TODO: Ask for these if missing. Maybe we can reuse the second
9899 // step of the wizard? Could also be nice if we automatically used
@@ -194,49 +195,42 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
194195
195196 companion object {
196197 val logger = Logger .getInstance(CoderGatewayConnectionProvider ::class .java.simpleName)
198+ }
199+ }
197200
198- /* *
199- * Return the agent matching the provided agent ID or name in the
200- * parameters. The name is ignored if the ID is set. If neither was
201- * supplied and the workspace has only one agent, return that.
202- * Otherwise throw an error.
203- *
204- * @throws [MissingArgumentException, IllegalArgumentException]
205- */
206- @JvmStatic
207- fun getMatchingAgent (parameters : Map <String , String ?>, workspace : Workspace ): WorkspaceAgentModel {
208- // A WorkspaceAgentModel will still be returned if there are no
209- // agents; in this case it represents the workspace instead.
210- // TODO: Seems confusing for something with "agent" in the name to
211- // potentially not actually be an agent; can we replace
212- // WorkspaceAgentModel with the original structs from the API?
213- val agents = workspace.toAgentModels()
214- if (agents.isEmpty() || (agents.size == 1 && agents.first().agentID == null )) {
215- throw IllegalArgumentException (" The workspace \" ${workspace.name} \" has no agents" )
216- }
217-
218- // If the agent is missing and the workspace has only one, use that.
219- // Prefer the ID over the name if both are set.
220- val agent = if (! parameters[AGENT_ID ].isNullOrBlank())
221- agents.firstOrNull { it.agentID.toString() == parameters[AGENT_ID ] }
222- else if (! parameters[AGENT_NAME ].isNullOrBlank())
223- agents.firstOrNull { it.name == " ${workspace.name} .${parameters[AGENT_NAME ]} " }
224- else if (agents.size == 1 ) agents.first()
225- else null
226-
227- if (agent == null ) {
228- if (! parameters[AGENT_ID ].isNullOrBlank()) {
229- throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent with ID \" ${parameters[AGENT_ID ]} \" " )
230- } else if (! parameters[AGENT_NAME ].isNullOrBlank()){
231- throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent named \" ${parameters[AGENT_NAME ]} \" " )
232- } else {
233- throw MissingArgumentException (" Unable to determine which agent to connect to; one of \" $AGENT_NAME \" or \" $AGENT_ID \" must be set because the workspace \" ${workspace.name} \" has more than one agent" )
234- }
235- }
201+ /* *
202+ * Return the agent matching the provided agent ID or name in the parameters.
203+ * The name is ignored if the ID is set. If neither was supplied and the
204+ * workspace has only one agent, return that. Otherwise throw an error.
205+ *
206+ * @throws [MissingArgumentException, IllegalArgumentException]
207+ */
208+ fun getMatchingAgent (parameters : Map <String , String ?>, workspace : Workspace ): WorkspaceAgent {
209+ val agents = workspace.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }
210+ if (agents.isEmpty()) {
211+ throw IllegalArgumentException (" The workspace \" ${workspace.name} \" has no agents" )
212+ }
236213
237- return agent
214+ // If the agent is missing and the workspace has only one, use that.
215+ // Prefer the ID over the name if both are set.
216+ val agent = if (! parameters[AGENT_ID ].isNullOrBlank())
217+ agents.firstOrNull { it.id.toString() == parameters[AGENT_ID ] }
218+ else if (! parameters[AGENT_NAME ].isNullOrBlank())
219+ agents.firstOrNull { it.name == parameters[AGENT_NAME ]}
220+ else if (agents.size == 1 ) agents.first()
221+ else null
222+
223+ if (agent == null ) {
224+ if (! parameters[AGENT_ID ].isNullOrBlank()) {
225+ throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent with ID \" ${parameters[AGENT_ID ]} \" " )
226+ } else if (! parameters[AGENT_NAME ].isNullOrBlank()){
227+ throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent named \" ${parameters[AGENT_NAME ]} \" " )
228+ } else {
229+ throw MissingArgumentException (" Unable to determine which agent to connect to; one of \" $AGENT_NAME \" or \" $AGENT_ID \" must be set because the workspace \" ${workspace.name} \" has more than one agent" )
238230 }
239231 }
232+
233+ return agent
240234}
241235
242236class MissingArgumentException (message : String ) : IllegalArgumentException(message)
0 commit comments