Skip to content

Commit bcd69e7

Browse files
committed
implementation
1 parent 5ee802d commit bcd69e7

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed

provider/decode_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/coder/terraform-provider-coder/provider"
77
"github.com/mitchellh/mapstructure"
8+
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
)
1011

@@ -18,15 +19,25 @@ func TestDecode(t *testing.T) {
1819

1920
aMap := map[string]interface{}{
2021
"name": "Parameter Name",
22+
"type": "number",
2123
"display_name": displayName,
2224
"legacy_variable": legacyVariable,
2325
"legacy_variable_name": legacyVariableName,
26+
"min": nil,
27+
"validation": []map[string]interface{}{
28+
{
29+
"min": nil,
30+
"max": 5,
31+
},
32+
},
2433
}
2534

2635
var param provider.Parameter
2736
err := mapstructure.Decode(aMap, &param)
2837
require.NoError(t, err)
29-
require.Equal(t, displayName, param.DisplayName)
30-
require.Equal(t, legacyVariable, param.LegacyVariable)
31-
require.Equal(t, legacyVariableName, param.LegacyVariableName)
38+
assert.Equal(t, displayName, param.DisplayName)
39+
assert.Equal(t, legacyVariable, param.LegacyVariable)
40+
assert.Equal(t, legacyVariableName, param.LegacyVariableName)
41+
assert.Equal(t, (*int)(nil), param.Validation[0].Min)
42+
assert.Equal(t, 5, *param.Validation[0].Max)
3243
}

provider/parameter.go

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"strconv"
1313

1414
"github.com/google/uuid"
15+
"github.com/hashicorp/go-cty/cty"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1819
"github.com/mitchellh/mapstructure"
20+
"golang.org/x/xerrors"
1921
)
2022

2123
type Option struct {
@@ -62,19 +64,12 @@ func parameterDataSource() *schema.Resource {
6264
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
6365
rd.SetId(uuid.NewString())
6466

65-
// 1. Read raw config to check if ptr fields are nil
66-
// 2. Update rd with nil values (it is already broken)
67-
68-
//vm := rd.GetRawConfig().AsValueMap()["validation"].AsValueSlice()[0].AsValueMap()
69-
//log.Println(vm)
67+
fixedValidation, err := fixValidationResourceData(rd.GetRawConfig(), rd.Get("validation"))
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
7071

71-
val := rd.Get("validation")
72-
v := val.([]interface{})
73-
k := v[0].(map[string]interface{})
74-
k["min"] = nil
75-
k["max"] = nil
76-
v[0] = k
77-
err := rd.Set("validation", v)
72+
err = rd.Set("validation", fixedValidation)
7873
if err != nil {
7974
return diag.FromErr(err)
8075
}
@@ -115,7 +110,7 @@ func parameterDataSource() *schema.Resource {
115110
}(),
116111
Icon: rd.Get("icon"),
117112
Option: rd.Get("option"),
118-
Validation: val,
113+
Validation: fixedValidation,
119114
Optional: func() bool {
120115
// This hack allows for checking if the "default" field is present in the .tf file.
121116
// If "default" is missing or is "null", then it means that this field is required,
@@ -339,6 +334,47 @@ func parameterDataSource() *schema.Resource {
339334
}
340335
}
341336

337+
func fixValidationResourceData(rawConfig cty.Value, validation interface{}) (interface{}, error) {
338+
// Read validation from raw config
339+
rawValidation, ok := rawConfig.AsValueMap()["validation"]
340+
if !ok {
341+
return validation, nil // no validation rules, nothing to fix
342+
}
343+
344+
rawValidationArr := rawValidation.AsValueSlice()
345+
if len(rawValidationArr) == 0 {
346+
return validation, nil // no validation rules, nothing to fix
347+
}
348+
349+
rawValidationRule := rawValidationArr[0].AsValueMap()
350+
351+
// Load validation from resource data
352+
vArr, ok := validation.([]interface{})
353+
if !ok {
354+
return nil, xerrors.New("validation should be an array")
355+
}
356+
357+
if len(vArr) == 0 {
358+
return validation, nil // no validation rules, nothing to fix
359+
}
360+
361+
validationRule, ok := vArr[0].(map[string]interface{})
362+
if !ok {
363+
return nil, xerrors.New("validation rule should be a map")
364+
}
365+
366+
// Fix the resource data
367+
if rawValidationRule["min"].IsNull() {
368+
validationRule["min"] = nil
369+
}
370+
if rawValidationRule["max"].IsNull() {
371+
validationRule["max"] = nil
372+
}
373+
374+
vArr[0] = validationRule
375+
return vArr, nil
376+
}
377+
342378
func valueIsType(typ, value string) diag.Diagnostics {
343379
switch typ {
344380
case "number":

provider/parameter_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ func TestParameter(t *testing.T) {
109109
}
110110
}
111111
`,
112+
}, {
113+
Name: "NumberValidation_Min",
114+
Config: `
115+
data "coder_parameter" "region" {
116+
name = "Region"
117+
type = "number"
118+
default = 2
119+
validation {
120+
min = 1
121+
}
122+
}
123+
`,
124+
}, {
125+
Name: "NumberValidation_Max",
126+
Config: `
127+
data "coder_parameter" "region" {
128+
name = "Region"
129+
type = "number"
130+
default = 2
131+
validation {
132+
max = 9
133+
}
134+
}
135+
`,
112136
}, {
113137
Name: "DefaultNotNumber",
114138
Config: `

0 commit comments

Comments
 (0)