Skip to content

Commit eb27aaf

Browse files
docs: add knownvalue func check documentation
1 parent 19ca12d commit eb27aaf

File tree

7 files changed

+247
-4
lines changed

7 files changed

+247
-4
lines changed

website/docs/plugin/testing/acceptance-tests/known-value-checks/bool.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
page_title: 'Plugin Development - Acceptance Testing: Known Values'
33
description: >-
4-
Bool Value Checks for use with Plan Checks.
4+
Bool Value Checks for use with Plan and State Checks.
55
---
66

77
# Bool Known Value Checks
88

99
The known value checks that are available for bool values are:
1010

1111
* [Bool](/terraform/plugin/testing/acceptance-tests/known-value-checks/bool#bool-check)
12+
* [BoolFunc](/terraform/plugin/testing/acceptance-tests/known-value-checks/bool#boolfunc-check)
1213

1314
## `Bool` Check
1415

@@ -40,3 +41,37 @@ func TestExpectKnownValue_CheckPlan_Bool(t *testing.T) {
4041
})
4142
}
4243
```
44+
45+
## `BoolFunc` Check
46+
47+
The [BoolFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#BoolFunc) check allows defining a custom function to validate whether the bool value of a resource attribute or output satisfies specific conditions.
48+
49+
Example usage of [BoolFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#BoolFunc) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
50+
51+
```go
52+
func TestExpectKnownValue_CheckState_BoolFunc(t *testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
// Provider definition omitted.
57+
Steps: []resource.TestStep{
58+
{
59+
// Example resource containing a boolean attribute named "configurable_attribute"
60+
Config: `resource "test_resource" "one" {}`,
61+
ConfigStateChecks: []statecheck.StateCheck{
62+
statecheck.ExpectKnownValue(
63+
"test_resource.one",
64+
tfjsonpath.New("configurable_attribute"),
65+
knownvalue.BoolFunc(func(v bool) error {
66+
if !v {
67+
return fmt.Errorf("expected true, got %t", v)
68+
}
69+
return nil
70+
}),
71+
),
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/float32.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
page_title: 'Plugin Development - Acceptance Testing: Known Values'
33
description: >-
4-
Float32 Value Checks for use with Plan Checks.
4+
Float32 Value Checks for use with Plan and State Checks.
55
---
66

77
# Float32 Known Value Checks
88

99
The known value checks that are available for float32 values are:
1010

1111
* [Float32Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/float32#float32exact-check)
12+
* [Float32Func](/terraform/plugin/testing/acceptance-tests/known-value-checks/float32#float32func-check)
1213

1314
## `Float32Exact` Check
1415

@@ -40,3 +41,37 @@ func TestExpectKnownValue_CheckPlan_Float32(t *testing.T) {
4041
})
4142
}
4243
```
44+
45+
## `Float32Func` Check
46+
47+
The [Float32Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float32Func) check allows defining a custom function to validate whether the float32 value of a resource attribute or output satisfies specific conditions.
48+
49+
Example usage of [Float32Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float32Func) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
50+
51+
```go
52+
func TestExpectKnownValue_CheckState_Float32Func(t *testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
// Provider definition omitted.
57+
Steps: []resource.TestStep{
58+
{
59+
// Example resource containing a float32 attribute named "configurable_attribute"
60+
Config: `resource "test_resource" "one" {}`,
61+
ConfigStateChecks: []statecheck.StateCheck{
62+
statecheck.ExpectKnownValue(
63+
"test_resource.one",
64+
tfjsonpath.New("configurable_attribute"),
65+
knownvalue.Float32Func(func(v float32) error {
66+
if v > 1.0 && v < 5.0 {
67+
return fmt.Errorf("value must be between 1.0 and 5.0")
68+
}
69+
return nil
70+
}),
71+
),
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/float64.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description: >-
99
The known value checks that are available for float64 values are:
1010

1111
* [Float64Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/float64#float64exact-check)
12+
* [Float64Func](/terraform/plugin/testing/acceptance-tests/known-value-checks/float64#float64func-check)
1213

1314
## `Float64Exact` Check
1415

@@ -40,3 +41,37 @@ func TestExpectKnownValue_CheckPlan_Float64(t *testing.T) {
4041
})
4142
}
4243
```
44+
45+
## `Float64Func` Check
46+
47+
The [Float64Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float64Func) check allows defining a custom function to validate whether the float64 value of a resource attribute or output satisfies specific conditions.
48+
49+
Example usage of [Float64Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float64Func) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
50+
51+
```go
52+
func TestExpectKnownValue_CheckState_Float64Func(t *testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
// Provider definition omitted.
57+
Steps: []resource.TestStep{
58+
{
59+
// Example resource containing a float64 attribute named "configurable_attribute"
60+
Config: `resource "test_resource" "one" {}`,
61+
ConfigStateChecks: []statecheck.StateCheck{
62+
statecheck.ExpectKnownValue(
63+
"test_resource.one",
64+
tfjsonpath.New("configurable_attribute"),
65+
knownvalue.Float64Func(func(v float64) error {
66+
if v > 1.0 && v < 5.0 {
67+
return fmt.Errorf("value must be between 1.0 and 5.0")
68+
}
69+
return nil
70+
}),
71+
),
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/int32.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
page_title: 'Plugin Development - Acceptance Testing: Known Values'
33
description: >-
4-
Int32 Value Checks for use with Plan Checks.
4+
Int32 Value Checks for use with Plan and State Checks.
55
---
66

77
# Int32 Known Value Checks
88

99
The known value checks that are available for int32 values are:
1010

1111
* [Int32Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/int32#int32exact-check)
12+
* [Int32Func](/terraform/plugin/testing/acceptance-tests/known-value-checks/int32#int32func-check)
1213

1314
## `Int32Exact` Check
1415

@@ -40,3 +41,37 @@ func TestExpectKnownValue_CheckPlan_Int32(t *testing.T) {
4041
})
4142
}
4243
```
44+
45+
## `Int32Func` Check
46+
47+
The [Int32Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int32Func) check allows defining a custom function to validate whether the int32 value of a resource attribute or output satisfies specific conditions.
48+
49+
Example usage of [Int32Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int32Func) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
50+
51+
```go
52+
func TestExpectKnownValue_CheckState_Int32Func(t *testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
// Provider definition omitted.
57+
Steps: []resource.TestStep{
58+
{
59+
// Example resource containing an int32 attribute named "configurable_attribute"
60+
Config: `resource "test_resource" "one" {}`,
61+
ConfigStateChecks: []statecheck.StateCheck{
62+
statecheck.ExpectKnownValue(
63+
"test_resource.one",
64+
tfjsonpath.New("configurable_attribute"),
65+
knownvalue.Int32Func(func(v int32) error {
66+
if v < 1 && v > 12 {
67+
return fmt.Errorf("range cannot be less than 1 or greater than 12")
68+
}
69+
return nil
70+
}),
71+
),
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/int64.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
page_title: 'Plugin Development - Acceptance Testing: Known Values'
33
description: >-
4-
Int64 Value Checks for use with Plan Checks.
4+
Int64 Value Checks for use with Plan and State Checks.
55
---
66

77
# Int64 Known Value Checks
88

99
The known value checks that are available for int64 values are:
1010

1111
* [Int64Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/int64#int64exact-check)
12+
* [Int64Func](/terraform/plugin/testing/acceptance-tests/known-value-checks/int64#int64func-check)
1213

1314
## `Int64Exact` Check
1415

@@ -40,3 +41,37 @@ func TestExpectKnownValue_CheckPlan_Int64(t *testing.T) {
4041
})
4142
}
4243
```
44+
45+
## `Int64Func` Check
46+
47+
The [Int64Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int64Func) check allows defining a custom function to validate whether the int64 value of a resource attribute or output satisfies specific conditions.
48+
49+
Example usage of [Int64Func](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int64Func) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
50+
51+
```go
52+
func TestExpectKnownValue_CheckState_Int64Func(t *testing.T) {
53+
t.Parallel()
54+
55+
resource.Test(t, resource.TestCase{
56+
// Provider definition omitted.
57+
Steps: []resource.TestStep{
58+
{
59+
// Example resource containing an int64 attribute named "configurable_attribute"
60+
Config: `resource "test_resource" "one" {}`,
61+
ConfigStateChecks: []statecheck.StateCheck{
62+
statecheck.ExpectKnownValue(
63+
"test_resource.one",
64+
tfjsonpath.New("configurable_attribute"),
65+
knownvalue.Int64Func(func(v int64) error {
66+
if v > 1 && v < 12 {
67+
return fmt.Errorf("value must be between 1 and 12")
68+
}
69+
return nil
70+
}),
71+
),
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/number.mdx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,36 @@ func TestExpectKnownValue_CheckPlan_Number(t *testing.T) {
4646
})
4747
}
4848
```
49+
50+
## `NumberFunc` Check
51+
52+
The [NumberFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#NumberFunc) check allows defining a custom function to validate whether the number value of a resource attribute or output satisfies specific conditions.
53+
54+
Example usage of [NumberFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#NumberFunc) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
55+
56+
```go
57+
func TestExpectKnownValue_CheckState_NumberFunc(t *testing.T) {
58+
t.Parallel()
59+
60+
resource.Test(t, resource.TestCase{
61+
// Provider definition omitted.
62+
Steps: []resource.TestStep{
63+
{
64+
// Example resource containing a number attribute named "configurable_attribute"
65+
Config: `resource "test_resource" "one" {}`,
66+
ConfigStateChecks: []statecheck.StateCheck{
67+
statecheck.ExpectKnownValue(
68+
"test_resource.one",
69+
tfjsonpath.New("configurable_attribute"),
70+
knownvalue.NumberFunc(func(v *big.Float) error {
71+
if err := testConfigurableAttribute(v); err != nil {
72+
return fmt.Errorf("attribute validation failed: %w", err)
73+
}
74+
return nil
75+
}),
76+
),
77+
},
78+
},
79+
},
80+
})
81+
}

website/docs/plugin/testing/acceptance-tests/known-value-checks/string.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The known value checks that are available for string values are:
1010

1111
* [StringExact](/terraform/plugin/testing/acceptance-tests/known-value-checks/string#stringexact-check)
1212
* [StringRegexp](/terraform/plugin/testing/acceptance-tests/known-value-checks/string#stringregexp-check)
13+
* [StringFunc](/terraform/plugin/testing/acceptance-tests/known-value-checks/string#stringfunc-check)
1314

1415
## `StringExact` Check
1516

@@ -70,3 +71,37 @@ func TestExpectKnownValue_CheckPlan_StringRegexp(t *testing.T) {
7071
})
7172
}
7273
```
74+
75+
## `StringFunc` Check
76+
77+
The [StringFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#StringFunc) check allows defining a custom function to validate whether the string value of a resource attribute or output satisfies specific conditions.
78+
79+
Example usage of [StringFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#StringFunc) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/state-checks/resource) state check.
80+
81+
```go
82+
func TestExpectKnownValue_CheckState_StringFunc(t *testing.T) {
83+
t.Parallel()
84+
85+
resource.Test(t, resource.TestCase{
86+
// Provider definition omitted.
87+
Steps: []resource.TestStep{
88+
{
89+
// Example resource containing a string attribute named "configurable_attribute"
90+
Config: `resource "test_resource" "one" {}`,
91+
ConfigStateChecks: []statecheck.StateCheck{
92+
statecheck.ExpectKnownValue(
93+
"test_resource.one",
94+
tfjsonpath.New("configurable_attribute"),
95+
knownvalue.StringFunc(func(v string) error {
96+
if !strings.HasPrefix(v, "str") {
97+
return fmt.Errorf("value must start with 'str'")
98+
}
99+
return nil
100+
}),
101+
),
102+
},
103+
},
104+
},
105+
})
106+
}
107+
```

0 commit comments

Comments
 (0)