Skip to content

Commit 271d68c

Browse files
authored
feat: Disallow using legacy params with rich params (#5974)
* feat: Disallow using legacy params with rich params * Fix * nolint
1 parent 01ebfdc commit 271d68c

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

cli/create.go

+15
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ type buildParameters struct {
194194
// Any missing params will be prompted to the user. It supports legacy and rich parameters.
195195
func prepWorkspaceBuild(cmd *cobra.Command, client *codersdk.Client, args prepWorkspaceBuildArgs) (*buildParameters, error) {
196196
ctx := cmd.Context()
197+
198+
var useRichParameters bool
199+
if len(args.ExistingRichParams) > 0 && len(args.RichParameterFile) > 0 {
200+
useRichParameters = true
201+
}
202+
203+
var useLegacyParameters bool
204+
if len(args.ExistingParams) > 0 || len(args.ParameterFile) > 0 {
205+
useLegacyParameters = true
206+
}
207+
208+
if useRichParameters && useLegacyParameters {
209+
return nil, xerrors.Errorf("Rich parameters can't be used together with legacy parameters.")
210+
}
211+
197212
templateVersion, err := client.TemplateVersion(ctx, args.Template.ActiveVersionID)
198213
if err != nil {
199214
return nil, err

coderd/coderdtest/swaggerparser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func assertGoCommentFirst(t *testing.T, comment SwaggerComment) {
260260
text := strings.TrimSpace(line.Text)
261261

262262
if inSwaggerBlock {
263-
if !strings.HasPrefix(text, "// @") {
263+
if !strings.HasPrefix(text, "// @") && !strings.HasPrefix(text, "// nolint:") {
264264
assert.Fail(t, "Go function comment must be placed before swagger comments")
265265
return
266266
}

coderd/database/dbgen/generator.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/coder/coder/cryptorand"
13-
"github.com/tabbed/pqtype"
14-
15-
"github.com/coder/coder/coderd/database"
1612
"github.com/google/uuid"
1713
"github.com/moby/moby/pkg/namesgenerator"
1814
"github.com/stretchr/testify/require"
15+
"github.com/tabbed/pqtype"
16+
17+
"github.com/coder/coder/coderd/database"
18+
"github.com/coder/coder/cryptorand"
1919
)
2020

2121
// All methods take in a 'seed' object. Any provided fields in the seed will be

coderd/workspacebuilds.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func (api *API) workspaceBuildByBuildNumber(rw http.ResponseWriter, r *http.Requ
297297
// @Param request body codersdk.CreateWorkspaceBuildRequest true "Create workspace build request"
298298
// @Success 200 {object} codersdk.WorkspaceBuild
299299
// @Router /workspaces/{workspace}/builds [post]
300+
// nolint:gocyclo
300301
func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
301302
ctx := r.Context()
302303
apiKey := httpmw.APIKey(r)
@@ -505,23 +506,34 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
505506
}
506507
}
507508

509+
legacyParameters, err := api.Database.ParameterValues(ctx, database.ParameterValuesParams{
510+
Scopes: []database.ParameterScope{database.ParameterScopeWorkspace},
511+
ScopeIds: []uuid.UUID{workspace.ID},
512+
})
513+
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
514+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
515+
Message: "Error fetching previous legacy parameters.",
516+
Detail: err.Error(),
517+
})
518+
return
519+
}
520+
521+
if len(legacyParameters) > 0 && len(parameters) > 0 {
522+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
523+
Message: "Rich parameters can't be used together with legacy parameters.",
524+
})
525+
return
526+
}
527+
508528
var workspaceBuild database.WorkspaceBuild
509529
var provisionerJob database.ProvisionerJob
510530
// This must happen in a transaction to ensure history can be inserted, and
511531
// the prior history can update it's "after" column to point at the new.
512532
err = api.Database.InTx(func(db database.Store) error {
513-
existing, err := db.ParameterValues(ctx, database.ParameterValuesParams{
514-
Scopes: []database.ParameterScope{database.ParameterScopeWorkspace},
515-
ScopeIds: []uuid.UUID{workspace.ID},
516-
})
517-
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
518-
return xerrors.Errorf("Fetch previous parameters: %w", err)
519-
}
520-
521533
// Write/Update any new params
522534
now := database.Now()
523535
for _, param := range createBuild.ParameterValues {
524-
for _, exists := range existing {
536+
for _, exists := range legacyParameters {
525537
// If the param exists, delete the old param before inserting the new one
526538
if exists.Name == param.Name {
527539
err = db.DeleteParameterValueByID(ctx, exists.ID)

provisionerd/runner/runner.go

+5
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ func (r *Runner) runTemplateImportProvisionWithRichParameters(ctx context.Contex
728728
return nil, nil, xerrors.New(msgType.Complete.Error)
729729
}
730730

731+
if len(msgType.Complete.Parameters) > 0 && len(values) > 0 {
732+
r.logger.Info(context.Background(), "template uses rich parameters which can't be used together with legacy parameters")
733+
return nil, nil, xerrors.Errorf("invalid use of rich parameters")
734+
}
735+
731736
r.logger.Info(context.Background(), "parse dry-run provision successful",
732737
slog.F("resource_count", len(msgType.Complete.Resources)),
733738
slog.F("resources", msgType.Complete.Resources),

0 commit comments

Comments
 (0)