From e7aae27fe492479c605ca9a355f5f0107ad81c03 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Tue, 30 Jun 2020 22:46:51 -0400 Subject: [PATCH] return error for singular data source --- aws/data_source_aws_efs_file_system.go | 30 +++++++++------------ aws/data_source_aws_efs_file_system_test.go | 22 ++++++++++++++- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/aws/data_source_aws_efs_file_system.go b/aws/data_source_aws_efs_file_system.go index e7cf6e66165..afcae937804 100644 --- a/aws/data_source_aws_efs_file_system.go +++ b/aws/data_source_aws_efs_file_system.go @@ -1,6 +1,7 @@ package aws import ( + "errors" "fmt" "log" @@ -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) @@ -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)))) diff --git a/aws/data_source_aws_efs_file_system_test.go b/aws/data_source_aws_efs_file_system_test.go index f0dd41d7c47..c501b08594a 100644 --- a/aws/data_source_aws_efs_file_system_test.go +++ b/aws/data_source_aws_efs_file_system_test.go @@ -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] @@ -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}" } `