Skip to content

Commit

Permalink
Merge pull request #14005 from terraform-providers/td-singular-data-s…
Browse files Browse the repository at this point in the history
…ource-efs-fs

data-source/efs_filesystem: return error if 0 results found
  • Loading branch information
anGie44 authored Jul 14, 2020
2 parents 4d5c659 + e7aae27 commit 28ad057
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
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

0 comments on commit 28ad057

Please sign in to comment.