Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cli: allow editing template metadata (#2159)
This PR adds a CLI command template edit which allows updating the following metadata fields of a template: - Description - Max TTL - Min Autostart Interval
- Loading branch information
Showing
12 changed files
with
447 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func templateEdit() *cobra.Command { | ||
var ( | ||
description string | ||
maxTTL time.Duration | ||
minAutostartInterval time.Duration | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "edit <template> [flags]", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Edit the metadata of a template by name.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("get current organization: %w", err) | ||
} | ||
template, err := client.TemplateByName(cmd.Context(), organization.ID, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace template: %w", err) | ||
} | ||
|
||
// NOTE: coderd will ignore empty fields. | ||
req := codersdk.UpdateTemplateMeta{ | ||
Description: description, | ||
MaxTTLMillis: maxTTL.Milliseconds(), | ||
MinAutostartIntervalMillis: minAutostartInterval.Milliseconds(), | ||
} | ||
|
||
_, err = client.UpdateTemplateMeta(cmd.Context(), template.ID, req) | ||
if err != nil { | ||
return xerrors.Errorf("update template metadata: %w", err) | ||
} | ||
_, _ = fmt.Printf("Updated template metadata!\n") | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&description, "description", "", "", "Edit the template description") | ||
cmd.Flags().DurationVarP(&maxTTL, "max_ttl", "", 0, "Edit the template maximum time before shutdown") | ||
cmd.Flags().DurationVarP(&minAutostartInterval, "min_autostart_interval", "", 0, "Edit the template minimum autostart interval") | ||
cliui.AllowSkipPrompt(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cli_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/coderd/util/ptr" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func TestTemplateEdit(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Modified", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) { | ||
ctr.Description = "original description" | ||
ctr.MaxTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds()) | ||
ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds()) | ||
}) | ||
|
||
// Test the cli command. | ||
desc := "lorem ipsum dolor sit amet et cetera" | ||
maxTTL := 12 * time.Hour | ||
minAutostartInterval := time.Minute | ||
cmdArgs := []string{ | ||
"templates", | ||
"edit", | ||
template.Name, | ||
"--description", desc, | ||
"--max_ttl", maxTTL.String(), | ||
"--min_autostart_interval", minAutostartInterval.String(), | ||
} | ||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
|
||
err := cmd.Execute() | ||
|
||
require.NoError(t, err) | ||
|
||
// Assert that the template metadata changed. | ||
updated, err := client.Template(context.Background(), template.ID) | ||
require.NoError(t, err) | ||
assert.Equal(t, desc, updated.Description) | ||
assert.Equal(t, maxTTL.Milliseconds(), updated.MaxTTLMillis) | ||
assert.Equal(t, minAutostartInterval.Milliseconds(), updated.MinAutostartIntervalMillis) | ||
}) | ||
|
||
t.Run("NotModified", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) { | ||
ctr.Description = "original description" | ||
ctr.MaxTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds()) | ||
ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds()) | ||
}) | ||
|
||
// Test the cli command. | ||
cmdArgs := []string{ | ||
"templates", | ||
"edit", | ||
template.Name, | ||
"--description", template.Description, | ||
"--max_ttl", (time.Duration(template.MaxTTLMillis) * time.Millisecond).String(), | ||
"--min_autostart_interval", (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond).String(), | ||
} | ||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
|
||
err := cmd.Execute() | ||
|
||
require.ErrorContains(t, err, "not modified") | ||
|
||
// Assert that the template metadata did not change. | ||
updated, err := client.Template(context.Background(), template.ID) | ||
require.NoError(t, err) | ||
assert.Equal(t, template.Description, updated.Description) | ||
assert.Equal(t, template.MaxTTLMillis, updated.MaxTTLMillis) | ||
assert.Equal(t, template.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.