Skip to content

Commit db40c29

Browse files
authored
fix: CLI do not ignore autostop (#6647)
* fix: CLI do not ignore autostop * make gen
1 parent e6d52b0 commit db40c29

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

cli/create.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,18 @@ func create() *cobra.Command {
142142
return err
143143
}
144144

145+
var ttlMillis *int64
146+
if stopAfter > 0 {
147+
ttlMillis = ptr.Ref(stopAfter.Milliseconds())
148+
} else if template.MaxTTLMillis > 0 {
149+
ttlMillis = &template.MaxTTLMillis
150+
}
151+
145152
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.Me, codersdk.CreateWorkspaceRequest{
146153
TemplateID: template.ID,
147154
Name: workspaceName,
148155
AutostartSchedule: schedSpec,
149-
TTLMillis: ptr.Ref(stopAfter.Milliseconds()),
156+
TTLMillis: ttlMillis,
150157
ParameterValues: buildParams.parameters,
151158
RichParameterValues: buildParams.richParameters,
152159
})
@@ -169,7 +176,7 @@ func create() *cobra.Command {
169176
cliflag.StringVarP(cmd.Flags(), &parameterFile, "parameter-file", "", "CODER_PARAMETER_FILE", "", "Specify a file path with parameter values.")
170177
cliflag.StringVarP(cmd.Flags(), &richParameterFile, "rich-parameter-file", "", "CODER_RICH_PARAMETER_FILE", "", "Specify a file path with values for rich parameters defined in the template.")
171178
cliflag.StringVarP(cmd.Flags(), &startAt, "start-at", "", "CODER_WORKSPACE_START_AT", "", "Specify the workspace autostart schedule. Check `coder schedule start --help` for the syntax.")
172-
cliflag.DurationVarP(cmd.Flags(), &stopAfter, "stop-after", "", "CODER_WORKSPACE_STOP_AFTER", 8*time.Hour, "Specify a duration after which the workspace should shut down (e.g. 8h).")
179+
cliflag.DurationVarP(cmd.Flags(), &stopAfter, "stop-after", "", "CODER_WORKSPACE_STOP_AFTER", 0, "Specify a duration after which the workspace should shut down (e.g. 8h).")
173180
return cmd
174181
}
175182

cli/create_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,58 @@ func TestCreate(t *testing.T) {
8484
}
8585
})
8686

87+
t.Run("InheritStopAfterFromTemplate", func(t *testing.T) {
88+
t.Parallel()
89+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
90+
user := coderdtest.CreateFirstUser(t, client)
91+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
92+
Parse: echo.ParseComplete,
93+
ProvisionApply: provisionCompleteWithAgent,
94+
ProvisionPlan: provisionCompleteWithAgent,
95+
})
96+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
97+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
98+
var defaultTTLMillis int64 = 2 * 60 * 60 * 1000 // 2 hours
99+
ctr.DefaultTTLMillis = &defaultTTLMillis
100+
})
101+
args := []string{
102+
"create",
103+
"my-workspace",
104+
"--template", template.Name,
105+
}
106+
cmd, root := clitest.New(t, args...)
107+
clitest.SetupConfig(t, client, root)
108+
doneChan := make(chan struct{})
109+
pty := ptytest.New(t)
110+
cmd.SetIn(pty.Input())
111+
cmd.SetOut(pty.Output())
112+
go func() {
113+
defer close(doneChan)
114+
err := cmd.Execute()
115+
assert.NoError(t, err)
116+
}()
117+
matches := []struct {
118+
match string
119+
write string
120+
}{
121+
{match: "compute.main"},
122+
{match: "smith (linux, i386)"},
123+
{match: "Confirm create", write: "yes"},
124+
}
125+
for _, m := range matches {
126+
pty.ExpectMatch(m.match)
127+
if len(m.write) > 0 {
128+
pty.WriteLine(m.write)
129+
}
130+
}
131+
<-doneChan
132+
133+
ws, err := client.WorkspaceByOwnerAndName(context.Background(), "testuser", "my-workspace", codersdk.WorkspaceOptions{})
134+
require.NoError(t, err, "expected workspace to be created")
135+
assert.Equal(t, ws.TemplateName, template.Name)
136+
assert.Equal(t, *ws.TTLMillis, template.DefaultTTLMillis)
137+
})
138+
87139
t.Run("CreateFromListWithSkip", func(t *testing.T) {
88140
t.Parallel()
89141
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})

cli/testdata/coder_create_--help.golden

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ Flags:
1515
Consumes $CODER_WORKSPACE_START_AT
1616
--stop-after duration Specify a duration after which the workspace
1717
should shut down (e.g. 8h).
18-
Consumes $CODER_WORKSPACE_STOP_AFTER (default
19-
8h0m0s)
18+
Consumes $CODER_WORKSPACE_STOP_AFTER
2019
-t, --template string Specify a template name.
2120
Consumes $CODER_TEMPLATE_NAME
2221
-y, --yes Bypass prompts

docs/cli/coder_create.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Specify a duration after which the workspace should shut down (e.g. 8h).
4343
| | |
4444
| --- | --- |
4545
| Consumes | <code>$CODER_WORKSPACE_STOP_AFTER</code> |
46-
| Default | <code>8h0m0s</code> |
46+
| Default | <code>0s</code> |
4747

4848
### --template, -t
4949

0 commit comments

Comments
 (0)