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
Next Next commit
WIP: feat: Update templates also updates parameters
  • Loading branch information
Emyrk committed Jun 14, 2022
commit ed33a1c738c29c363bc1fdea39120cf0c0dc89f1
37 changes: 29 additions & 8 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/google/uuid"

"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -82,7 +84,13 @@ func templateCreate() *cobra.Command {
}
spin.Stop()

job, parameters, err := createValidTemplateVersion(cmd, client, organization, database.ProvisionerType(provisioner), resp.Hash, parameterFile)
job, parameters, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
FileHash: resp.Hash,
ParameterFile: parameterFile,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -133,13 +141,26 @@ func templateCreate() *cobra.Command {
return cmd
}

func createValidTemplateVersion(cmd *cobra.Command, client *codersdk.Client, organization codersdk.Organization, provisioner database.ProvisionerType, hash string, parameterFile string, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) {
type createValidTemplateVersionArgs struct {
Client *codersdk.Client
Organization codersdk.Organization
Provisioner database.ProvisionerType
FileHash string
ParameterFile string
// TemplateID is only required if updating a template's active version.
TemplateID uuid.UUID
}

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()
version, err := client.CreateTemplateVersion(cmd.Context(), organization.ID, codersdk.CreateTemplateVersionRequest{
client := args.Client

version, err := client.CreateTemplateVersion(cmd.Context(), args.Organization.ID, codersdk.CreateTemplateVersionRequest{
StorageMethod: codersdk.ProvisionerStorageMethodFile,
StorageSource: hash,
Provisioner: codersdk.ProvisionerType(provisioner),
StorageSource: args.FileHash,
Provisioner: codersdk.ProvisionerType(args.Provisioner),
ParameterValues: parameters,
TemplateID: args.TemplateID,
})
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -195,9 +216,9 @@ func createValidTemplateVersion(cmd *cobra.Command, client *codersdk.Client, org

// parameterMapFromFile can be nil if parameter file is not specified
var parameterMapFromFile map[string]string
if parameterFile != "" {
if args.ParameterFile != "" {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should remove the ParameterFile in favor of a --var=asd=asd approach. Separate issue, but I think it conflates some of this logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

The file logic needs to be looked at for sure. I kept it as is for now.

_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Paragraph.Render("Attempting to read the variables from the parameter file.")+"\r\n")
parameterMapFromFile, err = createParameterMapFromFile(parameterFile)
parameterMapFromFile, err = createParameterMapFromFile(args.ParameterFile)
if err != nil {
return nil, nil, err
}
Expand All @@ -218,7 +239,7 @@ func createValidTemplateVersion(cmd *cobra.Command, client *codersdk.Client, org

// This recursion is only 1 level deep in practice.
// The first pass populates the missing parameters, so it does not enter this `if` block again.
return createValidTemplateVersion(cmd, client, organization, provisioner, hash, parameterFile, parameters...)
return createValidTemplateVersion(cmd, args, parameters...)
}

if version.Job.Status != codersdk.ProvisionerJobSucceeded {
Expand Down
86 changes: 62 additions & 24 deletions cli/templateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"os"
"time"

"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"

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

"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
Expand All @@ -16,8 +19,9 @@ import (

func templateUpdate() *cobra.Command {
var (
directory string
provisioner string
directory string
provisioner string
parameterFile string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -64,42 +68,75 @@ func templateUpdate() *cobra.Command {
}
spin.Stop()

before := time.Now()
templateVersion, err := client.CreateTemplateVersion(cmd.Context(), organization.ID, codersdk.CreateTemplateVersionRequest{
//before := time.Now()

job, parameters, err := createValidTemplateVersion(cmd, createValidTemplateVersionArgs{
Client: client,
Organization: organization,
Provisioner: database.ProvisionerType(provisioner),
FileHash: resp.Hash,
ParameterFile: parameterFile,
TemplateID: template.ID,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
StorageSource: resp.Hash,
Provisioner: codersdk.ProvisionerType(provisioner),
})
if err != nil {
return 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)

if job.Job.Status != codersdk.ProvisionerJobSucceeded {
return xerrors.Errorf("job failed: %s", job.Job.Status)
}
templateVersion, err = client.TemplateVersion(cmd.Context(), templateVersion.ID)

_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm create?",
IsConfirm: true,
})
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,
ID: job.ID,
ParameterValues: parameters,
})
if err != nil {
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 All @@ -108,6 +145,7 @@ func templateUpdate() *cobra.Command {
currentDirectory, _ := os.Getwd()
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cliui.AllowSkipPrompt(cmd)
// This is for testing!
err := cmd.Flags().MarkHidden("test.provisioner")
Expand Down
4 changes: 2 additions & 2 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
Name: parameterValue.Name,
CreatedAt: database.Now(),
UpdatedAt: database.Now(),
Scope: database.ParameterScopeTemplate,
ScopeID: dbTemplate.ID,
Scope: database.ParameterScopeImportJob,
ScopeID: templateVersion.JobID,
SourceScheme: database.ParameterSourceScheme(parameterValue.SourceScheme),
SourceValue: parameterValue.SourceValue,
DestinationScheme: database.ParameterDestinationScheme(parameterValue.DestinationScheme),
Expand Down
3 changes: 2 additions & 1 deletion codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Template struct {
}

type UpdateActiveTemplateVersion struct {
ID uuid.UUID `json:"id" validate:"required"`
ID uuid.UUID `json:"id" validate:"required"`
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
}

type UpdateTemplateMeta struct {
Expand Down