Skip to content

feat: cli: allow editing template metadata #2159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address PR comments
  • Loading branch information
johnstcn committed Jun 8, 2022
commit b71792cfc9628df7907ff81078de9a3b480fc266
28 changes: 10 additions & 18 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/spf13/cobra"
"golang.org/x/xerrors"

"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
Expand All @@ -18,42 +19,33 @@ func templateEdit() *cobra.Command {
)

cmd := &cobra.Command{
Use: "edit <template>",
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 err
return xerrors.Errorf("create client: %w", err)
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
return xerrors.Errorf("get current organization: %w", err)
}
template, err := client.TemplateByName(cmd.Context(), organization.ID, args[0])
if err != nil {
return err
return xerrors.Errorf("get workspace template: %w", err)
}

// NOTE: coderd will ignore empty fields.
req := codersdk.UpdateTemplateMeta{
Description: template.Description,
MaxTTLMillis: template.MaxTTLMillis,
MinAutostartIntervalMillis: template.MinAutostartIntervalMillis,
}

if description != "" {
req.Description = description
}
if maxTTL != 0 {
req.MaxTTLMillis = maxTTL.Milliseconds()
}
if minAutostartInterval != 0 {
req.MinAutostartIntervalMillis = minAutostartInterval.Milliseconds()
Description: description,
MaxTTLMillis: maxTTL.Milliseconds(),
MinAutostartIntervalMillis: minAutostartInterval.Milliseconds(),
}

_, err = client.UpdateTemplateMeta(cmd.Context(), template.ID, req)
if err != nil {
return err
return xerrors.Errorf("update template metadata: %w", err)
}
_, _ = fmt.Printf("Updated template metadata!\n")
return nil
Expand Down
18 changes: 17 additions & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,28 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
return
}

var validErrs []httpapi.Error
if req.MaxTTLMillis < 0 {
validErrs = append(validErrs, httpapi.Error{Field: "max_ttl_ms", Detail: "Must be a positive integer."})
}
if req.MinAutostartIntervalMillis < 0 {
validErrs = append(validErrs, httpapi.Error{Field: "min_autostart_interval_ms", Detail: "Must be a positive integer."})
}

if len(validErrs) > 0 {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "Invalid request to update template metadata!",
Validations: validErrs,
})
return
}

count := uint32(0)
var updated database.Template
err := api.Database.InTx(func(s database.Store) error {
// Fetch workspace counts
workspaceCounts, err := s.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), []uuid.UUID{template.ID})
if errors.Is(err, sql.ErrNoRows) {
if xerrors.Is(err, sql.ErrNoRows) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, should we add some linting for this? The actual xerrors.Is function is marked deprecated, and we have a lot of mixed usage in the code-base:

❯ rg xerrors.Is | wc -l
20

❯ rg '[^x]errors.Is' | wc -l
104

(Note: I'm not suggesting we fix the usage in this PR).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there are Reasons for using that instead of the original; something about stacktraces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there was a case for using Errorf and New at least, to get the stack trace. But the others are deprecated and are likely not updated in case there are any changes to stdlib. Interestingly, I now noticed Errorf was also marked deprecated in favor of fmt.Errorf even though the latter does not include a stack trace. Weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, might be a good topic for dicussion!

Copy link
Member

@Emyrk Emyrk Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri we can add a linter for it easy if we want to use xerrors.As/Is over errors.As/Is

	m.Match("errors.$f($*_)").
		Report("Use xerrors.$f to provide additional stacktrace information!")

err = nil
}
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ export interface UpdateRoles {

// From codersdk/templates.go:34:6
export interface UpdateTemplateMeta {
readonly description: string
readonly max_ttl_ms: number
readonly min_autostart_interval_ms: number
readonly description?: string
readonly max_ttl_ms?: number
readonly min_autostart_interval_ms?: number
}

// From codersdk/users.go:66:6
Expand Down