diff --git a/go.mod b/go.mod index 420714c02b..859615813e 100644 --- a/go.mod +++ b/go.mod @@ -15,10 +15,10 @@ require ( github.com/hashicorp/go-hclog v1.2.1 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/hcl/v2 v2.13.0 - github.com/hashicorp/terraform-plugin-framework v0.9.0 - github.com/hashicorp/terraform-plugin-go v0.10.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-go v0.12.0 + github.com/hashicorp/terraform-plugin-log v0.6.0 + github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0 github.com/jinzhu/inflection v1.0.0 github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24 github.com/mitchellh/cli v1.1.4 @@ -88,6 +88,6 @@ require ( golang.org/x/text v0.3.7 // indirect google.golang.org/appengine v1.6.6 // indirect google.golang.org/genproto v0.0.0-20200711021454-869866162049 // 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 eb74e8b005..370d3d415e 100644 --- a/go.sum +++ b/go.sum @@ -161,7 +161,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= @@ -186,17 +185,14 @@ github.com/hashicorp/terraform-exec v0.17.2 h1:EU7i3Fh7vDUI9nNRdMATCEfnm9axzTnad github.com/hashicorp/terraform-exec v0.17.2/go.mod h1:tuIbsL2l4MlwwIZx9HPM+LOV9vVyEfBYu2GsO1uH3/8= github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s= github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM= -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-go v0.9.1/go.mod h1:ItjVSlQs70otlzcCwlPcU8FRXLdO973oYFRZwAOxy8M= -github.com/hashicorp/terraform-plugin-go v0.10.0 h1:FIQDt/AZDSOXnN+znBnLLZA9aFk4/GwL40rwMLnvuTk= -github.com/hashicorp/terraform-plugin-go v0.10.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-log v0.4.1/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-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-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.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.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= @@ -433,9 +429,8 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 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/diag/diag.go b/internal/diag/diag.go index 8939241311..3ac1b78fd8 100644 --- a/internal/diag/diag.go +++ b/internal/diag/diag.go @@ -5,6 +5,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-go/tftypes" ) @@ -15,7 +16,7 @@ const ( summaryInvalidLength = "Invalid length" ) -func NewInvalidValueAttributeError(path *tftypes.AttributePath, detail string) diag.Diagnostic { +func NewInvalidValueAttributeError(path path.Path, detail string) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidValue, @@ -23,7 +24,7 @@ func NewInvalidValueAttributeError(path *tftypes.AttributePath, detail string) d ) } -func NewIncorrectValueTypeAttributeError(path *tftypes.AttributePath, v attr.Value) diag.Diagnostic { +func NewIncorrectValueTypeAttributeError(path path.Path, v attr.Value) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidValueType, @@ -38,7 +39,7 @@ func NewIncorrectValueTypeResourceConfigError(t tftypes.Type) diag.Diagnostic { ) } -func NewUnableToConvertValueTypeAttributeError(path *tftypes.AttributePath, err error) diag.Diagnostic { +func NewUnableToConvertValueTypeAttributeError(path path.Path, err error) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidValueType, @@ -53,7 +54,7 @@ func NewUnableToConvertValueTypeResourceConfigError(err error) diag.Diagnostic { ) } -func NewUnableToObtainValueAttributeError(path *tftypes.AttributePath, err error) diag.Diagnostic { +func NewUnableToObtainValueAttributeError(path path.Path, err error) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryNoTerraformValue, @@ -61,7 +62,7 @@ func NewUnableToObtainValueAttributeError(path *tftypes.AttributePath, err error ) } -func NewInvalidLengthBetweenAttributeError(path *tftypes.AttributePath, min, max, len int) diag.Diagnostic { +func NewInvalidLengthBetweenAttributeError(path path.Path, min, max, len int) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidLength, @@ -69,7 +70,7 @@ func NewInvalidLengthBetweenAttributeError(path *tftypes.AttributePath, min, max ) } -func NewInvalidLengthAtLeastAttributeError(path *tftypes.AttributePath, min, len int) diag.Diagnostic { +func NewInvalidLengthAtLeastAttributeError(path path.Path, min, len int) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidLength, @@ -77,7 +78,7 @@ func NewInvalidLengthAtLeastAttributeError(path *tftypes.AttributePath, min, len ) } -func NewInvalidLengthAtMostAttributeError(path *tftypes.AttributePath, max, len int) diag.Diagnostic { +func NewInvalidLengthAtMostAttributeError(path path.Path, max, len int) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidLength, @@ -85,7 +86,7 @@ func NewInvalidLengthAtMostAttributeError(path *tftypes.AttributePath, max, len ) } -func NewInvalidFormatAttributeError(path *tftypes.AttributePath, detail string) diag.Diagnostic { +func NewInvalidFormatAttributeError(path path.Path, detail string) diag.Diagnostic { return diag.NewAttributeErrorDiagnostic( path, summaryInvalidLength, diff --git a/internal/generic/default_value_test.go b/internal/generic/default_value_test.go index 335a785c11..a85d5cda36 100644 --- a/internal/generic/default_value_test.go +++ b/internal/generic/default_value_test.go @@ -7,9 +7,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" + "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/hashicorp/terraform-provider-awscc/internal/tfresource" ) @@ -268,7 +268,7 @@ func TestDefaultValue(t *testing.T) { t.Run(name, func(t *testing.T) { ctx := context.TODO() request := tfsdk.ModifyAttributePlanRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributePlan: test.plannedValue, AttributeState: test.currentValue, } diff --git a/internal/generic/jsonstring.go b/internal/generic/jsonstring.go index 8bda59652f..9c32930fd1 100644 --- a/internal/generic/jsonstring.go +++ b/internal/generic/jsonstring.go @@ -7,7 +7,9 @@ import ( "reflect" "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" "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-go/tftypes" ) @@ -19,7 +21,7 @@ const ( ) var ( - _ attr.TypeWithValidate = JSONStringType + _ xattr.TypeWithValidate = JSONStringType ) func (t jsonStringType) TerraformType(_ context.Context) tftypes.Type { @@ -66,12 +68,12 @@ func (t jsonStringType) String() string { return "JSONStringType" } -func (t jsonStringType) Validate(ctx context.Context, v tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics { +func (t jsonStringType) Validate(ctx context.Context, v tftypes.Value, p path.Path) diag.Diagnostics { var diags diag.Diagnostics if !v.Type().Is(tftypes.String) { diags.AddAttributeError( - path, + p, "Duration 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:\n\n"+ fmt.Sprintf("Expected String value, received %T with value: %v", v, v), @@ -89,7 +91,7 @@ func (t jsonStringType) Validate(ctx context.Context, v tftypes.Value, path *tft if err != nil { diags.AddAttributeError( - path, + p, "Duration 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:\n\n"+ fmt.Sprintf("Cannot convert value to String: %s", err), @@ -106,7 +108,7 @@ func (t jsonStringType) Validate(ctx context.Context, v tftypes.Value, path *tft if err != nil { diags.AddAttributeError( - path, + p, "JSONString Type Validation Error", fmt.Sprintf("Value %q cannot be parsed as a JSON string.", v), ) diff --git a/internal/generic/jsonstring_test.go b/internal/generic/jsonstring_test.go index 25f45f9ad2..48005c2ca9 100644 --- a/internal/generic/jsonstring_test.go +++ b/internal/generic/jsonstring_test.go @@ -6,6 +6,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -97,7 +98,7 @@ func TestJSONStringTypeValidate(t *testing.T) { t.Run(name, func(t *testing.T) { ctx := context.TODO() - attributePath := tftypes.NewAttributePath().WithAttributeName("test") + attributePath := path.Root("test") diags := JSONStringType.Validate(ctx, test.val, attributePath) if !diags.HasError() && test.expectError { @@ -158,7 +159,7 @@ func TestJSONStringTypeAttributePlanModifier(t *testing.T) { t.Run(name, func(t *testing.T) { ctx := context.TODO() request := tfsdk.ModifyAttributePlanRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributePlan: test.plannedValue, AttributeState: test.currentValue, } diff --git a/internal/generic/multiset_test.go b/internal/generic/multiset_test.go index a2407a1d87..156d36fd9b 100644 --- a/internal/generic/multiset_test.go +++ b/internal/generic/multiset_test.go @@ -6,9 +6,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" + "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/hashicorp/terraform-provider-awscc/internal/tfresource" ) @@ -118,7 +118,7 @@ func TestMultiset(t *testing.T) { t.Run(name, func(t *testing.T) { ctx := context.TODO() request := tfsdk.ModifyAttributePlanRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributePlan: test.plannedValue, AttributeState: test.currentValue, } diff --git a/internal/generic/plural_data_source.go b/internal/generic/plural_data_source.go index cd93cdabb4..6594e19295 100644 --- a/internal/generic/plural_data_source.go +++ b/internal/generic/plural_data_source.go @@ -101,7 +101,7 @@ func (pd *pluralDataSource) list(ctx context.Context, conn *cloudcontrol.Client) // cfnTypeContext injects the CloudFormation type name into logger contexts. func (pd *pluralDataSource) cfnTypeContext(ctx context.Context) context.Context { - ctx = tflog.With(ctx, LoggingKeyCFNType, pd.dataSourceType.cfTypeName) + ctx = tflog.SetField(ctx, LoggingKeyCFNType, pd.dataSourceType.cfTypeName) return ctx } diff --git a/internal/generic/resource.go b/internal/generic/resource.go index 6200d2d058..239e522f2c 100644 --- a/internal/generic/resource.go +++ b/internal/generic/resource.go @@ -12,8 +12,8 @@ import ( cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types" hclog "github.com/hashicorp/go-hclog" "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-go/tftypes" "github.com/hashicorp/terraform-plugin-log/tflog" tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol" "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" @@ -118,7 +118,7 @@ func resourceWithSyntheticIDAttribute(v bool) ResourceTypeOptionsFunc { // the previous calls' values. func resourceWithWriteOnlyPropertyPaths(v []string) ResourceTypeOptionsFunc { return func(o *resourceType) error { - writeOnlyAttributePaths := make([]*tftypes.AttributePath, 0) + writeOnlyAttributePaths := make([]*path.Path, 0) for _, writeOnlyPropertyPath := range v { writeOnlyAttributePath, err := o.propertyPathToAttributePath(writeOnlyPropertyPath) @@ -302,7 +302,7 @@ type resourceType struct { cfToTfNameMap map[string]string // Map of CloudFormation property name to Terraform attribute name isImmutableType bool // Resources cannot be updated and must be recreated syntheticIDAttribute bool // Resource type has a synthetic ID attribute - writeOnlyAttributePaths []*tftypes.AttributePath // Paths to any write-only attributes + writeOnlyAttributePaths []*path.Path // Paths to any write-only attributes createTimeout time.Duration // Maximum wait time for resource creation updateTimeout time.Duration // Maximum wait time for resource update deleteTimeout time.Duration // Maximum wait time for resource deletion @@ -341,7 +341,7 @@ func (rt *resourceType) NewResource(ctx context.Context, provider tfsdk.Provider } // propertyPathToAttributePath returns the AttributePath for the specified JSON Pointer property path. -func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*tftypes.AttributePath, error) { +func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*path.Path, error) { segments := strings.Split(propertyPath, "/") if got, expected := len(segments), 3; got < expected { @@ -356,7 +356,7 @@ func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*tftyp return nil, fmt.Errorf("expected %q for the second property path segment, got: %q", expected, got) } - attributePath := tftypes.NewAttributePath() + attributePath := path.Empty() for _, segment := range segments[2:] { switch segment { @@ -368,11 +368,11 @@ func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*tftyp if !ok { return nil, fmt.Errorf("attribute name mapping not found: %s", segment) } - attributePath = attributePath.WithAttributeName(attributeName) + attributePath = attributePath.AtName(attributeName) } } - return attributePath, nil + return &attributePath, nil } // Implements tfsdk.Resource. @@ -391,7 +391,7 @@ func newGenericResource(provider tfsdk.Provider, resourceType *resourceType) tfs var ( // Path to the "id" attribute which uniquely (for a specific resource type) identifies the resource. // This attribute is required for acceptance testing. - idAttributePath = tftypes.NewAttributePath().WithAttributeName("id") + idAttributePath = path.Root("id") ) func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceRequest, response *tfsdk.CreateResourceResponse) { @@ -551,7 +551,7 @@ func (r *resource) Read(ctx context.Context, request tfsdk.ReadResourceRequest, // Copy over any write-only values. // They can only be in the current state. for _, path := range r.resourceType.writeOnlyAttributePaths { - err = CopyValueAtPath(ctx, &response.State, &request.State, path) + err = CopyValueAtPath(ctx, &response.State, &request.State, *path) if err != nil { response.Diagnostics.AddError( @@ -816,7 +816,7 @@ func (r *resource) populateUnknownValues(ctx context.Context, id string, state * // cfnTypeContext injects the CloudFormation type name into logger contexts. func (r *resource) cfnTypeContext(ctx context.Context) context.Context { - ctx = tflog.With(ctx, LoggingKeyCFNType, r.resourceType.cfTypeName) + ctx = tflog.SetField(ctx, LoggingKeyCFNType, r.resourceType.cfTypeName) return ctx } diff --git a/internal/generic/resource_test.go b/internal/generic/resource_test.go index 7725e28af3..f30f38188c 100644 --- a/internal/generic/resource_test.go +++ b/internal/generic/resource_test.go @@ -3,7 +3,7 @@ package generic import ( "testing" - "github.com/hashicorp/terraform-plugin-go/tftypes" + "github.com/hashicorp/terraform-plugin-framework/path" ) func TestPropertyPathToAttributePath(t *testing.T) { @@ -11,7 +11,7 @@ func TestPropertyPathToAttributePath(t *testing.T) { TestName string PropertyPath string ExpectedError bool - ExpectedValue *tftypes.AttributePath + ExpectedValue path.Path }{ { TestName: "empty property path", @@ -35,12 +35,12 @@ func TestPropertyPathToAttributePath(t *testing.T) { { TestName: "one path segment", PropertyPath: "/properties/Tags", - ExpectedValue: tftypes.NewAttributePath().WithAttributeName("tags"), + ExpectedValue: path.Root("tags"), }, { TestName: "two path segments", PropertyPath: "/properties/BasicAuthParameters/Password", - ExpectedValue: tftypes.NewAttributePath().WithAttributeName("basic_auth_parameters").WithAttributeName("password"), + ExpectedValue: path.Root("basic_auth_parameters").AtName("password"), }, { TestName: "empty segment", @@ -74,7 +74,7 @@ func TestPropertyPathToAttributePath(t *testing.T) { t.Fatalf("unexpected error from propertyPathToAttributePath: %s", err) } - if !attributePath.Equal(testCase.ExpectedValue) { + if err == nil && !attributePath.Equal(testCase.ExpectedValue) { t.Errorf("got: %s, expected: %s", attributePath, testCase.ExpectedValue) } }) diff --git a/internal/generic/singular_data_source.go b/internal/generic/singular_data_source.go index 3872d1cb95..e567f80589 100644 --- a/internal/generic/singular_data_source.go +++ b/internal/generic/singular_data_source.go @@ -159,7 +159,7 @@ func (sd *singularDataSource) setId(ctx context.Context, val string, state *tfsd // cfnTypeContext injects the CloudFormation type name into logger contexts. func (sd *singularDataSource) cfnTypeContext(ctx context.Context) context.Context { - ctx = tflog.With(ctx, LoggingKeyCFNType, sd.dataSourceType.cfTypeName) + ctx = tflog.SetField(ctx, LoggingKeyCFNType, sd.dataSourceType.cfTypeName) return ctx } diff --git a/internal/generic/state.go b/internal/generic/state.go index 5a7b52a999..058c69d6fa 100644 --- a/internal/generic/state.go +++ b/internal/generic/state.go @@ -4,21 +4,21 @@ import ( "context" "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/tfsdk" - "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" ) // CopyValueAtPath copies the value at a specified path from source State to destination State. -func CopyValueAtPath(ctx context.Context, dst, src *tfsdk.State, path *tftypes.AttributePath) error { +func CopyValueAtPath(ctx context.Context, dst, src *tfsdk.State, p path.Path) error { var val attr.Value - diags := src.GetAttribute(ctx, path, &val) + diags := src.GetAttribute(ctx, p, &val) if diags.HasError() { return tfresource.DiagsError(diags) } - diags = dst.SetAttribute(ctx, path, val) + diags = dst.SetAttribute(ctx, p, val) if diags.HasError() { return tfresource.DiagsError(diags) diff --git a/internal/generic/state_test.go b/internal/generic/state_test.go index a4422b3ec8..0c8278fadd 100644 --- a/internal/generic/state_test.go +++ b/internal/generic/state_test.go @@ -6,6 +6,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -260,7 +261,7 @@ func TestCopyValueAtPath(t *testing.T) { TestName string SrcState tfsdk.State DstState tfsdk.State - Path *tftypes.AttributePath + Path path.Path ExpectedError bool ExpectedState tfsdk.State }{ @@ -298,7 +299,7 @@ func TestCopyValueAtPath(t *testing.T) { }), Schema: testSimpleSchema, }, - Path: tftypes.NewAttributePath().WithAttributeName("number"), + Path: path.Root("number"), ExpectedState: tfsdk.State{ Raw: tftypes.NewValue(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ @@ -350,7 +351,7 @@ func TestCopyValueAtPath(t *testing.T) { }), Schema: testSimpleSchema, }, - Path: tftypes.NewAttributePath().WithAttributeName("arn"), + Path: path.Root("arn"), ExpectedState: tfsdk.State{ Raw: tftypes.NewValue(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ @@ -402,7 +403,7 @@ func TestCopyValueAtPath(t *testing.T) { }), Schema: testSimpleSchema, }, - Path: tftypes.NewAttributePath().WithAttributeName("arn"), + Path: path.Root("arn"), ExpectedState: tfsdk.State{ Raw: tftypes.NewValue(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ @@ -454,7 +455,7 @@ func TestCopyValueAtPath(t *testing.T) { }), Schema: testSimpleSchema, }, - Path: tftypes.NewAttributePath().WithAttributeName("height"), + Path: path.Root("height"), ExpectedError: true, }, } diff --git a/internal/generic/unknown.go b/internal/generic/unknown.go index 12dcc3d1a7..58eb118315 100644 --- a/internal/generic/unknown.go +++ b/internal/generic/unknown.go @@ -2,9 +2,14 @@ package generic import ( "context" + "fmt" + "github.com/hashicorp/terraform-plugin-framework/attr" + "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-go/tftypes" + "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" ) // UnknownValuePaths returns all the paths to all the unknown values in the specified Terraform Value. @@ -73,20 +78,26 @@ func unknownValuePaths(ctx context.Context, path *tftypes.AttributePath, val tft func SetUnknownValuesFromResourceModel(ctx context.Context, state *tfsdk.State, unknowns []*tftypes.AttributePath, resourceModel string, cfToTfNameMap map[string]string) error { // Get the Terraform Value of the ResourceModel. translator := toTerraform{cfToTfNameMap: cfToTfNameMap} - schema := &state.Schema - val, err := translator.FromString(ctx, schema, resourceModel) + schema := state.Schema + val, err := translator.FromString(ctx, &schema, resourceModel) if err != nil { return err } src := tfsdk.State{ - Schema: *schema, + Schema: schema, Raw: val, } // Copy all unknown values from the ResourceModel to destination State. for _, path := range unknowns { + path, diags := attributePath(ctx, path, schema) + + if diags.HasError() { + return tfresource.DiagsError(diags) + } + err = CopyValueAtPath(ctx, state, &src, path) if err != nil { @@ -96,3 +107,118 @@ func SetUnknownValuesFromResourceModel(ctx context.Context, state *tfsdk.State, return nil } + +// Lifted from github.com/hashicorp/terraform-plugin-framework/tfsdk/tftypes_attribute_path.go. +// attributePath returns the path.Path equivalent of a *tftypes.AttributePath. +func attributePath(ctx context.Context, tfType *tftypes.AttributePath, schema tfsdk.Schema) (path.Path, diag.Diagnostics) { + fwPath := path.Empty() + + for tfTypeStepIndex, tfTypeStep := range tfType.Steps() { + currentTfTypeSteps := tfType.Steps()[:tfTypeStepIndex+1] + currentTfTypePath := tftypes.NewAttributePathWithSteps(currentTfTypeSteps) + attrType, err := schema.AttributeTypeAtPath(currentTfTypePath) + + if err != nil { + return path.Empty(), diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Convert Attribute Path", + "An unexpected error occurred while trying to convert an attribute path. "+ + "This is an error in terraform-plugin-framework used by the provider. "+ + "Please report the following to the provider developers.\n\n"+ + // Since this is an error with the attribute path + // conversion, we cannot return a protocol path-based + // diagnostic. Returning a framework human-readable + // representation seems like the next best thing to do. + fmt.Sprintf("Attribute Path: %s\n", currentTfTypePath.String())+ + fmt.Sprintf("Original Error: %s", err), + ), + } + } + + fwStep, err := attributePathStep(ctx, tfTypeStep, attrType) + + if err != nil { + return path.Empty(), diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Convert Attribute Path", + "An unexpected error occurred while trying to convert an attribute path. "+ + "This is either an error in terraform-plugin-framework or a custom attribute type used by the provider. "+ + "Please report the following to the provider developers.\n\n"+ + // Since this is an error with the attribute path + // conversion, we cannot return a protocol path-based + // diagnostic. Returning a framework human-readable + // representation seems like the next best thing to do. + fmt.Sprintf("Attribute Path: %s\n", currentTfTypePath.String())+ + fmt.Sprintf("Original Error: %s", err), + ), + } + } + + // In lieu of creating a path.NewPathFromSteps function, this path + // building logic is inlined to not expand the path package API. + switch fwStep := fwStep.(type) { + case path.PathStepAttributeName: + fwPath = fwPath.AtName(string(fwStep)) + case path.PathStepElementKeyInt: + fwPath = fwPath.AtListIndex(int(fwStep)) + case path.PathStepElementKeyString: + fwPath = fwPath.AtMapKey(string(fwStep)) + case path.PathStepElementKeyValue: + fwPath = fwPath.AtSetValue(fwStep.Value) + default: + return fwPath, diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Convert Attribute Path", + "An unexpected error occurred while trying to convert an attribute path. "+ + "This is an error in terraform-plugin-framework used by the provider. "+ + "Please report the following to the provider developers.\n\n"+ + // Since this is an error with the attribute path + // conversion, we cannot return a protocol path-based + // diagnostic. Returning a framework human-readable + // representation seems like the next best thing to do. + fmt.Sprintf("Attribute Path: %s\n", currentTfTypePath.String())+ + fmt.Sprintf("Original Error: unknown path.PathStep type: %#v", fwStep), + ), + } + } + } + + return fwPath, nil +} + +// Lifted from github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes/attribute_path_step.go. +func attributePathStep(ctx context.Context, tfType tftypes.AttributePathStep, attrType attr.Type) (path.PathStep, error) { + switch tfType := tfType.(type) { + case tftypes.AttributeName: + return path.PathStepAttributeName(string(tfType)), nil + case tftypes.ElementKeyInt: + return path.PathStepElementKeyInt(int64(tfType)), nil + case tftypes.ElementKeyString: + return path.PathStepElementKeyString(string(tfType)), nil + case tftypes.ElementKeyValue: + attrValue, err := value(ctx, tftypes.Value(tfType), attrType) + + if err != nil { + return nil, fmt.Errorf("unable to create PathStepElementKeyValue from tftypes.Value: %w", err) + } + + return path.PathStepElementKeyValue{Value: attrValue}, nil + default: + return nil, fmt.Errorf("unknown tftypes.AttributePathStep: %#v", tfType) + } +} + +// Lifted from github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes/value.go. +func value(ctx context.Context, tfType tftypes.Value, attrType attr.Type) (attr.Value, error) { + if attrType == nil { + return nil, fmt.Errorf("unable to convert tftypes.Value (%s) to attr.Value: missing attr.Type", tfType.String()) + } + + attrValue, err := attrType.ValueFromTerraform(ctx, tfType) + + if err != nil { + return nil, fmt.Errorf("unable to convert tftypes.Value (%s) to attr.Value: %w", tfType.String(), err) + } + + return attrValue, nil +} diff --git a/internal/generic/unknown_test.go b/internal/generic/unknown_test.go index 9e7e6c100a..6f0874de64 100644 --- a/internal/generic/unknown_test.go +++ b/internal/generic/unknown_test.go @@ -64,7 +64,7 @@ func TestUnknowns(t *testing.T) { } } -func TestUnknowsSetValue(t *testing.T) { +func TestUnknownsSetValue(t *testing.T) { testCases := []struct { TestName string State tfsdk.State diff --git a/internal/types/duration.go b/internal/types/duration.go index 42e5276031..9903fff347 100644 --- a/internal/types/duration.go +++ b/internal/types/duration.go @@ -6,7 +6,9 @@ import ( "time" "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-go/tftypes" ) @@ -17,7 +19,7 @@ const ( ) var ( - _ attr.TypeWithValidate = DurationType + _ xattr.TypeWithValidate = DurationType ) func (d durationType) TerraformType(_ context.Context) tftypes.Type { @@ -61,7 +63,7 @@ func (d durationType) String() string { } // Validate implements type validation. -func (d durationType) Validate(ctx context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics { +func (d durationType) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics { var diags diag.Diagnostics if !in.Type().Is(tftypes.String) { diff --git a/internal/types/duration_test.go b/internal/types/duration_test.go index c2f3e05b94..2237843104 100644 --- a/internal/types/duration_test.go +++ b/internal/types/duration_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" ) @@ -89,8 +90,7 @@ func TestDurationTypeValidate(t *testing.T) { t.Run(name, func(t *testing.T) { ctx := context.TODO() - attributePath := tftypes.NewAttributePath().WithAttributeName("test") - diags := DurationType.Validate(ctx, test.val, attributePath) + diags := DurationType.Validate(ctx, test.val, path.Root("test")) if !diags.HasError() && test.expectError { t.Fatal("expected error, got no error") diff --git a/internal/validate/arn.go b/internal/validate/arn.go index 292d24f314..ca89fa90d7 100644 --- a/internal/validate/arn.go +++ b/internal/validate/arn.go @@ -11,9 +11,7 @@ import ( ) // arnValidator validates that a string is an Amazon Resource Name (ARN). -type arnValidator struct { - tfsdk.AttributeValidator -} +type arnValidator struct{} // Description describes the validation in plain text formatting. func (validator arnValidator) Description(_ context.Context) string { @@ -48,9 +46,7 @@ func ARN() tfsdk.AttributeValidator { } // iamPolicyARNValidator validates that a string is a valid IAM Policy ARN. -type iamPolicyARNValidator struct { - tfsdk.AttributeValidator -} +type iamPolicyARNValidator struct{} func (validator iamPolicyARNValidator) Description(_ context.Context) string { return "string must be an IAM Policy ARN" diff --git a/internal/validate/arn_test.go b/internal/validate/arn_test.go index 723eba4021..ef071e669f 100644 --- a/internal/validate/arn_test.go +++ b/internal/validate/arn_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -55,7 +56,7 @@ func TestARNValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -121,7 +122,7 @@ func TestIAMPolicyARNValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/array.go b/internal/validate/array.go index ddc6666510..b9e27edbcc 100644 --- a/internal/validate/array.go +++ b/internal/validate/array.go @@ -6,16 +6,14 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "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" ccdiag "github.com/hashicorp/terraform-provider-awscc/internal/diag" ) // arrayLenBetweenValidator validates that an array (List/Set) Attribute's length is in a range. type arrayLenBetweenValidator struct { - tfsdk.AttributeValidator - minItems, maxItems int } @@ -59,8 +57,6 @@ func ArrayLenBetween(minItems, maxItems int) tfsdk.AttributeValidator { // arrayLenAtLeastValidator validates that an array (List/Set) Attribute's length is at least a certain value. type arrayLenAtLeastValidator struct { - tfsdk.AttributeValidator - minItems int } @@ -103,8 +99,6 @@ func ArrayLenAtLeast(minItems int) tfsdk.AttributeValidator { // arrayLenAtMostValidator validates that an array (List/Set) Attribute's length is at most a certain value. type arrayLenAtMostValidator struct { - tfsdk.AttributeValidator - maxItems int } @@ -147,19 +141,14 @@ func ArrayLenAtMost(maxItems int) tfsdk.AttributeValidator { } } -type arrayKeyer func(context.Context, *tftypes.AttributePath, int, attr.Value) (*tftypes.AttributePath, diag.Diagnostic) +type arrayKeyer func(context.Context, path.Path, int, attr.Value) (path.Path, diag.Diagnostic) -func listKeyer(ctx context.Context, path *tftypes.AttributePath, i int, v attr.Value) (*tftypes.AttributePath, diag.Diagnostic) { - return path.WithElementKeyInt(i), nil +func listKeyer(ctx context.Context, p path.Path, i int, v attr.Value) (path.Path, diag.Diagnostic) { + return p.AtListIndex(i), nil } -func setKeyer(ctx context.Context, path *tftypes.AttributePath, i int, v attr.Value) (*tftypes.AttributePath, diag.Diagnostic) { - val, err := v.ToTerraformValue(ctx) - if err != nil { - return nil, ccdiag.NewUnableToObtainValueAttributeError(path, err) - } - - return path.WithElementKeyValue(val), nil +func setKeyer(ctx context.Context, p path.Path, i int, v attr.Value) (path.Path, diag.Diagnostic) { + return p.AtSetValue(v), nil } func validateArray(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) ([]attr.Value, arrayKeyer, bool) { diff --git a/internal/validate/array_test.go b/internal/validate/array_test.go index b8db4c889b..578ca53952 100644 --- a/internal/validate/array_test.go +++ b/internal/validate/array_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -134,7 +135,7 @@ func TestArrayLenBetweenValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -255,7 +256,7 @@ func TestArrayLenAtLeastValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -366,7 +367,7 @@ func TestArrayLenAtMostValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/float.go b/internal/validate/float.go index 035f0b10d7..28e6126790 100644 --- a/internal/validate/float.go +++ b/internal/validate/float.go @@ -11,8 +11,6 @@ import ( // floatBetweenValidator validates that an float Attribute's value is in a range. type floatBetweenValidator struct { - tfsdk.AttributeValidator - min, max float64 } @@ -57,8 +55,6 @@ func FloatBetween(min, max float64) tfsdk.AttributeValidator { // floatAtLeastValidator validates that an float Attribute's value is at least a certain value. type floatAtLeastValidator struct { - tfsdk.AttributeValidator - min float64 } @@ -98,8 +94,6 @@ func FloatAtLeast(min float64) tfsdk.AttributeValidator { // floatAtMostValidator validates that an float Attribute's value is at most a certain value. type floatAtMostValidator struct { - tfsdk.AttributeValidator - max float64 } diff --git a/internal/validate/float_test.go b/internal/validate/float_test.go index 3e1c64b8c4..5c367866ba 100644 --- a/internal/validate/float_test.go +++ b/internal/validate/float_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -114,7 +115,7 @@ func TestFloatBetweenValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -205,7 +206,7 @@ func TestFloatAtLeastValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -296,7 +297,7 @@ func TestFloatAtMostValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/for_each.go b/internal/validate/for_each.go index ca6b958dfa..a5764489e7 100644 --- a/internal/validate/for_each.go +++ b/internal/validate/for_each.go @@ -9,8 +9,6 @@ import ( // arrayForEachValidator validates that a List Attribute's contents all satisfy the included validator. type arrayForEachValidator struct { - tfsdk.AttributeValidator - validator tfsdk.AttributeValidator } diff --git a/internal/validate/for_each_test.go b/internal/validate/for_each_test.go index f6445d32d4..e399ebbc2b 100644 --- a/internal/validate/for_each_test.go +++ b/internal/validate/for_each_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "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" @@ -17,7 +18,7 @@ import ( func TestArrayForEachValidator(t *testing.T) { t.Parallel() - rootPath := tftypes.NewAttributePath().WithAttributeName("test") + rootPath := path.Root("test") type testCase struct { val tftypes.Value diff --git a/internal/validate/int.go b/internal/validate/int.go index 74b1700960..491457a0be 100644 --- a/internal/validate/int.go +++ b/internal/validate/int.go @@ -5,16 +5,14 @@ import ( "fmt" "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" ccdiag "github.com/hashicorp/terraform-provider-awscc/internal/diag" ) // intBetweenValidator validates that an integer Attribute's value is in a range. type intBetweenValidator struct { - tfsdk.AttributeValidator - min, max int64 } @@ -59,8 +57,6 @@ func IntBetween(min, max int64) tfsdk.AttributeValidator { // intAtLeastValidator validates that an integer Attribute's value is at least a certain value. type intAtLeastValidator struct { - tfsdk.AttributeValidator - min int64 } @@ -100,8 +96,6 @@ func IntAtLeast(min int64) tfsdk.AttributeValidator { // intAtMostValidator validates that an integer Attribute's value is at most a certain value. type intAtMostValidator struct { - tfsdk.AttributeValidator - max int64 } @@ -141,8 +135,6 @@ func IntAtMost(max int64) tfsdk.AttributeValidator { // intInSliceValidator validates that an integer Attribute's value matches the value of an element in the valid slice. type intInSliceValidator struct { - tfsdk.AttributeValidator - valid []int } @@ -177,9 +169,9 @@ func (validator intInSliceValidator) Validate(ctx context.Context, request tfsdk } -func newIntNotInSliceError(path *tftypes.AttributePath, valid []int, value int64) diag.Diagnostic { +func newIntNotInSliceError(p path.Path, valid []int, value int64) diag.Diagnostic { return ccdiag.NewInvalidValueAttributeError( - path, + p, fmt.Sprintf("expected value to be one of %v, got %d", valid, value), ) } @@ -191,8 +183,8 @@ func IntInSlice(valid []int) tfsdk.AttributeValidator { } } -func newNotAnIntegerValueError(path *tftypes.AttributePath) diag.Diagnostic { - return ccdiag.NewInvalidValueAttributeError(path, "Not an integer") +func newNotAnIntegerValueError(p path.Path) diag.Diagnostic { + return ccdiag.NewInvalidValueAttributeError(p, "Not an integer") } func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) { diff --git a/internal/validate/int_test.go b/internal/validate/int_test.go index 236682e578..8e122298a6 100644 --- a/internal/validate/int_test.go +++ b/internal/validate/int_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -123,7 +124,7 @@ func TestIntBetweenValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -210,7 +211,7 @@ func TestIntAtLeastValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -297,7 +298,7 @@ func TestIntAtMostValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -371,7 +372,7 @@ func TestIntInSliceValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/meta.go b/internal/validate/meta.go index e09112428f..74c4cf5b9c 100644 --- a/internal/validate/meta.go +++ b/internal/validate/meta.go @@ -9,8 +9,6 @@ import ( // allValidator validates that a string Attribute's length is in a range. type allValidator struct { - tfsdk.AttributeValidator - validators []tfsdk.AttributeValidator } diff --git a/internal/validate/meta_test.go b/internal/validate/meta_test.go index 0c7186cc32..c581e9828c 100644 --- a/internal/validate/meta_test.go +++ b/internal/validate/meta_test.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "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" @@ -15,7 +16,7 @@ import ( func TestAllValidator(t *testing.T) { t.Parallel() - rootPath := tftypes.NewAttributePath().WithAttributeName("test") + rootPath := path.Root("test") type testCase struct { val tftypes.Value diff --git a/internal/validate/required.go b/internal/validate/required.go index dd110fc3c5..15c275540d 100644 --- a/internal/validate/required.go +++ b/internal/validate/required.go @@ -108,8 +108,6 @@ func OneOfRequired(fs ...RequiredAttributesFunc) RequiredAttributesFunc { // requiredAttributesValidator validates that required Attributes are specified. type requiredAttributesValidator struct { - tfsdk.AttributeValidator - fs []RequiredAttributesFunc } @@ -213,7 +211,7 @@ func (validator requiredAttributesValidator) Validate(ctx context.Context, reque var vals map[string]tftypes.Value if err := val.As(&vals); err != nil { response.Diagnostics.Append(ccdiag.NewUnableToConvertValueTypeAttributeError( - request.AttributePath.WithElementKeyInt(i), + request.AttributePath.AtListIndex(i), err, )) @@ -245,8 +243,6 @@ func RequiredAttributes(fs ...RequiredAttributesFunc) tfsdk.AttributeValidator { // requiredAttributesResourceConfigValidator validates that resource schema-level required Attributes are specified. type resourceConfigRequiredAttributesValidator struct { - tfsdk.ResourceConfigValidator - fs []RequiredAttributesFunc } @@ -261,7 +257,7 @@ func (validator resourceConfigRequiredAttributesValidator) MarkdownDescription(c } // Validate performs the validation. -func (validator resourceConfigRequiredAttributesValidator) Validate(ctx context.Context, request tfsdk.ValidateResourceConfigRequest, response *tfsdk.ValidateResourceConfigResponse) { +func (validator resourceConfigRequiredAttributesValidator) ValidateResource(ctx context.Context, request tfsdk.ValidateResourceConfigRequest, response *tfsdk.ValidateResourceConfigResponse) { val := request.Config.Raw if val.IsNull() || !val.IsFullyKnown() { diff --git a/internal/validate/required_test.go b/internal/validate/required_test.go index 98eb784905..9cd16c6b19 100644 --- a/internal/validate/required_test.go +++ b/internal/validate/required_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -346,7 +347,7 @@ func TestRequiredAttributesValidator_Object(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -508,7 +509,7 @@ func TestRequiredAttributesValidator_List(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -669,7 +670,7 @@ func TestRequiredAttributesValidator_Set(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -792,7 +793,7 @@ func TestResourceConfigRequiredAttributesValidator(t *testing.T) { }, } response := tfsdk.ValidateResourceConfigResponse{} - ResourceConfigRequiredAttributes(test.fs...).Validate(ctx, request, &response) + ResourceConfigRequiredAttributes(test.fs...).ValidateResource(ctx, request, &response) if !response.Diagnostics.HasError() && test.expectError { t.Fatal("expected error, got no error") diff --git a/internal/validate/string.go b/internal/validate/string.go index c93f415e8e..1a2800475d 100644 --- a/internal/validate/string.go +++ b/internal/validate/string.go @@ -7,16 +7,14 @@ import ( "regexp" "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" ccdiag "github.com/hashicorp/terraform-provider-awscc/internal/diag" ) // stringLenBetweenValidator validates that a string Attribute's length is in a range. type stringLenBetweenValidator struct { - tfsdk.AttributeValidator - minLength, maxLength int } @@ -60,8 +58,6 @@ func StringLenBetween(minLength, maxLength int) tfsdk.AttributeValidator { // stringLenAtLeastValidator validates that a string Attribute's length is at least a certain value. type stringLenAtLeastValidator struct { - tfsdk.AttributeValidator - minLength int } @@ -104,8 +100,6 @@ func StringLenAtLeast(minLength int) tfsdk.AttributeValidator { // stringLenAtMostValidator validates that a string Attribute's length is at most a certain value. type stringLenAtMostValidator struct { - tfsdk.AttributeValidator - maxLength int } @@ -148,8 +142,6 @@ func StringLenAtMost(maxLength int) tfsdk.AttributeValidator { // stringInSliceValidator validates that a string Attribute's value matches the value of an element in the valid slice. type stringInSliceValidator struct { - tfsdk.AttributeValidator - valid []string } @@ -183,9 +175,9 @@ func (validator stringInSliceValidator) Validate(ctx context.Context, request tf )) } -func newStringNotInSliceError(path *tftypes.AttributePath, valid []string, value string) diag.Diagnostic { +func newStringNotInSliceError(p path.Path, valid []string, value string) diag.Diagnostic { return ccdiag.NewInvalidValueAttributeError( - path, + p, fmt.Sprintf("expected value to be one of %v, got %s", valid, value), ) } @@ -199,8 +191,6 @@ func StringInSlice(valid []string) tfsdk.AttributeValidator { // stringMatchValidator validates that a string Attribute's value matches the specified regular expression. type stringMatchValidator struct { - tfsdk.AttributeValidator - re *regexp.Regexp message string } @@ -244,9 +234,7 @@ func StringMatch(re *regexp.Regexp, message string) tfsdk.AttributeValidator { } // stringIsJsonObjectValidator validates that a string Attribute's value is a valid JSON object. -type stringIsJsonObjectValidator struct { - tfsdk.AttributeValidator -} +type stringIsJsonObjectValidator struct{} // Description describes the validation in plain text formatting. func (validator stringIsJsonObjectValidator) Description(_ context.Context) string { diff --git a/internal/validate/string_test.go b/internal/validate/string_test.go index 35a4fc4c79..69eb84e134 100644 --- a/internal/validate/string_test.go +++ b/internal/validate/string_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -73,7 +74,7 @@ func TestStringLenBetweenValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -139,7 +140,7 @@ func TestStringLenAtLeastValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -205,7 +206,7 @@ func TestStringLenAtMostValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -269,7 +270,7 @@ func TestStringInSliceValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -333,7 +334,7 @@ func TestStringMatchValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} @@ -403,7 +404,7 @@ func TestStringIsJsonValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/time.go b/internal/validate/time.go index b2b773e40f..8ccd22862f 100644 --- a/internal/validate/time.go +++ b/internal/validate/time.go @@ -10,9 +10,7 @@ import ( ) // isRFC3339TimeValidator validates that a string Attribute's length is a valid RFC33349Time. -type isRFC3339TimeValidator struct { - tfsdk.AttributeValidator -} +type isRFC3339TimeValidator struct{} // Description describes the validation in plain text formatting. func (validator isRFC3339TimeValidator) Description(_ context.Context) string { diff --git a/internal/validate/time_test.go b/internal/validate/time_test.go index f633f08a0a..7e9291b432 100644 --- a/internal/validate/time_test.go +++ b/internal/validate/time_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -59,7 +60,7 @@ func TestIsRFC3339TimeValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/unique_items.go b/internal/validate/unique_items.go index 40dfcfa8af..04a704995e 100644 --- a/internal/validate/unique_items.go +++ b/internal/validate/unique_items.go @@ -9,9 +9,7 @@ import ( ) // uniqueItemsValidator validates that an Attribute's list items have unique values. -type uniqueItemsValidator struct { - tfsdk.AttributeValidator -} +type uniqueItemsValidator struct{} // Description describes the validation in plain text formatting. func (v uniqueItemsValidator) Description(_ context.Context) string { diff --git a/internal/validate/unique_items_test.go b/internal/validate/unique_items_test.go index fffa832185..27cfbe39f7 100644 --- a/internal/validate/unique_items_test.go +++ b/internal/validate/unique_items_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -171,7 +172,7 @@ func TestUniqueItemsValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} diff --git a/internal/validate/uri.go b/internal/validate/uri.go index 3604719253..4724f02ec9 100644 --- a/internal/validate/uri.go +++ b/internal/validate/uri.go @@ -9,9 +9,7 @@ import ( ) // uriValidator validates that a string is a URI. -type uriValidator struct { - tfsdk.AttributeValidator -} +type uriValidator struct{} // Description describes the validation in plain text formatting. func (validator uriValidator) Description(_ context.Context) string { diff --git a/internal/validate/uri_test.go b/internal/validate/uri_test.go index f705b25dd8..52621ad95c 100644 --- a/internal/validate/uri_test.go +++ b/internal/validate/uri_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-framework/attr" + "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" @@ -55,7 +56,7 @@ func TestURIValidator(t *testing.T) { } request := tfsdk.ValidateAttributeRequest{ - AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), + AttributePath: path.Root("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{}