|
3 | 3 | package com.coder.gateway
|
4 | 4 |
|
5 | 5 | import com.coder.gateway.models.TokenSource
|
| 6 | +import com.coder.gateway.models.WorkspaceAgentModel |
6 | 7 | import com.coder.gateway.sdk.CoderCLIManager
|
7 | 8 | import com.coder.gateway.sdk.CoderRestClient
|
8 | 9 | import com.coder.gateway.sdk.ex.AuthenticationResponseException
|
9 | 10 | import com.coder.gateway.sdk.toURL
|
| 11 | +import com.coder.gateway.sdk.v2.models.Workspace |
10 | 12 | import com.coder.gateway.sdk.v2.models.WorkspaceStatus
|
11 | 13 | import com.coder.gateway.sdk.v2.models.toAgentModels
|
12 | 14 | import com.coder.gateway.sdk.withPath
|
@@ -67,30 +69,8 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
|
67 | 69 | WorkspaceStatus.RUNNING -> Unit // All is well
|
68 | 70 | }
|
69 | 71 |
|
70 |
| - val agents = workspace.toAgentModels() |
71 |
| - if (agents.isEmpty()) { |
72 |
| - throw IllegalArgumentException("The workspace \"$workspaceName\" has no agents") |
73 |
| - } |
74 |
| - |
75 |
| - // If the agent is missing and the workspace has only one, use that. |
76 |
| - // Prefer the ID over the name if both are set. |
77 |
| - val agent = if (!parameters[AGENT_ID].isNullOrBlank()) |
78 |
| - agents.firstOrNull {it.agentID.toString() == parameters[AGENT_ID]} |
79 |
| - else if (!parameters[AGENT_NAME].isNullOrBlank()) |
80 |
| - agents.firstOrNull { it.name == "$workspaceName.${parameters[AGENT_NAME]}"} |
81 |
| - else if (agents.size == 1) agents.first() |
82 |
| - else null |
83 |
| - |
84 |
| - if (agent == null) { |
85 |
| - if (parameters[AGENT_ID].isNullOrBlank() && parameters[AGENT_NAME].isNullOrBlank()) { |
86 |
| - // TODO: Show a dropdown and ask for an agent. |
87 |
| - throw IllegalArgumentException("Unable to determine which agent to connect to; one of \"$AGENT_NAME\" or \"$AGENT_ID\" must be set because \"$workspaceName\" has more than one agent") |
88 |
| - } else if (parameters[AGENT_ID].isNullOrBlank()) { |
89 |
| - throw IllegalArgumentException("The workspace \"$workspaceName\" does not have an agent with ID \"${parameters[AGENT_ID]}\"") |
90 |
| - } else { |
91 |
| - throw IllegalArgumentException("The workspace \"$workspaceName\" does not have an agent named \"${parameters[AGENT_NAME]}\"") |
92 |
| - } |
93 |
| - } |
| 72 | + // TODO: Show a dropdown and ask for an agent if missing. |
| 73 | + val agent = getMatchingAgent(parameters, workspace) |
94 | 74 |
|
95 | 75 | if (agent.agentStatus.pending()) {
|
96 | 76 | // TODO: Wait for the agent to be ready.
|
@@ -211,5 +191,50 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
|
211 | 191 |
|
212 | 192 | companion object {
|
213 | 193 | val logger = Logger.getInstance(CoderGatewayConnectionProvider::class.java.simpleName)
|
| 194 | + |
| 195 | + /** |
| 196 | + * Given connection parameters and a list of agents, get the matching |
| 197 | + * agent or the first agent if nothing was specified and the workspace |
| 198 | + * only has one, or throw an error if a match cannot be found. |
| 199 | + * |
| 200 | + * It is assumed the agents provided belong to a single workspace. |
| 201 | + * |
| 202 | + * @throws [MissingArgumentException, IllegalArgumentException]. |
| 203 | + */ |
| 204 | + @JvmStatic |
| 205 | + fun getMatchingAgent(parameters: Map<String, String>, workspace: Workspace): WorkspaceAgentModel { |
| 206 | + // A WorkspaceAgentModel will still be returned if there are no |
| 207 | + // agents; in this case it represents the workspace instead. |
| 208 | + // TODO: Seems confusing for something with "agent" in the name to |
| 209 | + // potentially not actually be an agent; can we replace |
| 210 | + // WorkspaceAgentModel with the original structs from the API? |
| 211 | + val agents = workspace.toAgentModels() |
| 212 | + if (agents.isEmpty() || (agents.size == 1 && agents.first().agentID == null)) { |
| 213 | + throw IllegalArgumentException("The workspace \"${workspace.name}\" has no agents") |
| 214 | + } |
| 215 | + |
| 216 | + // If the agent is missing and the workspace has only one, use that. |
| 217 | + // Prefer the ID over the name if both are set. |
| 218 | + val agent = if (!parameters[AGENT_ID].isNullOrBlank()) |
| 219 | + agents.firstOrNull { it.agentID.toString() == parameters[AGENT_ID] } |
| 220 | + else if (!parameters[AGENT_NAME].isNullOrBlank()) |
| 221 | + agents.firstOrNull { it.name == "${workspace.name}.${parameters[AGENT_NAME]}"} |
| 222 | + else if (agents.size == 1) agents.first() |
| 223 | + else null |
| 224 | + |
| 225 | + if (agent == null) { |
| 226 | + if (!parameters[AGENT_ID].isNullOrBlank()) { |
| 227 | + throw IllegalArgumentException("The workspace \"${workspace.name}\" does not have an agent with ID \"${parameters[AGENT_ID]}\"") |
| 228 | + } else if (!parameters[AGENT_NAME].isNullOrBlank()){ |
| 229 | + throw IllegalArgumentException("The workspace \"${workspace.name}\"does not have an agent named \"${parameters[AGENT_NAME]}\"") |
| 230 | + } else { |
| 231 | + 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") |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + return agent |
| 236 | + } |
214 | 237 | }
|
215 | 238 | }
|
| 239 | + |
| 240 | +class MissingArgumentException(message: String) : IllegalArgumentException(message) |
0 commit comments