1
1
package s3control
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
"strings"
@@ -9,6 +10,8 @@ import (
9
10
"github.com/aws/aws-sdk-go/aws/arn"
10
11
"github.com/aws/aws-sdk-go/service/s3control"
11
12
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
13
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
14
17
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -17,12 +20,16 @@ import (
17
20
"github.com/hashicorp/terraform-provider-aws/internal/verify"
18
21
)
19
22
20
- func ResourceAccessPoint () * schema.Resource {
23
+ func init () {
24
+ _sp .registerSDKResourceFactory ("aws_s3_access_point" , resourceAccessPoint )
25
+ }
26
+
27
+ func resourceAccessPoint () * schema.Resource {
21
28
return & schema.Resource {
22
- Create : resourceAccessPointCreate ,
23
- Read : resourceAccessPointRead ,
24
- Update : resourceAccessPointUpdate ,
25
- Delete : resourceAccessPointDelete ,
29
+ CreateWithoutTimeout : resourceAccessPointCreate ,
30
+ ReadWithoutTimeout : resourceAccessPointRead ,
31
+ UpdateWithoutTimeout : resourceAccessPointUpdate ,
32
+ DeleteWithoutTimeout : resourceAccessPointDelete ,
26
33
27
34
Importer : & schema.ResourceImporter {
28
35
State : schema .ImportStatePassthrough ,
@@ -140,7 +147,7 @@ func ResourceAccessPoint() *schema.Resource {
140
147
}
141
148
}
142
149
143
- func resourceAccessPointCreate (d * schema.ResourceData , meta interface {}) error {
150
+ func resourceAccessPointCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
144
151
conn := meta .(* conns.AWSClient ).S3ControlConn ()
145
152
146
153
accountID := meta .(* conns.AWSClient ).AccountID
@@ -163,23 +170,22 @@ func resourceAccessPointCreate(d *schema.ResourceData, meta interface{}) error {
163
170
input .VpcConfiguration = expandVPCConfiguration (v .([]interface {})[0 ].(map [string ]interface {}))
164
171
}
165
172
166
- log .Printf ("[DEBUG] Creating S3 Access Point: %s" , input )
167
- output , err := conn .CreateAccessPoint (input )
173
+ output , err := conn .CreateAccessPointWithContext (ctx , input )
168
174
169
175
if err != nil {
170
- return fmt .Errorf ("error creating S3 Access Point (%s): %w " , name , err )
176
+ return diag .Errorf ("creating S3 Access Point (%s): %s " , name , err )
171
177
}
172
178
173
179
resourceID , err := AccessPointCreateResourceID (aws .StringValue (output .AccessPointArn ))
174
180
175
181
if err != nil {
176
- return err
182
+ return diag . FromErr ( err )
177
183
}
178
184
179
185
accountID , name , err = AccessPointParseResourceID (resourceID )
180
186
181
187
if err != nil {
182
- return err
188
+ return diag . FromErr ( err )
183
189
}
184
190
185
191
d .SetId (resourceID )
@@ -188,7 +194,7 @@ func resourceAccessPointCreate(d *schema.ResourceData, meta interface{}) error {
188
194
policy , err := structure .NormalizeJsonString (v .(string ))
189
195
190
196
if err != nil {
191
- return fmt .Errorf ("policy (%s) is invalid JSON: %w " , v .(string ), err )
197
+ return diag .Errorf ("policy (%s) is invalid JSON: %s " , v .(string ), err )
192
198
}
193
199
194
200
input := & s3control.PutAccessPointPolicyInput {
@@ -197,29 +203,28 @@ func resourceAccessPointCreate(d *schema.ResourceData, meta interface{}) error {
197
203
Policy : aws .String (policy ),
198
204
}
199
205
200
- log .Printf ("[DEBUG] Creating S3 Access Point policy: %s" , input )
201
- _ , err = conn .PutAccessPointPolicy (input )
206
+ _ , err = conn .PutAccessPointPolicyWithContext (ctx , input )
202
207
203
208
if err != nil {
204
- return fmt .Errorf ("error creating S3 Access Point (%s) policy: %w " , d .Id (), err )
209
+ return diag .Errorf ("creating S3 Access Point (%s) policy: %s " , d .Id (), err )
205
210
}
206
211
}
207
212
208
- return resourceAccessPointRead (d , meta )
213
+ return resourceAccessPointRead (ctx , d , meta )
209
214
}
210
215
211
- func resourceAccessPointRead (d * schema.ResourceData , meta interface {}) error {
216
+ func resourceAccessPointRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
212
217
conn := meta .(* conns.AWSClient ).S3ControlConn ()
213
218
214
219
accountID , name , err := AccessPointParseResourceID (d .Id ())
215
220
216
221
if err != nil {
217
- return err
222
+ return diag . FromErr ( err )
218
223
}
219
224
220
225
s3OnOutposts := arn .IsARN (name )
221
226
222
- output , err := FindAccessPointByAccountIDAndName ( conn , accountID , name )
227
+ output , err := FindAccessPointByTwoPartKey ( ctx , conn , accountID , name )
223
228
224
229
if ! d .IsNewResource () && tfresource .NotFound (err ) {
225
230
log .Printf ("[WARN] S3 Access Point (%s) not found, removing from state" , d .Id ())
@@ -228,14 +233,14 @@ func resourceAccessPointRead(d *schema.ResourceData, meta interface{}) error {
228
233
}
229
234
230
235
if err != nil {
231
- return fmt .Errorf ("error reading S3 Access Point (%s): %w " , d .Id (), err )
236
+ return diag .Errorf ("reading S3 Access Point (%s): %s " , d .Id (), err )
232
237
}
233
238
234
239
if s3OnOutposts {
235
240
accessPointARN , err := arn .Parse (name )
236
241
237
242
if err != nil {
238
- return err
243
+ return diag . FromErr ( err )
239
244
}
240
245
241
246
// https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazons3onoutposts.html#amazons3onoutposts-resources-for-iam-policies.
@@ -276,20 +281,20 @@ func resourceAccessPointRead(d *schema.ResourceData, meta interface{}) error {
276
281
d .Set ("network_origin" , output .NetworkOrigin )
277
282
if output .PublicAccessBlockConfiguration != nil {
278
283
if err := d .Set ("public_access_block_configuration" , []interface {}{flattenPublicAccessBlockConfiguration (output .PublicAccessBlockConfiguration )}); err != nil {
279
- return fmt .Errorf ("error setting public_access_block_configuration: %w " , err )
284
+ return diag .Errorf ("setting public_access_block_configuration: %s " , err )
280
285
}
281
286
} else {
282
287
d .Set ("public_access_block_configuration" , nil )
283
288
}
284
289
if output .VpcConfiguration != nil {
285
290
if err := d .Set ("vpc_configuration" , []interface {}{flattenVPCConfiguration (output .VpcConfiguration )}); err != nil {
286
- return fmt .Errorf ("error setting vpc_configuration: %w " , err )
291
+ return diag .Errorf ("setting vpc_configuration: %s " , err )
287
292
}
288
293
} else {
289
294
d .Set ("vpc_configuration" , nil )
290
295
}
291
296
292
- policy , status , err := FindAccessPointPolicyAndStatusByAccountIDAndName ( conn , accountID , name )
297
+ policy , status , err := FindAccessPointPolicyAndStatusByTwoPartKey ( ctx , conn , accountID , name )
293
298
294
299
if err == nil && policy != "" {
295
300
if s3OnOutposts {
@@ -301,35 +306,35 @@ func resourceAccessPointRead(d *schema.ResourceData, meta interface{}) error {
301
306
policyToSet , err := verify .PolicyToSet (d .Get ("policy" ).(string ), policy )
302
307
303
308
if err != nil {
304
- return err
309
+ return diag . FromErr ( err )
305
310
}
306
311
307
312
d .Set ("policy" , policyToSet )
308
313
} else if policy == "" || tfresource .NotFound (err ) {
309
314
d .Set ("has_public_access_policy" , false )
310
315
d .Set ("policy" , nil )
311
316
} else {
312
- return fmt .Errorf ("error reading S3 Access Point (%s) policy: %w " , d .Id (), err )
317
+ return diag .Errorf ("reading S3 Access Point (%s) policy: %s " , d .Id (), err )
313
318
}
314
319
315
320
return nil
316
321
}
317
322
318
- func resourceAccessPointUpdate (d * schema.ResourceData , meta interface {}) error {
323
+ func resourceAccessPointUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
319
324
conn := meta .(* conns.AWSClient ).S3ControlConn ()
320
325
321
326
accountID , name , err := AccessPointParseResourceID (d .Id ())
322
327
323
328
if err != nil {
324
- return err
329
+ return diag . FromErr ( err )
325
330
}
326
331
327
332
if d .HasChange ("policy" ) {
328
333
if v , ok := d .GetOk ("policy" ); ok && v .(string ) != "" && v .(string ) != "{}" {
329
334
policy , err := structure .NormalizeJsonString (v .(string ))
330
335
331
336
if err != nil {
332
- return fmt .Errorf ("policy (%s) is invalid JSON: %w " , v .(string ), err )
337
+ return diag .Errorf ("policy (%s) is invalid JSON: %s " , v .(string ), err )
333
338
}
334
339
335
340
input := & s3control.PutAccessPointPolicyInput {
@@ -338,39 +343,37 @@ func resourceAccessPointUpdate(d *schema.ResourceData, meta interface{}) error {
338
343
Policy : aws .String (policy ),
339
344
}
340
345
341
- log .Printf ("[DEBUG] Updating S3 Access Point policy: %s" , input )
342
- _ , err = conn .PutAccessPointPolicy (input )
346
+ _ , err = conn .PutAccessPointPolicyWithContext (ctx , input )
343
347
344
348
if err != nil {
345
- return fmt .Errorf ("error updating S3 Access Point (%s) policy: %w " , d .Id (), err )
349
+ return diag .Errorf ("updating S3 Access Point (%s) policy: %s " , d .Id (), err )
346
350
}
347
351
} else {
348
- log .Printf ("[DEBUG] Deleting S3 Access Point policy: %s" , d .Id ())
349
- _ , err := conn .DeleteAccessPointPolicy (& s3control.DeleteAccessPointPolicyInput {
352
+ _ , err := conn .DeleteAccessPointPolicyWithContext (ctx , & s3control.DeleteAccessPointPolicyInput {
350
353
AccountId : aws .String (accountID ),
351
354
Name : aws .String (name ),
352
355
})
353
356
354
357
if err != nil {
355
- return fmt .Errorf ("error deleting S3 Access Point (%s) policy: %w " , d .Id (), err )
358
+ return diag .Errorf ("deleting S3 Access Point (%s) policy: %s " , d .Id (), err )
356
359
}
357
360
}
358
361
}
359
362
360
- return resourceAccessPointRead (d , meta )
363
+ return resourceAccessPointRead (ctx , d , meta )
361
364
}
362
365
363
- func resourceAccessPointDelete (d * schema.ResourceData , meta interface {}) error {
366
+ func resourceAccessPointDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
364
367
conn := meta .(* conns.AWSClient ).S3ControlConn ()
365
368
366
369
accountID , name , err := AccessPointParseResourceID (d .Id ())
367
370
368
371
if err != nil {
369
- return err
372
+ return diag . FromErr ( err )
370
373
}
371
374
372
375
log .Printf ("[DEBUG] Deleting S3 Access Point: %s" , d .Id ())
373
- _ , err = conn .DeleteAccessPoint ( & s3control.DeleteAccessPointInput {
376
+ _ , err = conn .DeleteAccessPointWithContext ( ctx , & s3control.DeleteAccessPointInput {
374
377
AccountId : aws .String (accountID ),
375
378
Name : aws .String (name ),
376
379
})
@@ -380,12 +383,38 @@ func resourceAccessPointDelete(d *schema.ResourceData, meta interface{}) error {
380
383
}
381
384
382
385
if err != nil {
383
- return fmt .Errorf ("error deleting S3 Access Point (%s): %w " , d .Id (), err )
386
+ return diag .Errorf ("deleting S3 Access Point (%s): %s " , d .Id (), err )
384
387
}
385
388
386
389
return nil
387
390
}
388
391
392
+ func FindAccessPointByTwoPartKey (ctx context.Context , conn * s3control.S3Control , accountID string , name string ) (* s3control.GetAccessPointOutput , error ) {
393
+ input := & s3control.GetAccessPointInput {
394
+ AccountId : aws .String (accountID ),
395
+ Name : aws .String (name ),
396
+ }
397
+
398
+ output , err := conn .GetAccessPointWithContext (ctx , input )
399
+
400
+ if tfawserr .ErrCodeEquals (err , errCodeNoSuchAccessPoint ) {
401
+ return nil , & resource.NotFoundError {
402
+ LastError : err ,
403
+ LastRequest : input ,
404
+ }
405
+ }
406
+
407
+ if err != nil {
408
+ return nil , err
409
+ }
410
+
411
+ if output == nil {
412
+ return nil , tfresource .NewEmptyResultError (input )
413
+ }
414
+
415
+ return output , nil
416
+ }
417
+
389
418
const accessPointResourceIDSeparator = ":"
390
419
391
420
func AccessPointCreateResourceID (accessPointARN string ) (string , error ) {
0 commit comments