-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathresource_tc_dcdb_db_instance.go
832 lines (703 loc) · 24.1 KB
/
resource_tc_dcdb_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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package dcdb
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudDcdbDbInstance() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudDcdbDbInstanceCreate,
Read: resourceTencentCloudDcdbDbInstanceRead,
Update: resourceTencentCloudDcdbDbInstanceUpdate,
Delete: resourceTencentCloudDcdbDbInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"zones": {
Required: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The availability zone distribution of shard nodes can be filled with up to two availability zones. When the shard specification is one master and two slaves, two of the nodes are in the first availability zone.Note that the current availability zone that can be sold needs to be pulled through the DescribeDCDBSaleInfo interface.",
},
"period": {
Required: true,
Type: schema.TypeInt,
Description: "The length of time you want to buy, unit: month.",
},
"shard_memory": {
Required: true,
Type: schema.TypeInt,
Description: "Shard memory size, unit: GB, can pass DescribeShardSpec Query the instance specification to obtain.",
},
"shard_storage": {
Required: true,
Type: schema.TypeInt,
Description: " Shard storage size, unit: GB, can pass DescribeShardSpec Query the instance specification to obtain.",
},
"shard_node_count": {
Required: true,
Type: schema.TypeInt,
Description: " Number of single shard nodes, can pass DescribeShardSpec Query the instance specification to obtain.",
},
"shard_count": {
Required: true,
Type: schema.TypeInt,
Description: "The number of instance fragments, the optional range is 2-8, and new fragments can be added to a maximum of 64 fragments by upgrading the instance.",
},
// "count": {
// Optional: true,
// Type: schema.TypeInt,
// Description: "The number of instances to be purchased.",
// },
"project_id": {
Optional: true,
Type: schema.TypeInt,
Description: "Project ID, which can be obtained by viewing the project list, if not passed, it will be associated with the default project.",
},
"vpc_id": {
Optional: true,
Type: schema.TypeString,
Description: "Virtual private network ID, if not passed or passed empty, it means that it is created as a basic network.",
},
"subnet_id": {
Optional: true,
Type: schema.TypeString,
Description: "Virtual private network subnet ID, required when VpcId is not empty.",
},
"db_version_id": {
Optional: true,
Type: schema.TypeString,
Description: " Database engine version, currently available: 8.0.18, 10.1.9, 5.7.17. 8.0.18 - MySQL 8.0.18; 10.1.9 - Mariadb 10.1.9; 5.7.17 - Percona 5.7.17 If not filled, the default is 5.7.17, which means Percona 5.7.17.",
},
"auto_voucher": {
Optional: true,
Type: schema.TypeBool,
Description: "Whether to automatically use vouchers for payment, not used by default.",
},
"voucher_ids": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Voucher ID list, currently only supports specifying one voucher.",
},
"instance_name": {
Optional: true,
Type: schema.TypeString,
Description: "Instance name, you can set the name of the instance independently through this field.",
},
"ipv6_flag": {
Optional: true,
Type: schema.TypeInt,
Description: "Whether to support IPv6.",
},
"extranet_access": {
Optional: true,
Type: schema.TypeBool,
Description: "Whether to open the extranet access.",
},
"vip": {
Optional: true,
Computed: true,
Type: schema.TypeString,
Description: "The field is required to specify VIP.",
},
"vipv6": {
Optional: true,
Computed: true,
Type: schema.TypeString,
Description: "The field is required to specify VIPv6.",
},
"vport": {
Type: schema.TypeInt,
Computed: true,
Description: "Intranet port.",
},
"resource_tags": {
Optional: true,
Type: schema.TypeList,
Description: "Array of tag key-value pairs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_key": {
Type: schema.TypeString,
Required: true,
Description: "The key of tag.",
},
"tag_value": {
Type: schema.TypeString,
Required: true,
Description: "The value of tag.",
},
},
},
},
"init_params": {
Optional: true,
Type: schema.TypeList,
Description: " parameter list. The optional values of this interface are: character_set_server (character set, must be passed), lower_case_table_names (table name is case sensitive, must be passed, 0 - sensitive; 1 - insensitive), innodb_page_size (innodb data page, default 16K), sync_mode ( Synchronous mode: 0 - asynchronous; 1 - strong synchronous; 2 - strong synchronous degenerate. The default is strong synchronous degenerate) .",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"param": {
Type: schema.TypeString,
Required: true,
Description: "The name of parameter.",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "The value of parameter.",
},
},
},
},
"dcn_region": {
Optional: true,
Type: schema.TypeString,
Description: "DCN source region.",
},
"dcn_instance_id": {
Optional: true,
Type: schema.TypeString,
Description: "DCN source instance ID.",
},
"auto_renew_flag": {
Optional: true,
Type: schema.TypeInt,
Description: " Automatic renewal flag, 0 means the default state (the user has not set it, that is, the initial state is manual renewal, and the user has activated the prepaid non-stop privilege and will also perform automatic renewal). 1 means automatic renewal, 2 means no automatic renewal (user setting). if the business has no concept of renewal or automatic renewal is not required, it needs to be set to 0.",
},
"security_group_ids": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Security group ids, the security group can be passed in the form of an array, compatible with the previous SecurityGroupId parameter.",
},
},
}
}
func resourceTencentCloudDcdbDbInstanceCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_dcdb_db_instance.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var (
request = dcdb.NewCreateDCDBInstanceRequest()
response = dcdb.NewCreateDCDBInstanceResponse()
instanceId string
dcnInstanceId string
vpcId string
subnetId string
ipv6Flag int
service = DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
)
if v, ok := d.GetOk("zones"); ok {
zonesSet := v.(*schema.Set).List()
request.Zones = helper.InterfacesStringsPoint(zonesSet)
}
if v, _ := d.GetOk("period"); v != nil {
request.Period = helper.IntInt64(v.(int))
}
if v, _ := d.GetOk("shard_memory"); v != nil {
request.ShardMemory = helper.IntInt64(v.(int))
}
if v, _ := d.GetOk("shard_storage"); v != nil {
request.ShardStorage = helper.IntInt64(v.(int))
}
if v, _ := d.GetOk("shard_node_count"); v != nil {
request.ShardNodeCount = helper.IntInt64(v.(int))
}
if v, _ := d.GetOk("shard_count"); v != nil {
request.ShardCount = helper.IntInt64(v.(int))
}
if v, _ := d.GetOk("project_id"); v != nil {
request.ProjectId = helper.IntInt64(v.(int))
}
if v, ok := d.GetOk("vpc_id"); ok {
request.VpcId = helper.String(v.(string))
}
if v, ok := d.GetOk("subnet_id"); ok {
request.SubnetId = helper.String(v.(string))
}
if v, ok := d.GetOk("db_version_id"); ok {
request.DbVersionId = helper.String(v.(string))
}
if v, _ := d.GetOk("auto_voucher"); v != nil {
request.AutoVoucher = helper.Bool(v.(bool))
}
if v, ok := d.GetOk("voucher_ids"); ok {
voucherIdsSet := v.(*schema.Set).List()
request.VoucherIds = helper.InterfacesStringsPoint(voucherIdsSet)
}
if v, ok := d.GetOk("instance_name"); ok {
request.InstanceName = helper.String(v.(string))
}
if v, _ := d.GetOk("ipv6_flag"); v != nil {
request.Ipv6Flag = helper.IntInt64(v.(int))
ipv6Flag = v.(int)
}
if v, ok := d.GetOk("resource_tags"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
resourceTag := dcdb.ResourceTag{}
if v, ok := dMap["tag_key"]; ok {
resourceTag.TagKey = helper.String(v.(string))
}
if v, ok := dMap["tag_value"]; ok {
resourceTag.TagValue = helper.String(v.(string))
}
request.ResourceTags = append(request.ResourceTags, &resourceTag)
}
}
if v, ok := d.GetOk("init_params"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
dBParamValue := dcdb.DBParamValue{}
if v, ok := dMap["param"]; ok {
dBParamValue.Param = helper.String(v.(string))
}
if v, ok := dMap["value"]; ok {
dBParamValue.Value = helper.String(v.(string))
}
request.InitParams = append(request.InitParams, &dBParamValue)
}
}
if v, ok := d.GetOk("dcn_region"); ok {
request.DcnRegion = helper.String(v.(string))
}
if v, ok := d.GetOk("dcn_instance_id"); ok {
request.DcnInstanceId = helper.String(v.(string))
dcnInstanceId = v.(string)
}
if v, _ := d.GetOk("auto_renew_flag"); v != nil {
request.AutoRenewFlag = helper.IntInt64(v.(int))
}
if v, ok := d.GetOk("security_group_ids"); ok {
securityGroupIdsSet := v.(*schema.Set).List()
request.SecurityGroupIds = helper.InterfacesStringsPoint(securityGroupIdsSet)
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDcdbClient().CreateDCDBInstance(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create dcdb dbInstance failed, reason:%+v", logId, err)
return err
}
if response == nil || len(response.Response.InstanceIds) < 1 {
d.SetId("")
return fmt.Errorf("[CRITAL]%s create dcdb dbInstance failed.", d.Id())
}
instanceId = *response.Response.InstanceIds[0]
d.SetId(instanceId)
if len(request.InitParams) < 1 {
defaultInitParams := []*dcdb.DBParamValue{
{
Param: helper.String("character_set_server"),
Value: helper.String("utf8mb4"),
},
{
Param: helper.String("lower_case_table_names"),
Value: helper.String("1"),
},
{
Param: helper.String("sync_mode"),
Value: helper.String("2"),
},
{
Param: helper.String("innodb_page_size"),
Value: helper.String("16384"),
},
}
request.InitParams = defaultInitParams
}
initRet, flowId, e := service.InitDcdbDbInstance(ctx, instanceId, request.InitParams)
if e != nil {
return e
}
if !initRet {
return fmt.Errorf("db instance init failed")
}
if flowId != nil {
// need to wait init operation success
// 0:success; 1:failed, 2:running
conf := tccommon.BuildStateChangeConf([]string{}, []string{"0"}, 3*tccommon.ReadRetryTimeout, time.Second, service.DcdbDbInstanceStateRefreshFunc(helper.UInt64Int64(*flowId), []string{}))
if _, e := conf.WaitForState(); e != nil {
return e
}
}
if dcnInstanceId != "" {
// need to wait dcn init processing complete
// 0:none; 1:creating, 2:running
conf := tccommon.BuildStateChangeConf([]string{}, []string{"2"}, 3*tccommon.ReadRetryTimeout, time.Second, service.DcdbDcnStateRefreshFunc(instanceId, []string{}))
if _, e := conf.WaitForState(); e != nil {
return e
}
}
if v, ok := d.GetOkExists("extranet_access"); ok && v != nil {
flag := v.(bool)
err := service.SetDcdbExtranetAccess(ctx, instanceId, ipv6Flag, flag)
if err != nil {
return err
}
}
var (
vip string
vipv6 string
)
if v, ok := d.GetOk("vip"); ok {
vip = v.(string)
}
if v, ok := d.GetOk("vipv6"); ok {
vipv6 = v.(string)
}
if vip != "" || vipv6 != "" {
if vpcId == "" || subnetId == "" {
return fmt.Errorf("`vpc_id` and `subnet_id` cannot be empty when setting `vip` or `vipv6` fields!")
}
err := service.SetNetworkVip(ctx, instanceId, vpcId, subnetId, vip, vipv6)
if err != nil {
return err
}
}
return resourceTencentCloudDcdbDbInstanceRead(d, meta)
}
func resourceTencentCloudDcdbDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_dcdb_db_instance.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
instanceId := d.Id()
ret, err := service.DescribeDcdbDbInstance(ctx, instanceId)
if err != nil {
return err
}
if ret == nil || len(ret.Instances) < 1 {
d.SetId("")
return fmt.Errorf("resource `DcdbDbInstance` %s does not exist", d.Id())
}
dbInstance := ret.Instances[0]
// if dbInstance.Period != nil {
// _ = d.Set("period", dbInstance.Period)
// }
if dbInstance.ShardDetail[0] != nil { // Memory and Storage is params for one shard
shard := dbInstance.ShardDetail[0]
if shard.Memory != nil {
_ = d.Set("shard_memory", shard.Memory)
}
if shard.Storage != nil {
_ = d.Set("shard_storage", shard.Storage)
}
}
if dbInstance.NodeCount != nil {
_ = d.Set("shard_node_count", dbInstance.NodeCount)
}
if dbInstance.ShardCount != nil {
_ = d.Set("shard_count", dbInstance.ShardCount)
}
if dbInstance.ProjectId != nil {
_ = d.Set("project_id", dbInstance.ProjectId)
}
if dbInstance.UniqueVpcId != nil {
_ = d.Set("vpc_id", dbInstance.UniqueVpcId)
}
if dbInstance.UniqueSubnetId != nil {
_ = d.Set("subnet_id", dbInstance.UniqueSubnetId)
}
if dbInstance.DbVersionId != nil {
_ = d.Set("db_version_id", dbInstance.DbVersionId)
}
// if dbInstance.AutoVoucher != nil {
// _ = d.Set("auto_voucher", dbInstance.AutoVoucher)
// }
// if dbInstance.VoucherIds != nil {
// _ = d.Set("voucher_ids", dbInstance.VoucherIds)
// }
if dbInstance.InstanceName != nil {
_ = d.Set("instance_name", dbInstance.InstanceName)
}
if dbInstance.Ipv6Flag != nil {
_ = d.Set("ipv6_flag", dbInstance.Ipv6Flag)
}
if dbInstance.WanStatus != nil {
//0-未开通;1-已开通;2-关闭;3-开通中
if *dbInstance.WanStatus == DCDB_WAN_STATUS_UNOPEN || *dbInstance.WanStatus == DCDB_WAN_STATUS_CLOSED {
_ = d.Set("extranet_access", false)
}
if *dbInstance.WanStatus == DCDB_WAN_STATUS_OPENED {
_ = d.Set("extranet_access", true)
}
}
if dbInstance.ResourceTags != nil {
resourceTagsList := []interface{}{}
for _, resourceTags := range dbInstance.ResourceTags {
resourceTagsMap := map[string]interface{}{}
if resourceTags.TagKey != nil {
resourceTagsMap["tag_key"] = resourceTags.TagKey
}
if resourceTags.TagValue != nil {
resourceTagsMap["tag_value"] = resourceTags.TagValue
}
resourceTagsList = append(resourceTagsList, resourceTagsMap)
}
_ = d.Set("resource_tags", resourceTagsList)
}
// if dbInstance.InitParams != nil {
// initParamsList := []interface{}{}
// for _, initParams := range dbInstance.InitParams {
// initParamsMap := map[string]interface{}{}
// if dbInstance.InitParams.Param != nil {
// initParamsMap["param"] = dbInstance.InitParams.Param
// }
// if dbInstance.InitParams.Value != nil {
// initParamsMap["value"] = dbInstance.InitParams.Value
// }
// initParamsList = append(initParamsList, initParamsMap)
// }
// _ = d.Set("init_params", initParamsList)
// }
if dbInstance.AutoRenewFlag != nil {
_ = d.Set("auto_renew_flag", dbInstance.AutoRenewFlag)
}
if sg, err := service.DescribeDcdbSecurityGroup(ctx, instanceId); err == nil {
sgIds := []*string{}
for _, sg := range sg.Groups {
sgIds = append(sgIds, sg.SecurityGroupId)
}
// fake sg
var tmpSet []interface{}
if v, ok := d.GetOk("security_group_ids"); ok {
tmpSet = v.(*schema.Set).List()
sgIds = helper.InterfacesStringsPoint(tmpSet)
}
// end
_ = d.Set("security_group_ids", sgIds)
} else {
return err
}
// set dcn id and region
if dcns, err := service.DescribeDcnDetailById(ctx, instanceId); err == nil {
for _, dcn := range dcns {
var master *dcdb.DcnDetailItem
if *dcn.DcnFlag == DCDB_DCN_FLAG_MASTER {
master = dcn
_ = d.Set("dcn_region", master.Region)
_ = d.Set("dcn_instance_id", master.InstanceId)
}
}
} else {
return err
}
// set vip, vipv6 and vport
if detail, err := service.DescribeDcdbDbInstanceDetailById(ctx, instanceId); err == nil {
if detail != nil {
_ = d.Set("vip", detail.Vip)
_ = d.Set("vipv6", detail.Vip6)
_ = d.Set("vport", detail.Vport)
if detail.MasterZone != nil {
zones := []*string{detail.MasterZone}
if detail.SlaveZones != nil {
zones = append(zones, detail.SlaveZones...)
}
_ = d.Set("zones", zones)
}
}
} else {
return err
}
return nil
}
func resourceTencentCloudDcdbDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_dcdb_db_instance.update")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var (
request = dcdb.NewModifyDBInstanceNameRequest()
service = DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
)
instanceId := d.Id()
request.InstanceId = helper.String(instanceId)
if d.HasChange("zones") {
return fmt.Errorf("`zones` do not support change now.")
}
if d.HasChange("period") || d.HasChange("auto_voucher") || d.HasChange("voucher_ids") {
if period, ok := d.GetOk("period"); ok {
request := dcdb.NewRenewDCDBInstanceRequest()
request.InstanceId = &instanceId
request.Period = helper.IntInt64(period.(int))
if v, _ := d.GetOk("auto_voucher"); v != nil {
request.AutoVoucher = helper.Bool(v.(bool))
}
if v, ok := d.GetOk("voucher_ids"); ok {
voucherIdsSet := v.(*schema.Set).List()
for i := range voucherIdsSet {
if voucherIdsSet[i] != nil {
voucherIds := voucherIdsSet[i].(string)
request.VoucherIds = append(request.VoucherIds, &voucherIds)
}
}
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDcdbClient().RenewDCDBInstance(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate dcdb renewDCDBInstanceOperation failed, reason:%+v", logId, err)
return err
}
_ = d.Set("period", period)
}
time.Sleep(2 * time.Second)
}
if d.HasChange("shard_memory") {
return fmt.Errorf("`shard_memory` do not support change now.")
}
if d.HasChange("shard_storage") {
return fmt.Errorf("`shard_storage` do not support change now.")
}
if d.HasChange("shard_node_count") {
return fmt.Errorf("`shard_node_count` do not support change now.")
}
if d.HasChange("shard_count") {
return fmt.Errorf("`shard_count` do not support change now.")
}
if v, ok := d.GetOkExists("extranet_access"); ok && v != nil {
flag := v.(bool)
var ipv6Flag int
if v, _ := d.GetOk("ipv6_flag"); v != nil {
ipv6Flag = v.(int)
}
err := service.SetDcdbExtranetAccess(ctx, instanceId, ipv6Flag, flag)
if err != nil {
return err
}
time.Sleep(2 * time.Second)
}
if d.HasChange("project_id") {
if projectId, ok := d.GetOk("project_id"); ok {
request := dcdb.NewModifyDBInstancesProjectRequest()
request.InstanceIds = []*string{&instanceId}
request.ProjectId = helper.IntInt64(projectId.(int))
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDcdbClient().ModifyDBInstancesProject(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate dcdb modifyInstanceProjectOperation failed, reason:%+v", logId, err)
return err
}
}
time.Sleep(2 * time.Second)
}
if d.HasChange("vpc_id") || d.HasChange("subnet_id") || d.HasChange("vip") || d.HasChange("vipv6") {
var (
vip string
vipv6 string
vpcId string
subnetId string
)
if v, ok := d.GetOk("vip"); ok {
vip = v.(string)
}
if v, ok := d.GetOk("vipv6"); ok {
vipv6 = v.(string)
}
if v, ok := d.GetOk("vpc_id"); ok {
vpcId = v.(string)
}
if v, ok := d.GetOk("subnet_id"); ok {
subnetId = v.(string)
}
if vpcId == "" || subnetId == "" {
return fmt.Errorf("`vpc_id` and `subnet_id` cannot be empty when updating network configs!")
}
err := service.SetNetworkVip(ctx, instanceId, vpcId, subnetId, vip, vipv6)
if err != nil {
return err
}
}
if d.HasChange("db_version_id") {
return fmt.Errorf("`db_version_id` do not support change now.")
}
if d.HasChange("ipv6_flag") {
return fmt.Errorf("`ipv6_flag` do not support change now.")
}
if d.HasChange("resource_tags") {
return fmt.Errorf("`resource_tags` do not support change now.")
}
if d.HasChange("init_params") {
return fmt.Errorf("`init_params` do not support change now.")
}
if d.HasChange("dcn_region") {
return fmt.Errorf("`dcn_region` do not support change now.")
}
if d.HasChange("dcn_instance_id") {
return fmt.Errorf("`dcn_instance_id` do not support change now.")
}
if d.HasChange("auto_renew_flag") {
return fmt.Errorf("`auto_renew_flag` do not support change now.")
}
if d.HasChange("security_group_ids") {
return fmt.Errorf("`security_group_ids` do not support change now.")
}
if d.HasChange("instance_name") {
if v, ok := d.GetOk("instance_name"); ok {
request.InstanceName = helper.String(v.(string))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDcdbClient().ModifyDBInstanceName(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s update dcdb dbInstance failed, reason:%+v", logId, err)
return err
}
}
return resourceTencentCloudDcdbDbInstanceRead(d, meta)
}
func resourceTencentCloudDcdbDbInstanceDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_dcdb_db_instance.delete")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
instanceId := d.Id()
if err := service.DeleteDcdbDbInstanceById(ctx, instanceId); err != nil {
return err
}
return nil
}