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