Skip to content

Commit 681ac6e

Browse files
committed
add
1 parent d4bad48 commit 681ac6e

File tree

4 files changed

+192
-69
lines changed

4 files changed

+192
-69
lines changed

tencentcloud/services/cdb/resource_tc_mysql_ssl.go

Lines changed: 121 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78

89
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
910

@@ -25,15 +26,26 @@ func ResourceTencentCloudMysqlSsl() *schema.Resource {
2526
},
2627
Schema: map[string]*schema.Schema{
2728
"instance_id": {
28-
Required: true,
29-
Type: schema.TypeString,
30-
Description: "Instance ID. Example value: cdb-c1nl9rpv.",
29+
Type: schema.TypeString,
30+
Optional: true,
31+
ForceNew: true,
32+
ExactlyOneOf: []string{"ro_group_id"},
33+
Description: "Instance ID. Example value: cdb-c1nl9rpv.",
34+
},
35+
36+
"ro_group_id": {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
ForceNew: true,
40+
ExactlyOneOf: []string{"instance_id"},
41+
Description: "RO group ID. Example value: cdbrg-k9a6gup3.",
3142
},
3243

3344
"status": {
34-
Required: true,
35-
Type: schema.TypeString,
36-
Description: "Whether to enable SSL. `ON` means enabled, `OFF` means not enabled.",
45+
Type: schema.TypeString,
46+
Required: true,
47+
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"ON", "OFF"}),
48+
Description: "Whether to enable SSL. `ON` means enabled, `OFF` means not enabled.",
3749
},
3850

3951
"url": {
@@ -49,7 +61,32 @@ func resourceTencentCloudMysqlSslCreate(d *schema.ResourceData, meta interface{}
4961
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.create")()
5062
defer tccommon.InconsistentCheck(d, meta)()
5163

52-
d.SetId(d.Get("instance_id").(string))
64+
var (
65+
instanceId string
66+
roGroupId string
67+
)
68+
69+
if v, ok := d.GetOk("instance_id"); ok {
70+
instanceId = v.(string)
71+
if !strings.HasPrefix(instanceId, "cdb-") {
72+
return fmt.Errorf("`instance_id` parameter value is invalid. Example value: cdb-c1nl9rpv.")
73+
}
74+
}
75+
76+
if v, ok := d.GetOk("ro_group_id"); ok {
77+
roGroupId = v.(string)
78+
if !strings.HasPrefix(roGroupId, "cdbrg-") {
79+
return fmt.Errorf("`ro_group_id` parameter value is invalid. Example value: cdbrg-k9a6gup3.")
80+
}
81+
}
82+
83+
if instanceId != "" {
84+
d.SetId(instanceId)
85+
} else if roGroupId != "" {
86+
d.SetId(roGroupId)
87+
} else {
88+
return fmt.Errorf("`instance_id` or `ro_group_id` must set one of.")
89+
}
5390

5491
return resourceTencentCloudMysqlSslUpdate(d, meta)
5592
}
@@ -58,29 +95,34 @@ func resourceTencentCloudMysqlSslRead(d *schema.ResourceData, meta interface{})
5895
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.read")()
5996
defer tccommon.InconsistentCheck(d, meta)()
6097

61-
logId := tccommon.GetLogId(tccommon.ContextNil)
62-
63-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
64-
65-
service := MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
66-
67-
instanceId := d.Id()
98+
var (
99+
logId = tccommon.GetLogId(tccommon.ContextNil)
100+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
101+
service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
102+
resID = d.Id()
103+
instanceId string
104+
roGroupId string
105+
)
106+
107+
if strings.HasPrefix(resID, "cdb-") {
108+
instanceId = resID
109+
_ = d.Set("instance_id", instanceId)
110+
} else {
111+
roGroupId = resID
112+
_ = d.Set("ro_group_id", instanceId)
113+
}
68114

69-
ssl, err := service.DescribeMysqlSslById(ctx, instanceId)
115+
ssl, err := service.DescribeMysqlSslById(ctx, instanceId, roGroupId)
70116
if err != nil {
71117
return err
72118
}
73119

74120
if ssl == nil {
121+
log.Printf("[WARN]%s resource `tencentcloud_mysql_ssl` [%s] not found, please check if it has been deleted.", logId, instanceId)
75122
d.SetId("")
76-
log.Printf("[WARN]%s resource `tencentcloud_mysql_ssl` [%s] not found, please check if it has been deleted.",
77-
logId, instanceId,
78-
)
79123
return nil
80124
}
81125

82-
_ = d.Set("instance_id", instanceId)
83-
84126
if ssl.Status != nil {
85127
_ = d.Set("status", ssl.Status)
86128
}
@@ -96,18 +138,32 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
96138
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.update")()
97139
defer tccommon.InconsistentCheck(d, meta)()
98140

99-
logId := tccommon.GetLogId(tccommon.ContextNil)
100-
101-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
102-
103-
instanceId := d.Id()
141+
var (
142+
logId = tccommon.GetLogId(tccommon.ContextNil)
143+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
144+
service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
145+
resID = d.Id()
146+
instanceId string
147+
roGroupId string
148+
)
149+
150+
if strings.HasPrefix(resID, "cdb-") {
151+
instanceId = resID
152+
} else {
153+
roGroupId = resID
154+
}
104155

105-
status := ""
106156
if v, ok := d.GetOk("status"); ok {
107-
status = v.(string)
157+
status := v.(string)
108158
if status == "ON" {
109159
request := mysql.NewOpenSSLRequest()
110-
request.InstanceId = helper.String(instanceId)
160+
if instanceId != "" {
161+
request.InstanceId = helper.String(instanceId)
162+
}
163+
164+
if roGroupId != "" {
165+
request.RoGroupId = helper.String(roGroupId)
166+
}
111167

112168
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
113169
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().OpenSSL(request)
@@ -116,15 +172,23 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
116172
} else {
117173
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
118174
}
175+
119176
return nil
120177
})
178+
121179
if err != nil {
122-
log.Printf("[CRITAL]%s update mysql ssl failed, reason:%+v", logId, err)
180+
log.Printf("[CRITAL]%s Open mysql ssl failed, reason:%+v", logId, err)
123181
return err
124182
}
125183
} else if status == "OFF" {
126184
request := mysql.NewCloseSSLRequest()
127-
request.InstanceId = helper.String(instanceId)
185+
if instanceId != "" {
186+
request.InstanceId = helper.String(instanceId)
187+
}
188+
189+
if roGroupId != "" {
190+
request.RoGroupId = helper.String(roGroupId)
191+
}
128192

129193
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
130194
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().CloseSSL(request)
@@ -133,41 +197,45 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
133197
} else {
134198
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
135199
}
200+
136201
return nil
137202
})
203+
138204
if err != nil {
139-
log.Printf("[CRITAL]%s update mysql ssl failed, reason:%+v", logId, err)
205+
log.Printf("[CRITAL]%s Close mysql ssl failed, reason:%+v", logId, err)
140206
return err
141207
}
142208
} else {
143209
return fmt.Errorf("[CRITAL]%s update mysql ssl failed, reason:your status must be ON or OFF!", logId)
144210
}
145211

146-
if status != "" {
147-
service := MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
148-
err := resource.Retry(7*tccommon.ReadRetryTimeout, func() *resource.RetryError {
149-
ssl, err := service.DescribeMysqlSslById(ctx, instanceId)
150-
if err != nil {
151-
return resource.NonRetryableError(err)
152-
}
153-
if ssl == nil {
154-
err = fmt.Errorf("mysqlid %s instance ssl not exists", instanceId)
155-
return resource.NonRetryableError(err)
156-
}
157-
if *ssl.Status != status {
158-
return resource.RetryableError(fmt.Errorf("mysql ssl status is (%v)", *ssl.Status))
159-
}
160-
if *ssl.Status == status {
161-
return nil
162-
}
163-
err = fmt.Errorf("mysql ssl status is %v,we won't wait for it finish", *ssl.Status)
212+
// wait
213+
err := resource.Retry(10*tccommon.ReadRetryTimeout, func() *resource.RetryError {
214+
ssl, err := service.DescribeMysqlSslById(ctx, instanceId, roGroupId)
215+
if err != nil {
164216
return resource.NonRetryableError(err)
165-
})
217+
}
166218

167-
if err != nil {
168-
log.Printf("[CRITAL]%s mysql switchForUpgrade fail, reason:%s\n ", logId, err.Error())
169-
return err
219+
if ssl == nil {
220+
err = fmt.Errorf("mysqlid %s instance ssl not exists", instanceId)
221+
return resource.NonRetryableError(err)
222+
}
223+
224+
if *ssl.Status != status {
225+
return resource.RetryableError(fmt.Errorf("mysql ssl status is (%v)", *ssl.Status))
170226
}
227+
228+
if *ssl.Status == status {
229+
return nil
230+
}
231+
232+
err = fmt.Errorf("mysql ssl status is %v,we won't wait for it finish", *ssl.Status)
233+
return resource.NonRetryableError(err)
234+
})
235+
236+
if err != nil {
237+
log.Printf("[CRITAL]%s mysql switchForUpgrade fail, reason:%s\n ", logId, err.Error())
238+
return err
171239
}
172240
}
173241

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
Provides a resource to create a mysql ssl
1+
Provides a resource to create a MySQL SSL
22

33
Example Usage
44

5+
For mysql instance SSL
6+
57
```hcl
6-
resource "tencentcloud_mysql_ssl" "ssl" {
8+
resource "tencentcloud_mysql_ssl" "example" {
79
instance_id = "cdb-j5rprr8n"
810
status = "OFF"
911
}
1012
```
1113

14+
For mysql RO group SSL
15+
16+
```hcl
17+
resource "tencentcloud_mysql_ssl" "example" {
18+
ro_group_id = "cdbrg-k9a6gup3"
19+
status = "ON"
20+
}
21+
```
22+
1223
Import
1324

14-
mysql ssl can be imported using the id, e.g.
25+
MySQL SSL can be imported using the id, e.g.
1526

1627
```
17-
terraform import tencentcloud_mysql_ssl.ssl instanceId
18-
```
28+
terraform import tencentcloud_mysql_ssl.example cdb-j5rprr8n
29+
```
30+
31+
Or
32+
33+
```
34+
terraform import tencentcloud_mysql_ssl.example cdbrg-k9a6gup3
35+
```

tencentcloud/services/cdb/service_tencentcloud_mysql.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,26 +1506,46 @@ func (me *MysqlService) DescribeMysqlTimeWindowById(ctx context.Context, instanc
15061506
return
15071507
}
15081508

1509-
func (me *MysqlService) DescribeMysqlSslById(ctx context.Context, instanceId string) (ssl *cdb.DescribeSSLStatusResponseParams, errRet error) {
1509+
func (me *MysqlService) DescribeMysqlSslById(ctx context.Context, instanceId, roGroupId string) (ssl *cdb.DescribeSSLStatusResponseParams, errRet error) {
15101510
logId := tccommon.GetLogId(ctx)
15111511

15121512
request := cdb.NewDescribeSSLStatusRequest()
1513-
request.InstanceId = &instanceId
1513+
response := cdb.NewDescribeSSLStatusResponse()
1514+
if instanceId != "" {
1515+
request.InstanceId = &instanceId
1516+
}
1517+
1518+
if roGroupId != "" {
1519+
request.RoGroupId = &roGroupId
1520+
}
15141521

15151522
defer func() {
15161523
if errRet != nil {
15171524
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
15181525
}
15191526
}()
15201527

1521-
ratelimit.Check(request.GetAction())
1528+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1529+
ratelimit.Check(request.GetAction())
1530+
result, e := me.client.UseMysqlClient().DescribeSSLStatus(request)
1531+
if e != nil {
1532+
return tccommon.RetryError(e)
1533+
} else {
1534+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
1535+
}
1536+
1537+
if result == nil || result.Response == nil {
1538+
return resource.NonRetryableError(fmt.Errorf("Describe ssl status failed, Response is nil."))
1539+
}
1540+
1541+
response = result
1542+
return nil
1543+
})
15221544

1523-
response, err := me.client.UseMysqlClient().DescribeSSLStatus(request)
15241545
if err != nil {
15251546
errRet = err
15261547
return
15271548
}
1528-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
15291549

15301550
ssl = response.Response
15311551
return

0 commit comments

Comments
 (0)