@@ -7,6 +7,7 @@ import com.coder.gateway.sdk.CoderCLIManager
77import com.coder.gateway.sdk.CoderRestClient
88import com.coder.gateway.sdk.ex.AuthenticationResponseException
99import com.coder.gateway.sdk.toURL
10+ import com.coder.gateway.sdk.v2.models.WorkspaceStatus
1011import com.coder.gateway.sdk.v2.models.toAgentModels
1112import com.coder.gateway.sdk.withPath
1213import com.coder.gateway.services.CoderSettingsState
@@ -46,19 +47,48 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
4647
4748 val (client, username) = authenticate(deploymentURL.toURL(), parameters[TOKEN ])
4849
49- // TODO: If these are missing we could launch the wizard.
50- val name = parameters[WORKSPACE ] ? : throw IllegalArgumentException (" Query parameter \" $WORKSPACE \" is missing" )
51- val agent = parameters[AGENT ] ? : throw IllegalArgumentException (" Query parameter \" $AGENT \" is missing" )
50+ // TODO: If the workspace is missing we could launch the wizard.
51+ val workspaceName = parameters[WORKSPACE ] ? : throw IllegalArgumentException (" Query parameter \" $WORKSPACE \" is missing" )
5252
5353 val workspaces = client.workspaces()
54- val agents = workspaces.flatMap { it.toAgentModels() }
55- val workspace = agents.firstOrNull { it.name == " $name .$agent " }
56- ? : throw IllegalArgumentException (" The agent $agent does not exist on the workspace $name or the workspace is off" )
54+ val workspace = workspaces.firstOrNull{ it.name == workspaceName } ? : throw IllegalArgumentException (" The workspace $workspaceName does not exist" )
55+
56+ when (workspace.latestBuild.status) {
57+ WorkspaceStatus .PENDING , WorkspaceStatus .STARTING ->
58+ // TODO: Wait for the workspace to turn on.
59+ throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please wait then try again" )
60+ WorkspaceStatus .STOPPING , WorkspaceStatus .STOPPED ,
61+ WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ,
62+ WorkspaceStatus .FAILED , ->
63+ // TODO: Turn on the workspace.
64+ throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please turn on the workspace and try again" )
65+ WorkspaceStatus .DELETING , WorkspaceStatus .DELETED , ->
66+ throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; unable to connect" )
67+ WorkspaceStatus .RUNNING -> Unit // All is well
68+ }
69+
70+ val agents = workspace.toAgentModels()
71+ if (agents.isEmpty()) {
72+ throw IllegalArgumentException (" The workspace \" $workspaceName \" has no agents" )
73+ }
74+
75+ // If the agent is missing and the workspace has only one, use that.
76+ val agent = if (! parameters[AGENT ].isNullOrBlank())
77+ agents.firstOrNull { it.name == " $workspaceName .${parameters[AGENT ]} " }
78+ else if (agents.size == 1 ) agents.first()
79+ else null
5780
58- // TODO: Turn on the workspace if it is off then wait for the agent
59- // to be ready. Also, distinguish between whether the
60- // workspace is off or the agent does not exist in the error
61- // above instead of showing a combined error.
81+ if (agent == null ) {
82+ // TODO: Show a dropdown and ask for an agent.
83+ throw IllegalArgumentException (" Query parameter \" $AGENT \" is missing" )
84+ }
85+
86+ if (agent.agentStatus.pending()) {
87+ // TODO: Wait for the agent to be ready.
88+ throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${agent.agentStatus.toString().lowercase()} ; please wait then try again" )
89+ } else if (! agent.agentStatus.ready()) {
90+ throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${agent.agentStatus.toString().lowercase()} ; unable to connect" )
91+ }
6292
6393 val cli = CoderCLIManager .ensureCLI(
6494 deploymentURL.toURL(),
@@ -71,7 +101,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
71101 cli.login(client.token)
72102
73103 indicator.text = " Configuring Coder CLI..."
74- cli.configSsh(agents )
104+ cli.configSsh(workspaces.flatMap { it.toAgentModels() } )
75105
76106 // TODO: Ask for these if missing. Maybe we can reuse the second
77107 // step of the wizard? Could also be nice if we automatically used
@@ -90,11 +120,11 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
90120 val folder = parameters[FOLDER ] ? : throw IllegalArgumentException (" Query parameter \" $FOLDER \" is missing" )
91121
92122 parameters
93- .withWorkspaceHostname(CoderCLIManager .getHostName(deploymentURL.toURL(), workspace ))
123+ .withWorkspaceHostname(CoderCLIManager .getHostName(deploymentURL.toURL(), agent ))
94124 .withProjectPath(folder)
95125 .withWebTerminalLink(client.url.withPath(" /@$username /$workspace .name/terminal" ).toString())
96126 .withConfigDirectory(cli.coderConfigPath.toString())
97- .withName(name )
127+ .withName(workspaceName )
98128 }
99129 return null
100130 }
0 commit comments