Skip to content
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
WIP
  • Loading branch information
mtojek committed Mar 6, 2023
commit fbcbe2e98f623d8aea6cccd87f3a0cfed1a654dd
2 changes: 1 addition & 1 deletion cli/cliui/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.Templat
}

func validateRichPrompt(value string, p codersdk.TemplateVersionParameter) error {
return codersdk.ValidateWorkspaceBuildParameter(p, codersdk.WorkspaceBuildParameter{
return codersdk.ValidateWorkspaceBuildParameter(p, &codersdk.WorkspaceBuildParameter{
Name: p.Name,
Value: value,
}, nil)
Expand Down
8 changes: 7 additions & 1 deletion coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,7 @@ func TestWorkspaceWithOptionalRichParameters(t *testing.T) {
secondParameterType = "number"
secondParameterDescription = "_This_ is second *parameter*"
secondParameterRequired = true
secondParameterValue = "333"
)

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
Expand Down Expand Up @@ -1951,17 +1952,22 @@ func TestWorkspaceWithOptionalRichParameters(t *testing.T) {

expectedBuildParameters := []codersdk.WorkspaceBuildParameter{
{Name: firstParameterName, Value: firstParameterDefaultValue},
{Name: secondParameterName, Value: secondParameterValue},
}

template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
cwr.RichParameterValues = expectedBuildParameters
cwr.RichParameterValues = []codersdk.WorkspaceBuildParameter{
// First parameter is optional, so coder should pick the default value.
{Name: secondParameterName, Value: secondParameterValue},
}
})

workspaceBuild := coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
require.Equal(t, codersdk.WorkspaceStatusRunning, workspaceBuild.Status)

workspaceBuildParameters, err := client.WorkspaceBuildParameters(ctx, workspaceBuild.ID)
require.NoError(t, err)

require.ElementsMatch(t, expectedBuildParameters, workspaceBuildParameters)
}
12 changes: 9 additions & 3 deletions codersdk/richparameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ func ValidateWorkspaceBuildParameters(richParameters []TemplateVersionParameter,
continue // previous build parameters have been validated before the last build
}

err := ValidateWorkspaceBuildParameter(richParameter, *buildParameter, lastBuildParameter)
err := ValidateWorkspaceBuildParameter(richParameter, buildParameter, lastBuildParameter)
if err != nil {
return xerrors.Errorf("can't validate build parameter %q: %w", richParameter.Name, err)
}
}
return nil
}

func ValidateWorkspaceBuildParameter(richParameter TemplateVersionParameter, buildParameter WorkspaceBuildParameter, lastBuildParameter *WorkspaceBuildParameter) error {
value := buildParameter.Value
func ValidateWorkspaceBuildParameter(richParameter TemplateVersionParameter, buildParameter *WorkspaceBuildParameter, lastBuildParameter *WorkspaceBuildParameter) error {
var value string

if buildParameter != nil {
value = buildParameter.Value
}

if richParameter.Required && value == "" {
return xerrors.Errorf("parameter value is required")
}

if value == "" { // parameter is optional, so take the default value
value = richParameter.DefaultValue
}
Expand Down