Skip to content

Commit c8e503e

Browse files
committed
Support agent ID parameter
1 parent 42c2851 commit c8e503e

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/main/kotlin/com/coder/gateway/CoderGatewayConnectionProvider.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import java.net.URL
2323
private const val URL = "url"
2424
private const val TOKEN = "token"
2525
private const val WORKSPACE = "workspace"
26-
private const val AGENT = "agent"
26+
private const val AGENT_NAME = "agent"
27+
private const val AGENT_ID = "agent_id"
2728
private const val FOLDER = "folder"
2829
private const val IDE_DOWNLOAD_LINK = "ide_download_link"
2930
private const val IDE_PRODUCT_CODE = "ide_product_code"
@@ -72,14 +73,23 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
7273
}
7374

7475
// If the agent is missing and the workspace has only one, use that.
75-
val agent = if (!parameters[AGENT].isNullOrBlank())
76-
agents.firstOrNull { it.name == "$workspaceName.${parameters[AGENT]}"}
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]}"}
7781
else if (agents.size == 1) agents.first()
7882
else null
7983

8084
if (agent == null) {
81-
// TODO: Show a dropdown and ask for an agent.
82-
throw IllegalArgumentException("Query parameter \"$AGENT\" is missing")
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+
}
8393
}
8494

8595
if (agent.agentStatus.pending()) {

src/main/kotlin/com/coder/gateway/models/WorkspaceAgentModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import javax.swing.Icon
1414
// iterate over the list we can add the workspace row if it has no agents
1515
// otherwise iterate over the agents and then flatten the result.
1616
data class WorkspaceAgentModel(
17+
val agentID: UUID?,
1718
val workspaceID: UUID,
1819
val workspaceName: String,
1920
val name: String, // Name of the workspace OR the agent if this is for an agent.

src/main/kotlin/com/coder/gateway/sdk/v2/models/Workspace.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fun Workspace.toAgentModels(resources: List<WorkspaceResource> = this.latestBuil
3535
val wam = resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
3636
val workspaceWithAgentName = "${this.name}.${agent.name}"
3737
val wm = WorkspaceAgentModel(
38+
agent.id,
3839
this.id,
3940
this.name,
4041
workspaceWithAgentName,
@@ -55,6 +56,7 @@ fun Workspace.toAgentModels(resources: List<WorkspaceResource> = this.latestBuil
5556
}.toSet()
5657
if (wam.isNullOrEmpty()) {
5758
val wm = WorkspaceAgentModel(
59+
null,
5860
this.id,
5961
this.name,
6062
this.name,

src/test/groovy/DataGen.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.coder.gateway.sdk.v2.models.WorkspaceTransition
77
class DataGen {
88
static WorkspaceAgentModel workspace(String name, String workspaceName = name) {
99
return new WorkspaceAgentModel(
10+
UUID.randomUUID(),
1011
UUID.randomUUID(),
1112
workspaceName,
1213
name,

0 commit comments

Comments
 (0)