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" , roGroupId )
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
0 commit comments