2015-04-26 03:53:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-08-29 18:40:18 +02:00
|
|
|
"strings"
|
2015-04-26 03:53:21 +02:00
|
|
|
"time"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elasticache"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsElasticacheSubnetGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsElasticacheSubnetGroupCreate,
|
|
|
|
Read: resourceAwsElasticacheSubnetGroupRead,
|
2015-06-02 18:19:23 +02:00
|
|
|
Update: resourceAwsElasticacheSubnetGroupUpdate,
|
2015-04-26 03:53:21 +02:00
|
|
|
Delete: resourceAwsElasticacheSubnetGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-04-18 23:06:15 +02:00
|
|
|
Optional: true,
|
|
|
|
Default: "Managed by Terraform",
|
2015-04-26 03:53:21 +02:00
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-08-29 18:40:18 +02:00
|
|
|
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))
|
|
|
|
},
|
2015-04-26 03:53:21 +02:00
|
|
|
},
|
|
|
|
"subnet_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
2015-06-28 09:08:50 +02:00
|
|
|
Required: true,
|
2015-04-26 03:53:21 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-04-26 03:53:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElasticacheSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
// Get the group properties
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
desc := d.Get("description").(string)
|
|
|
|
subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Cache subnet group create: name: %s, description: %s", name, desc)
|
|
|
|
|
|
|
|
subnetIds := expandStringList(subnetIdsSet.List())
|
|
|
|
|
|
|
|
req := &elasticache.CreateCacheSubnetGroupInput{
|
|
|
|
CacheSubnetGroupDescription: aws.String(desc),
|
|
|
|
CacheSubnetGroupName: aws.String(name),
|
2015-08-17 20:27:16 +02:00
|
|
|
SubnetIds: subnetIds,
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.CreateCacheSubnetGroup(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating CacheSubnetGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the group name as the resource ID
|
2015-08-29 18:40:18 +02:00
|
|
|
// 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))
|
2015-04-26 03:53:21 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElasticacheSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
req := &elasticache.DescribeCacheSubnetGroupsInput{
|
|
|
|
CacheSubnetGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := conn.DescribeCacheSubnetGroups(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(res.CacheSubnetGroups) == 0 {
|
|
|
|
return fmt.Errorf("Error missing %v", d.Get("name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
var group *elasticache.CacheSubnetGroup
|
|
|
|
for _, g := range res.CacheSubnetGroups {
|
|
|
|
log.Printf("[DEBUG] %v %v", g.CacheSubnetGroupName, d.Id())
|
|
|
|
if *g.CacheSubnetGroupName == d.Id() {
|
|
|
|
group = g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if group == nil {
|
|
|
|
return fmt.Errorf("Error retrieving cache subnet group: %v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, len(group.Subnets))
|
|
|
|
for i, s := range group.Subnets {
|
|
|
|
ids[i] = *s.SubnetIdentifier
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", group.CacheSubnetGroupName)
|
|
|
|
d.Set("description", group.CacheSubnetGroupDescription)
|
|
|
|
d.Set("subnet_ids", ids)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-02 18:19:23 +02:00
|
|
|
func resourceAwsElasticacheSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
if d.HasChange("subnet_ids") || d.HasChange("description") {
|
|
|
|
var subnets []*string
|
|
|
|
if v := d.Get("subnet_ids"); v != nil {
|
|
|
|
for _, v := range v.(*schema.Set).List() {
|
|
|
|
subnets = append(subnets, aws.String(v.(string)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Updating ElastiCache Subnet Group")
|
|
|
|
|
|
|
|
_, err := conn.ModifyCacheSubnetGroup(&elasticache.ModifyCacheSubnetGroupInput{
|
|
|
|
CacheSubnetGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
CacheSubnetGroupDescription: aws.String(d.Get("description").(string)),
|
2015-08-17 20:27:16 +02:00
|
|
|
SubnetIds: subnets,
|
2015-06-02 18:19:23 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsElasticacheSubnetGroupRead(d, meta)
|
|
|
|
}
|
2015-04-26 03:53:21 +02:00
|
|
|
func resourceAwsElasticacheSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).elasticacheconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Cache subnet group delete: %s", d.Id())
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.Retry(5*time.Minute, func() *resource.RetryError {
|
2015-04-26 03:53:21 +02:00
|
|
|
_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
|
|
|
|
CacheSubnetGroupName: aws.String(d.Id()),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
apierr, ok := err.(awserr.Error)
|
2015-04-26 03:53:21 +02:00
|
|
|
if !ok {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
2016-02-24 16:48:32 +01:00
|
|
|
log.Printf("[DEBUG] APIError.Code: %v", apierr.Code())
|
2015-05-20 13:21:23 +02:00
|
|
|
switch apierr.Code() {
|
2015-04-26 03:53:21 +02:00
|
|
|
case "DependencyViolation":
|
|
|
|
// If it is a dependency violation, we want to retry
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2015-04-26 03:53:21 +02:00
|
|
|
default:
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-04-26 03:53:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|