-
Notifications
You must be signed in to change notification settings - Fork 879
fix: make template push a superset of template create #11299
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
Closed
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7ce08a1
fix: make template push a superset of template create
f0ssel 675feb0
add back work dir
f0ssel 06618d7
add groupacl
f0ssel 95d1a36
Add back workdir
f0ssel 7dd63a8
combine edit flags
f0ssel 4443322
fix edit
f0ssel 82b143b
unify edit and push
f0ssel 19a4dfc
make gen
f0ssel 5a2aa6a
fix test
f0ssel 4ce4ddb
make gen
f0ssel 39ed2cd
update golden
f0ssel 2ad127b
fix tests:
f0ssel f715efc
fix merge
f0ssel 6269898
add test
f0ssel d1cf919
remove test
f0ssel 6bcc688
add unset test
f0ssel a5ac9b7
Add deprecation warning
f0ssel 132efda
fix
f0ssel 9da9aed
rename functions
f0ssel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix: make template push a superset of template create
- Loading branch information
commit 7ce08a14c121425eedeb222867aa2f1c9fc1ec22
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,12 +2,15 @@ package cli | |||||
|
||||||
import ( | ||||||
"bufio" | ||||||
"errors" | ||||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"strings" | ||||||
"time" | ||||||
"unicode/utf8" | ||||||
|
||||||
"github.com/briandowns/spinner" | ||||||
"golang.org/x/xerrors" | ||||||
|
@@ -158,14 +161,20 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
var ( | ||||||
versionName string | ||||||
provisioner string | ||||||
workdir string | ||||||
variablesFile string | ||||||
commandLineVariables []string | ||||||
alwaysPrompt bool | ||||||
provisionerTags []string | ||||||
uploadFlags templateUploadFlags | ||||||
activate bool | ||||||
create bool | ||||||
|
||||||
requireActiveVersion bool | ||||||
disableEveryone bool | ||||||
defaultTTL time.Duration | ||||||
failureTTL time.Duration | ||||||
dormancyThreshold time.Duration | ||||||
dormancyAutoDeletion time.Duration | ||||||
maxTTL time.Duration | ||||||
) | ||||||
client := new(codersdk.Client) | ||||||
cmd := &clibase.Cmd{ | ||||||
|
@@ -176,7 +185,18 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
r.InitClient(client), | ||||||
), | ||||||
Handler: func(inv *clibase.Invocation) error { | ||||||
uploadFlags.setWorkdir(workdir) | ||||||
err := handleEntitlements(inv.Context(), handleEntitlementsArgs{ | ||||||
client: client, | ||||||
requireActiveVersion: requireActiveVersion, | ||||||
defaultTTL: defaultTTL, | ||||||
failureTTL: failureTTL, | ||||||
dormancyThreshold: dormancyThreshold, | ||||||
dormancyAutoDeletion: dormancyAutoDeletion, | ||||||
maxTTL: maxTTL, | ||||||
}) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
organization, err := CurrentOrganization(inv, client) | ||||||
if err != nil { | ||||||
|
@@ -188,10 +208,15 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
return err | ||||||
} | ||||||
|
||||||
if utf8.RuneCountInString(name) > 31 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
less cognitive overhead to read the if statement and comment this way |
||||||
return xerrors.Errorf("Template name must be less than 32 characters") | ||||||
} | ||||||
|
||||||
var createTemplate bool | ||||||
template, err := client.TemplateByName(inv.Context(), organization.ID, name) | ||||||
if err != nil { | ||||||
if !create { | ||||||
var apiError *codersdk.Error | ||||||
if errors.As(err, &apiError) && apiError.StatusCode() != http.StatusNotFound { | ||||||
return err | ||||||
} | ||||||
createTemplate = true | ||||||
|
@@ -268,6 +293,48 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
} | ||||||
} | ||||||
|
||||||
editTemplate := requireActiveVersion || | ||||||
disableEveryone || | ||||||
defaultTTL != 0 || | ||||||
failureTTL != 0 || | ||||||
dormancyThreshold != 0 || | ||||||
dormancyAutoDeletion != 0 || | ||||||
maxTTL != 0 | ||||||
if editTemplate { | ||||||
if defaultTTL == 0 { | ||||||
defaultTTL = time.Duration(template.DefaultTTLMillis) * time.Millisecond | ||||||
} | ||||||
if failureTTL == 0 { | ||||||
failureTTL = time.Duration(template.FailureTTLMillis) * time.Millisecond | ||||||
} | ||||||
if dormancyThreshold == 0 { | ||||||
dormancyThreshold = time.Duration(template.TimeTilDormantMillis) * time.Millisecond | ||||||
} | ||||||
if dormancyAutoDeletion == 0 { | ||||||
dormancyAutoDeletion = time.Duration(template.TimeTilDormantAutoDeleteMillis) * time.Millisecond | ||||||
} | ||||||
if maxTTL == 0 { | ||||||
maxTTL = time.Duration(template.MaxTTLMillis) * time.Millisecond | ||||||
} | ||||||
req := codersdk.UpdateTemplateMeta{ | ||||||
RequireActiveVersion: requireActiveVersion, | ||||||
DisableEveryone: disableEveryone, | ||||||
DefaultTTLMillis: defaultTTL.Milliseconds(), | ||||||
FailureTTLMillis: failureTTL.Milliseconds(), | ||||||
TimeTilDormantMillis: dormancyThreshold.Milliseconds(), | ||||||
TimeTilDormantAutoDeleteMillis: dormancyAutoDeletion.Milliseconds(), | ||||||
MaxTTLMillis: maxTTL.Milliseconds(), | ||||||
} | ||||||
|
||||||
_, err = client.UpdateTemplateMeta(inv.Context(), template.ID, req) | ||||||
if err != nil { | ||||||
return xerrors.Errorf("update template metadata: %w", err) | ||||||
} | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
_, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", pretty.Sprint(cliui.DefaultStyles.DateTimeStamp, time.Now().Format(time.Stamp))) | ||||||
return nil | ||||||
}, | ||||||
|
@@ -282,14 +349,6 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
// This is for testing! | ||||||
Hidden: true, | ||||||
}, | ||||||
{ | ||||||
Flag: "test.workdir", | ||||||
Description: "Customize the working directory.", | ||||||
Default: "", | ||||||
Value: clibase.StringOf(&workdir), | ||||||
// This is for testing! | ||||||
Hidden: true, | ||||||
}, | ||||||
{ | ||||||
Flag: "variables-file", | ||||||
Description: "Specify a file path with values for Terraform-managed variables.", | ||||||
|
@@ -327,10 +386,45 @@ func (r *RootCmd) templatePush() *clibase.Cmd { | |||||
Value: clibase.BoolOf(&activate), | ||||||
}, | ||||||
{ | ||||||
Flag: "create", | ||||||
Description: "Create the template if it does not exist.", | ||||||
Flag: "require-active-version", | ||||||
Description: "Requires workspace builds to use the active template version. This setting does not apply to template admins. This is an enterprise-only feature.", | ||||||
Value: clibase.BoolOf(&requireActiveVersion), | ||||||
Default: "false", | ||||||
Value: clibase.BoolOf(&create), | ||||||
}, | ||||||
{ | ||||||
Flag: "default-ttl", | ||||||
Description: "Specify a default TTL for workspaces created from this template. It is the default time before shutdown - workspaces created from this template default to this value. Maps to \"Default autostop\" in the UI.", | ||||||
Default: "24h", | ||||||
Value: clibase.DurationOf(&defaultTTL), | ||||||
}, | ||||||
{ | ||||||
Flag: "failure-ttl", | ||||||
Description: "Specify a failure TTL for workspaces created from this template. It is the amount of time after a failed \"start\" build before coder automatically schedules a \"stop\" build to cleanup.This licensed feature's default is 0h (off). Maps to \"Failure cleanup\"in the UI.", | ||||||
Default: "0h", | ||||||
Value: clibase.DurationOf(&failureTTL), | ||||||
}, | ||||||
{ | ||||||
Flag: "dormancy-threshold", | ||||||
Description: "Specify a duration workspaces may be inactive prior to being moved to the dormant state. This licensed feature's default is 0h (off). Maps to \"Dormancy threshold\" in the UI.", | ||||||
Default: "0h", | ||||||
Value: clibase.DurationOf(&dormancyThreshold), | ||||||
}, | ||||||
{ | ||||||
Flag: "dormancy-auto-deletion", | ||||||
Description: "Specify a duration workspaces may be in the dormant state prior to being deleted. This licensed feature's default is 0h (off). Maps to \"Dormancy Auto-Deletion\" in the UI.", | ||||||
Default: "0h", | ||||||
Value: clibase.DurationOf(&dormancyAutoDeletion), | ||||||
}, | ||||||
{ | ||||||
Flag: "max-ttl", | ||||||
Description: "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting. This is an enterprise-only feature.", | ||||||
Value: clibase.DurationOf(&maxTTL), | ||||||
}, | ||||||
{ | ||||||
Flag: "private", | ||||||
Description: "Disable the default behavior of granting template access to the 'everyone' group. " + | ||||||
"The template permissions must be updated to allow non-admin users to use this template.", | ||||||
Value: clibase.BoolOf(&disableEveryone), | ||||||
}, | ||||||
cliui.SkipPromptOption(), | ||||||
} | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: I've noticed us call this "WorkspaceActions" and "WorkspaceCleanup" in different places. E.g. here. @sreya can we standardize on one? Workspace Cleanup makes more sense to me.