Skip to content

feat(cli): add --create flag to coder templates push #8068

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

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
26fc837
ci: check for unused packages
matifali Jun 16, 2023
d125251
make fmt
matifali Jun 16, 2023
f235f7c
move to lint check
matifali Jun 16, 2023
f9b2ab2
move to Makefile
matifali Jun 16, 2023
d926d21
update sqlc
matifali Jun 16, 2023
9199745
install depcheck
matifali Jun 16, 2023
613e318
Merge branch 'main' into matifali/template-push-create
matifali Jun 16, 2023
a85a0aa
revert wrong merge
matifali Jun 16, 2023
c9220af
wip
matifali Jun 16, 2023
1e672a6
Merge branch 'main' into matifali/template-push-create
matifali Jun 16, 2023
76fe001
Discard changes to .github/workflows/ci.yaml
matifali Jun 16, 2023
eea488e
Discard changes to .github/workflows/security.yaml
matifali Jun 16, 2023
229af80
Discard changes to dogfood/Dockerfile
matifali Jun 16, 2023
72291f5
make lint
matifali Jun 16, 2023
910bfbb
make update-golden-files
matifali Jun 16, 2023
9635d82
Merge branch 'main' into matifali/template-push-create
matifali Jun 19, 2023
c99baaf
make gen
matifali Jun 19, 2023
f4e6eab
refactor
matifali Jun 19, 2023
d7272b9
more refactor
matifali Jun 19, 2023
379f670
more refactor
matifali Jun 19, 2023
3485352
better cli message
matifali Jun 19, 2023
8b31372
refactor
matifali Jun 19, 2023
c3d87e9
refactor
matifali Jun 21, 2023
66f9eb0
Merge branch 'main' into matifali/template-push-create
matifali Jun 22, 2023
4eadcf7
Merge branch 'main' into matifali/template-push-create
matifali Jun 26, 2023
c2b5c68
Apply suggestions from code review
matifali Jun 29, 2023
8794254
Merge remote-tracking branch 'origin/main' into matifali/template-pus…
matifali Jun 29, 2023
143e84d
make fmt and fix
matifali Jun 29, 2023
2fc0729
create version before template creation
matifali Jun 29, 2023
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
52 changes: 42 additions & 10 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"net/http"
"path/filepath"
"time"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisionersdk"
)
Expand Down Expand Up @@ -142,6 +144,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
provisionerTags []string
uploadFlags templateUploadFlags
activate bool
create bool
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Expand All @@ -164,24 +167,48 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return err
}

template, err := client.TemplateByName(inv.Context(), organization.ID, name)
resp, err := uploadFlags.upload(inv, client)
if err != nil {
return err
}

err = uploadFlags.checkForLockfile(inv)
if err != nil {
return xerrors.Errorf("check for lockfile: %w", err)
}

resp, err := uploadFlags.upload(inv, client)
tags, err := ParseProvisionerTags(provisionerTags)
if err != nil {
return err
}

tags, err := ParseProvisionerTags(provisionerTags)
template, err := client.TemplateByName(inv.Context(), organization.ID, name)
if err != nil {
return err
if !create {
if sdkErr, ok := codersdk.AsError(err); ok && sdkErr.StatusCode() != http.StatusNotFound {
return xerrors.Errorf("get template: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Create a new template with `coder templates create %s`.\n", name)
_, _ = fmt.Fprintf(inv.Stdout, "Or use `coder templates push %s --create`.\n", name)
return xerrors.Errorf("template %q not found: %w", name, err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Creating a new template: %s\n", name)
job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
FileID: resp.ID,
ProvisionerTags: tags,
VariablesFile: variablesFile,
Variables: variables,
})
if err != nil {
return err
}
defaultTTL := 24 * time.Hour
failureTTL := 0 * time.Hour
inactivityTTL := 0 * time.Hour
disableEveryone := false
createReq := codersdk.CreateTemplateRequest{Name: name, VersionID: job.ID, DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()), FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()), InactivityTTLMillis: ptr.Ref(inactivityTTL.Milliseconds()), DisableEveryoneGroupAccess: disableEveryone}
template, err = client.CreateTemplate(inv.Context(), organization.ID, createReq)
Copy link
Member Author

Choose a reason for hiding this comment

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

This line specifically. The object template isn't being updated as expected.
@johnstcn

Copy link
Member

Choose a reason for hiding this comment

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

Missing CreateTemplateRequest.VersionID - creating a template requires a template version to be present.

if err != nil {
return err
}
}

job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{
Expand Down Expand Up @@ -212,7 +239,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return err
}
}

_, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.DefaultStyles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
return nil
},
Expand Down Expand Up @@ -266,6 +292,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
Default: "true",
Value: clibase.BoolOf(&activate),
},
{
Flag: "create",
Description: "Create a new template if one does not already exist.",
Default: "false",
Value: clibase.BoolOf(&create),
},
cliui.SkipPromptOption(),
}
cmd.Options = append(cmd.Options, uploadFlags.options()...)
Expand Down
37 changes: 37 additions & 0 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,43 @@ func TestTemplatePush(t *testing.T) {
require.NotEqual(t, "example", templateVersions[0].Name)
})

t.Run("PushTemplateWithCreate", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
t.Cleanup(cancel)
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)

source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionApply: echo.ProvisionComplete,
})

inv, root := clitest.New(t, "templates", "push", "--create", "--directory", source, "--name", "example")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

clitest.Start(t, inv)

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
{match: "Creating a new template"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
if len(m.write) > 0 {
pty.WriteLine(m.write)
}
// Assert that the template was created.
template, err := client.TemplateByName(ctx, user.OrganizationID, "example")
require.NoError(t, err)
require.NotNil(t, template.ID)
}
})

t.Run("UseWorkingDir", func(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_templates_push_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Push a new template version from the current directory or as specified by flag
Always prompt all parameters. Does not pull parameter values from
active template version.

--create bool (default: false)
Create a new template if one does not already exist.

-d, --directory string (default: .)
Specify the directory to create from, use '-' to read tar from stdin.

Expand Down
9 changes: 9 additions & 0 deletions docs/cli/templates_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ Whether the new template will be marked active.

Always prompt all parameters. Does not pull parameter values from active template version.

### --create

| | |
| ------- | ------------------ |
| Type | <code>bool</code> |
| Default | <code>false</code> |

Create a new template if one does not already exist.

### -d, --directory

| | |
Expand Down