nolint:revive // firstbuild is a control flag to turn on immutable validation
( ctx context.Context, ownerID uuid.UUID, renderer Renderer, firstBuild bool, previousValues []database.WorkspaceBuildParameter, buildValues []codersdk.WorkspaceBuildParameter, presetValues []database.TemplateVersionPresetParameter, )
| 29 | |
| 30 | //nolint:revive // firstbuild is a control flag to turn on immutable validation |
| 31 | func ResolveParameters( |
| 32 | ctx context.Context, |
| 33 | ownerID uuid.UUID, |
| 34 | renderer Renderer, |
| 35 | firstBuild bool, |
| 36 | previousValues []database.WorkspaceBuildParameter, |
| 37 | buildValues []codersdk.WorkspaceBuildParameter, |
| 38 | presetValues []database.TemplateVersionPresetParameter, |
| 39 | ) (map[string]string, error) { |
| 40 | previousValuesMap := slice.ToMapFunc(previousValues, func(p database.WorkspaceBuildParameter) (string, string) { |
| 41 | return p.Name, p.Value |
| 42 | }) |
| 43 | |
| 44 | // Start with previous |
| 45 | values := parameterValueMap(slice.ToMapFunc(previousValues, func(p database.WorkspaceBuildParameter) (string, parameterValue) { |
| 46 | return p.Name, parameterValue{Source: sourcePrevious, Value: p.Value} |
| 47 | })) |
| 48 | |
| 49 | // Add build values (overwrite previous values if they exist) |
| 50 | for _, buildValue := range buildValues { |
| 51 | values[buildValue.Name] = parameterValue{Source: sourceBuild, Value: buildValue.Value} |
| 52 | } |
| 53 | |
| 54 | // Add preset values (overwrite previous and build values if they exist) |
| 55 | for _, preset := range presetValues { |
| 56 | values[preset.Name] = parameterValue{Source: sourcePreset, Value: preset.Value} |
| 57 | } |
| 58 | |
| 59 | // originalInputValues is going to be used to detect if a user tried to change |
| 60 | // an immutable parameter after the first build. |
| 61 | // The actual input values are mutated based on attributes like mutability |
| 62 | // and ephemerality. |
| 63 | originalInputValues := make(map[string]parameterValue, len(values)) |
| 64 | for name, value := range values { |
| 65 | // Store the original values for later use. |
| 66 | originalInputValues[name] = value |
| 67 | } |
| 68 | |
| 69 | // Render the parameters using the values that were supplied to the previous build. |
| 70 | // |
| 71 | // This is how the form should look to the user on their workspace settings page. |
| 72 | // This is the original form truth that our validations should initially be based on. |
| 73 | output, diags := renderer.Render(ctx, ownerID, previousValuesMap) |
| 74 | if diags.HasErrors() { |
| 75 | // Top level diagnostics should break the build. Previous values (and new) should |
| 76 | // always be valid. If there is a case where this is not true, then this has to |
| 77 | // be changed to allow the build to continue with a different set of values. |
| 78 | |
| 79 | return nil, parameterValidationError(diags) |
| 80 | } |
| 81 | |
| 82 | // The user's input now needs to be validated against the parameters. |
| 83 | // Mutability & Ephemeral parameters depend on sequential workspace builds. |
| 84 | // |
| 85 | // To enforce these, the user's input values are trimmed based on the |
| 86 | // mutability and ephemeral parameters defined in the template version. |
| 87 | for _, parameter := range output.Parameters { |
| 88 | // Ephemeral parameters should not be taken from the previous build. |