github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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/service/elasticache" 9 "github.com/hashicorp/aws-sdk-go/aws" 10 "github.com/hashicorp/terraform/helper/hashcode" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 ) 14 15 func resourceAwsElasticacheSubnetGroup() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsElasticacheSubnetGroupCreate, 18 Read: resourceAwsElasticacheSubnetGroupRead, 19 Delete: resourceAwsElasticacheSubnetGroupDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "description": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "subnet_ids": &schema.Schema{ 33 Type: schema.TypeSet, 34 Optional: true, 35 Computed: true, 36 ForceNew: 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 resourceAwsElasticacheSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 113 conn := meta.(*AWSClient).elasticacheconn 114 115 log.Printf("[DEBUG] Cache subnet group delete: %s", d.Id()) 116 117 return resource.Retry(5*time.Minute, func() error { 118 _, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{ 119 CacheSubnetGroupName: aws.String(d.Id()), 120 }) 121 if err != nil { 122 apierr, ok := err.(aws.APIError) 123 if !ok { 124 return err 125 } 126 log.Printf("[DEBUG] APIError.Code: %v", apierr.Code) 127 switch apierr.Code { 128 case "DependencyViolation": 129 // If it is a dependency violation, we want to retry 130 return err 131 default: 132 return resource.RetryError{Err: err} 133 } 134 } 135 return nil 136 }) 137 }