2
2
3
3
package com.coder.gateway
4
4
5
- import com.coder.gateway.models.TokenSource
6
- import com.coder.gateway.models.WorkspaceAgentModel
7
5
import 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
8
9
import com.coder.gateway.sdk.BaseCoderRestClient
9
10
import com.coder.gateway.sdk.CoderRestClient
10
- import com.coder.gateway.cli.ensureCLI
11
11
import com.coder.gateway.sdk.ex.AuthenticationResponseException
12
- import com.coder.gateway.util.toURL
13
12
import com.coder.gateway.sdk.v2.models.Workspace
13
+ import com.coder.gateway.sdk.v2.models.WorkspaceAgent
14
14
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
15
- import com.coder.gateway.sdk.v2.models.toAgentModels
16
15
import com.coder.gateway.services.CoderSettingsService
16
+ import com.coder.gateway.util.toURL
17
17
import com.coder.gateway.util.withPath
18
18
import com.intellij.openapi.components.service
19
19
import com.intellij.openapi.diagnostic.Logger
@@ -73,12 +73,13 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
73
73
74
74
// TODO: Show a dropdown and ask for an agent if missing.
75
75
val agent = getMatchingAgent(parameters, workspace)
76
+ val status = WorkspaceAndAgentStatus .from(workspace, agent)
76
77
77
- if (agent.agentStatus .pending()) {
78
+ if (status .pending()) {
78
79
// 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" )
82
83
}
83
84
84
85
val cli = ensureCLI(
@@ -92,7 +93,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
92
93
cli.login(client.token)
93
94
94
95
indicator.text = " Configuring Coder CLI..."
95
- cli.configSsh(client.agents(workspaces).map { it.name } )
96
+ cli.configSsh(client.agentNames() )
96
97
97
98
// TODO: Ask for these if missing. Maybe we can reuse the second
98
99
// step of the wizard? Could also be nice if we automatically used
@@ -194,49 +195,42 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
194
195
195
196
companion object {
196
197
val logger = Logger .getInstance(CoderGatewayConnectionProvider ::class .java.simpleName)
198
+ }
199
+ }
197
200
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
+ }
236
213
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" )
238
230
}
239
231
}
232
+
233
+ return agent
240
234
}
241
235
242
236
class MissingArgumentException (message : String ) : IllegalArgumentException(message)
0 commit comments