Skip to content

Commit f9db23e

Browse files
committed
Impl: add support for starting&stopping a workspace
1 parent 86654c3 commit f9db23e

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## [Unreleased]
66
### Added
77
- support for displaying workspace version
8+
- support for managing the lifecycle of a workspace, i.e. start and stop
89

910
### Changed
1011
- workspace panel is now updated every 5 seconds

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

+3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package com.coder.gateway.models
22

33
import com.coder.gateway.sdk.Arch
44
import com.coder.gateway.sdk.OS
5+
import java.util.UUID
56

67
data class WorkspaceAgentModel(
8+
val workspaceID: UUID,
9+
val workspaceName: String,
710
val name: String,
811
val templateName: String,
912
val status: WorkspaceVersionStatus,

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.coder.gateway.sdk.CoderCLIManager
1717
import com.coder.gateway.sdk.CoderRestClientService
1818
import com.coder.gateway.sdk.OS
1919
import com.coder.gateway.sdk.ex.AuthenticationResponseException
20+
import com.coder.gateway.sdk.ex.WorkspaceResponseException
2021
import com.coder.gateway.sdk.getOS
2122
import com.coder.gateway.sdk.toURL
2223
import com.coder.gateway.sdk.v2.models.Workspace
@@ -171,13 +172,37 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
171172

172173
private inner class StartWorkspaceAction : AnActionButton(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.start.text"), CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.start.text"), CoderIcons.RUN) {
173174
override fun actionPerformed(p0: AnActionEvent) {
174-
TODO("Not yet implemented")
175+
if (tableOfWorkspaces.selectedObject != null) {
176+
val workspace = tableOfWorkspaces.selectedObject as WorkspaceAgentModel
177+
cs.launch {
178+
withContext(Dispatchers.IO) {
179+
try {
180+
coderClient.startWorkspace(workspace.workspaceID, workspace.workspaceName)
181+
startWorkspaceAction.isEnabled = false
182+
} catch (e: WorkspaceResponseException) {
183+
logger.warn("Could not build workspace ${workspace.name}, reason: $e")
184+
}
185+
}
186+
}
187+
}
175188
}
176189
}
177190

178191
private inner class StopWorkspaceAction : AnActionButton(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.stop.text"), CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.stop.text"), CoderIcons.STOP) {
179192
override fun actionPerformed(p0: AnActionEvent) {
180-
TODO("Not yet implemented")
193+
if (tableOfWorkspaces.selectedObject != null) {
194+
val workspace = tableOfWorkspaces.selectedObject as WorkspaceAgentModel
195+
cs.launch {
196+
withContext(Dispatchers.IO) {
197+
try {
198+
coderClient.stopWorkspace(workspace.workspaceID, workspace.workspaceName)
199+
stopWorkspaceAction.isEnabled = false
200+
} catch (e: WorkspaceResponseException) {
201+
logger.warn("Could not stop workspace ${workspace.name}, reason: $e")
202+
}
203+
}
204+
}
205+
}
181206
}
182207
}
183208

@@ -310,6 +335,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
310335
0 -> {
311336
listOf(
312337
WorkspaceAgentModel(
338+
this.id,
339+
this.name,
313340
this.name,
314341
this.templateName,
315342
WorkspaceVersionStatus.from(this),
@@ -324,6 +351,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
324351
1 -> {
325352
listOf(
326353
WorkspaceAgentModel(
354+
this.id,
355+
this.name,
327356
this.name,
328357
this.templateName,
329358
WorkspaceVersionStatus.from(this),
@@ -338,6 +367,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
338367
else -> agents.map { agent ->
339368
val workspaceName = "${this.name}.${agent.name}"
340369
WorkspaceAgentModel(
370+
this.id,
371+
this.name,
341372
workspaceName,
342373
this.templateName,
343374
WorkspaceVersionStatus.from(this),
@@ -353,6 +384,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
353384
logger.warn("Agent(s) for ${this.name} could not be retrieved. Reason: $e")
354385
listOf(
355386
WorkspaceAgentModel(
387+
this.id,
388+
this.name,
356389
this.name,
357390
this.templateName,
358391
WorkspaceVersionStatus.from(this),

0 commit comments

Comments
 (0)