1
1
package efs
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"fmt"
6
7
"log"
@@ -9,6 +10,7 @@ import (
9
10
"github.com/aws/aws-sdk-go/aws"
10
11
"github.com/aws/aws-sdk-go/service/efs"
11
12
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
13
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -20,10 +22,10 @@ import (
20
22
21
23
func ResourceFileSystem () * schema.Resource {
22
24
return & schema.Resource {
23
- Create : resourceFileSystemCreate ,
24
- Read : resourceFileSystemRead ,
25
- Update : resourceFileSystemUpdate ,
26
- Delete : resourceFileSystemDelete ,
25
+ CreateWithoutTimeout : resourceFileSystemCreate ,
26
+ ReadWithoutTimeout : resourceFileSystemRead ,
27
+ UpdateWithoutTimeout : resourceFileSystemUpdate ,
28
+ DeleteWithoutTimeout : resourceFileSystemDelete ,
27
29
28
30
Importer : & schema.ResourceImporter {
29
31
State : schema .ImportStatePassthrough ,
@@ -141,7 +143,7 @@ func ResourceFileSystem() *schema.Resource {
141
143
}
142
144
}
143
145
144
- func resourceFileSystemCreate (d * schema.ResourceData , meta interface {}) error {
146
+ func resourceFileSystemCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
145
147
conn := meta .(* conns.AWSClient ).EFSConn
146
148
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
147
149
tags := defaultTagsConfig .MergeTags (tftags .New (d .Get ("tags" ).(map [string ]interface {})))
@@ -154,73 +156,71 @@ func resourceFileSystemCreate(d *schema.ResourceData, meta interface{}) error {
154
156
}
155
157
throughputMode := d .Get ("throughput_mode" ).(string )
156
158
157
- createOpts := & efs.CreateFileSystemInput {
159
+ input := & efs.CreateFileSystemInput {
158
160
CreationToken : aws .String (creationToken ),
159
161
Tags : Tags (tags .IgnoreAWS ()),
160
162
ThroughputMode : aws .String (throughputMode ),
161
163
}
162
164
163
165
if v , ok := d .GetOk ("availability_zone_name" ); ok {
164
- createOpts .AvailabilityZoneName = aws .String (v .(string ))
166
+ input .AvailabilityZoneName = aws .String (v .(string ))
165
167
}
166
168
167
169
if v , ok := d .GetOk ("performance_mode" ); ok {
168
- createOpts .PerformanceMode = aws .String (v .(string ))
170
+ input .PerformanceMode = aws .String (v .(string ))
169
171
}
170
172
171
173
if throughputMode == efs .ThroughputModeProvisioned {
172
- createOpts .ProvisionedThroughputInMibps = aws .Float64 (d .Get ("provisioned_throughput_in_mibps" ).(float64 ))
174
+ input .ProvisionedThroughputInMibps = aws .Float64 (d .Get ("provisioned_throughput_in_mibps" ).(float64 ))
173
175
}
174
176
175
177
encrypted , hasEncrypted := d .GetOk ("encrypted" )
176
178
kmsKeyId , hasKmsKeyId := d .GetOk ("kms_key_id" )
177
179
178
180
if hasEncrypted {
179
- createOpts .Encrypted = aws .Bool (encrypted .(bool ))
181
+ input .Encrypted = aws .Bool (encrypted .(bool ))
180
182
}
181
183
182
184
if hasKmsKeyId {
183
- createOpts .KmsKeyId = aws .String (kmsKeyId .(string ))
185
+ input .KmsKeyId = aws .String (kmsKeyId .(string ))
184
186
}
185
187
186
188
if encrypted == false && hasKmsKeyId {
187
- return errors .New ("encrypted must be set to true when kms_key_id is specified" )
189
+ return diag . FromErr ( errors .New ("encrypted must be set to true when kms_key_id is specified" ) )
188
190
}
189
191
190
- log .Printf ("[DEBUG] Creating EFS file system: %s" , createOpts )
191
- fs , err := conn .CreateFileSystem (createOpts )
192
+ output , err := conn .CreateFileSystemWithContext (ctx , input )
192
193
193
194
if err != nil {
194
- return fmt .Errorf ("error creating EFS file system: %w " , err )
195
+ return diag .Errorf ("creating EFS file system: %s " , err )
195
196
}
196
197
197
- d .SetId (aws .StringValue (fs .FileSystemId ))
198
+ d .SetId (aws .StringValue (output .FileSystemId ))
198
199
199
- if _ , err := waitFileSystemAvailable (conn , d .Id ()); err != nil {
200
- return fmt .Errorf ("error waiting for EFS file system (%s) to be available : %w " , d .Id (), err )
200
+ if _ , err := waitFileSystemAvailable (ctx , conn , d .Id ()); err != nil {
201
+ return diag .Errorf ("waiting for EFS file system (%s) create : %s " , d .Id (), err )
201
202
}
202
203
203
- _ , hasLifecyclePolicy := d .GetOk ("lifecycle_policy" )
204
- if hasLifecyclePolicy {
205
- _ , err := conn .PutLifecycleConfiguration (& efs.PutLifecycleConfigurationInput {
204
+ if v , ok := d .GetOk ("lifecycle_policy" ); ok {
205
+ _ , err := conn .PutLifecycleConfigurationWithContext (ctx , & efs.PutLifecycleConfigurationInput {
206
206
FileSystemId : aws .String (d .Id ()),
207
- LifecyclePolicies : expandFileSystemLifecyclePolicies (d . Get ( "lifecycle_policy" ) .([]interface {})),
207
+ LifecyclePolicies : expandFileSystemLifecyclePolicies (v .([]interface {})),
208
208
})
209
209
210
210
if err != nil {
211
- return fmt .Errorf ("error creating EFS file system (%s) lifecycle configuration: %w " , d .Id (), err )
211
+ return diag .Errorf ("putting EFS file system (%s) lifecycle configuration: %s " , d .Id (), err )
212
212
}
213
213
}
214
214
215
- return resourceFileSystemRead (d , meta )
215
+ return resourceFileSystemRead (ctx , d , meta )
216
216
}
217
217
218
- func resourceFileSystemRead (d * schema.ResourceData , meta interface {}) error {
218
+ func resourceFileSystemRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
219
219
conn := meta .(* conns.AWSClient ).EFSConn
220
220
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
221
221
ignoreTagsConfig := meta .(* conns.AWSClient ).IgnoreTagsConfig
222
222
223
- fs , err := FindFileSystemByID (conn , d .Id ())
223
+ fs , err := FindFileSystemByID (ctx , conn , d .Id ())
224
224
225
225
if ! d .IsNewResource () && tfresource .NotFound (err ) {
226
226
log .Printf ("[WARN] EFS file system (%s) not found, removing from state" , d .Id ())
@@ -229,54 +229,53 @@ func resourceFileSystemRead(d *schema.ResourceData, meta interface{}) error {
229
229
}
230
230
231
231
if err != nil {
232
- return fmt .Errorf ("error reading EFS file system (%s): %w " , d .Id (), err )
232
+ return diag .Errorf ("reading EFS file system (%s): %s " , d .Id (), err )
233
233
}
234
234
235
235
d .Set ("arn" , fs .FileSystemArn )
236
236
d .Set ("availability_zone_id" , fs .AvailabilityZoneId )
237
237
d .Set ("availability_zone_name" , fs .AvailabilityZoneName )
238
238
d .Set ("creation_token" , fs .CreationToken )
239
+ d .Set ("dns_name" , meta .(* conns.AWSClient ).RegionalHostname (fmt .Sprintf ("%s.efs" , aws .StringValue (fs .FileSystemId ))))
239
240
d .Set ("encrypted" , fs .Encrypted )
240
241
d .Set ("kms_key_id" , fs .KmsKeyId )
242
+ d .Set ("number_of_mount_targets" , fs .NumberOfMountTargets )
243
+ d .Set ("owner_id" , fs .OwnerId )
241
244
d .Set ("performance_mode" , fs .PerformanceMode )
242
245
d .Set ("provisioned_throughput_in_mibps" , fs .ProvisionedThroughputInMibps )
243
246
d .Set ("throughput_mode" , fs .ThroughputMode )
244
- d .Set ("owner_id" , fs .OwnerId )
245
- d .Set ("number_of_mount_targets" , fs .NumberOfMountTargets )
246
247
247
248
tags := KeyValueTags (fs .Tags ).IgnoreAWS ().IgnoreConfig (ignoreTagsConfig )
248
249
249
250
//lintignore:AWSR002
250
251
if err := d .Set ("tags" , tags .RemoveDefaultConfig (defaultTagsConfig ).Map ()); err != nil {
251
- return fmt .Errorf ("error setting tags: %w " , err )
252
+ return diag .Errorf ("setting tags: %s " , err )
252
253
}
253
254
254
255
if err := d .Set ("tags_all" , tags .Map ()); err != nil {
255
- return fmt .Errorf ("error setting tags_all: %w " , err )
256
+ return diag .Errorf ("setting tags_all: %s " , err )
256
257
}
257
258
258
259
if err := d .Set ("size_in_bytes" , flattenFileSystemSizeInBytes (fs .SizeInBytes )); err != nil {
259
- return fmt .Errorf ("error setting size_in_bytes: %w " , err )
260
+ return diag .Errorf ("setting size_in_bytes: %s " , err )
260
261
}
261
262
262
- d .Set ("dns_name" , meta .(* conns.AWSClient ).RegionalHostname (fmt .Sprintf ("%s.efs" , aws .StringValue (fs .FileSystemId ))))
263
-
264
- res , err := conn .DescribeLifecycleConfiguration (& efs.DescribeLifecycleConfigurationInput {
263
+ output , err := conn .DescribeLifecycleConfigurationWithContext (ctx , & efs.DescribeLifecycleConfigurationInput {
265
264
FileSystemId : aws .String (d .Id ()),
266
265
})
267
266
268
267
if err != nil {
269
- return fmt .Errorf ("error reading EFS file system (%s) lifecycle configuration: %w " , d .Id (), err )
268
+ return diag .Errorf ("reading EFS file system (%s) lifecycle configuration: %s " , d .Id (), err )
270
269
}
271
270
272
- if err := d .Set ("lifecycle_policy" , flattenFileSystemLifecyclePolicies (res .LifecyclePolicies )); err != nil {
273
- return fmt .Errorf ("error setting lifecycle_policy: %w " , err )
271
+ if err := d .Set ("lifecycle_policy" , flattenFileSystemLifecyclePolicies (output .LifecyclePolicies )); err != nil {
272
+ return diag .Errorf ("setting lifecycle_policy: %s " , err )
274
273
}
275
274
276
275
return nil
277
276
}
278
277
279
- func resourceFileSystemUpdate (d * schema.ResourceData , meta interface {}) error {
278
+ func resourceFileSystemUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
280
279
conn := meta .(* conns.AWSClient ).EFSConn
281
280
282
281
if d .HasChanges ("provisioned_throughput_in_mibps" , "throughput_mode" ) {
@@ -291,14 +290,14 @@ func resourceFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
291
290
input .ProvisionedThroughputInMibps = aws .Float64 (d .Get ("provisioned_throughput_in_mibps" ).(float64 ))
292
291
}
293
292
294
- _ , err := conn .UpdateFileSystem ( input )
293
+ _ , err := conn .UpdateFileSystemWithContext ( ctx , input )
295
294
296
295
if err != nil {
297
- return fmt .Errorf ("error updating EFS file system (%s): %w " , d .Id (), err )
296
+ return diag .Errorf ("updating EFS file system (%s): %s " , d .Id (), err )
298
297
}
299
298
300
- if _ , err := waitFileSystemAvailable (conn , d .Id ()); err != nil {
301
- return fmt .Errorf ("error waiting for EFS file system (%s) to be available : %w " , d .Id (), err )
299
+ if _ , err := waitFileSystemAvailable (ctx , conn , d .Id ()); err != nil {
300
+ return diag .Errorf ("waiting for EFS file system (%s) update : %s " , d .Id (), err )
302
301
}
303
302
}
304
303
@@ -315,29 +314,29 @@ func resourceFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
315
314
input .LifecyclePolicies = []* efs.LifecyclePolicy {}
316
315
}
317
316
318
- _ , err := conn .PutLifecycleConfiguration ( input )
317
+ _ , err := conn .PutLifecycleConfigurationWithContext ( ctx , input )
319
318
320
319
if err != nil {
321
- return fmt .Errorf ("error updating EFS file system (%s) lifecycle configuration: %w " , d .Id (), err )
320
+ return diag .Errorf ("putting EFS file system (%s) lifecycle configuration: %s " , d .Id (), err )
322
321
}
323
322
}
324
323
325
324
if d .HasChange ("tags_all" ) {
326
325
o , n := d .GetChange ("tags_all" )
327
326
328
- if err := UpdateTags ( conn , d .Id (), o , n ); err != nil {
329
- return fmt .Errorf ("error updating EFS file system (%s) tags: %w " , d .Id (), err )
327
+ if err := UpdateTagsWithContext ( ctx , conn , d .Id (), o , n ); err != nil {
328
+ return diag .Errorf ("updating EFS file system (%s) tags: %s " , d .Id (), err )
330
329
}
331
330
}
332
331
333
- return resourceFileSystemRead (d , meta )
332
+ return resourceFileSystemRead (ctx , d , meta )
334
333
}
335
334
336
- func resourceFileSystemDelete (d * schema.ResourceData , meta interface {}) error {
335
+ func resourceFileSystemDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
337
336
conn := meta .(* conns.AWSClient ).EFSConn
338
337
339
338
log .Printf ("[DEBUG] Deleting EFS file system: %s" , d .Id ())
340
- _ , err := conn .DeleteFileSystem ( & efs.DeleteFileSystemInput {
339
+ _ , err := conn .DeleteFileSystemWithContext ( ctx , & efs.DeleteFileSystemInput {
341
340
FileSystemId : aws .String (d .Id ()),
342
341
})
343
342
@@ -346,25 +345,22 @@ func resourceFileSystemDelete(d *schema.ResourceData, meta interface{}) error {
346
345
}
347
346
348
347
if err != nil {
349
- return fmt .Errorf ("error deleting EFS file system (%s): %w " , d .Id (), err )
348
+ return diag .Errorf ("deleting EFS file system (%s): %s " , d .Id (), err )
350
349
}
351
350
352
- if _ , err := waitFileSystemDeleted (conn , d .Id ()); err != nil {
353
- if tfawserr .ErrCodeEquals (err , efs .ErrCodeFileSystemNotFound ) {
354
- return nil
355
- }
356
- return fmt .Errorf ("error waiting for EFS file system (%s) deletion: %w" , d .Id (), err )
351
+ if _ , err := waitFileSystemDeleted (ctx , conn , d .Id ()); err != nil {
352
+ return diag .Errorf ("waiting for EFS file system (%s) delete: %s" , d .Id (), err )
357
353
}
358
354
359
355
return nil
360
356
}
361
357
362
- func FindFileSystemByID (conn * efs.EFS , id string ) (* efs.FileSystemDescription , error ) {
358
+ func FindFileSystemByID (ctx context. Context , conn * efs.EFS , id string ) (* efs.FileSystemDescription , error ) {
363
359
input := & efs.DescribeFileSystemsInput {
364
360
FileSystemId : aws .String (id ),
365
361
}
366
362
367
- output , err := conn .DescribeFileSystems ( input )
363
+ output , err := conn .DescribeFileSystemsWithContext ( ctx , input )
368
364
369
365
if tfawserr .ErrCodeEquals (err , efs .ErrCodeFileSystemNotFound ) {
370
366
return nil , & resource.NotFoundError {
@@ -384,9 +380,9 @@ func FindFileSystemByID(conn *efs.EFS, id string) (*efs.FileSystemDescription, e
384
380
return output .FileSystems [0 ], nil
385
381
}
386
382
387
- func statusFileSystemLifeCycleState (conn * efs.EFS , id string ) resource.StateRefreshFunc {
383
+ func statusFileSystemLifeCycleState (ctx context. Context , conn * efs.EFS , id string ) resource.StateRefreshFunc {
388
384
return func () (interface {}, string , error ) {
389
- output , err := FindFileSystemByID (conn , id )
385
+ output , err := FindFileSystemByID (ctx , conn , id )
390
386
391
387
if tfresource .NotFound (err ) {
392
388
return nil , "" , nil
@@ -409,17 +405,17 @@ const (
409
405
fileSystemDeletedMinTimeout = 3 * time .Second
410
406
)
411
407
412
- func waitFileSystemAvailable (conn * efs.EFS , fileSystemID string ) (* efs.FileSystemDescription , error ) { //nolint:unparam
408
+ func waitFileSystemAvailable (ctx context. Context , conn * efs.EFS , fileSystemID string ) (* efs.FileSystemDescription , error ) { //nolint:unparam
413
409
stateConf := & resource.StateChangeConf {
414
410
Pending : []string {efs .LifeCycleStateCreating , efs .LifeCycleStateUpdating },
415
411
Target : []string {efs .LifeCycleStateAvailable },
416
- Refresh : statusFileSystemLifeCycleState (conn , fileSystemID ),
412
+ Refresh : statusFileSystemLifeCycleState (ctx , conn , fileSystemID ),
417
413
Timeout : fileSystemAvailableTimeout ,
418
414
Delay : fileSystemAvailableDelayTimeout ,
419
415
MinTimeout : fileSystemAvailableMinTimeout ,
420
416
}
421
417
422
- outputRaw , err := stateConf .WaitForState ( )
418
+ outputRaw , err := stateConf .WaitForStateContext ( ctx )
423
419
424
420
if output , ok := outputRaw .(* efs.FileSystemDescription ); ok {
425
421
return output , err
@@ -428,17 +424,17 @@ func waitFileSystemAvailable(conn *efs.EFS, fileSystemID string) (*efs.FileSyste
428
424
return nil , err
429
425
}
430
426
431
- func waitFileSystemDeleted (conn * efs.EFS , fileSystemID string ) (* efs.FileSystemDescription , error ) {
427
+ func waitFileSystemDeleted (ctx context. Context , conn * efs.EFS , fileSystemID string ) (* efs.FileSystemDescription , error ) {
432
428
stateConf := & resource.StateChangeConf {
433
429
Pending : []string {efs .LifeCycleStateAvailable , efs .LifeCycleStateDeleting },
434
430
Target : []string {},
435
- Refresh : statusFileSystemLifeCycleState (conn , fileSystemID ),
431
+ Refresh : statusFileSystemLifeCycleState (ctx , conn , fileSystemID ),
436
432
Timeout : fileSystemDeletedTimeout ,
437
433
Delay : fileSystemDeletedDelayTimeout ,
438
434
MinTimeout : fileSystemDeletedMinTimeout ,
439
435
}
440
436
441
- outputRaw , err := stateConf .WaitForState ( )
437
+ outputRaw , err := stateConf .WaitForStateContext ( ctx )
442
438
443
439
if output , ok := outputRaw .(* efs.FileSystemDescription ); ok {
444
440
return output , err
0 commit comments