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

Commit e90a41b

Browse files
not_null: disallow unknown values and check if the underlying value is null.
1 parent 9725ed7 commit e90a41b

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

internal/provider/not_null_function.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,19 @@ func (r NotNullFunction) Run(ctx context.Context, req function.RunRequest, resp
4747
return
4848
}
4949

50-
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, !argument.IsNull()))
50+
if argument.IsNull() {
51+
if err := resp.Result.Set(ctx, false); err == nil {
52+
return
53+
}
54+
return
55+
}
56+
57+
if !argument.IsUnderlyingValueNull() {
58+
if err := resp.Result.Set(ctx, true); err == nil {
59+
return
60+
}
61+
return
62+
}
63+
64+
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, false))
5165
}

internal/provider/not_null_function_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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
)
@@ -220,13 +221,63 @@ output "test" {
220221
})
221222
}
222223

224+
func TestNotNullFunction_compoundValidation(t *testing.T) {
225+
t.Parallel()
226+
resource.UnitTest(t, resource.TestCase{
227+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
228+
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
229+
},
230+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
231+
Steps: []resource.TestStep{
232+
{
233+
Config: `
234+
variable "ipv4_ipam_pool_id" {
235+
default = null
236+
description = "ID of the IPv4 IPAM pool to use for the VPC."
237+
type = string
238+
}
239+
240+
variable "cidr_block" {
241+
default = null
242+
description = "CIDR block for the VPC."
243+
type = string
244+
245+
validation {
246+
condition = provider::assert::cidr(var.cidr_block)
247+
error_message = "CIDR block must be a valid CIDR range."
248+
}
249+
250+
validation {
251+
condition = anytrue([
252+
provider::assert::not_null(var.cidr_block),
253+
provider::assert::not_null(var.ipv4_ipam_pool_id)
254+
])
255+
error_message = "Exactly one of cidr_block or ipv4_ipam_pool_id must be provided."
256+
}
257+
}
258+
`,
259+
ConfigVariables: config.Variables{
260+
"cidr_block": config.StringVariable("10.0.42.0/24"),
261+
},
262+
Check: resource.ComposeAggregateTestCheckFunc(),
263+
},
264+
},
265+
})
266+
}
267+
223268
func TestNotNullFunction_falseCases(t *testing.T) {
224269
t.Parallel()
225270
resource.UnitTest(t, resource.TestCase{
226271
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
227272
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
228273
},
229274
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
275+
ExternalProviders: map[string]resource.ExternalProvider{
276+
"wireguard": {
277+
Source: "OJFord/wireguard",
278+
VersionConstraint: "0.3.1",
279+
},
280+
},
230281
Steps: []resource.TestStep{
231282
{
232283
Config: `
@@ -235,6 +286,23 @@ locals {
235286
}
236287
output "test" {
237288
value = provider::assert::not_null(local.object)
289+
}
290+
`,
291+
Check: resource.ComposeAggregateTestCheckFunc(
292+
resource.TestCheckOutput("test", "false"),
293+
),
294+
},
295+
{
296+
Config: `
297+
resource "wireguard_asymmetric_key" "main" {}
298+
299+
data "wireguard_config_document" "main" {
300+
private_key = wireguard_asymmetric_key.main.private_key
301+
}
302+
303+
output "test" {
304+
// .addresses is always null in this configuration
305+
value = provider::assert::not_null(data.wireguard_config_document.main.addresses)
238306
}
239307
`,
240308
Check: resource.ComposeAggregateTestCheckFunc(

0 commit comments

Comments
 (0)