Skip to content

fix: terraform-plugin-sdk zeros *int fields #123

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Merged
merged 14 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/data-sources/parameter.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,4 +63,9 @@ Optional:
- `monotonic` (String) Number monotonicity, either increasing or decreasing.
- `regex` (String) A regex for the input parameter to match against.

Read-Only:

- `max_disabled` (Boolean) Helper field to check if max is present
- `min_disabled` (Boolean) Helper field to check if min is present


16 changes: 10 additions & 6 deletions provider/decode_test.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,11 @@ package provider_test
import (
"testing"

".com/coder/terraform-provider-coder/provider"
".com/mitchellh/mapstructure"
".com/stretchr/testify/assert"
".com/stretchr/testify/require"

".com/coder/terraform-provider-coder/provider"
)

func TestDecode(t *testing.T) {
Expand All@@ -23,11 +24,12 @@ func TestDecode(t *testing.T) {
"display_name": displayName,
"legacy_variable": legacyVariable,
"legacy_variable_name": legacyVariableName,
"min": nil,
"validation": []map[string]interface{}{
{
"min": nil,
"max": 5,
"min": nil,
"min_disabled": false,
"max": 5,
"max_disabled": true,
},
},
}
Expand All@@ -38,6 +40,8 @@ func TestDecode(t *testing.T) {
assert.Equal(t, displayName, param.DisplayName)
assert.Equal(t, legacyVariable, param.LegacyVariable)
assert.Equal(t, legacyVariableName, param.LegacyVariableName)
assert.Equal(t, (*int)(nil), param.Validation[0].Min)
assert.Equal(t, 5, *param.Validation[0].Max)
assert.Equal(t, 5, param.Validation[0].Max)
assert.True(t, param.Validation[0].MaxDisabled)
assert.Equal(t, 0, param.Validation[0].Min)
assert.False(t, param.Validation[0].MinDisabled)
}
38 changes: 23 additions & 15 deletions provider/parameter.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,8 +28,11 @@ type Option struct {
}

type Validation struct {
Min *int
Max *int
Min int
MinDisabled bool `mapstructure:"min_disabled"`
Max int
MaxDisabled bool `mapstructure:"max_disabled"`

Monotonic string

Regex string
Expand DownExpand Up@@ -288,11 +291,21 @@ func parameterDataSource() *schema.Resource {
Optional: true,
Description: "The minimum of a number parameter.",
},
"min_disabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Helper field to check if min is present",
},
"max": {
Type: schema.TypeInt,
Optional: true,
Description: "The maximum of a number parameter.",
},
"max_disabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Helper field to check if max is present",
},
"monotonic": {
Type: schema.TypeString,
Optional: true,
Expand DownExpand Up@@ -363,13 +376,8 @@ func fixValidationResourceData(rawConfig cty.Value, validation interface{}) (int
return nil, xerrors.New("validation rule should be a map")
}

// Fix the resource data
if rawValidationRule["min"].IsNull() {
validationRule["min"] = nil
}
if rawValidationRule["max"].IsNull() {
validationRule["max"] = nil
}
validationRule["min_disabled"] = rawValidationRule["min"].IsNull()
validationRule["max_disabled"] = rawValidationRule["max"].IsNull()
return vArr, nil
}

Expand DownExpand Up@@ -401,10 +409,10 @@ func valueIsType(typ, value string) diag.Diagnostics {

func (v *Validation) Valid(typ, value string) error {
if typ != "number" {
if v.Min != nil {
if !v.MinDisabled {
return fmt.Errorf("a min cannot be specified for a %s type", typ)
}
if v.Max != nil {
if !v.MaxDisabled {
return fmt.Errorf("a max cannot be specified for a %s type", typ)
}
}
Expand DownExpand Up@@ -437,11 +445,11 @@ func (v *Validation) Valid(typ, value string) error {
if err != nil {
return fmt.Errorf("value %q is not a number", value)
}
if v.Min != nil && num < *v.Min {
return fmt.Errorf("value %d is less than the minimum %d", num, *v.Min)
if !v.MinDisabled && num < v.Min {
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
}
if v.Max != nil && num > *v.Max {
return fmt.Errorf("value %d is more than the maximum %d", num, *v.Max)
if !v.MaxDisabled && num > v.Max {
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
}
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
Expand Down
Loading