Skip to content

Commit 242a426

Browse files
committed
Impl: show working and non-working workspaces
1 parent 14233ee commit 242a426

File tree

4 files changed

+87
-45
lines changed

4 files changed

+87
-45
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# coder-gateway Changelog
44

55
## [Unreleased]
6+
### Added
7+
- support for displaying working and non-working workspaces
8+
69
### Fixed
710

811
- left panel is no longer visible when a new connection is triggered from Coder's "Recent Workspaces" panel.
@@ -31,8 +34,6 @@
3134
### Added
3235
- support for Gateway 2022.2
3336

34-
35-
3637
### Changed
3738
- Java 17 is now required to run the plugin
3839
- adapted the code to the new SSH API provided by Gateway

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import com.coder.gateway.sdk.v2.models.WorkspaceBuildTransition
88
data class WorkspaceAgentModel(
99
val name: String,
1010
val templateName: String,
11-
12-
val jobStatus: ProvisionerJobStatus,
13-
val buildTransition: WorkspaceBuildTransition,
14-
11+
val agentStatus: WorkspaceAgentStatus,
1512
val agentOS: OS?,
1613
val agentArch: Arch?,
1714
val homeDirectory: String?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coder.gateway.models
2+
3+
import com.coder.gateway.sdk.v2.models.ProvisionerJobStatus
4+
import com.coder.gateway.sdk.v2.models.Workspace
5+
import com.coder.gateway.sdk.v2.models.WorkspaceBuildTransition
6+
7+
enum class WorkspaceAgentStatus(val label: String) {
8+
QUEUED("◍ Queued"), STARTING("⦿ Starting"), STOPPING("◍ Stopping"), DELETING("⦸ Deleting"),
9+
RUNNING("⦿ Running"), STOPPED("◍ Stopped"), DELETED("⦸ Deleted"),
10+
CANCELING("◍ Canceling action"), CANCELED("◍ Canceled action"), FAILED("ⓧ Failed");
11+
12+
companion object {
13+
fun from(workspace: Workspace) = when (workspace.latestBuild.job.status) {
14+
ProvisionerJobStatus.PENDING -> QUEUED
15+
ProvisionerJobStatus.RUNNING -> when (workspace.latestBuild.workspaceTransition) {
16+
WorkspaceBuildTransition.START -> STARTING
17+
WorkspaceBuildTransition.STOP -> STOPPING
18+
WorkspaceBuildTransition.DELETE -> DELETING
19+
}
20+
21+
ProvisionerJobStatus.SUCCEEDED -> when (workspace.latestBuild.workspaceTransition) {
22+
WorkspaceBuildTransition.START -> RUNNING
23+
WorkspaceBuildTransition.STOP -> STOPPED
24+
WorkspaceBuildTransition.DELETE -> DELETED
25+
}
26+
27+
ProvisionerJobStatus.CANCELING -> CANCELING
28+
ProvisionerJobStatus.CANCELED -> CANCELED
29+
ProvisionerJobStatus.FAILED -> FAILED
30+
}
31+
}
32+
}

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

+51-39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import com.coder.gateway.CoderGatewayBundle
44
import com.coder.gateway.icons.CoderIcons
55
import com.coder.gateway.models.CoderWorkspacesWizardModel
66
import com.coder.gateway.models.WorkspaceAgentModel
7+
import com.coder.gateway.models.WorkspaceAgentStatus
8+
import com.coder.gateway.models.WorkspaceAgentStatus.DELETING
9+
import com.coder.gateway.models.WorkspaceAgentStatus.RUNNING
10+
import com.coder.gateway.models.WorkspaceAgentStatus.STARTING
11+
import com.coder.gateway.models.WorkspaceAgentStatus.STOPPING
712
import com.coder.gateway.sdk.Arch
813
import com.coder.gateway.sdk.CoderCLIManager
914
import com.coder.gateway.sdk.CoderRestClientService
@@ -78,7 +83,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
7883

7984
setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
8085
selectionModel.addListSelectionListener {
81-
enableNextButtonCallback(selectedObject != null)
86+
enableNextButtonCallback(selectedObject != null && selectedObject?.agentStatus == RUNNING)
8287
}
8388
}
8489

@@ -221,26 +226,55 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
221226
}
222227

223228
private fun List<Workspace>.collectAgents(): List<WorkspaceAgentModel> {
224-
return this.flatMap { workspace ->
225-
try {
226-
val agents = coderClient.workspaceAgents(workspace)
227-
val shouldContainAgentName = agents.size > 1
228-
return@flatMap agents.map { agent ->
229-
val workspaceName = if (shouldContainAgentName) "${workspace.name}.${agent.name}" else workspace.name
229+
return this.flatMap { it.agentModels() }.toList()
230+
}
231+
232+
private fun Workspace.agentModels(): List<WorkspaceAgentModel> {
233+
return try {
234+
val agents = coderClient.workspaceAgents(this)
235+
when (agents.size) {
236+
0 -> {
237+
listOf(
238+
WorkspaceAgentModel(
239+
this.name,
240+
this.templateName,
241+
WorkspaceAgentStatus.from(this),
242+
null,
243+
null,
244+
null
245+
)
246+
)
247+
}
248+
249+
1 -> {
250+
listOf(
251+
WorkspaceAgentModel(
252+
this.name,
253+
this.templateName,
254+
WorkspaceAgentStatus.from(this),
255+
OS.from(agents[0].operatingSystem),
256+
Arch.from(agents[0].architecture),
257+
agents[0].directory
258+
)
259+
)
260+
}
261+
262+
else -> agents.map { agent ->
263+
val workspaceName = "${this.name}.${agent.name}"
230264
WorkspaceAgentModel(
231265
workspaceName,
232-
workspace.templateName,
233-
workspace.latestBuild.job.status,
234-
workspace.latestBuild.workspaceTransition,
266+
this.templateName,
267+
WorkspaceAgentStatus.from(this),
235268
OS.from(agent.operatingSystem),
236269
Arch.from(agent.architecture),
237270
agent.directory
238271
)
239272
}
240-
} catch (e: Exception) {
241-
logger.error("Skipping workspace ${workspace.name} because we could not retrieve the agent(s). Reason: $e")
242-
emptyList()
273+
.toList()
243274
}
275+
} catch (e: Exception) {
276+
logger.error("Skipping workspace ${this.name} because we could not retrieve the agent(s). Reason: $e")
277+
emptyList()
244278
}
245279
}
246280

@@ -322,7 +356,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
322356

323357
private class WorkspaceStatusColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
324358
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
325-
return workspace?.statusLabel()
359+
return workspace?.agentStatus?.label
326360
}
327361

328362
override fun getRenderer(item: WorkspaceAgentModel?): TableCellRenderer {
@@ -339,31 +373,9 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
339373
}
340374
}
341375

342-
private fun WorkspaceAgentModel.statusLabel() = when (this.jobStatus) {
343-
ProvisionerJobStatus.PENDING -> "◍ Queued"
344-
ProvisionerJobStatus.RUNNING -> when (this.buildTransition) {
345-
WorkspaceBuildTransition.START -> "⦿ Starting"
346-
WorkspaceBuildTransition.STOP -> "◍ Stopping"
347-
WorkspaceBuildTransition.DELETE -> "⦸ Deleting"
348-
}
349-
350-
ProvisionerJobStatus.SUCCEEDED -> when (this.buildTransition) {
351-
WorkspaceBuildTransition.START -> "⦿ Running"
352-
WorkspaceBuildTransition.STOP -> "◍ Stopped"
353-
WorkspaceBuildTransition.DELETE -> "⦸ Deleted"
354-
}
355-
356-
ProvisionerJobStatus.CANCELING -> "◍ Canceling action"
357-
ProvisionerJobStatus.CANCELED -> "◍ Canceled action"
358-
ProvisionerJobStatus.FAILED -> "ⓧ Failed"
359-
}
360-
361-
private fun WorkspaceAgentModel.statusColor() = when (this.jobStatus) {
362-
ProvisionerJobStatus.SUCCEEDED -> if (this.buildTransition == WorkspaceBuildTransition.START) Color.GREEN else Color.RED
363-
ProvisionerJobStatus.RUNNING -> when (this.buildTransition) {
364-
WorkspaceBuildTransition.START, WorkspaceBuildTransition.STOP, WorkspaceBuildTransition.DELETE -> Color.GRAY
365-
}
366-
376+
private fun WorkspaceAgentModel.statusColor() = when (this.agentStatus) {
377+
RUNNING -> Color.GREEN
378+
STARTING, STOPPING, DELETING -> Color.GRAY
367379
else -> Color.RED
368380
}
369381
}

0 commit comments

Comments
 (0)