Skip to content

Commit

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

data-source/directory_service_directory: return error if 0 results found
  • Loading branch information
anGie44 authored Jul 14, 2020
2 parents bb94787 + 8532b3e commit dc334c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
41 changes: 22 additions & 19 deletions aws/data_source_aws_directory_service_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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": {
Expand Down Expand Up @@ -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)
Expand All @@ -160,40 +156,47 @@ 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)
d.Set("edition", dir.Edition)
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
Expand Down
21 changes: 21 additions & 0 deletions aws/data_source_aws_directory_service_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"

"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"
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit dc334c0

Please sign in to comment.