8000 feat: Allow inheriting parameters from previous template_versions when updating a template by Emyrk · Pull Request #2397 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 18 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 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
Prev Previous commit
Next Next commit
Enable always prompting of params
  • Loading branch information
Emyrk committed Jun 16, 2022
commit e5b90bc3155e6a594636a98ab8e5039e57449b5b
36 changes: 19 additions & 17 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/google/uuid"
"github.com/spf13/cobra"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -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),
Expand All @@ -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()),
}
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

👍 making this opt-in

Copy link
Member Author

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.

10000
}

func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) {
Copy link
Member
@johnstcn johnstcn Jun 16, 2022

Choose a reason for hiding this comment

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

yeah we're there now :D (args struct)

Copy link
Member Author

Choose a reason for hiding this comment

The 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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
})
Expand Down
38 changes: 2 additions & 36 deletions cli/templateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func templateUpdate() *cobra.Command {
Provisioner: database.ProvisionerType(provisioner),
FileHash: resp.Hash,
ParameterFile: parameterFile,
TemplateID: template.ID,
Template: &template,
ReuseParams: true,
})
if err != nil {
return err
Expand All @@ -97,41 +98,6 @@ func templateUpdate() *cobra.Command {
return err
}

// templateVersion, err := client.CreateTemplateVersion(cmd.Context(), organization.ID, codersdk.CreateTemplateVersionRequest{
// TemplateID: template.ID,
// StorageMethod: codersdk.ProvisionerStorageMethodFile,
// StorageSource: resp.Hash,
// Provisioner: codersdk.ProvisionerType(provisioner),
//})
//if err != nil {
// return xerrors.Errorf("create template: %w", err)
//}
//logs, err := client.TemplateVersionLogsAfter(cmd.Context(), templateVersion.ID, before)
//if err != nil {
// return err
//}
//for {
// log, ok := <-logs
// if !ok {
// break
// }
// _, _ = fmt.Printf("%s (%s): %s\n", provisioner, log.Level, log.Output)
//}
//templateVersion, err = client.TemplateVersion(cmd.Context(), templateVersion.ID)
//if err != nil {
// return err
//}
//
//if templateVersion.Job.Status != codersdk.ProvisionerJobSucceeded {
// return xerrors.Errorf("job failed: %s", templateVersion.Job.Error)
//}
//
//err = client.UpdateActiveTemplateVersion(cmd.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
// ID: templateVersion.ID,
//})
//if err != nil {
// return err
//}
_, _ = fmt.Printf("Updated version!\n")
return nil
},
Expand Down
7 changes: 5 additions & 2 deletions coderd/database/queries.sql.go

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

7 changes: 5 additions & 2 deletions coderd/database/queries/parame E299 tervalues.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ FROM
parameter_values
WHERE
CASE
WHEN @scope :: parameter_scope != '' THEN
scope = @scope
-- We need to double cast this. First cast is for the sqlc type,
-- the second case is to convert it to text as the empty string
-- is not a valid parameter_scope enum.
WHEN (@scope :: parameter_scope) :: text != '' THEN
scope = @scope :: parameter_scope
ELSE true
END
AND CASE
Expand Down
4 changes: 2 additions & 2 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
Name: parameterValue.Name,
CreatedAt: database.Now(),
UpdatedAt: database.Now(),
Scope: database.ParameterScopeImportJob,
ScopeID: templateVersion.JobID,
Scope: database.ParameterScopeTemplate,
ScopeID: template.ID,
SourceScheme: database.ParameterSourceScheme(parameterValue.SourceScheme),
SourceValue: parameterValue.SourceValue,
DestinationScheme: database.ParameterDestinationScheme(parameterValue.DestinationScheme),
Expand Down
0