Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import java.net.URL
private const val URL = "url"
private const val TOKEN = "token"
private const val WORKSPACE = "workspace"
private const val AGENT = "agent"
private const val AGENT_NAME = "agent"
private const val AGENT_ID = "agent_id"
private const val FOLDER = "folder"
private const val IDE_DOWNLOAD_LINK = "ide_download_link"
private const val IDE_PRODUCT_CODE = "ide_product_code"
Expand Down Expand Up @@ -72,14 +73,23 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
}

// If the agent is missing and the workspace has only one, use that.
val agent = if (!parameters[AGENT].isNullOrBlank())
agents.firstOrNull { it.name == "$workspaceName.${parameters[AGENT]}"}
// Prefer the ID over the name if both are set.
val agent = if (!parameters[AGENT_ID].isNullOrBlank())
agents.firstOrNull {it.agentID.toString() == parameters[AGENT_ID]}
else if (!parameters[AGENT_NAME].isNullOrBlank())
agents.firstOrNull { it.name == "$workspaceName.${parameters[AGENT_NAME]}"}
else if (agents.size == 1) agents.first()
else null

if (agent == null) {
// TODO: Show a dropdown and ask for an agent.
throw IllegalArgumentException("Query parameter \"$AGENT\" is missing")
if (parameters[AGENT_ID].isNullOrBlank() && parameters[AGENT_NAME].isNullOrBlank()) {
// TODO: Show a dropdown and ask for an agent.
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")
} else if (parameters[AGENT_ID].isNullOrBlank()) {
throw IllegalArgumentException("The workspace \"$workspaceName\" does not have an agent with ID \"${parameters[AGENT_ID]}\"")
} else {
throw IllegalArgumentException("The workspace \"$workspaceName\" does not have an agent named \"${parameters[AGENT_NAME]}\"")
}
}

if (agent.agentStatus.pending()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import javax.swing.Icon
// iterate over the list we can add the workspace row if it has no agents
// otherwise iterate over the agents and then flatten the result.
data class WorkspaceAgentModel(
val agentID: UUID?,
val workspaceID: UUID,
val workspaceName: String,
val name: String, // Name of the workspace OR the agent if this is for an agent.
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/coder/gateway/sdk/v2/models/Workspace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fun Workspace.toAgentModels(): Set<WorkspaceAgentModel> {
val wam = this.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
val workspaceWithAgentName = "${this.name}.${agent.name}"
val wm = WorkspaceAgentModel(
agent.id,
this.id,
this.name,
workspaceWithAgentName,
Expand All @@ -55,6 +56,7 @@ fun Workspace.toAgentModels(): Set<WorkspaceAgentModel> {
}.toSet()
if (wam.isNullOrEmpty()) {
val wm = WorkspaceAgentModel(
null,
this.id,
this.name,
this.name,
Expand Down
1 change: 1 addition & 0 deletions src/test/groovy/DataGen.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.coder.gateway.sdk.v2.models.WorkspaceTransition
class DataGen {
static WorkspaceAgentModel workspace(String name, String workspaceName = name) {
return new WorkspaceAgentModel(
UUID.randomUUID(),
UUID.randomUUID(),
workspaceName,
name,
Expand Down