Skip to content

Commit 619c833

Browse files
committed
chore: dynamic elements break preset validation
Dynamic elements are not taken into account during preset validation. We should omit preset validation if it's not accurate
1 parent b3c9e72 commit 619c833

3 files changed

Lines changed: 43 additions & 19 deletions

File tree

preset.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package preview
22

33
import (
44
"fmt"
5-
"slices"
65

76
"github.com/aquasecurity/trivy/pkg/iac/terraform"
87
"github.com/hashicorp/hcl/v2"
@@ -32,24 +31,6 @@ func presets(modules terraform.Modules, parameters []types.Parameter) []types.Pr
3231
defaultPreset = &preset
3332
}
3433

35-
for paramName, paramValue := range preset.Parameters {
36-
templateParamIndex := slices.IndexFunc(parameters, func(p types.Parameter) bool {
37-
return p.Name == paramName
38-
})
39-
if templateParamIndex == -1 {
40-
preset.Diagnostics = append(preset.Diagnostics, &hcl.Diagnostic{
41-
Severity: hcl.DiagError,
42-
Summary: "Undefined Parameter",
43-
Detail: fmt.Sprintf("Preset parameter %q is not defined by the template.", paramName),
44-
})
45-
continue
46-
}
47-
templateParam := parameters[templateParamIndex]
48-
for _, diag := range templateParam.Valid(types.StringLiteral(paramValue)) {
49-
preset.Diagnostics = append(preset.Diagnostics, diag)
50-
}
51-
}
52-
5334
foundPresets = append(foundPresets, preset)
5435
}
5536
}

preview_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,17 @@ func TestPresetValidation(t *testing.T) {
714714
"not_prebuild": aPre().noDiagnostics().prebuildCount(0),
715715
},
716716
},
717+
{
718+
name: "preset ok",
719+
dir: "presetok",
720+
input: preview.Input{},
721+
presetAssert: map[string]assertPreset{
722+
"valid_preset": aPre().
723+
value("use_custom_image", "true").
724+
value("custom_image_url", "docker.io/codercom/test:latest").
725+
noDiagnostics(),
726+
},
727+
},
717728
} {
718729
t.Run(tc.name, func(t *testing.T) {
719730
t.Parallel()

testdata/presetok/main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "2.8.0"
6+
}
7+
}
8+
}
9+
10+
data "coder_parameter" "use_custom_image" {
11+
name = "use_custom_image"
12+
type = "bool"
13+
default = "false"
14+
}
15+
16+
data "coder_parameter" "custom_image_url" {
17+
count = data.coder_parameter.use_custom_image.value == "true" ? 1 : 0
18+
name = "custom_image_url"
19+
type = "string"
20+
# No default - required when shown
21+
}
22+
23+
data "coder_workspace_preset" "valid_preset" {
24+
name = "valid_preset"
25+
parameters = {
26+
"use_custom_image" = "true"
27+
"custom_image_url" = "docker.io/codercom/test:latest"
28+
}
29+
prebuilds {
30+
instances = 1
31+
}
32+
}

0 commit comments

Comments
 (0)