Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: allow promoting an existing template version to active from CLI#15051

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
matifali merged 3 commits intocoder:mainfromjoobisb:issue#15042
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from1 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
NextNext commit
feat: allow promoting an existing template version to active from CLI
  • Loading branch information
@joobisb
joobisb committedOct 12, 2024
commit9259e38ead9376efd8c935317466fa5bdc99a38e
64 changes: 64 additions & 0 deletionscli/templateversions.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@ func (r *RootCmd) templateVersions() *serpent.Command {
r.templateVersionsList(),
r.archiveTemplateVersion(),
r.unarchiveTemplateVersion(),
r.templateVersionsPromote(),
},
}

Expand DownExpand Up@@ -169,3 +170,66 @@ func templateVersionsToRows(activeVersionID uuid.UUID, templateVersions ...coder

return rows
}

func (r *RootCmd) templateVersionsPromote() *serpent.Command {
var (
templateName string
templateVersionName string
orgContext = NewOrganizationContext()
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "promote",
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there is precedence for required flags, can we add to theUse to include the required flags?

Suggested change
Use:"promote",
Use:"promote --template=<template_name> --template-version=<template_version_name>",

We include required params in otherUse fields:

Use:"autoupdate <workspace> <always|never>",

joobisb reacted with thumbs up emoji
Short: "Promote a template version to active.",
Long: "Promote an existing template version to be the active version for the specified template.",
Middleware: serpent.Chain(
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
organization, err := orgContext.Selected(inv, client)
if err != nil {
return err
}

template, err := client.TemplateByName(inv.Context(), organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}

version, err := client.TemplateVersionByName(inv.Context(), template.ID, templateVersionName)
if err != nil {
return xerrors.Errorf("get template version by name: %w", err)
}

err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
ID: version.ID,
})
if err != nil {
return xerrors.Errorf("update active template version: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Successfully promoted version %q to active for template %q\n", templateVersionName, templateName)
return nil
},
}

cmd.Options = serpent.OptionSet{
{
Flag: "template",
FlagShorthand: "t",
Env: "CODER_TEMPLATE_NAME",
Description: "Specify the template name.",
Required: true,
Value: serpent.StringOf(&templateName),
},
{
Flag: "template-version",
Description: "Specify the template version name to promote.",
Env: "CODER_TEMPLATE_VERSION_NAME",
Required: true,
Value: serpent.StringOf(&templateVersionName),
},
}
orgContext.AttachOptions(cmd)
return cmd
}
85 changes: 85 additions & 0 deletionscli/templateversions_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
package cli_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/pty/ptytest"
)

Expand DownExpand Up@@ -38,3 +41,85 @@ func TestTemplateVersions(t *testing.T) {
pty.ExpectMatch("Active")
})
}

func TestTemplateVersionsPromote(t *testing.T) {
t.Parallel()

t.Run("PromoteVersion", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)

// Create a template with two versions
version1 := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent())
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version1.ID)

template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version1.ID)

version2 := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent(), func(ctvr *codersdk.CreateTemplateVersionRequest) {
ctvr.TemplateID = template.ID
ctvr.Name = "2.0.0"
})
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version2.ID)

// Ensure version1 is active
updatedTemplate, err := client.Template(context.Background(), template.ID)
assert.NoError(t, err)
assert.Equal(t, version1.ID, updatedTemplate.ActiveVersionID)

args := []string{
"templates",
"versions",
"promote",
"--template", template.Name,
"--template-version", version2.Name,
}

inv, root := clitest.New(t, args...)
//nolint:gocritic // Creating a workspace for another user requires owner permissions.
clitest.SetupConfig(t, client, root)
errC := make(chan error)
go func() {
errC <- inv.Run()
}()

require.NoError(t, <-errC)

// Verify that version2 is now the active version
updatedTemplate, err = client.Template(context.Background(), template.ID)
require.NoError(t, err)
assert.Equal(t, version2.ID, updatedTemplate.ActiveVersionID)
})

t.Run("PromoteNonExistentVersion", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)

version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)

inv, root := clitest.New(t, "templates", "versions", "promote", "--template", template.Name, "--template-version", "non-existent-version")
clitest.SetupConfig(t, member, root)

err := inv.Run()
require.Error(t, err)
require.Contains(t, err.Error(), "get template version by name")
})

t.Run("PromoteVersionInvalidTemplate", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)

inv, root := clitest.New(t, "templates", "versions", "promote", "--template", "non-existent-template", "--template-version", "some-version")
clitest.SetupConfig(t, member, root)

err := inv.Run()
require.Error(t, err)
require.Contains(t, err.Error(), "get template by name")
})
}
1 change: 1 addition & 0 deletionscli/testdata/coder_templates_versions_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,7 @@ USAGE:
SUBCOMMANDS:
archive Archive a template version(s).
list List all the versions of the specified template
promote Promote a template version to active.
unarchive Unarchive a template version(s).

———
Expand Down
22 changes: 22 additions & 0 deletionscli/testdata/coder_templates_versions_promote_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
coder v0.0.0-devel

USAGE:
coder templates versions promote [flags]
Copy link
Member

Choose a reason for hiding this comment

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

I know the output[flags] technically includes my usage comment earlier. Given they are required, I wonder if we should include them more explicitly.

joobisb reacted with thumbs up emoji

Promote a template version to active.

Promote an existing template version to be the active version for the
specified template.

OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Select which organization (uuid or name) to use.

-t, --template string, $CODER_TEMPLATE_NAME
Specify the template name.

--template-version string, $CODER_TEMPLATE_VERSION_NAME
Specify the template version name to promote.

———
Run `coder --help` for a list of global options.
5 changes: 5 additions & 0 deletionsdocs/manifest.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1314,6 +1314,11 @@
"description": "List all the versions of the specified template",
"path": "reference/cli/templates_versions_list.md"
},
{
"title": "templates versions promote",
"description": "Promote a template version to active.",
"path": "reference/cli/templates_versions_promote.md"
},
{
"title": "templates versions unarchive",
"description": "Unarchive a template version(s).",
Expand Down
1 change: 1 addition & 0 deletionsdocs/reference/cli/templates_versions.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

46 changes: 46 additions & 0 deletionsdocs/reference/cli/templates_versions_promote.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.


[8]ページ先頭

©2009-2026 Movatter.jp