diff --git a/go.mod b/go.mod index a67b844e..fd4a87e9 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,11 @@ require ( github.com/google/go-cmp v0.5.8 github.com/hashicorp/go-uuid v1.0.3 github.com/hashicorp/terraform-plugin-docs v0.13.0 - github.com/hashicorp/terraform-plugin-framework v0.9.0 - github.com/hashicorp/terraform-plugin-framework-validators v0.3.0 - github.com/hashicorp/terraform-plugin-go v0.11.0 - github.com/hashicorp/terraform-plugin-log v0.4.1 - github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 + github.com/hashicorp/terraform-plugin-framework v0.10.0 + github.com/hashicorp/terraform-plugin-framework-validators v0.4.0 + github.com/hashicorp/terraform-plugin-go v0.12.0 + github.com/hashicorp/terraform-plugin-log v0.6.0 + github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d ) @@ -62,11 +62,11 @@ require ( github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect github.com/vmihailenco/tagparser v0.1.2 // indirect github.com/zclconf/go-cty v1.10.0 // indirect - golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect - golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect + golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3 // indirect - google.golang.org/grpc v1.47.0 // indirect + google.golang.org/grpc v1.48.0 // indirect google.golang.org/protobuf v1.28.0 // indirect ) diff --git a/go.sum b/go.sum index 624d5506..ebec1625 100644 --- a/go.sum +++ b/go.sum @@ -123,7 +123,6 @@ github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/S github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI= github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs= github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= -github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8YmtEw= github.com/hashicorp/go-hclog v1.2.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= @@ -150,20 +149,20 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM= github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smPzZrt1Wlm9koLeKazY= github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ= -github.com/hashicorp/terraform-plugin-framework v0.9.0 h1:vOKG9+keJv062zGhXFgfOFEuGcfgV6LHciwleFTSek0= -github.com/hashicorp/terraform-plugin-framework v0.9.0/go.mod h1:ActelD2V6yt2m0MwIX4jESGDYJ573rAvZswGjSGm1rY= -github.com/hashicorp/terraform-plugin-framework-validators v0.3.0 h1:4gunYb8S4b2shVsL+1oS0ank+XBmUCxWwNz3cof+fXo= -github.com/hashicorp/terraform-plugin-framework-validators v0.3.0/go.mod h1:c1czSXzBmpJNhS/0WXTNPzUU8kHrHLLyrW8rXoFD0fs= -github.com/hashicorp/terraform-plugin-go v0.9.1/go.mod h1:ItjVSlQs70otlzcCwlPcU8FRXLdO973oYFRZwAOxy8M= +github.com/hashicorp/terraform-plugin-framework v0.10.0 h1:LGYcnvNdVaZA1ZHe53BHLVjaaGs7HTiq6+9Js29stL4= +github.com/hashicorp/terraform-plugin-framework v0.10.0/go.mod h1:CK7Opzukfu/2CPJs+HzUdfHrFlp+ZIQeSxjF0x8k464= +github.com/hashicorp/terraform-plugin-framework-validators v0.4.0 h1:GVX4gDFdtFZi/E4DjvPmRDf5zBLFAHi8M7Qzehgov+Q= +github.com/hashicorp/terraform-plugin-framework-validators v0.4.0/go.mod h1:hkGjFzftzT/HRe1It0e8//tGZ3j18PedTq4hHS8NJ/Q= github.com/hashicorp/terraform-plugin-go v0.10.0/go.mod h1:aphXBG8qtQH0yF1waMRlaw/3G+ZFlR/6Artnvt1QEDE= -github.com/hashicorp/terraform-plugin-go v0.11.0 h1:YXsvSCx7GbQO5jIUQd77FesqmIBxgSvYAtAX1NqErTk= -github.com/hashicorp/terraform-plugin-go v0.11.0/go.mod h1:aphXBG8qtQH0yF1waMRlaw/3G+ZFlR/6Artnvt1QEDE= -github.com/hashicorp/terraform-plugin-log v0.4.0/go.mod h1:9KclxdunFownr4pIm1jdmwKRmE4d6HVG2c9XDq47rpg= -github.com/hashicorp/terraform-plugin-log v0.4.1 h1:xpbmVhvuU3mgHzLetOmx9pkOL2rmgpu302XxddON6eo= +github.com/hashicorp/terraform-plugin-go v0.12.0 h1:6wW9mT1dSs0Xq4LR6HXj1heQ5ovr5GxXNJwkErZzpJw= +github.com/hashicorp/terraform-plugin-go v0.12.0/go.mod h1:kwhmaWHNDvT1B3QiSJdAtrB/D4RaKSY/v3r2BuoWK4M= github.com/hashicorp/terraform-plugin-log v0.4.1/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4= +github.com/hashicorp/terraform-plugin-log v0.6.0 h1:/Vq78uSIdUSZ3iqDc9PESKtwt8YqNKN6u+khD+lLjuw= +github.com/hashicorp/terraform-plugin-log v0.6.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4= github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 h1:/cdI5di5XA+N80gXzXF4YcHq36DprBskubk6Z8i26ZQ= github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0/go.mod h1:L3SHkD/Q8zPVgXviQmpVwy9nKwpXXZscVIpVEnQ/T50= -github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co= +github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0 h1:7gDAcfto/C4Cjtf90SdukQshsxdMxJ/P69QxiF3digI= +github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0/go.mod h1:/WYikYjhKB7c2j1HmXZhRsAARldRb4M38bLCLOhC3so= github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg= github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI= github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0= @@ -325,8 +324,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0= +golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -359,10 +358,11 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -406,8 +406,9 @@ google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= -google.golang.org/grpc v1.47.0 h1:9n77onPX5F3qfFCqjy9dhn8PbNQsIKeVU04J9G7umt8= google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= +google.golang.org/grpc v1.48.0 h1:rQOsyJ/8+ufEDJd/Gdsz7HG220Mh9HAhFHRGnIjda0w= +google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0/go.mod h1:DNq5QpG7LJqD2AamLZ7zvKE0DEpVl2BSEVjFycAAjRY= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= diff --git a/internal/provider/resource_password.go b/internal/provider/resource_password.go index 8780989b..7747b905 100644 --- a/internal/provider/resource_password.go +++ b/internal/provider/resource_password.go @@ -5,15 +5,14 @@ import ( "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-go/tftypes" "golang.org/x/crypto/bcrypt" "github.com/terraform-providers/terraform-provider-random/internal/diagnostics" "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" "github.com/terraform-providers/terraform-provider-random/internal/random" - "github.com/terraform-providers/terraform-provider-random/internal/validators" ) var _ tfsdk.ResourceType = (*passwordResourceType)(nil) @@ -290,11 +289,11 @@ func passwordSchemaV2() tfsdk.Schema { }, Validators: []tfsdk.AttributeValidator{ int64validator.AtLeast(1), - validators.NewIntIsAtLeastSumOfValidator( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - tftypes.NewAttributePath().WithAttributeName("min_numeric"), - tftypes.NewAttributePath().WithAttributeName("min_special"), + int64validator.AtLeastSumOf( + path.MatchRoot("min_upper"), + path.MatchRoot("min_lower"), + path.MatchRoot("min_numeric"), + path.MatchRoot("min_special"), ), }, }, @@ -451,11 +450,11 @@ func passwordSchemaV1() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()}, Validators: []tfsdk.AttributeValidator{ int64validator.AtLeast(1), - validators.NewIntIsAtLeastSumOfValidator( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - tftypes.NewAttributePath().WithAttributeName("min_numeric"), - tftypes.NewAttributePath().WithAttributeName("min_special"), + int64validator.AtLeastSumOf( + path.MatchRoot("min_upper"), + path.MatchRoot("min_lower"), + path.MatchRoot("min_numeric"), + path.MatchRoot("min_special"), ), }, }, @@ -609,11 +608,11 @@ func passwordSchemaV0() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()}, Validators: []tfsdk.AttributeValidator{ int64validator.AtLeast(1), - validators.NewIntIsAtLeastSumOfValidator( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - tftypes.NewAttributePath().WithAttributeName("min_numeric"), - tftypes.NewAttributePath().WithAttributeName("min_special"), + int64validator.AtLeastSumOf( + path.MatchRoot("min_upper"), + path.MatchRoot("min_lower"), + path.MatchRoot("min_numeric"), + path.MatchRoot("min_special"), ), }, }, diff --git a/internal/provider/resource_string.go b/internal/provider/resource_string.go index 74028153..7a0fa8f5 100644 --- a/internal/provider/resource_string.go +++ b/internal/provider/resource_string.go @@ -5,14 +5,13 @@ import ( "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/terraform-providers/terraform-provider-random/internal/diagnostics" "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" "github.com/terraform-providers/terraform-provider-random/internal/random" - "github.com/terraform-providers/terraform-provider-random/internal/validators" ) var _ tfsdk.ResourceType = (*stringResourceType)(nil) @@ -51,11 +50,11 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()}, Validators: []tfsdk.AttributeValidator{ int64validator.AtLeast(1), - validators.NewIntIsAtLeastSumOfValidator( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - tftypes.NewAttributePath().WithAttributeName("min_numeric"), - tftypes.NewAttributePath().WithAttributeName("min_special"), + int64validator.AtLeastSumOf( + path.MatchRoot("min_upper"), + path.MatchRoot("min_lower"), + path.MatchRoot("min_numeric"), + path.MatchRoot("min_special"), ), }, }, @@ -310,11 +309,11 @@ func (r *stringResource) UpgradeState(context.Context) map[int64]tfsdk.ResourceS PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()}, Validators: []tfsdk.AttributeValidator{ int64validator.AtLeast(1), - validators.NewIntIsAtLeastSumOfValidator( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - tftypes.NewAttributePath().WithAttributeName("min_numeric"), - tftypes.NewAttributePath().WithAttributeName("min_special"), + int64validator.AtLeastSumOf( + path.MatchRoot("min_upper"), + path.MatchRoot("min_lower"), + path.MatchRoot("min_numeric"), + path.MatchRoot("min_special"), ), }, }, diff --git a/internal/provider/resource_string_test.go b/internal/provider/resource_string_test.go index 19fcdb36..72b40f22 100644 --- a/internal/provider/resource_string_test.go +++ b/internal/provider/resource_string_test.go @@ -14,7 +14,7 @@ func TestAccResourceString(t *testing.T) { Steps: []resource.TestStep{ { Config: `resource "random_string" "basic" { - length = 12 + length = 12 }`, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrWith("random_string.basic", "result", testCheckLen(12)), @@ -288,7 +288,7 @@ func TestAccResourceString_LengthErrors(t *testing.T) { length = 2 min_lower = 3 }`, - ExpectError: regexp.MustCompile(`.*Attribute "length" \(2\) cannot be less than min_upper \+ min_lower \+\nmin_numeric \+ min_special \(3\).`), + ExpectError: regexp.MustCompile(`.*Value must be at least sum of min_upper \+ min_lower \+ min_numeric \+\nmin_special, got: 2`), }, { Config: `resource "random_string" "invalid_length" { diff --git a/internal/validators/validators.go b/internal/validators/validators.go deleted file mode 100644 index 20f86eb4..00000000 --- a/internal/validators/validators.go +++ /dev/null @@ -1,114 +0,0 @@ -package validators - -import ( - "context" - "fmt" - "strconv" - "strings" - - "github.com/hashicorp/terraform-plugin-framework/tfsdk" - "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-go/tftypes" - "github.com/hashicorp/terraform-plugin-log/tflog" -) - -// intIsAtLeastSumOfValidator checks that the value of the attribute in the configuration -// (i.e., AttributeConfig in ValidateAttributeRequest) is greater than or, equal to the sum of the values of the -// attributes in the slice of AttributePath. -// TODO: Remove once https://github.com/hashicorp/terraform-plugin-framework-validators/pull/29 is merged. -type intIsAtLeastSumOfValidator struct { - attributesToSum []*tftypes.AttributePath -} - -var _ tfsdk.AttributeValidator = (*intIsAtLeastSumOfValidator)(nil) - -func NewIntIsAtLeastSumOfValidator(attributePaths ...*tftypes.AttributePath) tfsdk.AttributeValidator { - return &intIsAtLeastSumOfValidator{attributePaths} -} - -func (av *intIsAtLeastSumOfValidator) Description(ctx context.Context) string { - return av.MarkdownDescription(ctx) -} - -func (av *intIsAtLeastSumOfValidator) MarkdownDescription(context.Context) string { - return fmt.Sprintf("Ensure that attribute has a value >= sum of: %q", av.attributesToSum) -} - -// Validate runs the following checks: -// 1. Determines if AttributeConfig can be reflected into types.Int64. -// 2. Checks that the AttributeConfig value is >= sum of values of the attributes defined in attributesToSum. -func (av *intIsAtLeastSumOfValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) { - tflog.Debug(ctx, "Validating that attribute has a value at least equal to the attributes to sum", map[string]interface{}{ - "attribute": attrPathToString(req.AttributePath), - "attributesToSum": av.attributesToSum, - }) - - // TODO: Remove once attr.Value interface includes IsNull. - attribConfigValue, err := req.AttributeConfig.ToTerraformValue(ctx) - if err != nil { - resp.Diagnostics.AddError( - "Int at least sum of validator failed", - fmt.Sprintf("Unable to convert attribute config (%s) to terraform value: %s", req.AttributeConfig.Type(ctx).String(), err), - ) - return - } - - if attribConfigValue.IsNull() || !attribConfigValue.IsKnown() { - return - } - - var attrib types.Int64 - - resp.Diagnostics.Append(tfsdk.ValueAs(ctx, req.AttributeConfig, &attrib)...) - if resp.Diagnostics.HasError() { - return - } - - var sumOfAttribs int64 - var attributesToSumPaths []string - - for _, path := range av.attributesToSum { - var attribToSum types.Int64 - resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path, &attribToSum)...) - if resp.Diagnostics.HasError() { - return - } - - sumOfAttribs += attribToSum.Value - attributesToSumPaths = append(attributesToSumPaths, attrPathToString(path)) - } - - if attrib.Value < sumOfAttribs { - attribPath := attrPathToString(req.AttributePath) - - resp.Diagnostics.AddAttributeError( - req.AttributePath, - fmt.Sprintf("Attribute %q is less than summed attributes.", attribPath), - fmt.Sprintf("Attribute %q (%d) cannot be less than %s (%d).", attribPath, attrib.Value, strings.Join(attributesToSumPaths, " + "), sumOfAttribs), - ) - } -} - -// attrPathToString takes all the tftypes.AttributePathStep in a tftypes.AttributePath and concatenates them, -// using `.` as separator. -// -// This should be used only when trying to "print out" a tftypes.AttributePath in a log or an error message. -func attrPathToString(a *tftypes.AttributePath) string { - var res strings.Builder - for pos, step := range a.Steps() { - if pos != 0 { - res.WriteString(".") - } - switch v := step.(type) { - case tftypes.AttributeName: - res.WriteString(string(v)) - case tftypes.ElementKeyString: - res.WriteString(string(v)) - case tftypes.ElementKeyInt: - res.WriteString(strconv.FormatInt(int64(v), 10)) - case tftypes.ElementKeyValue: - res.WriteString(tftypes.Value(v).String()) - } - } - return res.String() -} diff --git a/internal/validators/validators_test.go b/internal/validators/validators_test.go deleted file mode 100644 index 58d18e0e..00000000 --- a/internal/validators/validators_test.go +++ /dev/null @@ -1,147 +0,0 @@ -package validators - -import ( - "context" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/hashicorp/terraform-plugin-framework/attr" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" - "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-go/tftypes" -) - -func TestIsAtLeastSumOfValidator_Validate(t *testing.T) { - t.Parallel() - - req := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("length"), - AttributeConfig: types.Int64{Value: 16}, - Config: tfsdk.Config{ - Schema: tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "min_upper": { - Type: types.Int64Type, - }, - - "min_lower": { - Type: types.Int64Type, - }, - }, - }, - }, - } - - cases := []struct { - name string - reqAttribConfig attr.Value - reqConfigRaw tftypes.Value - attributesToSum []*tftypes.AttributePath - expectDiag bool - expectedValidatorDiags diag.Diagnostics - }{ - { - name: "attribute wrong type", - reqAttribConfig: types.String{Value: "16"}, - expectDiag: true, - }, - { - "attribute less than sum of attribute", - types.Int64{Value: 16}, - tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{ - "min_upper": tftypes.NewValue(tftypes.Number, 17), - }), - []*tftypes.AttributePath{ - tftypes.NewAttributePath().WithAttributeName("min_upper"), - }, - true, - diag.Diagnostics{ - diag.NewAttributeErrorDiagnostic( - tftypes.NewAttributePath().WithAttributeName("length"), - `Attribute "length" is less than summed attributes.`, - `Attribute "length" (16) cannot be less than min_upper (17).`, - ), - }, - }, - { - "attribute less than sum of attributes", - types.Int64{Value: 16}, - tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{ - "min_upper": tftypes.NewValue(tftypes.Number, 10), - "min_lower": tftypes.NewValue(tftypes.Number, 12), - }), - []*tftypes.AttributePath{ - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - }, - true, - diag.Diagnostics{ - diag.NewAttributeErrorDiagnostic( - tftypes.NewAttributePath().WithAttributeName("length"), - `Attribute "length" is less than summed attributes.`, - `Attribute "length" (16) cannot be less than min_upper + min_lower (22).`, - ), - }, - }, - { - "a summed attribute is of invalid type", - types.Int64{Value: 16}, - tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{ - "min_upper": tftypes.NewValue(tftypes.String, "17"), - }), - []*tftypes.AttributePath{ - tftypes.NewAttributePath().WithAttributeName("min_upper"), - }, - true, - diag.Diagnostics{ - diag.NewAttributeErrorDiagnostic( - tftypes.NewAttributePath().WithAttributeName("min_upper"), - `Int64 Type Validation Error`, - `An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer: - -Expected Number value, received tftypes.Value with value: tftypes.String<"17">`, - ), - }, - }, - { - name: "attribute equal to sum of attributes", - reqAttribConfig: types.Int64{Value: 16}, - reqConfigRaw: tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{ - "min_upper": tftypes.NewValue(tftypes.Number, 8), - "min_lower": tftypes.NewValue(tftypes.Number, 8), - }), - attributesToSum: []*tftypes.AttributePath{ - tftypes.NewAttributePath().WithAttributeName("min_upper"), - tftypes.NewAttributePath().WithAttributeName("min_lower"), - }, - expectDiag: false, - }, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - req.AttributeConfig = c.reqAttribConfig - req.Config.Raw = c.reqConfigRaw - resp := tfsdk.ValidateAttributeResponse{ - Diagnostics: diag.Diagnostics{}, - } - - validator := NewIntIsAtLeastSumOfValidator(c.attributesToSum...) - validator.Validate(context.Background(), req, &resp) - - if c.expectDiag { - if len(resp.Diagnostics) != 1 { - t.Errorf("expecting resp diags len: 1, actual resp diags len: %d", len(resp.Diagnostics)) - } - } - - // Only test the contents of diags that are explicitly under the control of the validator. - if c.expectedValidatorDiags != nil { - if !cmp.Equal(c.expectedValidatorDiags, resp.Diagnostics) { - t.Errorf("expecting resp diags: %s, actual resp diags: %s", c.expectedValidatorDiags, resp.Diagnostics) - } - } - }) - } -}