Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation(libs.retrofit.moshi)
implementation(libs.bundles.bouncycastle)
testImplementation(kotlin("test"))
testImplementation(libs.coroutines.test)
testImplementation(libs.mokk)
testImplementation(libs.bundles.toolbox.plugin.api)
}
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ toolbox-core-api = { module = "com.jetbrains.toolbox:core-api", version.ref = "t
toolbox-ui-api = { module = "com.jetbrains.toolbox:ui-api", version.ref = "toolbox-plugin-api" }
toolbox-remote-dev-api = { module = "com.jetbrains.toolbox:remote-dev-api", version.ref = "toolbox-plugin-api" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialization" }
serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" }
serialization-json-okio = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-okio", version.ref = "serialization" }
Expand Down
62 changes: 38 additions & 24 deletions src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CoderRemoteProvider(
) : RemoteProvider("Coder") {
// Current polling job.
private var pollJob: Job? = null
private val lastEnvironments = mutableSetOf<CoderRemoteEnvironment>()
internal val lastEnvironments = mutableListOf<CoderRemoteEnvironment>()

private val settings = context.settingsStore.readOnly()

Expand Down Expand Up @@ -116,29 +116,7 @@ class CoderRemoteProvider(
while (isActive) {
try {
context.logger.debug("Fetching workspace agents from ${client.url}")
val resolvedEnvironments = client.workspaces().flatMap { ws ->
// Agents are not included in workspaces that are off
// so fetch them separately.
when (ws.latestBuild.status) {
WorkspaceStatus.RUNNING -> ws.latestBuild.resources
else -> emptyList()
}.ifEmpty {
client.resources(ws)
}.flatMap { resource ->
resource.agents?.distinctBy {
// There can be duplicates with coder_agent_instance.
// TODO: Can we just choose one or do they hold
// different information?
it.name
}?.map { agent ->
lastEnvironments.firstOrNull { it.id == "${ws.name}.${agent.name}" }
?.also {
// If we have an environment already, update that.
it.update(ws, agent)
} ?: CoderRemoteEnvironment(context, client, cli, ws, agent)
} ?: emptyList()
}
}.toSet().sortedBy { it.id }
val resolvedEnvironments = resolveWorkspaceEnvironments(client, cli)

// In case we logged out while running the query.
if (!isActive) {
Expand Down Expand Up @@ -202,6 +180,42 @@ class CoderRemoteProvider(
}
}

/**
* Resolves workspace agents into remote environments.
*
* For each workspace:
* - If running, uses agents from the latest build resources
* - If not running, fetches resources separately
*
* @return a sorted list of resolved remote environments
*/
internal suspend fun resolveWorkspaceEnvironments(
client: CoderRestClient,
cli: CoderCLIManager,
): List<CoderRemoteEnvironment> {
return client.workspaces().flatMap { ws ->
// Agents are not included in workspaces that are off
// so fetch them separately.
val resources = when (ws.latestBuild.status) {
WorkspaceStatus.RUNNING -> ws.latestBuild.resources
else -> emptyList()
}.ifEmpty {
client.resources(ws)
}
resources
.flatMap { it.agents ?: emptyList() }
.distinctBy { it.name }
.map { agent ->
lastEnvironments.firstOrNull { it.id == "${ws.name}.${agent.name}" }
?.also {
// If we have an environment already, update that.
it.update(ws, agent)
} ?: CoderRemoteEnvironment(context, client, cli, ws, agent)
}

}.sortedBy { it.id }
}

/**
* Stop polling, clear the client and environments, then go back to the
* first page.
Expand Down
Loading
Loading