Skip to content

Commit bd6c474

Browse files
committed
glue: Replace 'PlanOnly' acceptance test steps with 'plancheck's.
1 parent 251905e commit bd6c474

File tree

3 files changed

+93
-63
lines changed

3 files changed

+93
-63
lines changed

internal/service/glue/exports_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
FindPartitionByValues = findPartitionByValues
3838
FindPartitionIndexByName = findPartitionIndexByName
3939
FindRegistryByID = findRegistryByID
40+
FindResourcePolicy = findResourcePolicy
4041
FindSchemaByID = findSchemaByID
4142
FindTableByName = findTableByName
4243
FindTriggerByName = findTriggerByName

internal/service/glue/resource_policy.go

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
"github.com/aws/aws-sdk-go-v2/service/glue"
1212
awstypes "github.com/aws/aws-sdk-go-v2/service/glue/types"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
16-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1717
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1818
"github.com/hashicorp/terraform-provider-aws/internal/enum"
1919
"github.com/hashicorp/terraform-provider-aws/internal/errs"
2020
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
21+
"github.com/hashicorp/terraform-provider-aws/internal/sdkv2"
22+
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2123
"github.com/hashicorp/terraform-provider-aws/internal/verify"
2224
"github.com/hashicorp/terraform-provider-aws/names"
2325
)
@@ -34,22 +36,12 @@ func resourceResourcePolicy() *schema.Resource {
3436
DeleteWithoutTimeout: resourceResourcePolicyDelete,
3537

3638
Schema: map[string]*schema.Schema{
37-
names.AttrPolicy: {
38-
Type: schema.TypeString,
39-
Required: true,
40-
ValidateFunc: validation.StringIsJSON,
41-
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
42-
DiffSuppressOnRefresh: true,
43-
StateFunc: func(v any) string {
44-
json, _ := structure.NormalizeJsonString(v)
45-
return json
46-
},
47-
},
4839
"enable_hybrid": {
4940
Type: schema.TypeString,
5041
Optional: true,
5142
ValidateDiagFunc: enum.Validate[awstypes.EnableHybridValues](),
5243
},
44+
names.AttrPolicy: sdkv2.IAMPolicyDocumentSchemaRequired(),
5345
},
5446
}
5547
}
@@ -60,25 +52,28 @@ func resourceResourcePolicyPut(condition awstypes.ExistCondition) func(context.C
6052
conn := meta.(*conns.AWSClient).GlueClient(ctx)
6153

6254
policy, err := structure.NormalizeJsonString(d.Get(names.AttrPolicy).(string))
63-
6455
if err != nil {
65-
return sdkdiag.AppendErrorf(diags, "policy is invalid JSON: %s", err)
56+
return sdkdiag.AppendFromErr(diags, err)
6657
}
6758

68-
input := &glue.PutResourcePolicyInput{
69-
PolicyInJson: aws.String(policy),
59+
input := glue.PutResourcePolicyInput{
7060
PolicyExistsCondition: condition,
61+
PolicyInJson: aws.String(policy),
7162
}
7263

7364
if v, ok := d.GetOk("enable_hybrid"); ok {
7465
input.EnableHybrid = awstypes.EnableHybridValues(v.(string))
7566
}
7667

77-
_, err = conn.PutResourcePolicy(ctx, input)
68+
_, err = conn.PutResourcePolicy(ctx, &input)
69+
7870
if err != nil {
79-
return sdkdiag.AppendErrorf(diags, "putting policy request: %s", err)
71+
return sdkdiag.AppendErrorf(diags, "putting Glue Resource Policy: %s", err)
72+
}
73+
74+
if d.IsNewResource() {
75+
d.SetId(meta.(*conns.AWSClient).Region(ctx))
8076
}
81-
d.SetId(meta.(*conns.AWSClient).Region(ctx))
8277

8378
return append(diags, resourceResourcePolicyRead(ctx, d, meta)...)
8479
}
@@ -88,42 +83,64 @@ func resourceResourcePolicyRead(ctx context.Context, d *schema.ResourceData, met
8883
var diags diag.Diagnostics
8984
conn := meta.(*conns.AWSClient).GlueClient(ctx)
9085

91-
resourcePolicy, err := conn.GetResourcePolicy(ctx, &glue.GetResourcePolicyInput{})
92-
if errs.IsA[*awstypes.EntityNotFoundException](err) {
93-
log.Printf("[WARN] Glue Resource (%s) not found, removing from state", d.Id())
86+
output, err := findResourcePolicy(ctx, conn)
87+
88+
if !d.IsNewResource() && tfresource.NotFound(err) {
89+
log.Printf("[WARN] Glue Resource Policy (%s) not found, removing from state", d.Id())
9490
d.SetId("")
9591
return diags
9692
}
93+
9794
if err != nil {
9895
return sdkdiag.AppendErrorf(diags, "reading Glue Resource Policy (%s): %s", d.Id(), err)
9996
}
10097

101-
if aws.ToString(resourcePolicy.PolicyInJson) == "" {
102-
//Since the glue resource policy is global we expect it to be deleted when the policy is empty
103-
d.SetId("")
104-
} else {
105-
policyToSet, err := verify.PolicyToSet(d.Get(names.AttrPolicy).(string), aws.ToString(resourcePolicy.PolicyInJson))
98+
policyToSet, err := verify.PolicyToSet(d.Get(names.AttrPolicy).(string), aws.ToString(output.PolicyInJson))
99+
if err != nil {
100+
return sdkdiag.AppendFromErr(diags, err)
101+
}
106102

107-
if err != nil {
108-
return sdkdiag.AppendErrorf(diags, "reading Glue Resource Policy (%s): %s", d.Id(), err)
109-
}
103+
d.Set(names.AttrPolicy, policyToSet)
110104

111-
d.Set(names.AttrPolicy, policyToSet)
112-
}
113105
return diags
114106
}
115107

116108
func resourceResourcePolicyDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
117109
var diags diag.Diagnostics
118110
conn := meta.(*conns.AWSClient).GlueClient(ctx)
119111

120-
_, err := conn.DeleteResourcePolicy(ctx, &glue.DeleteResourcePolicyInput{})
112+
input := glue.DeleteResourcePolicyInput{}
113+
_, err := conn.DeleteResourcePolicy(ctx, &input)
114+
115+
if errs.IsA[*awstypes.EntityNotFoundException](err) {
116+
return diags
117+
}
118+
121119
if err != nil {
122-
if errs.IsA[*awstypes.EntityNotFoundException](err) {
123-
return diags
124-
}
125-
return sdkdiag.AppendErrorf(diags, "deleting policy request: %s", err)
120+
return sdkdiag.AppendErrorf(diags, "deleting Glue Resource Policy (%s): %s", d.Id(), err)
126121
}
127122

128123
return diags
129124
}
125+
126+
func findResourcePolicy(ctx context.Context, conn *glue.Client) (*glue.GetResourcePolicyOutput, error) {
127+
input := &glue.GetResourcePolicyInput{}
128+
output, err := conn.GetResourcePolicy(ctx, input)
129+
130+
if errs.IsA[*awstypes.EntityNotFoundException](err) {
131+
return nil, &retry.NotFoundError{
132+
LastError: err,
133+
LastRequest: input,
134+
}
135+
}
136+
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
if output == nil || aws.ToString(output.PolicyInJson) == "" {
142+
return nil, tfresource.NewEmptyResultError(input)
143+
}
144+
145+
return output, nil
146+
}

internal/service/glue/resource_policy_test.go

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import (
99
"testing"
1010

1111
"github.com/aws/aws-sdk-go-v2/aws"
12-
"github.com/aws/aws-sdk-go-v2/service/glue"
13-
awstypes "github.com/aws/aws-sdk-go-v2/service/glue/types"
1412
awspolicy "github.com/hashicorp/awspolicyequivalence"
1513
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
14+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
1615
"github.com/hashicorp/terraform-plugin-testing/terraform"
1716
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
1817
"github.com/hashicorp/terraform-provider-aws/internal/conns"
19-
"github.com/hashicorp/terraform-provider-aws/internal/errs"
2018
tfglue "github.com/hashicorp/terraform-provider-aws/internal/service/glue"
19+
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2120
"github.com/hashicorp/terraform-provider-aws/names"
2221
)
2322

@@ -149,36 +148,43 @@ func testAccResourcePolicy_ignoreEquivalent(t *testing.T) {
149148
Check: resource.ComposeTestCheckFunc(
150149
testAccResourcePolicy(ctx, resourceName, "glue:CreateTable"),
151150
),
151+
ConfigPlanChecks: resource.ConfigPlanChecks{
152+
PreApply: []plancheck.PlanCheck{
153+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
154+
},
155+
},
152156
},
153157
{
154-
Config: testAccResourcePolicyConfig_equivalent2(),
155-
PlanOnly: true,
158+
Config: testAccResourcePolicyConfig_equivalent2(),
159+
ConfigPlanChecks: resource.ConfigPlanChecks{
160+
PreApply: []plancheck.PlanCheck{
161+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionNoop),
162+
},
163+
PostApplyPostRefresh: []plancheck.PlanCheck{
164+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionNoop),
165+
},
166+
},
156167
},
157168
},
158169
})
159170
}
160171

161172
func testAccResourcePolicy(ctx context.Context, n string, action string) resource.TestCheckFunc {
162173
return func(s *terraform.State) error {
163-
rs, ok := s.RootModule().Resources[n]
174+
_, ok := s.RootModule().Resources[n]
164175
if !ok {
165176
return fmt.Errorf("Not found: %s", n)
166177
}
167178

168-
if rs.Primary.ID == "" {
169-
return fmt.Errorf("No policy id set")
170-
}
171-
172179
conn := acctest.Provider.Meta().(*conns.AWSClient).GlueClient(ctx)
173180

174-
policy, err := conn.GetResourcePolicy(ctx, &glue.GetResourcePolicyInput{})
181+
output, err := tfglue.FindResourcePolicy(ctx, conn)
182+
175183
if err != nil {
176-
return fmt.Errorf("Get resource policy error: %v", err)
184+
return err
177185
}
178186

179-
actualPolicyText := aws.ToString(policy.PolicyInJson)
180-
181-
expectedPolicy := CreateTablePolicy(ctx, action)
187+
actualPolicyText, expectedPolicy := aws.ToString(output.PolicyInJson), testAccNewResourcePolicy(ctx, action)
182188
equivalent, err := awspolicy.PoliciesAreEquivalent(actualPolicyText, expectedPolicy)
183189
if err != nil {
184190
return fmt.Errorf("Error testing policy equivalence: %s", err)
@@ -196,35 +202,41 @@ func testAccCheckResourcePolicyDestroy(ctx context.Context) resource.TestCheckFu
196202
return func(s *terraform.State) error {
197203
conn := acctest.Provider.Meta().(*conns.AWSClient).GlueClient(ctx)
198204

199-
policy, err := conn.GetResourcePolicy(ctx, &glue.GetResourcePolicyInput{})
205+
for _, rs := range s.RootModule().Resources {
206+
if rs.Type != "aws_glue_resource_policy" {
207+
continue
208+
}
200209

201-
if err != nil {
202-
if errs.IsAErrorMessageContains[*awstypes.EntityNotFoundException](err, "Policy not found") {
203-
return nil
210+
_, err := tfglue.FindResourcePolicy(ctx, conn)
211+
212+
if tfresource.NotFound(err) {
213+
continue
204214
}
205-
return err
206-
}
207215

208-
if *policy.PolicyInJson != "" {
209-
return fmt.Errorf("Aws glue resource policy still exists: %s", *policy.PolicyInJson)
216+
if err != nil {
217+
return err
218+
}
219+
220+
return fmt.Errorf("Glue Resource Policy %s still exists", rs.Primary.ID)
210221
}
222+
211223
return nil
212224
}
213225
}
214226

215-
func CreateTablePolicy(ctx context.Context, action string) string {
227+
func testAccNewResourcePolicy(ctx context.Context, action string) string {
216228
return fmt.Sprintf(`{
217229
"Version" : "2012-10-17",
218230
"Statement" : [
219231
{
220232
"Effect" : "Allow",
221233
"Action" : [
222-
"%s"
234+
%[1]q
223235
],
224236
"Principal" : {
225237
"AWS": "*"
226238
},
227-
"Resource" : "arn:%s:glue:%s:%s:*"
239+
"Resource" : "arn:%[2]s:glue:%[3]s:%[4]s:*"
228240
}
229241
]
230242
}`, action, acctest.Partition(), acctest.Region(), acctest.AccountID(ctx))

0 commit comments

Comments
 (0)