@@ -385,7 +385,7 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
385385 cpu = v .(int )
386386 }
387387
388- if v , ok := d .GetOk ("period" ); ok {
388+ if v , ok := d .GetOkExists ("period" ); ok {
389389 log .Printf ("period set" )
390390 period = v .(int )
391391 } else {
@@ -404,7 +404,7 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
404404 dbKernelVersion = v .(string )
405405 }
406406
407- if v , ok := d .GetOk ("need_support_tde" ); ok {
407+ if v , ok := d .GetOkExists ("need_support_tde" ); ok {
408408 needSupportTde = v .(int )
409409 }
410410
@@ -416,12 +416,12 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
416416 kmsRegion = v .(string )
417417 }
418418
419- if v , ok := d .Get ("auto_renew_flag" ).( int ); ok {
420- autoRenewFlag = v
419+ if v , ok := d .GetOkExists ("auto_renew_flag" ); ok {
420+ autoRenewFlag = v .( int )
421421 }
422422
423- if v , ok := d .Get ("auto_voucher" ).( int ); ok {
424- autoVoucher = v
423+ if v , ok := d .GetOkExists ("auto_voucher" ); ok {
424+ autoVoucher = v .( int )
425425 }
426426
427427 if v , ok := d .GetOk ("voucher_ids" ); ok {
@@ -1004,10 +1004,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
10041004 d .Partial (true )
10051005
10061006 if err := helper .ImmutableArgsChek (d ,
1007- // "charge_type",
1008- // "period",
1009- // "auto_renew_flag",
1010- // "auto_voucher",
1007+ "auto_voucher" ,
10111008 "voucher_ids" ,
10121009 "root_user" ,
10131010 ); err != nil {
@@ -1023,18 +1020,11 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
10231020 waitSwitch = v .(int )
10241021 }
10251022
1026- if d .HasChange ("period" ) && ! d .HasChange ("charge_type" ) {
1027- return fmt .Errorf ("The `period` field can be changed only when updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`." )
1028- }
1029-
10301023 if d .HasChange ("charge_type" ) {
10311024 var (
1025+ request = postgresql .NewModifyDBInstanceChargeTypeRequest ()
10321026 chargeTypeOld string
10331027 chargeTypeNew string
1034- period = 1
1035- autoRenew = 0
1036- autoVoucher = 0
1037- request = postgresql .NewModifyDBInstanceChargeTypeRequest ()
10381028 )
10391029
10401030 old , new := d .GetChange ("charge_type" )
@@ -1050,34 +1040,81 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
10501040 return fmt .Errorf ("It only support to update the charge type from `POSTPAID_BY_HOUR` to `PREPAID`." )
10511041 }
10521042
1053- if v , ok := d .GetOk ("period" ); ok {
1054- log .Printf ("period set" )
1055- period = v .(int )
1056- } else {
1057- log .Printf ("period not set" )
1043+ request .DBInstanceId = & instanceId
1044+ request .InstanceChargeType = & chargeTypeNew
1045+ request .Period = helper .IntInt64 (1 )
1046+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1047+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().ModifyDBInstanceChargeType (request )
1048+ if e != nil {
1049+ return tccommon .RetryError (e )
1050+ } else {
1051+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
1052+ }
1053+
1054+ return nil
1055+ })
1056+
1057+ if err != nil {
1058+ log .Printf ("[CRITAL]%s operate postgresql ModifyDbInstanceChargeType failed, reason:%+v" , logId , err )
1059+ return err
10581060 }
10591061
1060- if v , ok := d . GetOk ( "auto_renew_flag" ); ok {
1061- log . Printf ( "auto_renew_flag set" )
1062- autoRenew = v .( int )
1063- } else {
1064- log . Printf ( "auto_renew_flag not set" )
1062+ // wait unit charge type changing operation of instance done
1063+ service := PostgresqlService { client : meta .(tccommon. ProviderMeta ). GetAPIV3Conn ()}
1064+ conf := tccommon . BuildStateChangeConf ([] string {}, [] string { "running" }, 2 * tccommon . ReadRetryTimeout , time . Second , service . PostgresqlDBInstanceStateRefreshFunc ( instanceId , [] string {}) )
1065+ if _ , e := conf . WaitForState (); e != nil {
1066+ return e
10651067 }
1068+ }
10661069
1067- if v , ok := d .GetOk ("auto_voucher" ); ok {
1068- log .Printf ("auto_voucher set" )
1069- autoVoucher = v .(int )
1070- } else {
1071- log .Printf ("auto_voucher not set" )
1070+ if d .HasChange ("auto_renew_flag" ) {
1071+ request := postgresql .NewSetAutoRenewFlagRequest ()
1072+ request .DBInstanceIdSet = helper .Strings ([]string {instanceId })
1073+ if v , ok := d .GetOkExists ("auto_renew_flag" ); ok {
1074+ request .AutoRenewFlag = helper .IntInt64 (v .(int ))
1075+ }
1076+
1077+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1078+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().SetAutoRenewFlag (request )
1079+ if e != nil {
1080+ return tccommon .RetryError (e )
1081+ } else {
1082+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
1083+ }
1084+
1085+ return nil
1086+ })
1087+
1088+ if err != nil {
1089+ log .Printf ("[CRITAL]%s operate postgresql SetAutoRenewFlag failed, reason:%+v" , logId , err )
1090+ return err
1091+ }
1092+
1093+ // wait unit charge type changing operation of instance done
1094+ service := PostgresqlService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
1095+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"running" }, 2 * tccommon .ReadRetryTimeout , time .Second , service .PostgresqlDBInstanceStateRefreshFunc (instanceId , []string {}))
1096+ if _ , e := conf .WaitForState (); e != nil {
1097+ return e
10721098 }
1099+ }
10731100
1101+ if d .HasChange ("period" ) {
1102+ request := postgresql .NewRenewInstanceRequest ()
10741103 request .DBInstanceId = & instanceId
1075- request .InstanceChargeType = & chargeTypeNew
1076- request .Period = helper .IntInt64 (period )
1077- request .AutoRenewFlag = helper .IntInt64 (autoRenew )
1078- request .AutoVoucher = helper .IntInt64 (autoVoucher )
1104+ if v , ok := d .GetOkExists ("period" ); ok {
1105+ request .Period = helper .IntInt64 (v .(int ))
1106+ }
1107+
1108+ if v , ok := d .GetOkExists ("auto_voucher" ); ok {
1109+ request .AutoVoucher = helper .IntInt64 (v .(int ))
1110+ }
1111+
1112+ if v , ok := d .GetOk ("voucher_ids" ); ok {
1113+ request .VoucherIds = helper .InterfacesStringsPoint (v .([]interface {}))
1114+ }
1115+
10791116 err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1080- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().ModifyDBInstanceChargeType (request )
1117+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().RenewInstance (request )
10811118 if e != nil {
10821119 return tccommon .RetryError (e )
10831120 } else {
@@ -1088,7 +1125,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
10881125 })
10891126
10901127 if err != nil {
1091- log .Printf ("[CRITAL]%s operate postgresql ModifyDbInstanceChargeType failed, reason:%+v" , logId , err )
1128+ log .Printf ("[CRITAL]%s operate postgresql RenewInstance failed, reason:%+v" , logId , err )
10921129 return err
10931130 }
10941131
0 commit comments