Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pagination support to DescribeFileSystems in EFS #33336

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/33336.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_efs_file_system: Fix `Search returned 0 results` errors when there are more than 101 file systems in the configured Region
```
39 changes: 32 additions & 7 deletions internal/service/efs/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -343,12 +344,32 @@ func resourceFileSystemDelete(ctx context.Context, d *schema.ResourceData, meta
return nil
}

func FindFileSystemByID(ctx context.Context, conn *efs.EFS, id string) (*efs.FileSystemDescription, error) {
input := &efs.DescribeFileSystemsInput{
FileSystemId: aws.String(id),
func findFileSystem(ctx context.Context, conn *efs.EFS, input *efs.DescribeFileSystemsInput, filter tfslices.Predicate[*efs.FileSystemDescription]) (*efs.FileSystemDescription, error) {
output, err := findFileSystems(ctx, conn, input, filter)

if err != nil {
return nil, err
}

output, err := conn.DescribeFileSystemsWithContext(ctx, input)
return tfresource.AssertSinglePtrResult(output)
}

func findFileSystems(ctx context.Context, conn *efs.EFS, input *efs.DescribeFileSystemsInput, filter tfslices.Predicate[*efs.FileSystemDescription]) ([]*efs.FileSystemDescription, error) {
var output []*efs.FileSystemDescription

err := conn.DescribeFileSystemsPagesWithContext(ctx, input, func(page *efs.DescribeFileSystemsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.FileSystems {
if v != nil && filter(v) {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, efs.ErrCodeFileSystemNotFound) {
return nil, &retry.NotFoundError{
Expand All @@ -361,11 +382,15 @@ func FindFileSystemByID(ctx context.Context, conn *efs.EFS, id string) (*efs.Fil
return nil, err
}

if output == nil || output.FileSystems == nil || len(output.FileSystems) == 0 || output.FileSystems[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
return output, nil
}

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

return output.FileSystems[0], nil
return findFileSystem(ctx, conn, input, tfslices.PredicateTrue[*efs.FileSystemDescription]())
}

func statusFileSystemLifeCycleState(ctx context.Context, conn *efs.EFS, id string) retry.StateRefreshFunc {
Expand Down
45 changes: 13 additions & 32 deletions internal/service/efs/file_system_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

// @SDKDataSource("aws_efs_file_system")
Expand Down Expand Up @@ -104,50 +106,29 @@ func dataSourceFileSystemRead(ctx context.Context, d *schema.ResourceData, meta
conn := meta.(*conns.AWSClient).EFSConn(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

tagsToMatch := tftags.New(ctx, d.Get("tags").(map[string]interface{})).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

describeEfsOpts := &efs.DescribeFileSystemsInput{}
input := &efs.DescribeFileSystemsInput{}

if v, ok := d.GetOk("creation_token"); ok {
describeEfsOpts.CreationToken = aws.String(v.(string))
input.CreationToken = aws.String(v.(string))
}

if v, ok := d.GetOk("file_system_id"); ok {
describeEfsOpts.FileSystemId = aws.String(v.(string))
}

describeResp, err := conn.DescribeFileSystemsWithContext(ctx, describeEfsOpts)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EFS FileSystem: %s", err)
input.FileSystemId = aws.String(v.(string))
}

if describeResp == nil || len(describeResp.FileSystems) == 0 {
return sdkdiag.AppendErrorf(diags, "reading EFS FileSystem: empty output")
}

results := describeResp.FileSystems

if len(tagsToMatch) > 0 {
var fileSystems []*efs.FileSystemDescription
filter := tfslices.PredicateTrue[*efs.FileSystemDescription]()

for _, fileSystem := range describeResp.FileSystems {
tags := KeyValueTags(ctx, fileSystem.Tags)

if !tags.ContainsAll(tagsToMatch) {
continue
}

fileSystems = append(fileSystems, fileSystem)
if tagsToMatch := tftags.New(ctx, d.Get("tags").(map[string]interface{})).IgnoreAWS().IgnoreConfig(ignoreTagsConfig); len(tagsToMatch) > 0 {
filter = func(v *efs.FileSystemDescription) bool {
return KeyValueTags(ctx, v.Tags).ContainsAll(tagsToMatch)
}

results = fileSystems
}

if count := len(results); count != 1 {
return sdkdiag.AppendErrorf(diags, "Search returned %d results, please revise so only one is returned", count)
}
fs, err := findFileSystem(ctx, conn, input, filter)

fs := results[0]
if err != nil {
return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EFS file system", err))
}

d.SetId(aws.StringValue(fs.FileSystemId))
d.Set("arn", fs.FileSystemArn)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/efs/file_system_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestAccEFSFileSystemDataSource_nonExistent_tags(t *testing.T) {
},
{
Config: testAccFileSystemDataSourceConfig_tagsNonExistent(rName),
ExpectError: regexache.MustCompile(`Search returned 0 results`),
ExpectError: regexache.MustCompile(`no matching EFS file system found`),
},
},
})
Expand Down