@@ -11,13 +11,15 @@ import (
11
11
"github.com/aws/aws-sdk-go-v2/service/glue"
12
12
awstypes "github.com/aws/aws-sdk-go-v2/service/glue/types"
13
13
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
14
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
16
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
17
17
"github.com/hashicorp/terraform-provider-aws/internal/conns"
18
18
"github.com/hashicorp/terraform-provider-aws/internal/enum"
19
19
"github.com/hashicorp/terraform-provider-aws/internal/errs"
20
20
"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"
21
23
"github.com/hashicorp/terraform-provider-aws/internal/verify"
22
24
"github.com/hashicorp/terraform-provider-aws/names"
23
25
)
@@ -34,22 +36,12 @@ func resourceResourcePolicy() *schema.Resource {
34
36
DeleteWithoutTimeout : resourceResourcePolicyDelete ,
35
37
36
38
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
- },
48
39
"enable_hybrid" : {
49
40
Type : schema .TypeString ,
50
41
Optional : true ,
51
42
ValidateDiagFunc : enum .Validate [awstypes.EnableHybridValues ](),
52
43
},
44
+ names .AttrPolicy : sdkv2 .IAMPolicyDocumentSchemaRequired (),
53
45
},
54
46
}
55
47
}
@@ -60,25 +52,28 @@ func resourceResourcePolicyPut(condition awstypes.ExistCondition) func(context.C
60
52
conn := meta .(* conns.AWSClient ).GlueClient (ctx )
61
53
62
54
policy , err := structure .NormalizeJsonString (d .Get (names .AttrPolicy ).(string ))
63
-
64
55
if err != nil {
65
- return sdkdiag .AppendErrorf (diags , "policy is invalid JSON: %s" , err )
56
+ return sdkdiag .AppendFromErr (diags , err )
66
57
}
67
58
68
- input := & glue.PutResourcePolicyInput {
69
- PolicyInJson : aws .String (policy ),
59
+ input := glue.PutResourcePolicyInput {
70
60
PolicyExistsCondition : condition ,
61
+ PolicyInJson : aws .String (policy ),
71
62
}
72
63
73
64
if v , ok := d .GetOk ("enable_hybrid" ); ok {
74
65
input .EnableHybrid = awstypes .EnableHybridValues (v .(string ))
75
66
}
76
67
77
- _ , err = conn .PutResourcePolicy (ctx , input )
68
+ _ , err = conn .PutResourcePolicy (ctx , & input )
69
+
78
70
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 ))
80
76
}
81
- d .SetId (meta .(* conns.AWSClient ).Region (ctx ))
82
77
83
78
return append (diags , resourceResourcePolicyRead (ctx , d , meta )... )
84
79
}
@@ -88,42 +83,64 @@ func resourceResourcePolicyRead(ctx context.Context, d *schema.ResourceData, met
88
83
var diags diag.Diagnostics
89
84
conn := meta .(* conns.AWSClient ).GlueClient (ctx )
90
85
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 ())
94
90
d .SetId ("" )
95
91
return diags
96
92
}
93
+
97
94
if err != nil {
98
95
return sdkdiag .AppendErrorf (diags , "reading Glue Resource Policy (%s): %s" , d .Id (), err )
99
96
}
100
97
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
+ }
106
102
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 )
110
104
111
- d .Set (names .AttrPolicy , policyToSet )
112
- }
113
105
return diags
114
106
}
115
107
116
108
func resourceResourcePolicyDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
117
109
var diags diag.Diagnostics
118
110
conn := meta .(* conns.AWSClient ).GlueClient (ctx )
119
111
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
+
121
119
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 )
126
121
}
127
122
128
123
return diags
129
124
}
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
+ }
0 commit comments