Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
cli.login(client.token)

indicator.text = "Configuring Coder CLI..."
cli.configSsh(workspaces.flatMap { it.toAgentModels() }, settings.headerCommand)
cli.configSsh(client.agents(workspaces), settings.headerCommand)

// TODO: Ask for these if missing. Maybe we can reuse the second
// step of the wizard? Could also be nice if we automatically used
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coder.gateway.sdk

import com.coder.gateway.models.WorkspaceAgentModel
import com.coder.gateway.sdk.convertors.InstantConverter
import com.coder.gateway.sdk.ex.AuthenticationResponseException
import com.coder.gateway.sdk.ex.TemplateResponseException
Expand All @@ -12,6 +13,7 @@ import com.coder.gateway.sdk.v2.models.User
import com.coder.gateway.sdk.v2.models.Workspace
import com.coder.gateway.sdk.v2.models.WorkspaceBuild
import com.coder.gateway.sdk.v2.models.WorkspaceTransition
import com.coder.gateway.sdk.v2.models.toAgentModels
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.intellij.ide.plugins.PluginManagerCore
Expand Down Expand Up @@ -105,6 +107,23 @@ class CoderRestClient(var url: URL, var token: String, var headerCommand: String
return workspacesResponse.body()!!.workspaces
}

/**
* Retrieves agents for the specified workspaces. Since the workspaces
* response does not include agents when the workspace is off, this fires
* off separate queries to get the agents for each workspace, just like
* `coder config-ssh` does (otherwise we risk removing hosts from the SSH
* config when they are off).
*/
fun agents(workspaces: List<Workspace>): List<WorkspaceAgentModel> {
return workspaces.flatMap {
val resourcesResponse = retroRestClient.templateVersionResources(it.latestBuild.templateVersionID).execute()
if (!resourcesResponse.isSuccessful) {
throw WorkspaceResponseException("Unable to retrieve template resources for ${it.name} from $url: code ${resourcesResponse.code()}, reason: ${resourcesResponse.message().ifBlank { "no reason provided" }}")
}
it.toAgentModels(resourcesResponse.body()!!)
}
}

fun buildInfo(): BuildInfo {
val buildInfoResponse = retroRestClient.buildInfo().execute()
if (!buildInfoResponse.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.coder.gateway.sdk.v2.models.CreateWorkspaceBuildRequest
import com.coder.gateway.sdk.v2.models.Template
import com.coder.gateway.sdk.v2.models.User
import com.coder.gateway.sdk.v2.models.WorkspaceBuild
import com.coder.gateway.sdk.v2.models.WorkspaceResource
import com.coder.gateway.sdk.v2.models.WorkspacesResponse
import retrofit2.Call
import retrofit2.http.Body
Expand Down Expand Up @@ -39,4 +40,7 @@ interface CoderV2RestFacade {

@GET("api/v2/templates/{templateID}")
fun template(@Path("templateID") templateID: UUID): Call<Template>
}

@GET("api/v2/templateversions/{templateID}/resources")
fun templateVersionResources(@Path("templateID") templateID: UUID): Call<List<WorkspaceResource>>
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/coder/gateway/sdk/v2/models/Workspace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ data class Workspace(
@SerializedName("last_used_at") val lastUsedAt: Instant,
)

fun Workspace.toAgentModels(): Set<WorkspaceAgentModel> {
val wam = this.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
fun Workspace.toAgentModels(resources: List<WorkspaceResource> = this.latestBuild.resources): Set<WorkspaceAgentModel> {
val wam = resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
val workspaceWithAgentName = "${this.name}.${agent.name}"
val wm = WorkspaceAgentModel(
this.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
poller?.cancel()

logger.info("Configuring Coder CLI...")
cli.configSsh(tableOfWorkspaces.items, settings.headerCommand)
val workspaces = clientService.client.workspaces()
cli.configSsh(clientService.client.agents(workspaces), settings.headerCommand)

// The config directory can be used to pull the URL and token in
// order to query this workspace's status in other flows, for
Expand Down