Skip to content

Commit 1937ba6

Browse files
tongyimingmikatong
andauthored
support ckafka postpaid scaling down (#2209)
* support ckafka postpaid scaling down * add changelog * update unit test --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 0d24364 commit 1937ba6

File tree

4 files changed

+149
-24
lines changed

4 files changed

+149
-24
lines changed

.changelog/2209.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
tencentcloud_ckafka_instance: support param `upgrade_strategy` and postpaid scaling down
3+
```

tencentcloud/resource_tc_ckafka_instance.go

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tencentcloud/resource_tc_ckafka_instance_test.go

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestAccTencentCloudCkafkaInstanceResource_postpaid(t *testing.T) {
7373
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "zone_id", "100007"),
7474
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "msg_retention_time", "1300"),
7575
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "kafka_version", "1.1.1"),
76-
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_size", "200"),
76+
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_size", "500"),
7777
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_type", "CLOUD_BASIC"),
7878
resource.TestCheckResourceAttrSet("tencentcloud_ckafka_instance.kafka_instance_postpaid", "vip"),
7979
resource.TestCheckResourceAttrSet("tencentcloud_ckafka_instance.kafka_instance_postpaid", "vport"),
@@ -87,15 +87,22 @@ func TestAccTencentCloudCkafkaInstanceResource_postpaid(t *testing.T) {
8787
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "zone_id", "100007"),
8888
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "msg_retention_time", "1200"),
8989
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "kafka_version", "1.1.1"),
90-
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_size", "200"),
90+
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_size", "500"),
9191
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_type", "CLOUD_BASIC"),
9292
),
9393
},
94+
{
95+
Config: testAccKafkaInstanceUpdatePostpaidDiskSize,
96+
Check: resource.ComposeTestCheckFunc(
97+
testAccCheckKafkaInstanceExists("tencentcloud_ckafka_instance.kafka_instance_postpaid"),
98+
resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance_postpaid", "disk_size", "400"),
99+
),
100+
},
94101
{
95102
ResourceName: "tencentcloud_ckafka_instance.kafka_instance_postpaid",
96103
ImportState: true,
97104
ImportStateVerify: true,
98-
ImportStateVerifyIgnore: []string{"period", "max_message_byte", "charge_type"},
105+
ImportStateVerifyIgnore: []string{"period", "max_message_byte", "charge_type", "upgrade_strategy"},
99106
},
100107
},
101108
})
@@ -280,7 +287,7 @@ resource "tencentcloud_ckafka_instance" "kafka_instance_postpaid" {
280287
subnet_id = var.subnet_id
281288
msg_retention_time = 1300
282289
kafka_version = "1.1.1"
283-
disk_size = 200
290+
disk_size = 500
284291
band_width = 20
285292
disk_type = "CLOUD_BASIC"
286293
partition = 400
@@ -296,6 +303,18 @@ resource "tencentcloud_ckafka_instance" "kafka_instance_postpaid" {
296303
enable = 1
297304
}
298305
}
306+
307+
resource "tencentcloud_ckafka_topic" "foo" {
308+
instance_id = tencentcloud_ckafka_instance.kafka_instance_postpaid.id
309+
topic_name = "tmp"
310+
note = "topic note"
311+
replica_num = 2
312+
partition_num = 1
313+
clean_up_policy = "delete"
314+
sync_replica_min_num = 1
315+
unclean_leader_election_enable = false
316+
retention = 60000
317+
}
299318
`
300319

301320
const testAccKafkaInstanceUpdatePostpaid = defaultKafkaVariable + `
@@ -307,7 +326,7 @@ resource "tencentcloud_ckafka_instance" "kafka_instance_postpaid" {
307326
msg_retention_time = 1200
308327
kafka_version = "1.1.1"
309328
disk_type = "CLOUD_BASIC"
310-
disk_size = 200
329+
disk_size = 500
311330
band_width = 20
312331
charge_type = "POSTPAID_BY_HOUR"
313332
@@ -325,6 +344,59 @@ resource "tencentcloud_ckafka_instance" "kafka_instance_postpaid" {
325344
createdBy = "terraform"
326345
}
327346
}
347+
348+
resource "tencentcloud_ckafka_topic" "foo" {
349+
instance_id = tencentcloud_ckafka_instance.kafka_instance_postpaid.id
350+
topic_name = "tmp"
351+
note = "topic note"
352+
replica_num = 2
353+
partition_num = 1
354+
clean_up_policy = "delete"
355+
sync_replica_min_num = 1
356+
unclean_leader_election_enable = false
357+
retention = 60000
358+
}
359+
`
360+
361+
const testAccKafkaInstanceUpdatePostpaidDiskSize = defaultKafkaVariable + `
362+
resource "tencentcloud_ckafka_instance" "kafka_instance_postpaid" {
363+
instance_name = "ckafka-instance-postpaid"
364+
zone_id = 100007
365+
vpc_id = var.vpc_id
366+
subnet_id = var.subnet_id
367+
msg_retention_time = 1200
368+
kafka_version = "1.1.1"
369+
disk_type = "CLOUD_BASIC"
370+
disk_size = 400
371+
band_width = 20
372+
charge_type = "POSTPAID_BY_HOUR"
373+
374+
config {
375+
auto_create_topic_enable = true
376+
default_num_partitions = 3
377+
default_replication_factor = 3
378+
}
379+
380+
dynamic_retention_config {
381+
enable = 1
382+
}
383+
384+
tag_set = {
385+
createdBy = "terraform"
386+
}
387+
}
388+
389+
resource "tencentcloud_ckafka_topic" "foo" {
390+
instance_id = tencentcloud_ckafka_instance.kafka_instance_postpaid.id
391+
topic_name = "tmp"
392+
note = "topic note"
393+
replica_num = 2
394+
partition_num = 1
395+
clean_up_policy = "delete"
396+
sync_replica_min_num = 1
397+
unclean_leader_election_enable = false
398+
retention = 60000
399+
}
328400
`
329401

330402
const testAccKafkaInstanceMAZ = defaultKafkaVariable + `

website/docs/r/ckafka_instance.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ The following arguments are supported:
158158
* `subnet_id` - (Optional, String) Subnet id, it will be basic network if not set.
159159
* `tag_set` - (Optional, Map) Tag set of instance.
160160
* `tags` - (Optional, List, **Deprecated**) It has been deprecated from version 1.78.5, because it do not support change. Use `tag_set` instead. Tags of instance. Partition size, the professional version does not need tag.
161+
* `upgrade_strategy` - (Optional, Int) POSTPAID_BY_HOUR scale-down mode
162+
- 1: stable transformation;
163+
- 2: High-speed transformer.
161164
* `vpc_id` - (Optional, String) Vpc id, it will be basic network if not set.
162165
* `zone_ids` - (Optional, Set: [`Int`]) List of available zone id. NOTE: this argument must set together with `multi_zone_flag`.
163166

0 commit comments

Comments
 (0)