@@ -137,6 +137,7 @@ import (
137137 "log"
138138 "reflect"
139139 "strings"
140+ "time"
140141
141142 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
142143 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -204,6 +205,14 @@ func resourceTencentCloudCkafkaInstance() *schema.Resource {
204205 ValidateFunc : validateAllowedIntValue ([]int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }),
205206 Description : "Description of instance type. `profession`: 1, `standard`: 1(general), 2(standard), 3(advanced), 4(capacity), 5(specialized-1), 6(specialized-2), 7(specialized-3), 8(specialized-4), 9(exclusive)." ,
206207 },
208+ "upgrade_strategy" : {
209+ Type : schema .TypeInt ,
210+ Optional : true ,
211+ Default : 1 ,
212+ Description : "POSTPAID_BY_HOUR scale-down mode\n " +
213+ "- 1: stable transformation;\n " +
214+ "- 2: High-speed transformer." ,
215+ },
207216 "vpc_id" : {
208217 Type : schema .TypeString ,
209218 Optional : true ,
@@ -837,17 +846,20 @@ func resourceTencentCloudCkafkaInstanceUpdate(d *schema.ResourceData, meta inter
837846 }
838847
839848 instanceId := d .Id ()
849+ modifyInstanceAttributesFlag := false
840850 request := ckafka .NewModifyInstanceAttributesRequest ()
841851 request .InstanceId = & instanceId
842852 if d .HasChange ("instance_name" ) {
843853 if v , ok := d .GetOk ("instance_name" ); ok {
844854 request .InstanceName = helper .String (v .(string ))
855+ modifyInstanceAttributesFlag = true
845856 }
846857 }
847858
848859 if d .HasChange ("msg_retention_time" ) {
849860 if v , ok := d .GetOk ("msg_retention_time" ); ok {
850861 request .MsgRetentionTime = helper .Int64 (int64 (v .(int )))
862+ modifyInstanceAttributesFlag = true
851863 }
852864 }
853865
@@ -866,6 +878,7 @@ func resourceTencentCloudCkafkaInstanceUpdate(d *schema.ResourceData, meta inter
866878 configInfo .DefaultReplicationFactor = helper .Int64 (int64 (defaultReplicationFactor .(int )))
867879 }
868880 request .Config = & configInfo
881+ modifyInstanceAttributesFlag = true
869882 }
870883 }
871884
@@ -887,52 +900,86 @@ func resourceTencentCloudCkafkaInstanceUpdate(d *schema.ResourceData, meta inter
887900 dynamicInfo .BottomRetention = helper .Int64 (int64 (bottomRetention .(int )))
888901 }
889902 request .DynamicRetentionConfig = & dynamicInfo
903+ modifyInstanceAttributesFlag = true
890904 }
891905 }
892906
893907 if d .HasChange ("rebalance_time" ) {
894908 if v , ok := d .GetOk ("rebalance_time" ); ok {
895909 request .RebalanceTime = helper .Int64 (int64 (v .(int )))
910+ modifyInstanceAttributesFlag = true
896911 }
897912 }
898913
899914 if d .HasChange ("public_network" ) {
900915 if v , ok := d .GetOk ("public_network" ); ok {
901916 request .PublicNetwork = helper .Int64 (int64 (v .(int )))
917+ modifyInstanceAttributesFlag = true
902918 }
903919 }
904920
905921 if d .HasChange ("max_message_byte" ) {
906922 if v , ok := d .GetOkExists ("max_message_byte" ); ok {
907923 request .MaxMessageByte = helper .Uint64 (uint64 (v .(int )))
924+ modifyInstanceAttributesFlag = true
908925 }
909926 }
910927
911- err := service .ModifyCkafkaInstanceAttributes (ctx , request )
912- if err != nil {
913- return fmt .Errorf ("[API]Set kafka instance attributes fail, reason:%s" , err .Error ())
928+ if modifyInstanceAttributesFlag {
929+ err := service .ModifyCkafkaInstanceAttributes (ctx , request )
930+ if err != nil {
931+ return fmt .Errorf ("[API]Set kafka instance attributes fail, reason:%s" , err .Error ())
932+ }
914933 }
915934
916935 if d .HasChange ("band_width" ) || d .HasChange ("disk_size" ) || d .HasChange ("partition" ) {
917- request := ckafka .NewModifyInstancePreRequest ()
918- request .InstanceId = helper .String (instanceId )
919- if v , ok := d .GetOk ("band_width" ); ok {
920- request .BandWidth = helper .Int64 (int64 (v .(int )))
921- }
922- if v , ok := d .GetOk ("disk_size" ); ok {
923- request .DiskSize = helper .Int64 (int64 (v .(int )))
924- }
925- if v , ok := d .GetOk ("partition" ); ok {
926- request .Partition = helper .Int64 (int64 (v .(int )))
927- }
936+ chargeType := d .Get ("charge_type" ).(string )
937+ if chargeType == CKAFKA_CHARGE_TYPE_POSTPAID {
938+ request := ckafka .NewInstanceScalingDownRequest ()
939+ request .InstanceId = helper .String (instanceId )
940+ upgradeStrategy := d .Get ("upgrade_strategy" ).(int )
941+ request .UpgradeStrategy = helper .IntInt64 (upgradeStrategy )
942+ if v , ok := d .GetOk ("band_width" ); ok && d .HasChange ("band_width" ) {
943+ request .BandWidth = helper .Int64 (int64 (v .(int )))
944+ }
945+ if v , ok := d .GetOk ("disk_size" ); ok && d .HasChange ("disk_size" ) {
946+ request .DiskSize = helper .Int64 (int64 (v .(int )))
947+ }
948+ if v , ok := d .GetOk ("partition" ); ok && d .HasChange ("partition" ) {
949+ request .Partition = helper .Int64 (int64 (v .(int )))
950+ }
928951
929- _ , err := service .client .UseCkafkaClient ().ModifyInstancePre (request )
930- if err != nil {
931- return fmt .Errorf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" , logId ,
932- request .GetAction (), request .ToJsonString (), err .Error ())
952+ _ , err := service .client .UseCkafkaClient ().InstanceScalingDown (request )
953+ if err != nil {
954+ return fmt .Errorf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" , logId ,
955+ request .GetAction (), request .ToJsonString (), err .Error ())
956+ }
957+ } else if chargeType == CKAFKA_CHARGE_TYPE_PREPAID {
958+ request := ckafka .NewModifyInstancePreRequest ()
959+ request .InstanceId = helper .String (instanceId )
960+
961+ if v , ok := d .GetOk ("band_width" ); ok {
962+ request .BandWidth = helper .Int64 (int64 (v .(int )))
963+ }
964+ if v , ok := d .GetOk ("disk_size" ); ok {
965+ request .DiskSize = helper .Int64 (int64 (v .(int )))
966+ }
967+ if v , ok := d .GetOk ("partition" ); ok {
968+ request .Partition = helper .Int64 (int64 (v .(int )))
969+ }
970+
971+ _ , err := service .client .UseCkafkaClient ().ModifyInstancePre (request )
972+ if err != nil {
973+ return fmt .Errorf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" , logId ,
974+ request .GetAction (), request .ToJsonString (), err .Error ())
975+ }
976+ } else {
977+ return fmt .Errorf ("invalid `charge_type` value" )
933978 }
979+ // InstanceScalingDown statue delay
980+ time .Sleep (5 * time .Second )
934981
935- err = resource .Retry (5 * readRetryTimeout , func () * resource.RetryError {
982+ err : = resource .Retry (10 * readRetryTimeout , func () * resource.RetryError {
936983 _ , ready , err := service .CheckCkafkaInstanceReady (ctx , instanceId )
937984 if err != nil {
938985 return resource .NonRetryableError (err )
0 commit comments