Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit 9725ed7

Browse files
null: disallow unknown values and validate if the underlying value is null.
1 parent 1cd1992 commit 9725ed7

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

internal/provider/null_function.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (r IsNullFunction) Definition(_ context.Context, _ function.DefinitionReque
3030
Parameters: []function.Parameter{
3131
function.DynamicParameter{
3232
AllowNullValue: true,
33-
AllowUnknownValues: true,
33+
AllowUnknownValues: false,
3434
Description: "The argument to check",
3535
Name: "argument",
3636
},
@@ -47,7 +47,14 @@ func (r IsNullFunction) Run(ctx context.Context, req function.RunRequest, resp *
4747
return
4848
}
4949

50-
if argument.UnderlyingValue() == nil {
50+
if argument.IsNull() {
51+
if err := resp.Result.Set(ctx, true); err == nil {
52+
return
53+
}
54+
return
55+
}
56+
57+
if argument.IsUnderlyingValueNull() {
5158
if err := resp.Result.Set(ctx, true); err == nil {
5259
return
5360
}

internal/provider/null_function_test.go

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"testing"
88

99
"github.com/hashicorp/go-version"
10+
"github.com/hashicorp/terraform-plugin-testing/config"
1011
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1213
)
1314

14-
func TestIsNullFunction(t *testing.T) {
15+
func TestNullFunction(t *testing.T) {
1516
t.Parallel()
1617
resource.UnitTest(t, resource.TestCase{
1718
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
@@ -36,7 +37,86 @@ output "test" {
3637
})
3738
}
3839

39-
func TestIsNullFunction_falseCases(t *testing.T) {
40+
func TestNullFunction_crossObjectValidation(t *testing.T) {
41+
t.Parallel()
42+
resource.UnitTest(t, resource.TestCase{
43+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
44+
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
45+
},
46+
ExternalProviders: map[string]resource.ExternalProvider{
47+
"wireguard": {
48+
Source: "OJFord/wireguard",
49+
VersionConstraint: "0.3.1",
50+
},
51+
},
52+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
53+
Steps: []resource.TestStep{
54+
{
55+
Config: `
56+
resource "wireguard_asymmetric_key" "main" {}
57+
58+
data "wireguard_config_document" "main" {
59+
private_key = wireguard_asymmetric_key.main.private_key
60+
}
61+
62+
output "test" {
63+
// .addresses is always null in this configuration
64+
value = provider::assert::null(data.wireguard_config_document.main.addresses)
65+
}
66+
`,
67+
Check: resource.ComposeAggregateTestCheckFunc(
68+
resource.TestCheckOutput("test", "true"),
69+
),
70+
},
71+
},
72+
})
73+
}
74+
75+
func TestNullFunction_compoundValidation(t *testing.T) {
76+
t.Parallel()
77+
resource.UnitTest(t, resource.TestCase{
78+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
79+
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
80+
},
81+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
82+
Steps: []resource.TestStep{
83+
{
84+
Config: `
85+
variable "ipv4_ipam_pool_id" {
86+
default = null
87+
description = "ID of the IPv4 IPAM pool to use for the VPC."
88+
type = string
89+
}
90+
91+
variable "cidr_block" {
92+
default = null
93+
description = "CIDR block for the VPC."
94+
type = string
95+
96+
validation {
97+
condition = provider::assert::cidr(var.cidr_block)
98+
error_message = "CIDR block must be a valid CIDR range."
99+
}
100+
101+
validation {
102+
condition = anytrue([
103+
!provider::assert::null(var.cidr_block),
104+
!provider::assert::null(var.ipv4_ipam_pool_id)
105+
])
106+
error_message = "Exactly one of cidr_block or ipv4_ipam_pool_id must be provided."
107+
}
108+
}
109+
`,
110+
ConfigVariables: config.Variables{
111+
"cidr_block": config.StringVariable("10.0.42.0/24"),
112+
},
113+
Check: resource.ComposeAggregateTestCheckFunc(),
114+
},
115+
},
116+
})
117+
}
118+
119+
func TestNullFunction_falseCases(t *testing.T) {
40120
t.Parallel()
41121
resource.UnitTest(t, resource.TestCase{
42122
TerraformVersionChecks: []tfversion.TerraformVersionCheck{

0 commit comments

Comments
 (0)