Skip to content

Commit 00d7321

Browse files
committed
Implement custom environment state
This way, we can show our status labels.
1 parent d6f9709 commit 00d7321

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

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

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.coder.gateway
22

3+
import com.coder.gateway.models.WorkspaceAndAgentStatus
34
import com.coder.gateway.sdk.CoderRestClient
45
import com.coder.gateway.sdk.v2.models.Workspace
56
import com.coder.gateway.sdk.v2.models.WorkspaceAgent
6-
import com.coder.gateway.sdk.v2.models.WorkspaceAgentStatus
7-
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
87
import com.coder.gateway.views.EnvironmentView
98
import com.jetbrains.toolbox.gateway.AbstractRemoteProviderEnvironment
109
import com.jetbrains.toolbox.gateway.EnvironmentVisibilityState
1110
import com.jetbrains.toolbox.gateway.environments.EnvironmentContentsView
1211
import com.jetbrains.toolbox.gateway.states.EnvironmentStateConsumer
13-
import com.jetbrains.toolbox.gateway.states.StandardRemoteEnvironmentState
1412
import com.jetbrains.toolbox.gateway.ui.ObservablePropertiesFactory
1513
import java.util.concurrent.CompletableFuture
1614

@@ -27,28 +25,11 @@ class CoderRemoteEnvironment(
2725
) : AbstractRemoteProviderEnvironment(observablePropertiesFactory) {
2826
override fun getId(): String = "${workspace.name}.${agent.name}"
2927
override fun getName(): String = "${workspace.name}.${agent.name}"
28+
private val status = WorkspaceAndAgentStatus.from(workspace, agent)
3029

31-
// Active (and unhealthy) here indicate that the workspace is in a state
32-
// where a connection can be attempted, not that the workspace is up and
33-
// running. Once a connection is actually initiated, the CLI will then
34-
// start the workspace if it is off.
35-
private var state = when (workspace.latestBuild.status) {
36-
WorkspaceStatus.PENDING -> StandardRemoteEnvironmentState.Active
37-
WorkspaceStatus.STARTING -> StandardRemoteEnvironmentState.Active
38-
WorkspaceStatus.RUNNING -> when (agent.status) {
39-
WorkspaceAgentStatus.CONNECTED -> StandardRemoteEnvironmentState.Active
40-
WorkspaceAgentStatus.DISCONNECTED -> StandardRemoteEnvironmentState.Unreachable
41-
WorkspaceAgentStatus.TIMEOUT -> StandardRemoteEnvironmentState.Unhealthy
42-
WorkspaceAgentStatus.CONNECTING -> StandardRemoteEnvironmentState.Active
43-
}
44-
WorkspaceStatus.STOPPING -> StandardRemoteEnvironmentState.Initializing
45-
WorkspaceStatus.STOPPED -> StandardRemoteEnvironmentState.Active
46-
WorkspaceStatus.FAILED -> StandardRemoteEnvironmentState.Unhealthy
47-
WorkspaceStatus.CANCELING -> StandardRemoteEnvironmentState.Initializing
48-
WorkspaceStatus.CANCELED -> StandardRemoteEnvironmentState.Active
49-
WorkspaceStatus.DELETING -> StandardRemoteEnvironmentState.Deleting
50-
WorkspaceStatus.DELETED -> StandardRemoteEnvironmentState.Deleted
51-
}
30+
31+
// Map each state to whether a connection can be attempted.
32+
private var state = status.toRemoteEnvironmentState()
5233

5334
/**
5435
* The contents are provided by the SSH view provided by Toolbox, all we

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

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.coder.gateway.sdk.v2.models.WorkspaceAgent
55
import com.coder.gateway.sdk.v2.models.WorkspaceAgentLifecycleState
66
import com.coder.gateway.sdk.v2.models.WorkspaceAgentStatus
77
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
8+
import com.jetbrains.toolbox.gateway.states.Color
9+
import com.jetbrains.toolbox.gateway.states.CustomRemoteEnvironmentState
810

911
/**
1012
* WorkspaceAndAgentStatus represents the combined status of a single agent and
@@ -46,6 +48,30 @@ enum class WorkspaceAndAgentStatus(val label: String, val description: String) {
4648
READY("Ready", "The agent is ready to accept connections."),
4749
;
4850

51+
/**
52+
* Return the environment state for Toolbox, which tells it the label, color
53+
* and whether the environment is reachable.
54+
*
55+
* We mark all ready and pending states as reachable since if the workspace
56+
* is pending the cli will wait for it anyway.
57+
*
58+
* Additionally, terminal states like stopped are also marked as reachable,
59+
* since the cli will start them.
60+
*/
61+
fun toRemoteEnvironmentState(): CustomRemoteEnvironmentState {
62+
// Use comments; no named arguments for non-Kotlin functions.
63+
// TODO@JB: Is there a set of default colors we could use?
64+
return CustomRemoteEnvironmentState(
65+
label,
66+
Color(200, 200, 200, 200), // darkThemeColor
67+
Color(104, 112, 128, 255), // lightThemeColor
68+
Color(224, 224, 240, 26), // darkThemeBackgroundColor
69+
Color(224, 224, 245, 250), // lightThemeBackgroundColor
70+
ready() || pending() || canStart(), // reachable
71+
null, // iconId
72+
)
73+
}
74+
4975
/**
5076
* Return true if the agent is in a connectable state.
5177
*/
@@ -68,6 +94,12 @@ enum class WorkspaceAndAgentStatus(val label: String, val description: String) {
6894
.contains(this)
6995
}
7096

97+
/**
98+
* Return true if the workspace can be started.
99+
*/
100+
fun canStart(): Boolean = listOf(STOPPED, FAILED, CANCELED)
101+
.contains(this)
102+
71103
// We want to check that the workspace is `running`, the agent is
72104
// `connected`, and the agent lifecycle state is `ready` to ensure the best
73105
// possible scenario for attempting a connection.

0 commit comments

Comments
 (0)