4
4
"context"
5
5
"fmt"
6
6
"log"
7
+ "strings"
7
8
8
9
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9
10
@@ -25,15 +26,26 @@ func ResourceTencentCloudMysqlSsl() *schema.Resource {
25
26
},
26
27
Schema : map [string ]* schema.Schema {
27
28
"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." ,
31
42
},
32
43
33
44
"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." ,
37
49
},
38
50
39
51
"url" : {
@@ -49,7 +61,32 @@ func resourceTencentCloudMysqlSslCreate(d *schema.ResourceData, meta interface{}
49
61
defer tccommon .LogElapsed ("resource.tencentcloud_mysql_ssl.create" )()
50
62
defer tccommon .InconsistentCheck (d , meta )()
51
63
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
+ }
53
90
54
91
return resourceTencentCloudMysqlSslUpdate (d , meta )
55
92
}
@@ -58,29 +95,34 @@ func resourceTencentCloudMysqlSslRead(d *schema.ResourceData, meta interface{})
58
95
defer tccommon .LogElapsed ("resource.tencentcloud_mysql_ssl.read" )()
59
96
defer tccommon .InconsistentCheck (d , meta )()
60
97
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
+ }
68
114
69
- ssl , err := service .DescribeMysqlSslById (ctx , instanceId )
115
+ ssl , err := service .DescribeMysqlSslById (ctx , instanceId , roGroupId )
70
116
if err != nil {
71
117
return err
72
118
}
73
119
74
120
if ssl == nil {
121
+ log .Printf ("[WARN]%s resource `tencentcloud_mysql_ssl` [%s] not found, please check if it has been deleted." , logId , instanceId )
75
122
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
- )
79
123
return nil
80
124
}
81
125
82
- _ = d .Set ("instance_id" , instanceId )
83
-
84
126
if ssl .Status != nil {
85
127
_ = d .Set ("status" , ssl .Status )
86
128
}
@@ -96,18 +138,32 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
96
138
defer tccommon .LogElapsed ("resource.tencentcloud_mysql_ssl.update" )()
97
139
defer tccommon .InconsistentCheck (d , meta )()
98
140
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
+ }
104
155
105
- status := ""
106
156
if v , ok := d .GetOk ("status" ); ok {
107
- status = v .(string )
157
+ status : = v .(string )
108
158
if status == "ON" {
109
159
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
+ }
111
167
112
168
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
113
169
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseMysqlClient ().OpenSSL (request )
@@ -116,15 +172,23 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
116
172
} else {
117
173
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
118
174
}
175
+
119
176
return nil
120
177
})
178
+
121
179
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 )
123
181
return err
124
182
}
125
183
} else if status == "OFF" {
126
184
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
+ }
128
192
129
193
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
130
194
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseMysqlClient ().CloseSSL (request )
@@ -133,41 +197,45 @@ func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}
133
197
} else {
134
198
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
135
199
}
200
+
136
201
return nil
137
202
})
203
+
138
204
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 )
140
206
return err
141
207
}
142
208
} else {
143
209
return fmt .Errorf ("[CRITAL]%s update mysql ssl failed, reason:your status must be ON or OFF!" , logId )
144
210
}
145
211
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 {
164
216
return resource .NonRetryableError (err )
165
- })
217
+ }
166
218
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 ))
170
226
}
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
171
239
}
172
240
}
173
241
0 commit comments