Skip to content

Commit

Permalink
Merge pull request #11247 from hashicorp/b-aws-rds-mssql-timezone
Browse files Browse the repository at this point in the history
provider/aws: Add support for setting MSSQL Timezone in aws_db_instance
  • Loading branch information
catsby authored Jan 18, 2017
2 parents 1faf17f + 549fd22 commit c5f899d
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 0 deletions.
13 changes: 13 additions & 0 deletions builtin/providers/aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ func resourceAwsDbInstance() *schema.Resource {
ValidateFunc: validateArn,
},

"timezone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -529,6 +536,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.CharacterSetName = aws.String(attr.(string))
}

if attr, ok := d.GetOk("timezone"); ok {
opts.Timezone = aws.String(attr.(string))
}

if attr, ok := d.GetOk("maintenance_window"); ok {
opts.PreferredMaintenanceWindow = aws.String(attr.(string))
}
Expand Down Expand Up @@ -679,6 +690,8 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("character_set_name", v.CharacterSetName)
}

d.Set("timezone", v.Timezone)

if len(v.DBParameterGroups) > 0 {
d.Set("parameter_group_name", v.DBParameterGroups[0].DBParameterGroupName)
}
Expand Down
190 changes: 190 additions & 0 deletions builtin/providers/aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,41 @@ func TestAccAWSDBInstance_portUpdate(t *testing.T) {
})
}

func TestAccAWSDBInstance_MSSQL_TZ(t *testing.T) {
var v rds.DBInstance

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSDBMSSQL_timezone,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.mssql", &v),
testAccCheckAWSDBInstanceAttributes_MSSQL(&v, ""),
resource.TestCheckResourceAttr(
"aws_db_instance.mssql", "allocated_storage", "20"),
resource.TestCheckResourceAttr(
"aws_db_instance.mssql", "engine", "sqlserver-ex"),
),
},

resource.TestStep{
Config: testAccAWSDBMSSQL_timezone_AKST,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.mssql", &v),
testAccCheckAWSDBInstanceAttributes_MSSQL(&v, "Alaskan Standard Time"),
resource.TestCheckResourceAttr(
"aws_db_instance.mssql", "allocated_storage", "20"),
resource.TestCheckResourceAttr(
"aws_db_instance.mssql", "engine", "sqlserver-ex"),
),
},
},
})
}

func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn

Expand Down Expand Up @@ -328,6 +363,26 @@ func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFu
}
}

func testAccCheckAWSDBInstanceAttributes_MSSQL(v *rds.DBInstance, tz string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if *v.Engine != "sqlserver-ex" {
return fmt.Errorf("bad engine: %#v", *v.Engine)
}

rtz := ""
if v.Timezone != nil {
rtz = *v.Timezone
}

if tz != rtz {
return fmt.Errorf("Expected (%s) Timezone for MSSQL test, got (%s)", tz, rtz)
}

return nil
}
}

func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {

Expand Down Expand Up @@ -923,3 +978,138 @@ resource "aws_db_instance" "bar" {
apply_immediately = true
}`, rName)
}

const testAccAWSDBMSSQL_timezone = `
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
enable_dns_hostnames = true
tags {
Name = "tf-rds-mssql-timezone-test"
}
}
resource "aws_db_subnet_group" "rds_one" {
name = "rds_one_db"
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"
}
resource "aws_subnet" "other" {
vpc_id = "${aws_vpc.foo.id}"
availability_zone = "us-west-2b"
cidr_block = "10.1.2.0/24"
}
resource "aws_db_instance" "mssql" {
#identifier = "tf-test-mssql"
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
#publicly_accessible = true
vpc_security_group_ids = ["${aws_security_group.rds-mssql.id}"]
}
resource "aws_security_group" "rds-mssql" {
name = "tf-rds-mssql-test"
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}"
}
`

const testAccAWSDBMSSQL_timezone_AKST = `
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
enable_dns_hostnames = true
tags {
Name = "tf-rds-mssql-timezone-test"
}
}
resource "aws_db_subnet_group" "rds_one" {
name = "rds_one_db"
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"
}
resource "aws_subnet" "other" {
vpc_id = "${aws_vpc.foo.id}"
availability_zone = "us-west-2b"
cidr_block = "10.1.2.0/24"
}
resource "aws_db_instance" "mssql" {
#identifier = "tf-test-mssql"
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
#publicly_accessible = true
vpc_security_group_ids = ["${aws_security_group.rds-mssql.id}"]
timezone = "Alaskan Standard Time"
}
resource "aws_security_group" "rds-mssql" {
name = "tf-rds-mssql-test"
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}"
}
`
2 changes: 2 additions & 0 deletions website/source/docs/providers/aws/r/db_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ what IAM permissions are needed to allow Enhanced Monitoring for RDS Instances.
* `character_set_name` - (Optional) The character set name to use for DB encoding in Oracle instances. This can't be changed.
[Oracle Character Sets Supported in Amazon RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.OracleCharacterSets.html)
* `tags` - (Optional) A mapping of tags to assign to the resource.
* `timezone` - (Optional) Time zone of the DB instance. `timezone` is currently only supported by Microsoft SQL Server.
The `timezone` can only be set on creation. See [MSSQL User Guide](http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html#SQLServer.Concepts.General.TimeZone) for more information

~> **NOTE:** Removing the `replicate_source_db` attribute from an existing RDS
Replicate database managed by Terraform will promote the database to a fully
Expand Down

0 comments on commit c5f899d

Please sign in to comment.