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: implement feature to support template version while creating workspace using cli#14880

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#14860
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
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
23 changes: 19 additions & 4 deletionscli/create.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,10 +22,11 @@ import (

func (r *RootCmd) create() *serpent.Command {
var (
templateName string
startAt string
stopAfter time.Duration
workspaceName string
templateName string
templateVersion string
startAt string
stopAfter time.Duration
workspaceName string

parameterFlags workspaceParameterFlags
autoUpdates string
Expand DownExpand Up@@ -202,6 +203,14 @@ func (r *RootCmd) create() *serpent.Command {
templateVersionID = template.ActiveVersionID
}

if len(templateVersion) > 0 {
version, err := client.TemplateVersionByName(inv.Context(), template.ID, templateVersion)
if err != nil {
return xerrors.Errorf("get template version by name: %w", err)
}
templateVersionID = version.ID
Comment on lines +207 to +211
Copy link
Member

Choose a reason for hiding this comment

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

This unfortunately does not accept the template version uuid. It would be better if the name or uuid worked.

We should fix this in the route handler imo:https://github.com/coder/coder/blob/main/coderd/templateversions.go#L885-L885

Not needed in this PR.

joobisb reacted with thumbs up emoji
}

// If the user specified an organization via a flag or env var, the template **must**
// be in that organization. Otherwise, we should throw an error.
orgValue, orgValueSource := orgContext.ValueSource(inv)
Expand DownExpand Up@@ -314,6 +323,12 @@ func (r *RootCmd) create() *serpent.Command {
Description: "Specify a template name.",
Value: serpent.StringOf(&templateName),
},
serpent.Option{
Flag: "template-version",
Env: "CODER_TEMPLATE_VERSION",
Description: "Specify a template version name.",
Value: serpent.StringOf(&templateVersion),
},
serpent.Option{
Flag: "start-at",
Env: "CODER_WORKSPACE_START_AT",
Expand Down
64 changes: 64 additions & 0 deletionscli/create_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,70 @@ func TestCreate(t *testing.T) {
}
})

t.Run("CreateWithSpecificTemplateVersion", 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, completeWithAgent())
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)

// Create a new version
version2 := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent(), func(ctvr *codersdk.CreateTemplateVersionRequest) {
ctvr.TemplateID = template.ID
})
Comment on lines +146 to +148
Copy link
Member

Choose a reason for hiding this comment

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

Addcoderdtest.AwaitTemplateVersionJobCompleted(t, client, version2.ID) after creating the version. Otherwise we get a race condition in some tests.

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

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

got it, I've updated the test

coderdtest.AwaitTemplateVersionJobCompleted(t, client, version2.ID)

args := []string{
"create",
"my-workspace",
"--template", template.Name,
"--template-version", version2.Name,
"--start-at", "9:30AM Mon-Fri US/Central",
"--stop-after", "8h",
"--automatic-updates", "always",
}
inv, root := clitest.New(t, args...)
clitest.SetupConfig(t, member, root)
doneChan := make(chan struct{})
pty := ptytest.New(t).Attach(inv)
go func() {
defer close(doneChan)
err := inv.Run()
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 := member.WorkspaceByOwnerAndName(context.Background(), codersdk.Me, "my-workspace", codersdk.WorkspaceOptions{})
if assert.NoError(t, err, "expected workspace to be created") {
assert.Equal(t, ws.TemplateName, template.Name)
// Check if the workspace is using the new template version
assert.Equal(t, ws.LatestBuild.TemplateVersionID, version2.ID, "expected workspace to use the specified template version")
if assert.NotNil(t, ws.AutostartSchedule) {
assert.Equal(t, *ws.AutostartSchedule, "CRON_TZ=US/Central 30 9 * * Mon-Fri")
}
if assert.NotNil(t, ws.TTLMillis) {
assert.Equal(t, *ws.TTLMillis, 8*time.Hour.Milliseconds())
}
assert.Equal(t, codersdk.AutomaticUpdatesAlways, ws.AutomaticUpdates)
}
})

t.Run("InheritStopAfterFromTemplate", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
Expand Down
3 changes: 3 additions & 0 deletionscli/testdata/coder_create_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,6 +41,9 @@ OPTIONS:
-t, --template string, $CODER_TEMPLATE_NAME
Specify a template name.

--template-version string, $CODER_TEMPLATE_VERSION
Specify a template version name.

-y, --yes bool
Bypass prompts.

Expand Down
9 changes: 9 additions & 0 deletionsdocs/reference/cli/create.md
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp