forked from hashicorp/terraform
/
resource_aws_elasticache_subnet_group.go
137 lines (116 loc) · 3.53 KB
/
resource_aws_elasticache_subnet_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package aws
import (
"fmt"
"log"
"time"
"github.com/awslabs/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsElasticacheSubnetGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElasticacheSubnetGroupCreate,
Read: resourceAwsElasticacheSubnetGroupRead,
Delete: resourceAwsElasticacheSubnetGroupDelete,
Schema: map[string]*schema.Schema{
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_ids": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
},
}
}
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),
SubnetIDs: subnetIds,
}
_, err := conn.CreateCacheSubnetGroup(req)
if err != nil {
return fmt.Errorf("Error creating CacheSubnetGroup: %s", err)
}
// Assign the group name as the resource ID
d.SetId(name)
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
}
func resourceAwsElasticacheSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
log.Printf("[DEBUG] Cache subnet group delete: %s", d.Id())
return resource.Retry(5*time.Minute, func() error {
_, err := conn.DeleteCacheSubnetGroup(&elasticache.DeleteCacheSubnetGroupInput{
CacheSubnetGroupName: aws.String(d.Id()),
})
if err != nil {
apierr, ok := err.(aws.APIError)
if !ok {
return err
}
log.Printf("[DEBUG] APIError.Code: %v", apierr.Code)
switch apierr.Code {
case "DependencyViolation":
// If it is a dependency violation, we want to retry
return err
default:
return resource.RetryError{Err: err}
}
}
return nil
})
}