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

New Data Source: aws_directory_service_directory #11282

Merged
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
175 changes: 175 additions & 0 deletions aws/data_source_aws_directory_service_directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directoryservice"
)

func dataSourceAwsDirectoryServiceDirectory() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDirectoryServiceDirectoryRead,

Schema: map[string]*schema.Schema{
"directory_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeString,
Computed: true,
},
"alias": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"short_name": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"vpc_settings": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"connect_settings": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"customer_username": {
Type: schema.TypeString,
Computed: true,
},
"customer_dns_ips": {
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,
Computed: true,
},
},
},
},
"enable_sso": {
Type: schema.TypeBool,
Computed: true,
},
"access_url": {
Type: schema.TypeString,
Computed: true,
},
"dns_ip_addresses": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"edition": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dsconn

directoryID := d.Get("directory_id").(string)
out, err := conn.DescribeDirectories(&directoryservice.DescribeDirectoriesInput{
DirectoryIds: []*string{aws.String(directoryID)},
})
if err != nil {
return err
}

if len(out.DirectoryDescriptions) == 0 {
log.Printf("[WARN] Directory %s not found", d.Id())
d.SetId("")
return nil
}

d.SetId(directoryID)

dir := out.DirectoryDescriptions[0]
log.Printf("[DEBUG] Received DS directory: %s", dir)

d.Set("access_url", dir.AccessUrl)
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)))
} else {
d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.DnsIpAddrs)))
}
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)
d.Set("vpc_settings", flattenDSVpcSettings(dir.VpcSettings))
d.Set("connect_settings", flattenDSConnectSettings(dir.DnsIpAddrs, dir.ConnectSettings))
d.Set("enable_sso", dir.SsoEnabled)

if aws.StringValue(dir.Type) == directoryservice.DirectoryTypeAdconnector {
d.Set("security_group_id", aws.StringValue(dir.ConnectSettings.SecurityGroupId))
} else {
d.Set("security_group_id", aws.StringValue(dir.VpcSettings.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)
}

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
156 changes: 156 additions & 0 deletions aws/data_source_aws_directory_service_directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceAwsDirectoryServiceDirectory_SimpleAD(t *testing.T) {
alias := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_directory_service_directory.test-simple-ad"
dataSourceName := "data.aws_directory_service_directory.test-simple-ad"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsDirectoryServiceDirectoryConfig_SimpleAD(alias),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "type", "SimpleAD"),
resource.TestCheckResourceAttr(dataSourceName, "size", "Small"),
resource.TestCheckResourceAttr(dataSourceName, "name", "tf-testacc-corp.neverland.com"),
resource.TestCheckResourceAttr(dataSourceName, "description", "tf-testacc SimpleAD"),
resource.TestCheckResourceAttr(dataSourceName, "short_name", "corp"),
resource.TestCheckResourceAttr(dataSourceName, "alias", alias),
resource.TestCheckResourceAttr(dataSourceName, "enable_sso", "false"),
resource.TestCheckResourceAttr(dataSourceName, "vpc_settings.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "vpc_settings.0.vpc_id", resourceName, "vpc_settings.0.vpc_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "vpc_settings.0.subnet_ids", resourceName, "vpc_settings.0.subnet_ids"),
resource.TestCheckResourceAttr(dataSourceName, "access_url", fmt.Sprintf("%s.awsapps.com", alias)),
resource.TestCheckResourceAttrPair(dataSourceName, "dns_ip_addresses", resourceName, "dns_ip_addresses"),
resource.TestCheckResourceAttrPair(dataSourceName, "security_group_id", resourceName, "security_group_id"),
),
},
},
})
}

func TestAccDataSourceAwsDirectoryServiceDirectory_MicrosoftAD(t *testing.T) {
alias := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_directory_service_directory.test-microsoft-ad"
dataSourceName := "data.aws_directory_service_directory.test-microsoft-ad"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsDirectoryServiceDirectoryConfig_MicrosoftAD(alias),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "type", "MicrosoftAD"),
resource.TestCheckResourceAttr(dataSourceName, "edition", "Standard"),
resource.TestCheckResourceAttr(dataSourceName, "name", "tf-testacc-corp.neverland.com"),
resource.TestCheckResourceAttr(dataSourceName, "description", "tf-testacc MicrosoftAD"),
resource.TestCheckResourceAttr(dataSourceName, "short_name", "corp"),
resource.TestCheckResourceAttr(dataSourceName, "alias", alias),
resource.TestCheckResourceAttr(dataSourceName, "enable_sso", "false"),
resource.TestCheckResourceAttr(dataSourceName, "vpc_settings.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "vpc_settings.0.vpc_id", resourceName, "vpc_settings.0.vpc_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "vpc_settings.0.subnet_ids", resourceName, "vpc_settings.0.subnet_ids"),
resource.TestCheckResourceAttr(dataSourceName, "access_url", fmt.Sprintf("%s.awsapps.com", alias)),
resource.TestCheckResourceAttrPair(dataSourceName, "dns_ip_addresses", resourceName, "dns_ip_addresses"),
resource.TestCheckResourceAttrPair(dataSourceName, "security_group_id", resourceName, "security_group_id"),
),
},
},
})
}

func testAccDataSourceAwsDirectoryServiceDirectoryConfig_Prerequisites(adType string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
}

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "tf-testacc-%s"
}
}

resource "aws_subnet" "primary" {
vpc_id = "${aws_vpc.main.id}"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
cidr_block = "10.0.1.0/24"

tags = {
Name = "tf-testacc-%s-primary"
}
}
resource "aws_subnet" "secondary" {
vpc_id = "${aws_vpc.main.id}"
availability_zone = "${data.aws_availability_zones.available.names[1]}"
cidr_block = "10.0.2.0/24"

tags = {
Name = "tf-testacc-%s-secondary"
}
}
`, adType, adType, adType)
}

func testAccDataSourceAwsDirectoryServiceDirectoryConfig_SimpleAD(alias string) string {
return testAccDataSourceAwsDirectoryServiceDirectoryConfig_Prerequisites("simple-ad") + fmt.Sprintf(`
resource "aws_directory_service_directory" "test-simple-ad" {
type = "SimpleAD"
size = "Small"
name = "tf-testacc-corp.neverland.com"
description = "tf-testacc SimpleAD"
short_name = "corp"
password = "#S1ncerely"

alias = %q
enable_sso = false

vpc_settings {
vpc_id = "${aws_vpc.main.id}"
subnet_ids = ["${aws_subnet.primary.id}", "${aws_subnet.secondary.id}"]
}
}

data "aws_directory_service_directory" "test-simple-ad" {
directory_id = "${aws_directory_service_directory.test-simple-ad.id}"
}
`, alias)
}

func testAccDataSourceAwsDirectoryServiceDirectoryConfig_MicrosoftAD(alias string) string {
return testAccDataSourceAwsDirectoryServiceDirectoryConfig_Prerequisites("microsoft-ad") + fmt.Sprintf(`
resource "aws_directory_service_directory" "test-microsoft-ad" {
type = "MicrosoftAD"
edition = "Standard"
name = "tf-testacc-corp.neverland.com"
description = "tf-testacc MicrosoftAD"
short_name = "corp"
password = "#S1ncerely"

alias = %q
enable_sso = false

vpc_settings {
vpc_id = "${aws_vpc.main.id}"
subnet_ids = ["${aws_subnet.primary.id}", "${aws_subnet.secondary.id}"]
}
}

data "aws_directory_service_directory" "test-microsoft-ad" {
directory_id = "${aws_directory_service_directory.test-microsoft-ad.id}"
}
`, alias)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func Provider() terraform.ResourceProvider {
"aws_db_event_categories": dataSourceAwsDbEventCategories(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_directory_service_directory": dataSourceAwsDirectoryServiceDirectory(),
"aws_dx_gateway": dataSourceAwsDxGateway(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_default_kms_key": dataSourceAwsEbsDefaultKmsKey(),
Expand Down
8 changes: 8 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,14 @@
<li>
<a href="#">Directory Service</a>
<ul class="nav">
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/r/directory_service_directory.html">aws_directory_service_directory</a>
</li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="nav nav-auto-expand">
Expand Down
Loading