aws_elasticache_subnet_group normalizes name to lowercase.

The Elasticache API accepts a mixed-case subnet name on create, but
normalizes it to lowercase before storing it. When retrieving a subnet,
the name is treated as case-sensitive, so the lowercase version must be
used.

Given that case within subnet names is not significant, the new StateFunc
on the name attribute causes the state to reflect the lowercase version
that the API uses, and changes in case alone will not show as a diff.

Given that we must look up subnet names in lower case, we set the
instance id to be a lowercase version of the user's provided name. This
then allows a later Refresh call to succeed even if the user provided
a mixed-case name.

Previously users could work around this by just avoiding putting uppercase
letters in the name, but that is often inconvenient if e.g. the name is
being constructed from variables defined elsewhere that may already have
uppercase letters present.
This commit is contained in:
Martin Atkins 2015-08-29 09:40:18 -07:00
parent 7d142134f2
commit 4ae3a17eab
3 changed files with 17 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package aws
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -29,6 +30,12 @@ func resourceAwsElasticacheSubnetGroup() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
StateFunc: func(val interface{}) string {
// Elasticache normalizes subnet names to lowercase,
// so we have to do this too or else we can end up
// with non-converging diffs.
return strings.ToLower(val.(string))
},
}, },
"subnet_ids": &schema.Schema{ "subnet_ids": &schema.Schema{
Type: schema.TypeSet, Type: schema.TypeSet,
@ -66,7 +73,10 @@ func resourceAwsElasticacheSubnetGroupCreate(d *schema.ResourceData, meta interf
} }
// Assign the group name as the resource ID // Assign the group name as the resource ID
d.SetId(name) // Elasticache always retains the name in lower case, so we have to
// mimic that or else we won't be able to refresh a resource whose
// name contained uppercase characters.
d.SetId(strings.ToLower(name))
return nil return nil
} }

View File

@ -150,7 +150,10 @@ resource "aws_subnet" "foo" {
} }
resource "aws_elasticache_subnet_group" "bar" { resource "aws_elasticache_subnet_group" "bar" {
name = "tf-test-cache-subnet-%03d" // Including uppercase letters in this name to ensure
// that we correctly handle the fact that the API
// normalizes names to lowercase.
name = "tf-TEST-cache-subnet-%03d"
description = "tf-test-cache-subnet-group-descr" description = "tf-test-cache-subnet-group-descr"
subnet_ids = ["${aws_subnet.foo.id}"] subnet_ids = ["${aws_subnet.foo.id}"]
} }

View File

@ -45,8 +45,8 @@ resource "aws_elasticache_subnet_group" "bar" {
The following arguments are supported: The following arguments are supported:
* `description` (Required) Description for the cache subnet group * `description` (Required) Description for the cache subnet group
* `name` (Required) Name for the cache subnet group. This value is stored as * `name` (Required) Name for the cache subnet group. Elasticache converts
a lowercase string this name to lowercase.
* `subnet_ids` (Required) List of VPC Subnet IDs for the cache subnet group * `subnet_ids` (Required) List of VPC Subnet IDs for the cache subnet group
## Attributes Reference ## Attributes Reference