Skip to content

feat: accept immutable parameters when used first time #7000

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 2 commits into from
Apr 4, 2023
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
CLI: adjust update flow
  • Loading branch information
mtojek committed Apr 4, 2023
commit d18bb7f42da8cd6a35b01d02048f4883518a1f15
29 changes: 27 additions & 2 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"time"

"github.com/google/uuid"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -213,6 +214,7 @@ type prepWorkspaceBuildArgs struct {
NewWorkspaceName string

UpdateWorkspace bool
WorkspaceID uuid.UUID
}

type buildParameters struct {
Expand Down Expand Up @@ -340,8 +342,17 @@ PromptRichParamLoop:
}

if args.UpdateWorkspace && !templateVersionParameter.Mutable {
_, _ = fmt.Fprintln(inv.Stdout, cliui.Styles.Warn.Render(fmt.Sprintf(`Parameter %q is not mutable, so can't be customized after workspace creation.`, templateVersionParameter.Name)))
continue
// Check if the immutable parameter was used in the previous build. If so, then it isn't a fresh one
// and the user should be warned.
exists, err := workspaceBuildParameterExists(ctx, client, args.WorkspaceID, templateVersionParameter)
if err != nil {
return nil, err
}

if exists {
_, _ = fmt.Fprintln(inv.Stdout, cliui.Styles.Warn.Render(fmt.Sprintf(`Parameter %q is not mutable, so can't be customized after workspace creation.`, templateVersionParameter.Name)))
continue
}
}

parameterValue, err := getWorkspaceBuildParameterValueFromMapOrInput(inv, parameterMapFromFile, templateVersionParameter)
Expand Down Expand Up @@ -414,3 +425,17 @@ PromptRichParamLoop:
richParameters: richParameters,
}, nil
}

func workspaceBuildParameterExists(ctx context.Context, client *codersdk.Client, workspaceID uuid.UUID, templateVersionParameter codersdk.TemplateVersionParameter) (bool, error) {
lastBuildParameters, err := client.WorkspaceBuildParameters(ctx, workspaceID)
if err != nil {
return false, xerrors.Errorf("can't fetch last workspace build parameters: %w", err)
}

for _, p := range lastBuildParameters {
if p.Name == templateVersionParameter.Name {
return true, nil
}
}
return false, nil
}
4 changes: 3 additions & 1 deletion cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (r *RootCmd) update() *clibase.Cmd {
ExistingRichParams: existingRichParams,
RichParameterFile: richParameterFile,
NewWorkspaceName: workspace.Name,
UpdateWorkspace: true,

UpdateWorkspace: true,
WorkspaceID: workspace.LatestBuild.ID,
})
if err != nil {
return nil
Expand Down