diff --git a/aws/resource_aws_db_instance.go b/aws/resource_aws_db_instance.go index 92b4a307041..4ce207ee0bc 100644 --- a/aws/resource_aws_db_instance.go +++ b/aws/resource_aws_db_instance.go @@ -404,6 +404,18 @@ func resourceAwsDbInstance() *schema.Resource { }, }, + "domain": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "domain_iam_role_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "tags": tagsSchema(), }, } @@ -891,6 +903,14 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error opts.EnableIAMDatabaseAuthentication = aws.Bool(attr.(bool)) } + if attr, ok := d.GetOk("domain"); ok { + opts.Domain = aws.String(attr.(string)) + } + + if attr, ok := d.GetOk("domain_iam_role_name"); ok { + opts.DomainIAMRoleName = aws.String(attr.(string)) + } + log.Printf("[DEBUG] DB Instance create configuration: %#v", opts) var err error err = resource.Retry(5*time.Minute, func() *resource.RetryError { @@ -1012,6 +1032,11 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("error setting enabled_cloudwatch_logs_exports: %s", err) } + if v.DomainMemberships != nil { + d.Set("domain", v.DomainMemberships[0].Domain) + d.Set("domain_iam_role_name", v.DomainMemberships[0].IAMRoleName) + } + // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn @@ -1262,6 +1287,18 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error requestUpdate = true } + if d.HasChange("domain") && !d.IsNewResource() { + d.SetPartial("domain") + req.Domain = aws.String(d.Get("domain").(string)) + requestUpdate = true + } + + if d.HasChange("domain_iam_role_name") && !d.IsNewResource() { + d.SetPartial("domain_iam_role_name") + req.DomainIAMRoleName = aws.String(d.Get("domain_iam_role_name").(string)) + requestUpdate = true + } + log.Printf("[DEBUG] Send DB Instance Modification request: %t", requestUpdate) if requestUpdate { log.Printf("[DEBUG] DB Instance Modification request: %s", req) diff --git a/aws/resource_aws_db_instance_test.go b/aws/resource_aws_db_instance_test.go index c36e7de49b2..cf309d9c7e8 100644 --- a/aws/resource_aws_db_instance_test.go +++ b/aws/resource_aws_db_instance_test.go @@ -459,6 +459,33 @@ func TestAccAWSDBInstance_MSSQL_TZ(t *testing.T) { }) } +func TestAccAWSDBInstance_MSSQL_Domain(t *testing.T) { + var v rds.DBInstance + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBMSSQL_domain(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBInstanceExists("aws_db_instance.mssql", &v), + resource.TestCheckResourceAttrSet( + "aws_db_instance.mssql", "domain"), + resource.TestCheckResourceAttrSet( + "aws_db_instance.mssql", "domain_iam_role_name"), + resource.TestCheckResourceAttr( + "aws_db_instance.mssql", "allocated_storage", "20"), + resource.TestCheckResourceAttr( + "aws_db_instance.mssql", "engine", "sqlserver-ex"), + ), + }, + }, + }) +} + func TestAccAWSDBInstance_MinorVersion(t *testing.T) { var v rds.DBInstance @@ -1633,6 +1660,116 @@ resource "aws_security_group_rule" "rds-mssql-1" { `, rInt, rInt, rInt) } +func testAccAWSDBMSSQL_domain(rInt int) string { + return fmt.Sprintf(` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" + enable_dns_hostnames = true + tags { + Name = "terraform-testacc-db-instance-mssql-domain" + } +} + +resource "aws_db_subnet_group" "rds_one" { + name = "tf_acc_test_%d" + description = "db subnets for rds_one" + + subnet_ids = ["${aws_subnet.main.id}", "${aws_subnet.other.id}"] +} + +resource "aws_subnet" "main" { + vpc_id = "${aws_vpc.foo.id}" + availability_zone = "us-west-2a" + cidr_block = "10.1.1.0/24" + tags { + Name = "tf-acc-db-instance-mssql-domain-main" + } +} + +resource "aws_subnet" "other" { + vpc_id = "${aws_vpc.foo.id}" + availability_zone = "us-west-2b" + cidr_block = "10.1.2.0/24" + tags { + Name = "tf-acc-db-instance-mssql-domain-other" + } +} + +resource "aws_db_instance" "mssql" { + identifier = "tf-test-mssql-%d" + + db_subnet_group_name = "${aws_db_subnet_group.rds_one.name}" + + instance_class = "db.t2.micro" + allocated_storage = 20 + username = "somecrazyusername" + password = "somecrazypassword" + engine = "sqlserver-ex" + backup_retention_period = 0 + skip_final_snapshot = true + + domain = "${aws_directory_service_directory.directory.id}" + domain_iam_role_name = "${aws_iam_role.role.name}" + + vpc_security_group_ids = ["${aws_security_group.rds-mssql.id}"] +} + +resource "aws_security_group" "rds-mssql" { + name = "tf-rds-mssql-test-%d" + + description = "TF Testing" + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_security_group_rule" "rds-mssql-1" { + type = "egress" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + + security_group_id = "${aws_security_group.rds-mssql.id}" +} + +resource "aws_directory_service_directory" "directory" { + name = "corp.somedomain.com" + password = "SuperSecretPassw0rd" + type = "MicrosoftAD" + edition = "Standard" + + vpc_settings { + vpc_id = "${aws_vpc.foo.id}" + subnet_ids = ["${aws_subnet.main.id}", "${aws_subnet.other.id}"] + } +} + +resource "aws_iam_role" "role" { + name = "tf-acc-db-instance-mssql-domain-role" + + assume_role_policy = <