@@ -2,6 +2,7 @@ package extract
22
33import (
44 "fmt"
5+ "slices"
56 "strings"
67
78 "github.com/aquasecurity/trivy/pkg/iac/terraform"
@@ -11,6 +12,7 @@ import (
1112
1213 "github.com/coder/preview/hclext"
1314 "github.com/coder/preview/types"
15+ "github.com/coder/terraform-provider-coder/v2/provider"
1416)
1517
1618func ParameterFromBlock (block * terraform.Block ) (* types.Parameter , hcl.Diagnostics ) {
@@ -26,6 +28,16 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
2628 diags = diags .Append (typDiag )
2729 }
2830
31+ formType , formTypeDiags := optionalStringEnum [provider.ParameterFormType ](block , "form_type" , provider .ParameterFormTypeDefault , func (s provider.ParameterFormType ) error {
32+ if ! slices .Contains (provider .ParameterFormTypes (), s ) {
33+ return fmt .Errorf ("invalid form type %q, expected one of [%s]" , s , strings .Join (toStrings (provider .ParameterFormTypes ()), ", " ))
34+ }
35+ return nil
36+ })
37+ if formTypeDiags != nil {
38+ diags = diags .Append (formTypeDiags )
39+ }
40+
2941 pName , nameDiag := requiredString (block , "name" )
3042 if nameDiag != nil {
3143 diags = diags .Append (nameDiag )
@@ -36,16 +48,24 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
3648 }
3749
3850 pVal := richParameterValue (block )
51+
52+ def := types .StringLiteral ("" )
53+ defAttr := block .GetAttribute ("default" )
54+ if ! defAttr .IsNil () {
55+ def = types .ToHCLString (block , defAttr )
56+ }
57+
3958 p := types.Parameter {
4059 Value : pVal ,
41- RichParameter : types.RichParameter {
60+ ParameterData : types.ParameterData {
4261 Name : pName ,
4362 Description : optionalString (block , "description" ),
4463 Type : pType ,
64+ FormType : formType ,
4565 Mutable : optionalBoolean (block , "mutable" ),
4666 // Default value is always written as a string, then converted
4767 // to the correct type.
48- DefaultValue : optionalString ( block , "default" ) ,
68+ DefaultValue : def ,
4969 Icon : optionalString (block , "icon" ),
5070 Options : make ([]* types.ParameterOption , 0 ),
5171 Validations : make ([]* types.ParameterValidation , 0 ),
@@ -58,7 +78,25 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
5878 },
5979 }
6080
61- for _ , b := range block .GetBlocks ("option" ) {
81+ optBlocks := block .GetBlocks ("option" )
82+
83+ optionType , newFormType , err := provider .ValidateFormType (string (p .Type ), len (optBlocks ), p .FormType )
84+ if err != nil {
85+ diags = diags .Append (& hcl.Diagnostic {
86+ Severity : hcl .DiagError ,
87+ Summary : fmt .Sprintf ("Invalid parameter `type=%q` and `form_type=%q`" , p .Type , p .FormType ),
88+ Detail : err .Error (),
89+ Context : & block .HCLBlock ().DefRange ,
90+ })
91+
92+ // Parameter cannot be used
93+ p .FormType = provider .ParameterFormTypeError
94+ } else {
95+ p .Type = types .ParameterType (optionType )
96+ p .FormType = newFormType
97+ }
98+
99+ for _ , b := range optBlocks {
62100 opt , optDiags := ParameterOptionFromBlock (b )
63101 diags = diags .Extend (optDiags )
64102
@@ -107,6 +145,7 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
107145 paramTypeDiag .EvalContext = block .Context ().Inner ()
108146 }
109147 diags = diags .Append (paramTypeDiag )
148+ p .FormType = provider .ParameterFormTypeError
110149 }
111150
112151 if ctyType != cty .NilType && pVal .Value .Type ().Equals (cty .String ) {
@@ -126,10 +165,9 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
126165 }
127166 }
128167
129- // Parameter usage diags are useful.
130168 usageDiags := ParameterUsageDiagnostics (p )
131169 if usageDiags .HasErrors () {
132- p .FormControl = types . FormControlError
170+ p .FormType = provider . ParameterFormTypeError
133171 }
134172 diags = diags .Extend (usageDiags )
135173
@@ -215,20 +253,14 @@ func ParameterOptionFromBlock(block *terraform.Block) (types.ParameterOption, hc
215253
216254 valAttr := block .GetAttribute ("value" )
217255
218- pVal := types.HCLString {
219- Value : hclext .Value (valAttr .HCLAttribute ().Expr , block .Context ().Inner ()),
220- ValueDiags : nil ,
221- ValueExpr : valAttr .HCLAttribute ().Expr ,
222- }
223-
224256 if diags .HasErrors () {
225257 return types.ParameterOption {}, diags
226258 }
227259
228260 p := types.ParameterOption {
229261 Name : pName ,
230262 Description : optionalString (block , "description" ),
231- Value : pVal ,
263+ Value : types . ToHCLString ( block , valAttr ) ,
232264 Icon : optionalString (block , "icon" ),
233265 }
234266
@@ -355,7 +387,7 @@ func optionalString(block *terraform.Block, key string) string {
355387 return ""
356388 }
357389 val := attr .Value ()
358- if val .Type () != cty .String {
390+ if ! val .Type (). Equals ( cty .String ) {
359391 return ""
360392 }
361393
@@ -440,3 +472,11 @@ func ParameterCtyType(typ string) (cty.Type, error) {
440472 return cty.Type {}, fmt .Errorf ("unsupported type: %q" , typ )
441473 }
442474}
475+
476+ func toStrings [A ~ string ](l []A ) []string {
477+ var r []string
478+ for _ , v := range l {
479+ r = append (r , string (v ))
480+ }
481+ return r
482+ }
0 commit comments