github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/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/awslabs/aws-sdk-go/aws"
     9  	"github.com/awslabs/aws-sdk-go/aws/awserr"
    10  	"github.com/awslabs/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  		Delete: resourceAwsElasticacheSubnetGroupDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"description": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: 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  				Optional: true,
    36  				Computed: true,
    37  				ForceNew: true,
    38  				Elem:     &schema.Schema{Type: schema.TypeString},
    39  				Set: func(v interface{}) int {
    40  					return hashcode.String(v.(string))
    41  				},
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceAwsElasticacheSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    48  	conn := meta.(*AWSClient).elasticacheconn
    49  
    50  	// Get the group properties
    51  	name := d.Get("name").(string)
    52  	desc := d.Get("description").(string)
    53  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    54  
    55  	log.Printf("[DEBUG] Cache subnet group create: name: %s, description: %s", name, desc)
    56  
    57  	subnetIds := expandStringList(subnetIdsSet.List())
    58  
    59  	req := &elasticache.CreateCacheSubnetGroupInput{
    60  		CacheSubnetGroupDescription: aws.String(desc),
    61  		CacheSubnetGroupName:        aws.String(name),
    62  		SubnetIDs:                   subnetIds,
    63  	}
    64  
    65  	_, err := conn.CreateCacheSubnetGroup(req)
    66  	if err != nil {
    67  		return fmt.Errorf("Error creating CacheSubnetGroup: %s", err)
    68  	}
    69  
    70  	// Assign the group name as the resource ID
    71  	d.SetId(name)
    72  
    73  	return nil
    74  }
    75  
    76  func resourceAwsElasticacheSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    77  	conn := meta.(*AWSClient).elasticacheconn
    78  	req := &elasticache.DescribeCacheSubnetGroupsInput{
    79  		CacheSubnetGroupName: aws.String(d.Get("name").(string)),
    80  	}
    81  
    82  	res, err := conn.DescribeCacheSubnetGroups(req)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	if len(res.CacheSubnetGroups) == 0 {
    87  		return fmt.Errorf("Error missing %v", d.Get("name"))
    88  	}
    89  
    90  	var group *elasticache.CacheSubnetGroup
    91  	for _, g := range res.CacheSubnetGroups {
    92  		log.Printf("[DEBUG] %v %v", g.CacheSubnetGroupName, d.Id())
    93  		if *g.CacheSubnetGroupName == d.Id() {
    94  			group = g
    95  		}
    96  	}
    97  	if group == nil {
    98  		return fmt.Errorf("Error retrieving cache subnet group: %v", res)
    99  	}
   100  
   101  	ids := make([]string, len(group.Subnets))
   102  	for i, s := range group.Subnets {
   103  		ids[i] = *s.SubnetIdentifier
   104  	}
   105  
   106  	d.Set("name", group.CacheSubnetGroupName)
   107  	d.Set("description", group.CacheSubnetGroupDescription)
   108  	d.Set("subnet_ids", ids)
   109  
   110  	return nil
   111  }
   112  
   113  func resourceAwsElasticacheSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   114  	conn := meta.(*AWSClient).elasticacheconn
   115  
   116  	log.Printf("[DEBUG] Cache subnet group delete: %s", d.Id())
   117  
   118  	return resource.Retry(5*time.Minute, func() error {
   119  		_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
   120  			CacheSubnetGroupName: aws.String(d.Id()),
   121  		})
   122  		if err != nil {
   123  			apierr, ok := err.(awserr.Error)
   124  			if !ok {
   125  				return err
   126  			}
   127  			log.Printf("[DEBUG] APIError.Code: %v", apierr.Code)
   128  			switch apierr.Code() {
   129  			case "DependencyViolation":
   130  				// If it is a dependency violation, we want to retry
   131  				return err
   132  			default:
   133  				return resource.RetryError{Err: err}
   134  			}
   135  		}
   136  		return nil
   137  	})
   138  }