@@ -36,8 +36,8 @@ import (
3636 "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
3737)
3838
39- func TencentSqlServerBasicInfo () map [string ]* schema.Schema {
40- return map [string ]* schema.Schema {
39+ func TencentSqlServerBasicInfo (isROInstance bool ) map [string ]* schema.Schema {
40+ basicSchema := map [string ]* schema.Schema {
4141 "name" : {
4242 Type : schema .TypeString ,
4343 Required : true ,
@@ -49,8 +49,28 @@ func TencentSqlServerBasicInfo() map[string]*schema.Schema {
4949 Optional : true ,
5050 Default : COMMON_PAYTYPE_POSTPAID ,
5151 ForceNew : true ,
52- ValidateFunc : validateAllowedStringValue (POSTGRESQL_PAYTYPE ),
53- Description : "Pay type of the SQL Server instance. For now, only `POSTPAID_BY_HOUR` is valid." ,
52+ ValidateFunc : validateAllowedStringValue ([]string {COMMON_PAYTYPE_PREPAID , COMMON_PAYTYPE_POSTPAID }),
53+ Description : "Pay type of the SQL Server instance. Available values `PREPAID`, `POSTPAID_BY_HOUR`." ,
54+ },
55+ "period" : {
56+ Type : schema .TypeInt ,
57+ Optional : true ,
58+ ValidateFunc : validateIntegerInRange (1 , 48 ),
59+ Description : "Purchase instance period in month. The value does not exceed 48." ,
60+ },
61+ "auto_voucher" : {
62+ Type : schema .TypeInt ,
63+ Optional : true ,
64+ Default : 0 ,
65+ Description : "Whether to use the voucher automatically; 1 for yes, 0 for no, the default is 0." ,
66+ },
67+ "voucher_ids" : {
68+ Type : schema .TypeSet ,
69+ Optional : true ,
70+ Elem : & schema.Schema {
71+ Type : schema .TypeString ,
72+ },
73+ Description : "An array of voucher IDs, currently only one can be used for a single order." ,
5474 },
5575 "vpc_id" : {
5676 Type : schema .TypeString ,
@@ -121,6 +141,16 @@ func TencentSqlServerBasicInfo() map[string]*schema.Schema {
121141 Description : "The tags of the SQL Server." ,
122142 },
123143 }
144+
145+ if ! isROInstance {
146+ basicSchema ["auto_renew" ] = & schema.Schema {
147+ Type : schema .TypeInt ,
148+ Optional : true ,
149+ Description : "Automatic renewal sign. 0 for normal renewal, 1 for automatic renewal (Default). Only valid when purchasing a prepaid instance." ,
150+ }
151+ }
152+
153+ return basicSchema
124154}
125155
126156func resourceTencentCloudSqlserverInstance () * schema.Resource {
@@ -173,7 +203,7 @@ func resourceTencentCloudSqlserverInstance() *schema.Resource {
173203 Description : "Project ID, default value is 0." ,
174204 },
175205 }
176- basic := TencentSqlServerBasicInfo ()
206+ basic := TencentSqlServerBasicInfo (false )
177207 for k , v := range basic {
178208 specialInfo [k ] = v
179209 }
@@ -223,9 +253,7 @@ func resourceTencentCloudSqlserverInstanceCreate(d *schema.ResourceData, meta in
223253 weekSet = append (weekSet , vv .(int ))
224254 }
225255 }
226- if payType == COMMON_PAYTYPE_POSTPAID {
227- payType = "POSTPAID"
228- }
256+
229257 var instanceId string
230258 var outErr , inErr error
231259
@@ -235,8 +263,65 @@ func resourceTencentCloudSqlserverInstanceCreate(d *schema.ResourceData, meta in
235263 securityGroups = append (securityGroups , sg .(string ))
236264 }
237265 }
266+
267+ request := sqlserver .NewCreateDBInstancesRequest ()
268+ request .DBVersion = & dbVersion
269+ request .Memory = helper .IntInt64 (memory )
270+ request .Storage = helper .IntInt64 (storage )
271+ request .SubnetId = & subnetId
272+ request .VpcId = & vpcId
273+ request .HAType = & haType
274+ request .MultiZones = & multiZones
275+
276+ if payType == COMMON_PAYTYPE_POSTPAID {
277+ request .InstanceChargeType = helper .String ("POSTPAID" )
278+ }
279+ if payType == COMMON_PAYTYPE_PREPAID {
280+ request .InstanceChargeType = helper .String ("PREPAID" )
281+ if v , ok := d .Get ("auto_renew" ).(int ); ok {
282+ request .AutoRenewFlag = helper .IntInt64 (v )
283+ }
284+
285+ if v , ok := d .Get ("period" ).(int ); ok {
286+ request .Period = helper .IntInt64 (v )
287+ }
288+ }
289+
290+ if v , ok := d .Get ("auto_voucher" ).(int ); ok {
291+ request .AutoVoucher = helper .IntInt64 (v )
292+ }
293+
294+ if v , ok := d .Get ("voucher_ids" ).([]interface {}); ok {
295+ request .VoucherIds = helper .InterfacesStringsPoint (v )
296+ }
297+
298+ if projectId != 0 {
299+ request .ProjectId = helper .IntInt64 (projectId )
300+ }
301+
302+ if len (weekSet ) > 0 {
303+ request .Weekly = make ([]* int64 , 0 )
304+ for _ , i := range weekSet {
305+ request .Weekly = append (request .Weekly , helper .IntInt64 (i ))
306+ }
307+ }
308+ if startTime != "" {
309+ request .StartTime = & startTime
310+ }
311+ if timeSpan != 0 {
312+ request .Span = helper .IntInt64 (timeSpan )
313+ }
314+
315+ request .SecurityGroupList = make ([]* string , 0 , len (securityGroups ))
316+ for _ , v := range securityGroups {
317+ request .SecurityGroupList = append (request .SecurityGroupList , & v )
318+ }
319+
320+ request .GoodsNum = helper .IntInt64 (1 )
321+ request .Zone = & zone
322+
238323 outErr = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
239- instanceId , inErr = sqlserverService .CreateSqlserverInstance (ctx , dbVersion , payType , memory , 0 , projectId , subnetId , vpcId , zone , storage , weekSet , startTime , timeSpan , multiZones , haType , securityGroups )
324+ instanceId , inErr = sqlserverService .CreateSqlserverInstance (ctx , request )
240325 if inErr != nil {
241326 return retryError (inErr )
242327 }
@@ -294,20 +379,37 @@ func sqlServerAllInstanceRoleUpdate(ctx context.Context, d *schema.ResourceData,
294379 }
295380
296381 //upgrade storage and memory size
297- if d .HasChange ("memory" ) || d .HasChange ("storage" ) {
382+ if d .HasChange ("memory" ) || d .HasChange ("storage" ) || d . HasChange ( "auto_voucher" ) || d . HasChange ( "voucher_ids" ) {
298383 memory := d .Get ("memory" ).(int )
299384 storage := d .Get ("storage" ).(int )
385+ autoVoucher := d .Get ("auto_voucher" ).(int )
386+ voucherIds := d .Get ("voucher_ids" ).([]interface {})
300387 outErr = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
301- inErr = sqlserverService .UpgradeSqlserverInstance (ctx , instanceId , memory , storage )
388+ inErr = sqlserverService .UpgradeSqlserverInstance (ctx , instanceId , memory , storage , autoVoucher , helper . InterfacesStringsPoint ( voucherIds ) )
302389 if inErr != nil {
303390 return retryError (inErr )
304391 }
305392 return nil
306393 })
394+
307395 if outErr != nil {
308396 return outErr
309397 }
310398
399+ outErr = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
400+ instance , _ , inErr := sqlserverService .DescribeSqlserverInstanceById (ctx , instanceId )
401+
402+ if inErr != nil {
403+ return retryError (inErr )
404+ }
405+ //specAsExpected := int(*instance.Memory) != memory && int(*instance.Storage) != storage
406+
407+ if * instance .Status == SQLSERVER_DB_UPGRADING {
408+ return resource .RetryableError (fmt .Errorf ("instance status code is: %d, waiting for upgrade complete" , * instance .Status ))
409+ }
410+ return nil
411+ })
412+
311413 d .SetPartial ("memory" )
312414 d .SetPartial ("storage" )
313415 }
@@ -479,6 +581,9 @@ func tencentSqlServerBasicInfoRead(ctx context.Context, d *schema.ResourceData,
479581
480582 if int (* instance .PayMode ) == 1 {
481583 _ = d .Set ("charge_type" , COMMON_PAYTYPE_PREPAID )
584+ if _ , ok := d .GetOk ("auto_renew" ); ok {
585+ _ = d .Set ("auto_renew" , instance .RenewFlag )
586+ }
482587 } else {
483588 _ = d .Set ("charge_type" , COMMON_PAYTYPE_POSTPAID )
484589 }
0 commit comments