forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#34087 from hashicorp/td-framework-goodies
Additional Framework helpers
- Loading branch information
Showing
18 changed files
with
192 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validators | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/YakDriver/regexache" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// awsAccountIDValidator validates that a string Attribute's value is a valid AWS account ID. | ||
type awsAccountIDValidator struct{} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (validator awsAccountIDValidator) Description(_ context.Context) string { | ||
return "value must be a valid AWS account ID" | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (validator awsAccountIDValidator) MarkdownDescription(ctx context.Context) string { | ||
return validator.Description(ctx) | ||
} | ||
|
||
// ValidateString performs the validation. | ||
func (validator awsAccountIDValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { | ||
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
// https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-identifiers.html. | ||
if !regexache.MustCompile(`^\d{12}$`).MatchString(request.ConfigValue.ValueString()) { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
request.Path, | ||
validator.Description(ctx), | ||
request.ConfigValue.ValueString(), | ||
)) | ||
return | ||
} | ||
} | ||
|
||
// AWSAccountID returns a string validator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a string, which represents a valid AWS account ID. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func AWSAccountID() validator.String { // nosemgrep:ci.aws-in-func-name | ||
return awsAccountIDValidator{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validators_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"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" | ||
) | ||
|
||
func TestAWSAccountIDValidator(t *testing.T) { // nosemgrep:ci.aws-in-func-name | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val types.String | ||
expectedDiagnostics diag.Diagnostics | ||
} | ||
tests := map[string]testCase{ | ||
"unknown String": { | ||
val: types.StringUnknown(), | ||
}, | ||
"null String": { | ||
val: types.StringNull(), | ||
}, | ||
"invalid String": { | ||
val: types.StringValue("test-value"), | ||
expectedDiagnostics: diag.Diagnostics{ | ||
diag.NewAttributeErrorDiagnostic( | ||
path.Root("test"), | ||
"Invalid Attribute Value", | ||
`Attribute test value must be a valid AWS account ID, got: test-value`, | ||
), | ||
}, | ||
}, | ||
"valid AWS account ID": { | ||
val: types.StringValue("123456789012"), | ||
}, | ||
"too long AWS account ID": { | ||
val: types.StringValue("1234567890123"), | ||
expectedDiagnostics: diag.Diagnostics{ | ||
diag.NewAttributeErrorDiagnostic( | ||
path.Root("test"), | ||
"Invalid Attribute Value", | ||
`Attribute test value must be a valid AWS account ID, got: 1234567890123`, | ||
), | ||
}, | ||
}, | ||
"too short AWS account ID": { | ||
val: types.StringValue("12345678901"), | ||
expectedDiagnostics: diag.Diagnostics{ | ||
diag.NewAttributeErrorDiagnostic( | ||
path.Root("test"), | ||
"Invalid Attribute Value", | ||
`Attribute test value must be a valid AWS account ID, got: 12345678901`, | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
request := validator.StringRequest{ | ||
Path: path.Root("test"), | ||
PathExpression: path.MatchRoot("test"), | ||
ConfigValue: test.val, | ||
} | ||
response := validator.StringResponse{} | ||
fwvalidators.AWSAccountID().ValidateString(ctx, request, &response) | ||
|
||
if diff := cmp.Diff(response.Diagnostics, test.expectedDiagnostics); diff != "" { | ||
t.Errorf("unexpected diagnostics difference: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.