Skip to content

Commit

Permalink
[wip] refactor(fw): GetSchema -> Schema
Browse files Browse the repository at this point in the history
fix: update custom arn attribute type

fix(tfsdk2fw): update Schema return

fix: update IDAttribute to resource schema

fix: update NormalizeIPProtocol plan modifier

fix(fw): update ip cidr validators

fix(enum): update to string validator interface

fix(fw): update int64 between validator
  • Loading branch information
jar-b committed Dec 2, 2022
1 parent 7fc8e6f commit 3378f70
Show file tree
Hide file tree
Showing 28 changed files with 407 additions and 877 deletions.
19 changes: 8 additions & 11 deletions internal/acctest/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand All @@ -31,22 +30,20 @@ func DeleteFrameworkResource(factory func(context.Context) (fwresource.ResourceW

resource.Configure(ctx, fwresource.ConfigureRequest{ProviderData: meta}, &fwresource.ConfigureResponse{})

var schema tfsdk.Schema
var diags diag.Diagnostics
if v, ok := resource.(fwresource.ResourceWithGetSchema); ok {
schema, diags = v.GetSchema(ctx)

if diags.HasError() {
return fwdiag.DiagnosticsError(diags)
schemaResp := fwresource.SchemaResponse{}
if v, ok := resource.(fwresource.ResourceWithSchema); ok {
v.Schema(ctx, fwresource.SchemaRequest{}, &schemaResp)
if schemaResp.Diagnostics.HasError() {
return fwdiag.DiagnosticsError(schemaResp.Diagnostics)
}
} else {
return errors.New("resource does not implement GetSchema method")
return errors.New("resource does not implement Schema method")
}

// Construct a simple Framework State that contains just top-level attributes.
state := tfsdk.State{
Raw: tftypes.NewValue(schema.Type().TerraformType(ctx), nil),
Schema: schema,
Raw: tftypes.NewValue(schemaResp.Schema.Type().TerraformType(ctx), nil),
Schema: schemaResp.Schema,
}

for name, v := range is.Attributes {
Expand Down
4 changes: 2 additions & 2 deletions internal/enum/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package enum

import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -11,6 +11,6 @@ func Validate[T valueser[T]]() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(validation.StringInSlice(Values[T](), false))
}

func FrameworkValidate[T valueser[T]]() tfsdk.AttributeValidator {
func FrameworkValidate[T valueser[T]]() validator.String {
return stringvalidator.OneOf(Values[T]()...)
}
15 changes: 7 additions & 8 deletions internal/framework/id_attribute.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package framework

import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
)

func IDAttribute() tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.StringType,
func IDAttribute() schema.StringAttribute {
return schema.StringAttribute{
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.UseStateForUnknown(),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
}
}
26 changes: 26 additions & 0 deletions internal/framework/types/arn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -118,6 +119,27 @@ func (t arnType) Description() string {
return `An Amazon Resource Name.`
}

func (t arnType) ValueFromString(ctx context.Context, st types.String) (types.StringValuable, diag.Diagnostics) {
if st.IsNull() {
return ARNNull(), nil
}
if st.IsUnknown() {
return ARNUnknown(), nil
}

var diags diag.Diagnostics
v, err := arn.Parse(st.String())
if err != nil {
diags.AddError(
"ARN ValueFromString Error",
fmt.Sprintf("String %s cannot be parsed as an ARN.", st),
)
return nil, diags
}

return ARNValue(v), diags
}

func ARNNull() ARN {
return ARN{
state: attr.ValueStateNull,
Expand Down Expand Up @@ -150,6 +172,10 @@ func (a ARN) Type(_ context.Context) attr.Type {
return ARNType
}

func (a ARN) ToStringValue(ctx context.Context) (types.String, diag.Diagnostics) {
return types.StringValue(a.value.String()), nil
}

func (a ARN) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
t := ARNType.TerraformType(ctx)

Expand Down
31 changes: 13 additions & 18 deletions internal/framework/validators/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

Expand All @@ -22,31 +22,28 @@ func (validator ipv4CIDRNetworkAddressValidator) MarkdownDescription(ctx context
}

// Validate performs the validation.
func (validator ipv4CIDRNetworkAddressValidator) Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) {
s, ok := validateString(ctx, request, response)

if !ok {
func (validator ipv4CIDRNetworkAddressValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}

if err := verify.ValidateIPv4CIDRBlock(s); err != nil {
if err := verify.ValidateIPv4CIDRBlock(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.AttributePath,
request.Path,
validator.Description(ctx),
err.Error(),
))

return
}
}

// IPv4CIDRNetworkAddress returns an AttributeValidator which ensures that any configured
// IPv4CIDRNetworkAddress returns a string validator which ensures that any configured
// attribute value:
//
// - Is a string, which represents a valid IPv4 CIDR network address.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func IPv4CIDRNetworkAddress() tfsdk.AttributeValidator {
func IPv4CIDRNetworkAddress() validator.String {
return ipv4CIDRNetworkAddressValidator{}
}

Expand All @@ -64,16 +61,14 @@ func (validator ipv6CIDRNetworkAddressValidator) MarkdownDescription(ctx context
}

// Validate performs the validation.
func (validator ipv6CIDRNetworkAddressValidator) Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) {
s, ok := validateString(ctx, request, response)

if !ok {
func (validator ipv6CIDRNetworkAddressValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}

if err := verify.ValidateIPv6CIDRBlock(s); err != nil {
if err := verify.ValidateIPv6CIDRBlock(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.AttributePath,
request.Path,
validator.Description(ctx),
err.Error(),
))
Expand All @@ -82,12 +77,12 @@ func (validator ipv6CIDRNetworkAddressValidator) Validate(ctx context.Context, r
}
}

// IPv6CIDRNetworkAddress returns an AttributeValidator which ensures that any configured
// IPv6CIDRNetworkAddress returns a string validator which ensures that any configured
// attribute value:
//
// - Is a string, which represents a valid IPv6 CIDR network address.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func IPv6CIDRNetworkAddress() tfsdk.AttributeValidator {
func IPv6CIDRNetworkAddress() validator.String {
return ipv6CIDRNetworkAddressValidator{}
}
39 changes: 15 additions & 24 deletions internal/framework/validators/cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
fwvalidators "github.com/hashicorp/terraform-provider-aws/internal/framework/validators"
)
Expand All @@ -15,14 +14,10 @@ func TestIPv4CIDRNetworkAddressValidator(t *testing.T) {
t.Parallel()

type testCase struct {
val attr.Value
val types.String
expectError bool
}
tests := map[string]testCase{
"not a String": {
val: types.BoolValue(true),
expectError: true,
},
"unknown String": {
val: types.StringUnknown(),
},
Expand All @@ -49,13 +44,13 @@ func TestIPv4CIDRNetworkAddressValidator(t *testing.T) {
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
request := tfsdk.ValidateAttributeRequest{
AttributePath: path.Root("test"),
AttributePathExpression: path.MatchRoot("test"),
AttributeConfig: test.val,
request := validator.StringRequest{
Path: path.Root("test"),
PathExpression: path.MatchRoot("test"),
ConfigValue: test.val,
}
response := tfsdk.ValidateAttributeResponse{}
fwvalidators.IPv4CIDRNetworkAddress().Validate(context.Background(), request, &response)
response := validator.StringResponse{}
fwvalidators.IPv4CIDRNetworkAddress().ValidateString(context.Background(), request, &response)

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand All @@ -72,14 +67,10 @@ func TestIPv6CIDRNetworkAddressValidator(t *testing.T) {
t.Parallel()

type testCase struct {
val attr.Value
val types.String
expectError bool
}
tests := map[string]testCase{
"not a String": {
val: types.BoolValue(true),
expectError: true,
},
"unknown String": {
val: types.StringUnknown(),
},
Expand All @@ -106,13 +97,13 @@ func TestIPv6CIDRNetworkAddressValidator(t *testing.T) {
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
request := tfsdk.ValidateAttributeRequest{
AttributePath: path.Root("test"),
AttributePathExpression: path.MatchRoot("test"),
AttributeConfig: test.val,
request := validator.StringRequest{
Path: path.Root("test"),
PathExpression: path.MatchRoot("test"),
ConfigValue: test.val,
}
response := tfsdk.ValidateAttributeResponse{}
fwvalidators.IPv6CIDRNetworkAddress().Validate(context.Background(), request, &response)
response := validator.StringResponse{}
fwvalidators.IPv6CIDRNetworkAddress().ValidateString(context.Background(), request, &response)

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
63 changes: 0 additions & 63 deletions internal/framework/validators/int64string.go

This file was deleted.

Loading

0 comments on commit 3378f70

Please sign in to comment.