Skip to content

Commit

Permalink
efs - add transition_to_primary_storage_class
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 authored and ewbankkit committed Sep 11, 2021
1 parent 9314968 commit 113c715
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 185 deletions.
28 changes: 28 additions & 0 deletions aws/internal/service/efs/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ import (
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func FileSystemByID(conn *efs.EFS, id string) (*efs.FileSystemDescription, error) {
input := &efs.DescribeFileSystemsInput{
FileSystemId: aws.String(id),
}

output, err := conn.DescribeFileSystems(input)

if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.FileSystems == nil || len(output.FileSystems) == 0 || output.FileSystems[0] == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return output.FileSystems[0], nil
}

func BackupPolicyByID(conn *efs.EFS, id string) (*efs.BackupPolicy, error) {
input := &efs.DescribeBackupPolicyInput{
FileSystemId: aws.String(id),
Expand Down
61 changes: 26 additions & 35 deletions aws/resource_aws_efs_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/efs"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/efs/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsEfsFileSystem() *schema.Resource {
Expand Down Expand Up @@ -112,9 +115,14 @@ func resourceAwsEfsFileSystem() *schema.Resource {
Schema: map[string]*schema.Schema{
"transition_to_ia": {
Type: schema.TypeString,
Required: true,
Optional: true,
ValidateFunc: validation.StringInSlice(efs.TransitionToIARules_Values(), false),
},
"transition_to_primary_storage_class": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(efs.TransitionToPrimaryStorageClassRules_Values(), false),
},
},
},
},
Expand Down Expand Up @@ -279,35 +287,17 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
FileSystemId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") {
log.Printf("[WARN] EFS file system (%s) could not be found.", d.Id())
d.SetId("")
return nil
}
return err
}

if hasEmptyFileSystems(resp) {
return fmt.Errorf("EFS file system %q could not be found.", d.Id())
}

var fs *efs.FileSystemDescription
for _, f := range resp.FileSystems {
if d.Id() == *f.FileSystemId {
fs = f
break
}
}
if fs == nil {
log.Printf("[WARN] EFS File System (%s) not found, removing from state", d.Id())
fs, err := finder.FileSystemByID(conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EFS file system (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading EFS file system (%s): %w", d.Id(), err)
}

d.Set("arn", fs.FileSystemArn)
d.Set("availability_zone_id", fs.AvailabilityZoneId)
d.Set("availability_zone_name", fs.AvailabilityZoneName)
Expand Down Expand Up @@ -359,14 +349,14 @@ func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) er
FileSystemId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") {
if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) {
return nil
}
return fmt.Errorf("Error delete file system: %s with err %s", d.Id(), err.Error())
}

if _, err := waiter.FileSystemDeleted(conn, d.Id()); err != nil {
if isAWSErr(err, efs.ErrCodeFileSystemNotFound, "") {
if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) {
return nil
}
return fmt.Errorf("error waiting for EFS file system (%s) deletion: %w", d.Id(), err)
Expand All @@ -375,13 +365,6 @@ func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) er
return nil
}

func hasEmptyFileSystems(fs *efs.DescribeFileSystemsOutput) bool {
if fs != nil && len(fs.FileSystems) > 0 {
return false
}
return true
}

func flattenEfsFileSystemLifecyclePolicies(apiObjects []*efs.LifecyclePolicy) []interface{} {
var tfList []interface{}

Expand All @@ -396,6 +379,10 @@ func flattenEfsFileSystemLifecyclePolicies(apiObjects []*efs.LifecyclePolicy) []
tfMap["transition_to_ia"] = aws.StringValue(apiObject.TransitionToIA)
}

if apiObject.TransitionToPrimaryStorageClass != nil {
tfMap["transition_to_primary_storage_class"] = aws.StringValue(apiObject.TransitionToPrimaryStorageClass)
}

tfList = append(tfList, tfMap)
}

Expand All @@ -418,6 +405,10 @@ func expandEfsFileSystemLifecyclePolicies(tfList []interface{}) []*efs.Lifecycle
apiObject.TransitionToIA = aws.String(v)
}

if v, ok := tfMap["transition_to_primary_storage_class"].(string); ok && v != "" {
apiObject.TransitionToPrimaryStorageClass = aws.String(v)
}

apiObjects = append(apiObjects, apiObject)
}

Expand Down
Loading

0 comments on commit 113c715

Please sign in to comment.