diff --git a/aws/data_source_aws_directory_service_directory.go b/aws/data_source_aws_directory_service_directory.go index 659c4279546..2981e2c60cb 100644 --- a/aws/data_source_aws_directory_service_directory.go +++ b/aws/data_source_aws_directory_service_directory.go @@ -50,7 +50,6 @@ func dataSourceAwsDirectoryServiceDirectory() *schema.Resource { Type: schema.TypeSet, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "vpc_id": { Type: schema.TypeString, @@ -73,7 +72,6 @@ func dataSourceAwsDirectoryServiceDirectory() *schema.Resource { Type: schema.TypeSet, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "customer_username": { Type: schema.TypeString, @@ -83,13 +81,11 @@ func dataSourceAwsDirectoryServiceDirectory() *schema.Resource { Type: schema.TypeSet, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "subnet_ids": { Type: schema.TypeSet, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "vpc_id": { Type: schema.TypeString, @@ -114,7 +110,6 @@ func dataSourceAwsDirectoryServiceDirectory() *schema.Resource { "dns_ip_addresses": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, Computed: true, }, "security_group_id": { @@ -142,13 +137,14 @@ func dataSourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta int DirectoryIds: []*string{aws.String(directoryID)}, }) if err != nil { - return err + if isAWSErr(err, directoryservice.ErrCodeEntityDoesNotExistException, "") { + return fmt.Errorf("DirectoryService Directory (%s) not found", directoryID) + } + return fmt.Errorf("error reading DirectoryService Directory: %w", err) } - if len(out.DirectoryDescriptions) == 0 { - log.Printf("[WARN] Directory %s not found", d.Id()) - d.SetId("") - return nil + if out == nil || len(out.DirectoryDescriptions) == 0 { + return fmt.Errorf("error reading DirectoryService Directory (%s): empty output", directoryID) } d.SetId(directoryID) @@ -160,11 +156,16 @@ func dataSourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta int d.Set("alias", dir.Alias) d.Set("description", dir.Description) - if *dir.Type == directoryservice.DirectoryTypeAdconnector { - d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.ConnectSettings.ConnectIps))) + var addresses []interface{} + if aws.StringValue(dir.Type) == directoryservice.DirectoryTypeAdconnector { + addresses = flattenStringList(dir.ConnectSettings.ConnectIps) } else { - d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.DnsIpAddrs))) + addresses = flattenStringList(dir.DnsIpAddrs) + } + if err := d.Set("dns_ip_addresses", addresses); err != nil { + return fmt.Errorf("error setting dns_ip_addresses: %w", err) } + d.Set("name", dir.Name) d.Set("short_name", dir.ShortName) d.Set("size", dir.Size) @@ -172,28 +173,30 @@ func dataSourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta int d.Set("type", dir.Type) if err := d.Set("vpc_settings", flattenDSVpcSettings(dir.VpcSettings)); err != nil { - return fmt.Errorf("error setting VPC settings: %s", err) + return fmt.Errorf("error setting VPC settings: %w", err) } if err := d.Set("connect_settings", flattenDSConnectSettings(dir.DnsIpAddrs, dir.ConnectSettings)); err != nil { - return fmt.Errorf("error setting connect settings: %s", err) + return fmt.Errorf("error setting connect settings: %w", err) } d.Set("enable_sso", dir.SsoEnabled) + var securityGroupId *string if aws.StringValue(dir.Type) == directoryservice.DirectoryTypeAdconnector { - d.Set("security_group_id", aws.StringValue(dir.ConnectSettings.SecurityGroupId)) + securityGroupId = dir.ConnectSettings.SecurityGroupId } else { - d.Set("security_group_id", aws.StringValue(dir.VpcSettings.SecurityGroupId)) + securityGroupId = dir.VpcSettings.SecurityGroupId } + d.Set("security_group_id", aws.StringValue(securityGroupId)) tags, err := keyvaluetags.DirectoryserviceListTags(conn, d.Id()) if err != nil { - return fmt.Errorf("error listing tags for Directory Service Directory (%s): %s", d.Id(), err) + return fmt.Errorf("error listing tags for Directory Service Directory (%s): %w", d.Id(), err) } if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return fmt.Errorf("error setting tags: %s", err) + return fmt.Errorf("error setting tags: %w", err) } return nil diff --git a/aws/data_source_aws_directory_service_directory_test.go b/aws/data_source_aws_directory_service_directory_test.go index 768e6c25b1a..38f6f7f6bd1 100644 --- a/aws/data_source_aws_directory_service_directory_test.go +++ b/aws/data_source_aws_directory_service_directory_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" @@ -9,6 +10,20 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) +func TestAccDataSourceAwsDirectoryServiceDirectory_NonExistent(t *testing.T) { + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsDirectoryServiceDirectoryConfig_NonExistent, + ExpectError: regexp.MustCompile(`not found`), + }, + }, + }) +} + func TestAccDataSourceAwsDirectoryServiceDirectory_SimpleAD(t *testing.T) { alias := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_directory_service_directory.test-simple-ad" @@ -93,6 +108,12 @@ func TestAccDataSourceAWSDirectoryServiceDirectory_connector(t *testing.T) { }) } +const testAccDataSourceAwsDirectoryServiceDirectoryConfig_NonExistent = ` +data "aws_directory_service_directory" "test" { + directory_id = "d-abc0123456" +} +` + func testAccDataSourceAwsDirectoryServiceDirectoryConfig_Prerequisites(adType string) string { return fmt.Sprintf(` data "aws_availability_zones" "available" {