Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit852b282

Browse files
committed
chore: add more unit tests for parameter resolution
1 parent0e0829b commit852b282

File tree

6 files changed

+378
-79
lines changed

6 files changed

+378
-79
lines changed

‎coderd/dynamicparameters/resolver.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
typeparameterValueSourceint
1616

1717
const (
18-
sourcePreviousparameterValueSource=iota
18+
sourceDefaultparameterValueSource=iota
19+
sourcePrevious
1920
sourceBuild
2021
sourcePreset
2122
)
@@ -36,24 +37,26 @@ func ResolveParameters(
3637
presetValues []database.TemplateVersionPresetParameter,
3738
) (map[string]string, hcl.Diagnostics) {
3839
previousValuesMap:=slice.ToMap(previousValues,func(p database.WorkspaceBuildParameter) (string,string) {
39-
returnp.Value,p.Value
40+
returnp.Name,p.Value
4041
})
4142

4243
// Start with previous
4344
values:=parameterValueMap(slice.ToMap(previousValues,func(p database.WorkspaceBuildParameter) (string,parameterValue) {
4445
returnp.Name,parameterValue{Source:sourcePrevious,Value:p.Value}
4546
}))
4647

47-
// Add build values
48+
// Add build values (overwrite previous values if they exist)
4849
for_,buildValue:=rangebuildValues {
4950
values[buildValue.Name]=parameterValue{Source:sourceBuild,Value:buildValue.Value}
5051
}
5152

52-
// Add preset values
53+
// Add preset values (overwrite previous and build values if they exist)
5354
for_,preset:=rangepresetValues {
5455
values[preset.Name]=parameterValue{Source:sourcePreset,Value:preset.Value}
5556
}
5657

58+
// originalValues is going to be used to detect if a user tried to change
59+
// an immutable parameter after the first build.
5760
originalValues:=make(map[string]parameterValue,len(values))
5861
forname,value:=rangevalues {
5962
// Store the original values for later use.
@@ -63,7 +66,7 @@ func ResolveParameters(
6366
// Render the parameters using the values that were supplied to the previous build.
6467
//
6568
// This is how the form should look to the user on their workspace settings page.
66-
// This is the original form truth that our validations should be based on going forward.
69+
// This is the original form truth that our validations shouldinitiallybe based on.
6770
output,diags:=renderer.Render(ctx,ownerID,values.ValuesMap())
6871
ifdiags.HasErrors() {
6972
// Top level diagnostics should break the build. Previous values (and new) should
@@ -81,7 +84,8 @@ func ResolveParameters(
8184
// The output parameters
8285
for_,parameter:=rangeoutput.Parameters {
8386
// Ephemeral parameters should not be taken from the previous build.
84-
// Remove their values from the input if they are sourced from the previous build.
87+
// They must always be explicitly set in every build.
88+
// So remove their values if they are sourced from the previous build.
8589
ifparameter.Ephemeral {
8690
v:=values[parameter.Name]
8791
ifv.Source==sourcePrevious {
@@ -92,6 +96,8 @@ func ResolveParameters(
9296
// Immutable parameters should also not be allowed to be changed from
9397
// the previous build. Remove any values taken from the preset or
9498
// new build params. This forces the value to be the same as it was before.
99+
//
100+
// We do this so the next form render uses the original immutable value.
95101
if!firstBuild&&!parameter.Mutable {
96102
delete(values,parameter.Name)
97103
prev,ok:=previousValuesMap[parameter.Name]
@@ -111,8 +117,15 @@ func ResolveParameters(
111117
returnnil,diags
112118
}
113119

120+
// parameterNames is going to be used to remove any excess values that were left
121+
// around without a parameter.
122+
parameterNames:=make(map[string]struct{},len(output.Parameters))
114123
for_,parameter:=rangeoutput.Parameters {
124+
parameterNames[parameter.Name]=struct{}{}
125+
115126
if!firstBuild&&!parameter.Mutable {
127+
// Immutable parameters should not be changed after the first build.
128+
// They can match the original value though!
116129
ifparameter.Value.AsString()!=originalValues[parameter.Name].Value {
117130
varsrc*hcl.Range
118131
ifparameter.Source!=nil {
@@ -135,12 +148,34 @@ func ResolveParameters(
135148
// All validation errors are raised here.
136149
diags=diags.Extend(hcl.Diagnostics(parameter.Diagnostics))
137150
}
151+
152+
// If the parameter has a value, but it was not set explicitly by the user at any
153+
// build, then save the default value. An example where this is important is if a
154+
// template has a default value of 'region = us-west-2', but the user never sets
155+
// it. If the default value changes to 'region = us-east-1', we want to preserve
156+
// the original value of 'us-west-2' for the existing workspaces.
157+
//
158+
// parameter.Value will be populated from the default at this point. So grab it
159+
// from there.
160+
if_,ok:=values[parameter.Name];!ok&&parameter.Value.IsKnown()&&parameter.Value.Valid() {
161+
values[parameter.Name]=parameterValue{
162+
Value:parameter.Value.AsString(),
163+
Source:sourceDefault,
164+
}
165+
}
166+
}
167+
168+
// Delete any values that do not belong to a parameter. This is to not save
169+
// parameter values that have no effect. These leaky parameter values can cause
170+
// problems in the future, as it makes it challenging to remove values from the
171+
// database
172+
fork:=rangevalues {
173+
if_,ok:=parameterNames[k];!ok {
174+
delete(values,k)
175+
}
138176
}
139177

140178
// Return the values to be saved for the build.
141-
// TODO: The previous code always returned parameter names and values, even if they were not set
142-
// by the user. So this should loop over the parameters and return all of them.
143-
// This catches things like if a default value changes, we keep the old value.
144179
returnvalues.ValuesMap(),diags
145180
}
146181

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp