forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_ots_instance.go
220 lines (199 loc) · 6.44 KB
/
resource_alicloud_ots_instance.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package alicloud
import (
"fmt"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ots"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudOtsInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunOtsInstanceCreate,
Read: resourceAliyunOtsInstanceRead,
Update: resourceAliyunOtsInstanceUpdate,
Delete: resourceAliyunOtsInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateStringLengthInRange(3, 16),
},
"accessed_by": {
Type: schema.TypeString,
Optional: true,
Default: AnyNetwork,
ValidateFunc: validateAllowedStringValue([]string{
string(AnyNetwork), string(VpcOnly), string(VpcOrConsole),
}),
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: OtsHighPerformance,
ValidateFunc: validateAllowedStringValue([]string{
string(OtsCapacity), string(OtsHighPerformance),
}),
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Id() != ""
},
},
"tags": tagsSchema(),
},
}
}
func resourceAliyunOtsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
instanceType := d.Get("instance_type").(string)
req := ots.CreateInsertInstanceRequest()
req.ClusterType = convertInstanceType(OtsInstanceType(instanceType))
types, err := otsService.DescribeOtsInstanceTypes()
if err != nil {
return err
}
valid := false
for _, t := range types {
if req.ClusterType == t {
valid = true
break
}
}
if !valid {
return fmt.Errorf("The instance type %s is not available in the region %s.", instanceType, client.RegionId)
}
req.InstanceName = d.Get("name").(string)
req.Description = d.Get("description").(string)
req.Network = convertInstanceAccessedBy(InstanceAccessedByType(d.Get("accessed_by").(string)))
_, err = client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.InsertInstance(req)
})
if err != nil {
return fmt.Errorf("failed to create instance with error: %s", err)
}
d.SetId(req.InstanceName)
if err := otsService.WaitForOtsInstance(req.InstanceName, Running, DefaultTimeout); err != nil {
return err
}
return resourceAliyunOtsInstanceUpdate(d, meta)
}
func resourceAliyunOtsInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
inst, err := otsService.DescribeOtsInstance(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("failed to describe instance with error: %s", err)
}
d.Set("name", inst.InstanceName)
d.Set("accessed_by", convertInstanceAccessedByRevert(inst.Network))
d.Set("instance_type", convertInstanceTypeRevert(inst.ClusterType))
d.Set("description", inst.Description)
d.Set("tags", otsTagsToMap(inst.TagInfos.TagInfo))
return nil
}
func resourceAliyunOtsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
d.Partial(true)
if !d.IsNewResource() && d.HasChange("accessed_by") {
req := ots.CreateUpdateInstanceRequest()
req.InstanceName = d.Id()
req.Network = convertInstanceAccessedBy(InstanceAccessedByType(d.Get("accessed_by").(string)))
_, err := client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.UpdateInstance(req)
})
if err != nil {
return fmt.Errorf("UpdateInstance %s got an error: %#v.", d.Id(), err)
}
d.SetPartial("accessed_by")
}
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTags(tagsFromMap(o), tagsFromMap(n))
if len(remove) > 0 {
args := ots.CreateDeleteTagsRequest()
args.InstanceName = d.Id()
var tags []ots.DeleteTagsTagInfo
for _, t := range remove {
tags = append(tags, ots.DeleteTagsTagInfo{
TagKey: t.Key,
TagValue: t.Value,
})
}
args.TagInfo = &tags
_, err := client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.DeleteTags(args)
})
if err != nil {
return fmt.Errorf("Remove tags got error: %s", err)
}
}
if len(create) > 0 {
args := ots.CreateInsertTagsRequest()
args.InstanceName = d.Id()
var tags []ots.InsertTagsTagInfo
for _, t := range create {
tags = append(tags, ots.InsertTagsTagInfo{
TagKey: t.Key,
TagValue: t.Value,
})
}
args.TagInfo = &tags
_, err := client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.InsertTags(args)
})
if err != nil {
return fmt.Errorf("Insertting tags got error: %s", err)
}
}
d.SetPartial("tags")
}
if err := otsService.WaitForOtsInstance(d.Id(), Running, DefaultTimeout); err != nil {
return err
}
d.Partial(false)
return resourceAliyunOtsInstanceRead(d, meta)
}
func resourceAliyunOtsInstanceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
req := ots.CreateDeleteInstanceRequest()
req.InstanceName = d.Id()
return resource.Retry(10*time.Minute, func() *resource.RetryError {
if _, err := otsService.DescribeOtsInstance(d.Id()); err != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(fmt.Errorf("When deleting instance, failed to describe instance with error: %s", err))
}
_, err := client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.DeleteInstance(req)
})
if err != nil {
if NotFoundError(err) {
return nil
}
if IsExceptedErrors(err, []string{"AuthFailed", "InvalidStatus", "ValidationFailed"}) {
return resource.RetryableError(fmt.Errorf("Deleting instance %s timeout and got an error: %#v.", d.Id(), err))
}
return resource.NonRetryableError(err)
}
return resource.RetryableError(fmt.Errorf("Deleting instance %s timeout.", d.Id()))
})
}