@@ -14,24 +14,27 @@ import (
14
14
"github.com/coder/terraform-provider-coder/v2/provider"
15
15
)
16
16
17
+ // formTypeTestCase is the config for a single test case.
17
18
type formTypeTestCase struct {
18
19
name string
19
20
config formTypeCheck
20
21
assert paramAssert
21
22
expectError * regexp.Regexp
22
23
}
23
24
25
+ // paramAssert is asserted on the provider's parsed terraform state.
24
26
type paramAssert struct {
25
27
FormType provider.ParameterFormType
26
28
Type provider.OptionType
27
29
Styling string
28
30
}
29
31
32
+ // formTypeCheck is a struct that helps build the terraform config
30
33
type formTypeCheck struct {
31
34
formType provider.ParameterFormType
32
35
optionType provider.OptionType
33
- defValue string
34
36
options bool
37
+ defValue string
35
38
customOptions []string
36
39
}
37
40
@@ -283,18 +286,8 @@ func TestValidateFormType(t *testing.T) {
283
286
})
284
287
}
285
288
289
+ // ezconfig converts a formTypeCheck into a terraform config string.
286
290
func ezconfig (paramName string , cfg formTypeCheck ) (defaultValue string , tf string ) {
287
- var body strings.Builder
288
- if cfg .defValue != "" {
289
- body .WriteString (fmt .Sprintf ("default = %q\n " , cfg .defValue ))
290
- }
291
- if cfg .formType != "" {
292
- body .WriteString (fmt .Sprintf ("form_type = %q\n " , cfg .formType ))
293
- }
294
- if cfg .optionType != "" {
295
- body .WriteString (fmt .Sprintf ("type = %q\n " , cfg .optionType ))
296
- }
297
-
298
291
options := cfg .customOptions
299
292
if cfg .options && len (cfg .customOptions ) == 0 {
300
293
switch cfg .optionType {
@@ -309,22 +302,34 @@ func ezconfig(paramName string, cfg formTypeCheck) (defaultValue string, tf stri
309
302
defaultValue = "1"
310
303
case provider .OptionTypeListString :
311
304
options = []string {`["red", "blue"]` }
312
- defaultValue = `["red"]`
305
+ defaultValue = `["red", "blue" ]`
313
306
default :
314
307
panic (fmt .Sprintf ("unknown option type %q when generating options" , cfg .optionType ))
315
308
}
316
309
}
317
310
311
+ if cfg .defValue == "" {
312
+ cfg .defValue = defaultValue
313
+ }
314
+
315
+ var body strings.Builder
316
+ if cfg .defValue != "" {
317
+ body .WriteString (fmt .Sprintf ("default = %q\n " , cfg .defValue ))
318
+ }
319
+ if cfg .formType != "" {
320
+ body .WriteString (fmt .Sprintf ("form_type = %q\n " , cfg .formType ))
321
+ }
322
+ if cfg .optionType != "" {
323
+ body .WriteString (fmt .Sprintf ("type = %q\n " , cfg .optionType ))
324
+ }
325
+
318
326
for i , opt := range options {
319
327
body .WriteString ("option {\n " )
320
328
body .WriteString (fmt .Sprintf ("name = \" val_%d\" \n " , i ))
321
329
body .WriteString (fmt .Sprintf ("value = %q\n " , opt ))
322
330
body .WriteString ("}\n " )
323
331
}
324
332
325
- if cfg .defValue == "" {
326
- cfg .defValue = defaultValue
327
- }
328
333
return cfg .defValue , fmt .Sprintf (`
329
334
provider "coder" {
330
335
}
@@ -353,13 +358,6 @@ func formTypeTest(t *testing.T, c formTypeTestCase) {
353
358
assert .Equal (t , string (c .assert .Type ), param .Primary .Attributes ["type" ], "type" )
354
359
assert .JSONEq (t , c .assert .Styling , param .Primary .Attributes ["styling" ], "styling" )
355
360
356
- //ft := provider.ParameterFormType(param.Primary.Attributes["form_type"])
357
- //ot := provider.OptionType(param.Primary.Attributes["type"])
358
-
359
- // Option blocks are not stored in a very friendly format
360
- // here.
361
- //options := param.Primary.Attributes["option.0.name"] != ""
362
-
363
361
return nil
364
362
}
365
363
if c .expectError != nil {
0 commit comments