Skip to content

Commit 173cf34

Browse files
authored
Merge pull request #34564 from hashicorp/f-s3-access-grants
Amazon S3 Access Grants
2 parents b34152e + 2365d9f commit 173cf34

30 files changed

+2995
-57
lines changed

.changelog/34564.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:new-resource
2+
aws_s3control_access_grants_instance
3+
```
4+
5+
```release-note:new-resource
6+
aws_s3control_access_grants_location
7+
```
8+
9+
```release-note:new-resource
10+
aws_s3control_access_grant
11+
```
12+
13+
```release-note:new-resource
14+
aws_s3control_access_grants_instance_resource_policy
15+
```

internal/framework/validators/json.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validators
5+
6+
import (
7+
"context"
8+
"encoding/json"
9+
10+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+
)
13+
14+
// jsonValidator validates that a string Attribute's value is valid JSON.
15+
type jsonValidator struct{}
16+
17+
// Description describes the validation in plain text formatting.
18+
func (validator jsonValidator) Description(_ context.Context) string {
19+
return "value must be valid JSON"
20+
}
21+
22+
// MarkdownDescription describes the validation in Markdown formatting.
23+
func (validator jsonValidator) MarkdownDescription(ctx context.Context) string {
24+
return validator.Description(ctx)
25+
}
26+
27+
// Validate performs the validation.
28+
func (validator jsonValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
29+
configValue := request.ConfigValue
30+
31+
if configValue.IsNull() || configValue.IsUnknown() {
32+
return
33+
}
34+
35+
// https://datatracker.ietf.org/doc/html/rfc7159.
36+
if valueString := configValue.ValueString(); !json.Valid([]byte(valueString)) {
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
38+
request.Path,
39+
validator.Description(ctx),
40+
valueString,
41+
))
42+
return
43+
}
44+
}
45+
46+
// JSON returns a string validator which ensures that any configured
47+
// attribute value:
48+
//
49+
// - Is a string, which represents valid JSON.
50+
//
51+
// Null (unconfigured) and unknown (known after apply) values are skipped.
52+
func JSON() validator.String {
53+
return jsonValidator{}
54+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validators_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/path"
13+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
14+
"github.com/hashicorp/terraform-plugin-framework/types"
15+
fwvalidators "github.com/hashicorp/terraform-provider-aws/internal/framework/validators"
16+
)
17+
18+
func TestJSONValidator(t *testing.T) {
19+
t.Parallel()
20+
21+
type testCase struct {
22+
val types.String
23+
expectedDiagnostics diag.Diagnostics
24+
}
25+
tests := map[string]testCase{
26+
"unknown String": {
27+
val: types.StringUnknown(),
28+
},
29+
"null String": {
30+
val: types.StringNull(),
31+
},
32+
"invalid String": {
33+
val: types.StringValue("test-value"),
34+
expectedDiagnostics: diag.Diagnostics{
35+
diag.NewAttributeErrorDiagnostic(
36+
path.Root("test"),
37+
"Invalid Attribute Value",
38+
`Attribute test value must be valid JSON, got: test-value`,
39+
),
40+
},
41+
},
42+
"valid JSON": {
43+
val: types.StringValue(`{"Key1": "Value", "Key2": [1, 2, 3]}`),
44+
},
45+
}
46+
47+
for name, test := range tests {
48+
name, test := name, test
49+
t.Run(name, func(t *testing.T) {
50+
t.Parallel()
51+
52+
ctx := context.Background()
53+
54+
request := validator.StringRequest{
55+
Path: path.Root("test"),
56+
PathExpression: path.MatchRoot("test"),
57+
ConfigValue: test.val,
58+
}
59+
response := validator.StringResponse{}
60+
fwvalidators.JSON().ValidateString(ctx, request, &response)
61+
62+
if diff := cmp.Diff(response.Diagnostics, test.expectedDiagnostics); diff != "" {
63+
t.Errorf("unexpected diagnostics difference: %s", diff)
64+
}
65+
})
66+
}
67+
}

internal/service/s3/sweep.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func RegisterSweepers() {
3535
Dependencies: []string{
3636
"aws_s3_access_point",
3737
"aws_s3_object",
38+
"aws_s3control_access_grants_instance",
3839
"aws_s3control_multi_region_access_point",
3940
},
4041
})

0 commit comments

Comments
 (0)