-
Notifications
You must be signed in to change notification settings - Fork 943
feat: Allow inheriting parameters from previous template_versions when updating a template #2397
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
Changes from 1 commit
ed33a1c
1f2892c
8d62c96
aceca62
effbde7
e569f0f
2e5bf44
e5b90bc
9d17dac
04098c3
3422844
5f881aa
bcca07a
c43aedc
fa9b230
9438db8
5c590ae
f46b4af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import ( | |
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/google/uuid" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
|
@@ -83,7 +82,7 @@ func templateCreate() *cobra.Command { | |
} | ||
spin.Stop() | ||
|
||
job, parameters, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{ | ||
job, _, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{ | ||
Client: client, | ||
Organization: organization, | ||
Provisioner: database.ProvisionerType(provisioner), | ||
|
@@ -105,7 +104,6 @@ func templateCreate() *cobra.Command { | |
createReq := codersdk.CreateTemplateRequest{ | ||
Name: templateName, | ||
VersionID: job.ID, | ||
ParameterValues: parameters, | ||
MaxTTLMillis: ptr.Ref(maxTTL.Milliseconds()), | ||
MinAutostartIntervalMillis: ptr.Ref(minAutostartInterval.Milliseconds()), | ||
} | ||
|
@@ -146,21 +144,28 @@ type createValidTemplateVersionArgs struct { | |
Provisioner database.ProvisionerType | ||
FileHash string | ||
ParameterFile string | ||
// TemplateID is only required if updating a template's active version. | ||
TemplateID uuid.UUID | ||
// Template is only required if updating a template's active version. | ||
Template *codersdk.Template | ||
// ReuseParams will attempt to reuse params from the Template field | ||
// before prompting the user. Set to false to always prompt for param | ||
// values. | ||
ReuseParams bool | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) { | ||
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. yeah we're there now :D (args struct) 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. I really want to try refactoring this function. It's growing limbs and becoming quite large lol. "Get it working" was my first step... |
||
before := time.Now() | ||
client := args.Client | ||
|
||
version, err := client.CreateTemplateVersion(cmd.Context(), args.Organization.ID, codersdk.CreateTemplateVersionRequest{ | ||
req := codersdk.CreateTemplateVersionRequest{ | ||
StorageMethod: codersdk.ProvisionerStorageMethodFile, | ||
StorageSource: args.FileHash, | ||
Provisioner: codersdk.ProvisionerType(args.Provisioner), | ||
ParameterValues: parameters, | ||
TemplateID: args.TemplateID, | ||
}) | ||
} | ||
if args.Template != nil { | ||
req.TemplateID = args.Template.ID | ||
} | ||
version, err := client.CreateTemplateVersion(cmd.Context(), args.Organization.ID, req) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
@@ -197,15 +202,10 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers | |
|
||
// lastParameterValues are pulled from the current active template version if | ||
// templateID is provided. This allows pulling params from the last | ||
// version if we are updating template versions. | ||
// version instead of prompting if we are updating template versions. | ||
lastParameterValues := make(map[string]codersdk.Parameter) | ||
if args.TemplateID != uuid.Nil { | ||
template, err := client.Template(cmd.Context(), args.TemplateID) | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("Fetch template: %w", err) | ||
} | ||
|
||
activeVersion, err := client.TemplateVersion(cmd.Context(), template.ActiveVersionID) | ||
if args.ReuseParams && args.Template != nil { | ||
activeVersion, err := client.TemplateVersion(cmd.Context(), args.Template.ActiveVersionID) | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("Fetch current active template version: %w", err) | ||
} | ||
|
@@ -249,7 +249,9 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers | |
} | ||
} | ||
for _, parameterSchema := range missingSchemas { | ||
if inherit, ok := lastParameterValues[parameterSchema.Name]; ok { | ||
// If the value is in the file, skip trying to reuse the param | ||
_, fileOk := parameterMapFromFile[parameterSchema.Name] | ||
if inherit, ok := lastParameterValues[parameterSchema.Name]; ok && !fileOk { | ||
parameters = append(parameters, codersdk.CreateParameterRequest{ | ||
CopyFromParameter: inherit.ID, | ||
}) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
👍 making this opt-in
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.
I plan to add some kind of
--always-prompt
flag if you want to change the params.