forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_db_instance.go
464 lines (396 loc) · 14.6 KB
/
resource_alicloud_db_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package alicloud
import (
"fmt"
"strings"
"time"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAlicloudDBInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudDBInstanceCreate,
Read: resourceAlicloudDBInstanceRead,
Update: resourceAlicloudDBInstanceUpdate,
Delete: resourceAlicloudDBInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"engine": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAllowedStringValue([]string{string(MySQL), string(SQLServer), string(PostgreSQL), string(PPAS)}),
ForceNew: true,
Required: true,
},
"engine_version": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAllowedStringValue([]string{"5.5", "5.6", "5.7", "2008r2", "2012", "9.4", "9.3", "10.0"}),
ForceNew: true,
Required: true,
},
"db_instance_class": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Deprecated: "Field 'db_instance_class' has been deprecated from provider version 1.5.0. New field 'instance_type' replaces it.",
},
"instance_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"db_instance_storage": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Deprecated: "Field 'db_instance_storage' has been deprecated from provider version 1.5.0. New field 'instance_storage' replaces it.",
},
"instance_storage": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"instance_charge_type": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAllowedStringValue([]string{string(Postpaid), string(Prepaid)}),
Optional: true,
ForceNew: true,
Default: Postpaid,
},
"period": &schema.Schema{
Type: schema.TypeInt,
ValidateFunc: validateAllowedIntValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36}),
Optional: true,
Default: 1,
DiffSuppressFunc: rdsPostPaidDiffSuppressFunc,
},
"zone_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"multi_az": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Deprecated: "Field 'multi_az' has been deprecated from provider version 1.8.1. Please use field 'zone_id' to specify multiple availability zone.",
},
"vswitch_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"instance_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateDBInstanceName,
},
"connection_string": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"port": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"db_instance_net_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
Deprecated: "Field 'db_instance_net_type' has been deprecated from provider version 1.5.0.",
},
"allocate_public_connection": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Deprecated: "Field 'allocate_public_connection' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_connection' replaces it.",
},
"instance_network_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
Deprecated: "Field 'instance_network_type' has been deprecated from provider version 1.5.0.",
},
"master_user_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Deprecated: "Field 'master_user_name' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_account' field 'name' replaces it.",
},
"master_user_password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Deprecated: "Field 'master_user_password' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_account' field 'password' replaces it.",
},
"preferred_backup_period": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Deprecated: "Field 'preferred_backup_period' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_backup_policy' field 'backup_period' replaces it.",
},
"preferred_backup_time": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Deprecated: "Field 'preferred_backup_time' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_backup_policy' field 'backup_time' replaces it.",
},
"backup_retention_period": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Deprecated: "Field 'backup_retention_period' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_backup_policy' field 'retention_period' replaces it.",
},
"security_ips": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Optional: true,
},
"connections": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"connection_string": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
Deprecated: "Field 'connections' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_connection' replaces it.",
},
"db_mappings": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"db_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"character_set_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"db_description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
Deprecated: "Field 'db_mappings' has been deprecated from provider version 1.5.0. New resource 'alicloud_db_database' replaces it.",
},
},
}
}
func resourceAlicloudDBInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
conn := client.rdsconn
request, err := buildDBCreateRequest(d, meta)
if err != nil {
return err
}
resp, err := conn.CreateDBInstance(request)
if err != nil {
return fmt.Errorf("Error creating Alicloud db instance: %#v", err)
}
d.SetId(resp.DBInstanceId)
// wait instance status change from Creating to running
if err := client.WaitForDBInstance(d.Id(), Running, DefaultLongTimeout); err != nil {
return fmt.Errorf("WaitForInstance %s got error: %#v", Running, err)
}
return resourceAlicloudDBInstanceUpdate(d, meta)
}
func resourceAlicloudDBInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
conn := client.rdsconn
d.Partial(true)
if d.HasChange("security_ips") && !d.IsNewResource() {
ipList := expandStringList(d.Get("security_ips").(*schema.Set).List())
ipstr := strings.Join(ipList[:], COMMA_SEPARATED)
// default disable connect from outside
if ipstr == "" {
ipstr = LOCAL_HOST_IP
}
if err := client.ModifyDBSecurityIps(d.Id(), ipstr); err != nil {
return fmt.Errorf("Moodify DB security ips %s got an error: %#v", ipstr, err)
}
d.SetPartial("security_ips")
}
update := false
request := rds.CreateModifyDBInstanceSpecRequest()
request.DBInstanceId = d.Id()
request.PayType = string(Postpaid)
if d.HasChange("instance_type") && !d.IsNewResource() {
request.DBInstanceClass = d.Get("instance_type").(string)
update = true
d.SetPartial("instance_type")
}
if d.HasChange("instance_storage") && !d.IsNewResource() {
request.DBInstanceStorage = requests.NewInteger(d.Get("instance_storage").(int))
update = true
d.SetPartial("instance_storage")
}
if update {
// wait instance status is running before modifying
if err := client.WaitForDBInstance(d.Id(), Running, 500); err != nil {
return fmt.Errorf("WaitForInstance %s got error: %#v", Running, err)
}
if _, err := conn.ModifyDBInstanceSpec(request); err != nil {
return err
}
// wait instance status is running after modifying
if err := client.WaitForDBInstance(d.Id(), Running, 1800); err != nil {
return fmt.Errorf("WaitForInstance %s got error: %#v", Running, err)
}
}
if d.HasChange("instance_name") {
request := rds.CreateModifyDBInstanceDescriptionRequest()
request.DBInstanceId = d.Id()
request.DBInstanceDescription = d.Get("instance_name").(string)
if _, err := conn.ModifyDBInstanceDescription(request); err != nil {
return fmt.Errorf("ModifyDBInstanceDescription got an error: %#v", err)
}
}
d.Partial(false)
return resourceAlicloudDBInstanceRead(d, meta)
}
func resourceAlicloudDBInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
instance, err := client.DescribeDBInstanceById(d.Id())
if err != nil {
if NotFoundDBInstance(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error Describe DB InstanceAttribute: %#v", err)
}
ips, err := client.GetSecurityIps(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] Describe DB security ips error: %#v", err)
}
d.Set("security_ips", ips)
d.Set("engine", instance.Engine)
d.Set("engine_version", instance.EngineVersion)
d.Set("instance_type", instance.DBInstanceClass)
d.Set("port", instance.Port)
d.Set("instance_storage", instance.DBInstanceStorage)
d.Set("zone_id", instance.ZoneId)
d.Set("instance_charge_type", instance.PayType)
d.Set("period", d.Get("period"))
d.Set("vswitch_id", instance.VSwitchId)
d.Set("connection_string", instance.ConnectionString)
d.Set("instance_name", instance.DBInstanceDescription)
return nil
}
func resourceAlicloudDBInstanceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
instance, err := client.DescribeDBInstanceById(d.Id())
if err != nil {
if NotFoundDBInstance(err) {
return nil
}
return fmt.Errorf("Error Describe DB InstanceAttribute: %#v", err)
}
if PayType(instance.PayType) == Prepaid {
return fmt.Errorf("At present, 'Prepaid' instance cannot be deleted and must wait it to be expired and release it automatically.")
}
request := rds.CreateDeleteDBInstanceRequest()
request.DBInstanceId = d.Id()
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.rdsconn.DeleteDBInstance(request)
if err != nil {
if NotFoundDBInstance(err) {
return nil
}
return resource.RetryableError(fmt.Errorf("Delete DB instance timeout and got an error: %#v.", err))
}
instance, err := client.DescribeDBInstanceById(d.Id())
if err != nil {
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceNameNotFound) {
return nil
}
return resource.NonRetryableError(fmt.Errorf("Error Describe DB InstanceAttribute: %#v", err))
}
if instance == nil {
return nil
}
return resource.RetryableError(fmt.Errorf("Delete DB instance timeout and got an error: %#v.", err))
})
}
func buildDBCreateRequest(d *schema.ResourceData, meta interface{}) (*rds.CreateDBInstanceRequest, error) {
client := meta.(*AliyunClient)
request := rds.CreateCreateDBInstanceRequest()
request.RegionId = string(getRegion(d, meta))
request.EngineVersion = Trim(d.Get("engine_version").(string))
request.Engine = Trim(d.Get("engine").(string))
request.DBInstanceStorage = requests.NewInteger(d.Get("instance_storage").(int))
request.DBInstanceClass = Trim(d.Get("instance_type").(string))
request.DBInstanceNetType = string(Intranet)
if zone, ok := d.GetOk("zone_id"); ok && Trim(zone.(string)) != "" {
request.ZoneId = Trim(zone.(string))
}
vswitchId := Trim(d.Get("vswitch_id").(string))
request.InstanceNetworkType = string(Classic)
if vswitchId != "" {
request.VSwitchId = vswitchId
request.InstanceNetworkType = strings.ToUpper(string(Vpc))
// check vswitchId in zone
vsw, err := client.DescribeVswitch(vswitchId)
if err != nil {
return nil, fmt.Errorf("DescribeVSwitche got an error: %#v.", err)
}
if request.ZoneId == "" {
request.ZoneId = vsw.ZoneId
} else if strings.Contains(request.ZoneId, MULTI_IZ_SYMBOL) {
zonestr := strings.Split(strings.SplitAfter(request.ZoneId, "(")[1], ")")[0]
if !strings.Contains(zonestr, string([]byte(vsw.ZoneId)[len(vsw.ZoneId)-1])) {
return nil, fmt.Errorf("The specified vswitch %s isn't in the multi zone %s.", vsw.VSwitchId, request.ZoneId)
}
} else if request.ZoneId != vsw.ZoneId {
return nil, fmt.Errorf("The specified vswitch %s isn't in the zone %s.", vsw.VSwitchId, request.ZoneId)
}
request.VPCId = vsw.VpcId
}
request.PayType = Trim(d.Get("instance_charge_type").(string))
// if charge type is postpaid, the commodity code must set to bards
//args.CommodityCode = rds.Bards
// At present, API supports two charge options about 'Prepaid'.
// 'Month': valid period ranges [1-9]; 'Year': valid period range [1-3]
// This resource only supports to input Month period [1-9, 12, 24, 36] and the values need to be converted before using them.
if PayType(request.PayType) == Prepaid {
period := d.Get("period").(int)
request.UsedTime = strconv.Itoa(period)
request.Period = string(Month)
if period > 9 {
request.UsedTime = strconv.Itoa(period / 12)
request.Period = string(Year)
}
}
request.SecurityIPList = LOCAL_HOST_IP
if len(d.Get("security_ips").(*schema.Set).List()) > 0 {
request.SecurityIPList = strings.Join(expandStringList(d.Get("security_ips").(*schema.Set).List())[:], COMMA_SEPARATED)
}
uuid, err := uuid.GenerateUUID()
if err != nil {
uuid = resource.UniqueId()
}
request.ClientToken = fmt.Sprintf("Terraform-Alicloud-%d-%s", time.Now().Unix(), uuid)
return request, nil
}