fix: CLI do not ignore autostop (#6647)

* fix: CLI do not ignore autostop

* make gen
This commit is contained in:
Marcin Tojek
2023-03-17 18:14:46 +01:00
committed by GitHub
parent e6d52b07b7
commit db40c29f26
4 changed files with 63 additions and 5 deletions

View File

@ -142,11 +142,18 @@ func create() *cobra.Command {
return err return err
} }
var ttlMillis *int64
if stopAfter > 0 {
ttlMillis = ptr.Ref(stopAfter.Milliseconds())
} else if template.MaxTTLMillis > 0 {
ttlMillis = &template.MaxTTLMillis
}
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.Me, codersdk.CreateWorkspaceRequest{ workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.Me, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID, TemplateID: template.ID,
Name: workspaceName, Name: workspaceName,
AutostartSchedule: schedSpec, AutostartSchedule: schedSpec,
TTLMillis: ptr.Ref(stopAfter.Milliseconds()), TTLMillis: ttlMillis,
ParameterValues: buildParams.parameters, ParameterValues: buildParams.parameters,
RichParameterValues: buildParams.richParameters, RichParameterValues: buildParams.richParameters,
}) })
@ -169,7 +176,7 @@ func create() *cobra.Command {
cliflag.StringVarP(cmd.Flags(), &parameterFile, "parameter-file", "", "CODER_PARAMETER_FILE", "", "Specify a file path with parameter values.") cliflag.StringVarP(cmd.Flags(), &parameterFile, "parameter-file", "", "CODER_PARAMETER_FILE", "", "Specify a file path with parameter values.")
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.") 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.")
cliflag.StringVarP(cmd.Flags(), &startAt, "start-at", "", "CODER_WORKSPACE_START_AT", "", "Specify the workspace autostart schedule. Check `coder schedule start --help` for the syntax.") cliflag.StringVarP(cmd.Flags(), &startAt, "start-at", "", "CODER_WORKSPACE_START_AT", "", "Specify the workspace autostart schedule. Check `coder schedule start --help` for the syntax.")
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).") 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).")
return cmd return cmd
} }

View File

@ -84,6 +84,58 @@ func TestCreate(t *testing.T) {
} }
}) })
t.Run("InheritStopAfterFromTemplate", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionApply: provisionCompleteWithAgent,
ProvisionPlan: provisionCompleteWithAgent,
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
var defaultTTLMillis int64 = 2 * 60 * 60 * 1000 // 2 hours
ctr.DefaultTTLMillis = &defaultTTLMillis
})
args := []string{
"create",
"my-workspace",
"--template", template.Name,
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
assert.NoError(t, err)
}()
matches := []struct {
match string
write string
}{
{match: "compute.main"},
{match: "smith (linux, i386)"},
{match: "Confirm create", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
if len(m.write) > 0 {
pty.WriteLine(m.write)
}
}
<-doneChan
ws, err := client.WorkspaceByOwnerAndName(context.Background(), "testuser", "my-workspace", codersdk.WorkspaceOptions{})
require.NoError(t, err, "expected workspace to be created")
assert.Equal(t, ws.TemplateName, template.Name)
assert.Equal(t, *ws.TTLMillis, template.DefaultTTLMillis)
})
t.Run("CreateFromListWithSkip", func(t *testing.T) { t.Run("CreateFromListWithSkip", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})

View File

@ -15,8 +15,7 @@ Flags:
Consumes $CODER_WORKSPACE_START_AT Consumes $CODER_WORKSPACE_START_AT
--stop-after duration Specify a duration after which the workspace --stop-after duration Specify a duration after which the workspace
should shut down (e.g. 8h). should shut down (e.g. 8h).
Consumes $CODER_WORKSPACE_STOP_AFTER (default Consumes $CODER_WORKSPACE_STOP_AFTER
8h0m0s)
-t, --template string Specify a template name. -t, --template string Specify a template name.
Consumes $CODER_TEMPLATE_NAME Consumes $CODER_TEMPLATE_NAME
-y, --yes Bypass prompts -y, --yes Bypass prompts

View File

@ -43,7 +43,7 @@ Specify a duration after which the workspace should shut down (e.g. 8h).
| | | | | |
| --- | --- | | --- | --- |
| Consumes | <code>$CODER_WORKSPACE_STOP_AFTER</code> | | Consumes | <code>$CODER_WORKSPACE_STOP_AFTER</code> |
| Default | <code>8h0m0s</code> | | Default | <code>0s</code> |
### --template, -t ### --template, -t