|
| 1 | +package dynamicparameters |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | + |
| 7 | +"github.com/google/uuid" |
| 8 | +"github.com/hashicorp/hcl/v2" |
| 9 | + |
| 10 | +"github.com/coder/coder/v2/coderd/database" |
| 11 | +"github.com/coder/coder/v2/coderd/util/slice" |
| 12 | +"github.com/coder/coder/v2/codersdk" |
| 13 | +) |
| 14 | + |
| 15 | +typeparameterValueSourceint |
| 16 | + |
| 17 | +const ( |
| 18 | +sourceDefaultparameterValueSource=iota |
| 19 | +sourcePrevious |
| 20 | +sourceBuild |
| 21 | +sourcePreset |
| 22 | +) |
| 23 | + |
| 24 | +typeparameterValuestruct { |
| 25 | +Valuestring |
| 26 | +SourceparameterValueSource |
| 27 | +} |
| 28 | + |
| 29 | +//nolint:revive // firstbuild is a control flag to turn on immutable validation |
| 30 | +funcResolveParameters( |
| 31 | +ctx context.Context, |
| 32 | +ownerID uuid.UUID, |
| 33 | +rendererRenderer, |
| 34 | +firstBuildbool, |
| 35 | +previousValues []database.WorkspaceBuildParameter, |
| 36 | +buildValues []codersdk.WorkspaceBuildParameter, |
| 37 | +presetValues []database.TemplateVersionPresetParameter, |
| 38 | +) (map[string]string, hcl.Diagnostics) { |
| 39 | +previousValuesMap:=slice.ToMapFunc(previousValues,func(p database.WorkspaceBuildParameter) (string,string) { |
| 40 | +returnp.Name,p.Value |
| 41 | +}) |
| 42 | + |
| 43 | +// Start with previous |
| 44 | +values:=parameterValueMap(slice.ToMapFunc(previousValues,func(p database.WorkspaceBuildParameter) (string,parameterValue) { |
| 45 | +returnp.Name,parameterValue{Source:sourcePrevious,Value:p.Value} |
| 46 | +})) |
| 47 | + |
| 48 | +// Add build values (overwrite previous values if they exist) |
| 49 | +for_,buildValue:=rangebuildValues { |
| 50 | +values[buildValue.Name]=parameterValue{Source:sourceBuild,Value:buildValue.Value} |
| 51 | +} |
| 52 | + |
| 53 | +// Add preset values (overwrite previous and build values if they exist) |
| 54 | +for_,preset:=rangepresetValues { |
| 55 | +values[preset.Name]=parameterValue{Source:sourcePreset,Value:preset.Value} |
| 56 | +} |
| 57 | + |
| 58 | +// originalValues is going to be used to detect if a user tried to change |
| 59 | +// an immutable parameter after the first build. |
| 60 | +originalValues:=make(map[string]parameterValue,len(values)) |
| 61 | +forname,value:=rangevalues { |
| 62 | +// Store the original values for later use. |
| 63 | +originalValues[name]=value |
| 64 | +} |
| 65 | + |
| 66 | +// Render the parameters using the values that were supplied to the previous build. |
| 67 | +// |
| 68 | +// This is how the form should look to the user on their workspace settings page. |
| 69 | +// This is the original form truth that our validations should initially be based on. |
| 70 | +output,diags:=renderer.Render(ctx,ownerID,values.ValuesMap()) |
| 71 | +ifdiags.HasErrors() { |
| 72 | +// Top level diagnostics should break the build. Previous values (and new) should |
| 73 | +// always be valid. If there is a case where this is not true, then this has to |
| 74 | +// be changed to allow the build to continue with a different set of values. |
| 75 | + |
| 76 | +returnnil,diags |
| 77 | +} |
| 78 | + |
| 79 | +// The user's input now needs to be validated against the parameters. |
| 80 | +// Mutability & Ephemeral parameters depend on sequential workspace builds. |
| 81 | +// |
| 82 | +// To enforce these, the user's input values are trimmed based on the |
| 83 | +// mutability and ephemeral parameters defined in the template version. |
| 84 | +for_,parameter:=rangeoutput.Parameters { |
| 85 | +// Ephemeral parameters should not be taken from the previous build. |
| 86 | +// They must always be explicitly set in every build. |
| 87 | +// So remove their values if they are sourced from the previous build. |
| 88 | +ifparameter.Ephemeral { |
| 89 | +v:=values[parameter.Name] |
| 90 | +ifv.Source==sourcePrevious { |
| 91 | +delete(values,parameter.Name) |
| 92 | +} |
| 93 | +} |
| 94 | + |
| 95 | +// Immutable parameters should also not be allowed to be changed from |
| 96 | +// the previous build. Remove any values taken from the preset or |
| 97 | +// new build params. This forces the value to be the same as it was before. |
| 98 | +// |
| 99 | +// We do this so the next form render uses the original immutable value. |
| 100 | +if!firstBuild&&!parameter.Mutable { |
| 101 | +delete(values,parameter.Name) |
| 102 | +prev,ok:=previousValuesMap[parameter.Name] |
| 103 | +ifok { |
| 104 | +values[parameter.Name]=parameterValue{ |
| 105 | +Value:prev, |
| 106 | +Source:sourcePrevious, |
| 107 | +} |
| 108 | +} |
| 109 | +} |
| 110 | +} |
| 111 | + |
| 112 | +// This is the final set of values that will be used. Any errors at this stage |
| 113 | +// are fatal. Additional validation for immutability has to be done manually. |
| 114 | +output,diags=renderer.Render(ctx,ownerID,values.ValuesMap()) |
| 115 | +ifdiags.HasErrors() { |
| 116 | +returnnil,diags |
| 117 | +} |
| 118 | + |
| 119 | +// parameterNames is going to be used to remove any excess values that were left |
| 120 | +// around without a parameter. |
| 121 | +parameterNames:=make(map[string]struct{},len(output.Parameters)) |
| 122 | +for_,parameter:=rangeoutput.Parameters { |
| 123 | +parameterNames[parameter.Name]=struct{}{} |
| 124 | + |
| 125 | +if!firstBuild&&!parameter.Mutable { |
| 126 | +// Immutable parameters should not be changed after the first build. |
| 127 | +// They can match the original value though! |
| 128 | +ifparameter.Value.AsString()!=originalValues[parameter.Name].Value { |
| 129 | +varsrc*hcl.Range |
| 130 | +ifparameter.Source!=nil { |
| 131 | +src=¶meter.Source.HCLBlock().TypeRange |
| 132 | +} |
| 133 | + |
| 134 | +// An immutable parameter was changed, which is not allowed. |
| 135 | +// Add the failed diagnostic to the output. |
| 136 | +diags=diags.Append(&hcl.Diagnostic{ |
| 137 | +Severity:hcl.DiagError, |
| 138 | +Summary:"Immutable parameter changed", |
| 139 | +Detail:fmt.Sprintf("Parameter %q is not mutable, so it can't be updated after creating a workspace.",parameter.Name), |
| 140 | +Subject:src, |
| 141 | +}) |
| 142 | +} |
| 143 | +} |
| 144 | + |
| 145 | +// TODO: Fix the `hcl.Diagnostics(...)` type casting. It should not be needed. |
| 146 | +ifhcl.Diagnostics(parameter.Diagnostics).HasErrors() { |
| 147 | +// All validation errors are raised here. |
| 148 | +diags=diags.Extend(hcl.Diagnostics(parameter.Diagnostics)) |
| 149 | +} |
| 150 | + |
| 151 | +// If the parameter has a value, but it was not set explicitly by the user at any |
| 152 | +// build, then save the default value. An example where this is important is if a |
| 153 | +// template has a default value of 'region = us-west-2', but the user never sets |
| 154 | +// it. If the default value changes to 'region = us-east-1', we want to preserve |
| 155 | +// the original value of 'us-west-2' for the existing workspaces. |
| 156 | +// |
| 157 | +// parameter.Value will be populated from the default at this point. So grab it |
| 158 | +// from there. |
| 159 | +if_,ok:=values[parameter.Name];!ok&¶meter.Value.IsKnown()&¶meter.Value.Valid() { |
| 160 | +values[parameter.Name]=parameterValue{ |
| 161 | +Value:parameter.Value.AsString(), |
| 162 | +Source:sourceDefault, |
| 163 | +} |
| 164 | +} |
| 165 | +} |
| 166 | + |
| 167 | +// Delete any values that do not belong to a parameter. This is to not save |
| 168 | +// parameter values that have no effect. These leaky parameter values can cause |
| 169 | +// problems in the future, as it makes it challenging to remove values from the |
| 170 | +// database |
| 171 | +fork:=rangevalues { |
| 172 | +if_,ok:=parameterNames[k];!ok { |
| 173 | +delete(values,k) |
| 174 | +} |
| 175 | +} |
| 176 | + |
| 177 | +// Return the values to be saved for the build. |
| 178 | +returnvalues.ValuesMap(),diags |
| 179 | +} |
| 180 | + |
| 181 | +typeparameterValueMapmap[string]parameterValue |
| 182 | + |
| 183 | +func (pparameterValueMap)ValuesMap()map[string]string { |
| 184 | +values:=make(map[string]string,len(p)) |
| 185 | +forname,paramValue:=rangep { |
| 186 | +values[name]=paramValue.Value |
| 187 | +} |
| 188 | +returnvalues |
| 189 | +} |