github.com/econnell/terraform@v0.5.4-0.20150722160631-78eb236786a4/builtin/providers/aws/resource_aws_elasticache_subnet_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/elasticache"
    11  	"github.com/hashicorp/terraform/helper/hashcode"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsElasticacheSubnetGroup() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsElasticacheSubnetGroupCreate,
    19  		Read:   resourceAwsElasticacheSubnetGroupRead,
    20  		Update: resourceAwsElasticacheSubnetGroupUpdate,
    21  		Delete: resourceAwsElasticacheSubnetGroupDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"description": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"name": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"subnet_ids": &schema.Schema{
    34  				Type:     schema.TypeSet,
    35  				Required: true,
    36  				Elem:     &schema.Schema{Type: schema.TypeString},
    37  				Set: func(v interface{}) int {
    38  					return hashcode.String(v.(string))
    39  				},
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceAwsElasticacheSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    46  	conn := meta.(*AWSClient).elasticacheconn
    47  
    48  	// Get the group properties
    49  	name := d.Get("name").(string)
    50  	desc := d.Get("description").(string)
    51  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    52  
    53  	log.Printf("[DEBUG] Cache subnet group create: name: %s, description: %s", name, desc)
    54  
    55  	subnetIds := expandStringList(subnetIdsSet.List())
    56  
    57  	req := &elasticache.CreateCacheSubnetGroupInput{
    58  		CacheSubnetGroupDescription: aws.String(desc),
    59  		CacheSubnetGroupName:        aws.String(name),
    60  		SubnetIDs:                   subnetIds,
    61  	}
    62  
    63  	_, err := conn.CreateCacheSubnetGroup(req)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating CacheSubnetGroup: %s", err)
    66  	}
    67  
    68  	// Assign the group name as the resource ID
    69  	d.SetId(name)
    70  
    71  	return nil
    72  }
    73  
    74  func resourceAwsElasticacheSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    75  	conn := meta.(*AWSClient).elasticacheconn
    76  	req := &elasticache.DescribeCacheSubnetGroupsInput{
    77  		CacheSubnetGroupName: aws.String(d.Get("name").(string)),
    78  	}
    79  
    80  	res, err := conn.DescribeCacheSubnetGroups(req)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	if len(res.CacheSubnetGroups) == 0 {
    85  		return fmt.Errorf("Error missing %v", d.Get("name"))
    86  	}
    87  
    88  	var group *elasticache.CacheSubnetGroup
    89  	for _, g := range res.CacheSubnetGroups {
    90  		log.Printf("[DEBUG] %v %v", g.CacheSubnetGroupName, d.Id())
    91  		if *g.CacheSubnetGroupName == d.Id() {
    92  			group = g
    93  		}
    94  	}
    95  	if group == nil {
    96  		return fmt.Errorf("Error retrieving cache subnet group: %v", res)
    97  	}
    98  
    99  	ids := make([]string, len(group.Subnets))
   100  	for i, s := range group.Subnets {
   101  		ids[i] = *s.SubnetIdentifier
   102  	}
   103  
   104  	d.Set("name", group.CacheSubnetGroupName)
   105  	d.Set("description", group.CacheSubnetGroupDescription)
   106  	d.Set("subnet_ids", ids)
   107  
   108  	return nil
   109  }
   110  
   111  func resourceAwsElasticacheSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   112  	conn := meta.(*AWSClient).elasticacheconn
   113  	if d.HasChange("subnet_ids") || d.HasChange("description") {
   114  		var subnets []*string
   115  		if v := d.Get("subnet_ids"); v != nil {
   116  			for _, v := range v.(*schema.Set).List() {
   117  				subnets = append(subnets, aws.String(v.(string)))
   118  			}
   119  		}
   120  		log.Printf("[DEBUG] Updating ElastiCache Subnet Group")
   121  
   122  		_, err := conn.ModifyCacheSubnetGroup(&elasticache.ModifyCacheSubnetGroupInput{
   123  			CacheSubnetGroupName:        aws.String(d.Get("name").(string)),
   124  			CacheSubnetGroupDescription: aws.String(d.Get("description").(string)),
   125  			SubnetIDs:                   subnets,
   126  		})
   127  		if err != nil {
   128  			return err
   129  		}
   130  	}
   131  
   132  	return resourceAwsElasticacheSubnetGroupRead(d, meta)
   133  }
   134  func resourceAwsElasticacheSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   135  	conn := meta.(*AWSClient).elasticacheconn
   136  
   137  	log.Printf("[DEBUG] Cache subnet group delete: %s", d.Id())
   138  
   139  	return resource.Retry(5*time.Minute, func() error {
   140  		_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
   141  			CacheSubnetGroupName: aws.String(d.Id()),
   142  		})
   143  		if err != nil {
   144  			apierr, ok := err.(awserr.Error)
   145  			if !ok {
   146  				return err
   147  			}
   148  			log.Printf("[DEBUG] APIError.Code: %v", apierr.Code)
   149  			switch apierr.Code() {
   150  			case "DependencyViolation":
   151  				// If it is a dependency violation, we want to retry
   152  				return err
   153  			default:
   154  				return resource.RetryError{Err: err}
   155  			}
   156  		}
   157  		return nil
   158  	})
   159  }