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

data-source/efs_filesystem: return error if 0 results found #14005

Merged
merged 1 commit into from
Jul 14, 2020
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
30 changes: 12 additions & 18 deletions aws/data_source_aws_efs_file_system.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -94,27 +95,20 @@ func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) er
log.Printf("[DEBUG] Reading EFS File System: %s", describeEfsOpts)
describeResp, err := efsconn.DescribeFileSystems(describeEfsOpts)
if err != nil {
return fmt.Errorf("Error retrieving EFS: %s", err)
return fmt.Errorf("error reading EFS FileSystem: %w", err)
}
if len(describeResp.FileSystems) != 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.FileSystems))
}

d.SetId(*describeResp.FileSystems[0].FileSystemId)

var fs *efs.FileSystemDescription
for _, f := range describeResp.FileSystems {
if d.Id() == *f.FileSystemId {
fs = f
break
}
if describeResp == nil || len(describeResp.FileSystems) == 0 {
return errors.New("error reading EFS FileSystem: empty output")
}
if fs == nil {
log.Printf("[WARN] EFS (%s) not found, removing from state", d.Id())
d.SetId("")
return nil

if len(describeResp.FileSystems) > 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.FileSystems))
}

fs := describeResp.FileSystems[0]

d.SetId(aws.StringValue(fs.FileSystemId))
d.Set("creation_token", fs.CreationToken)
d.Set("performance_mode", fs.PerformanceMode)

Expand Down Expand Up @@ -144,12 +138,12 @@ func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) er
FileSystemId: fs.FileSystemId,
})
if err != nil {
return fmt.Errorf("Error describing lifecycle configuration for EFS file system (%s): %s",
return fmt.Errorf("Error describing lifecycle configuration for EFS file system (%s): %w",
aws.StringValue(fs.FileSystemId), err)
}

if err := d.Set("lifecycle_policy", flattenEfsFileSystemLifecyclePolicies(res.LifecyclePolicies)); err != nil {
return fmt.Errorf("error setting lifecycle_policy: %s", err)
return fmt.Errorf("error setting lifecycle_policy: %w", err)
}

d.Set("dns_name", meta.(*AWSClient).RegionalHostname(fmt.Sprintf("%s.efs", aws.StringValue(fs.FileSystemId))))
Expand Down
22 changes: 21 additions & 1 deletion aws/data_source_aws_efs_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ func TestAccDataSourceAwsEfsFileSystem_name(t *testing.T) {
})
}

func TestAccDataSourceAwsEfsFileSystem_NonExistent(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEfsFileSystemIDConfig_NonExistent,
ExpectError: regexp.MustCompile(`error reading EFS FileSystem`),
},
},
})
}

func testAccDataSourceAwsEfsFileSystemCheck(dName, rName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[dName]
Expand Down Expand Up @@ -101,11 +115,17 @@ func testAccDataSourceAwsEfsFileSystemCheck(dName, rName string) resource.TestCh
}
}

const testAccDataSourceAwsEfsFileSystemIDConfig_NonExistent = `
data "aws_efs_file_system" "test" {
file_system_id = "fs-nonexistent"
}
`

const testAccDataSourceAwsEfsFileSystemNameConfig = `
resource "aws_efs_file_system" "test" {}
data "aws_efs_file_system" "test" {
creation_token = "${aws_efs_file_system.test.creation_token}"
creation_token = "${aws_efs_file_system.test.creation_token}"
}
`

Expand Down