Skip to content

Commit

Permalink
Merge pull request #2123 from tencentcloudstack/feat/add_trocket_ability
Browse files Browse the repository at this point in the history
flatten trocket
  • Loading branch information
andrew-tx authored Sep 14, 2023
2 parents ddae174 + 1a8e189 commit f9bf320
Show file tree
Hide file tree
Showing 19 changed files with 2,339 additions and 87 deletions.
7 changes: 7 additions & 0 deletions .changelog/2123.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
tencentcloud_trocket_rocketmq_topic
```

```release-note:new-resource
tencentcloud_trocket_rocketmq_consumer_group
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.711
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.745
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.746
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.589
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
Expand Down Expand Up @@ -88,7 +88,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.730
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.691
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket v1.0.744
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket v1.0.746
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.732
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
Expand Down
68 changes: 13 additions & 55 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,8 @@ TDMQ for RocketMQ(trocket)
tencentcloud_tdmq_send_rocketmq_message
tencentcloud_tdmq_rocketmq_vip_instance
tencentcloud_trocket_rocketmq_instance
tencentcloud_trocket_rocketmq_topic
tencentcloud_trocket_rocketmq_consumer_group
TDMQ for RabbitMQ(trabbit)
Resource
Expand Down Expand Up @@ -2888,6 +2890,8 @@ func Provider() *schema.Provider {
"tencentcloud_tdmq_rocketmq_environment_role": resourceTencentCloudTdmqRocketmqEnvironmentRole(),
"tencentcloud_tdmq_rocketmq_vip_instance": resourceTencentCloudTdmqRocketmqVipInstance(),
"tencentcloud_trocket_rocketmq_instance": resourceTencentCloudTrocketRocketmqInstance(),
"tencentcloud_trocket_rocketmq_topic": resourceTencentCloudTrocketRocketmqTopic(),
"tencentcloud_trocket_rocketmq_consumer_group": resourceTencentCloudTrocketRocketmqConsumerGroup(),
"tencentcloud_dts_sync_job": resourceTencentCloudDtsSyncJob(),
"tencentcloud_dts_sync_config": resourceTencentCloudDtsSyncConfig(),
"tencentcloud_dts_sync_check_job_operation": resourceTencentCloudDtsSyncCheckJobOperation(),
Expand Down
294 changes: 294 additions & 0 deletions tencentcloud/resource_tc_trocket_rocketmq_consumer_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
/*
Provides a resource to create a trocket rocketmq_consumer_group
Example Usage
```hcl
resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" {
instance_type = "EXPERIMENT"
name = "test"
sku_code = "experiment_500"
remark = "test"
vpc_id = "vpc-xxxxxx"
subnet_id = "subnet-xxxxx"
tags = {
tag_key = "rocketmq"
tag_value = "5.x"
}
}
resource "tencentcloud_trocket_rocketmq_consumer_group" "rocketmq_consumer_group" {
instance_id = tencentcloud_trocket_rocketmq_instance.rocketmq_instance.id
consumer_group = "test_consumer_group"
max_retry_times = 20
consume_enable = false
consume_message_orderly = true
remark = "test for terraform"
}
```
Import
trocket rocketmq_consumer_group can be imported using the id, e.g.
```
terraform import tencentcloud_trocket_rocketmq_consumer_group.rocketmq_consumer_group instanceId#consumerGroup
```
*/
package tencentcloud

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
trocket "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket/v20230308"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func resourceTencentCloudTrocketRocketmqConsumerGroup() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudTrocketRocketmqConsumerGroupCreate,
Read: resourceTencentCloudTrocketRocketmqConsumerGroupRead,
Update: resourceTencentCloudTrocketRocketmqConsumerGroupUpdate,
Delete: resourceTencentCloudTrocketRocketmqConsumerGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
Type: schema.TypeString,
ForceNew: true,
Description: "Instance ID.",
},

"consumer_group": {
Required: true,
Type: schema.TypeString,
ForceNew: true,
Description: "Name of consumer group.",
},

"max_retry_times": {
Required: true,
Type: schema.TypeInt,
Description: "Max retry times.",
},

"consume_enable": {
Required: true,
Type: schema.TypeBool,
Description: "Whether to enable consumption.",
},

"consume_message_orderly": {
Required: true,
Type: schema.TypeBool,
Description: "`true`: Sequential delivery, `false`: Concurrent delivery.",
},

"remark": {
Optional: true,
Type: schema.TypeString,
Description: "remark.",
},
},
}
}

func resourceTencentCloudTrocketRocketmqConsumerGroupCreate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.create")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

var (
request = trocket.NewCreateConsumerGroupRequest()
response = trocket.NewCreateConsumerGroupResponse()
instanceId string
consumerGroup string
)
if v, ok := d.GetOk("instance_id"); ok {
request.InstanceId = helper.String(v.(string))
}

if v, ok := d.GetOk("consumer_group"); ok {
request.ConsumerGroup = helper.String(v.(string))
}

if v, ok := d.GetOkExists("max_retry_times"); ok {
request.MaxRetryTimes = helper.IntInt64(v.(int))
}

if v, ok := d.GetOkExists("consume_enable"); ok {
request.ConsumeEnable = helper.Bool(v.(bool))
}

if v, ok := d.GetOkExists("consume_message_orderly"); ok {
request.ConsumeMessageOrderly = helper.Bool(v.(bool))
}

if v, ok := d.GetOk("remark"); ok {
request.Remark = helper.String(v.(string))
}

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTrocketClient().CreateConsumerGroup(request)
if e != nil {
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create trocket rocketmqConsumerGroup failed, reason:%+v", logId, err)
return err
}

instanceId = *response.Response.InstanceId
consumerGroup = *response.Response.ConsumerGroup
d.SetId(instanceId + FILED_SP + consumerGroup)

return resourceTencentCloudTrocketRocketmqConsumerGroupRead(d, meta)
}

func resourceTencentCloudTrocketRocketmqConsumerGroupRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.read")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

ctx := context.WithValue(context.TODO(), logIdKey, logId)

service := TrocketService{client: meta.(*TencentCloudClient).apiV3Conn}

idSplit := strings.Split(d.Id(), FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", d.Id())
}
instanceId := idSplit[0]
consumerGroup := idSplit[1]

rocketmqConsumerGroup, err := service.DescribeTrocketRocketmqConsumerGroupById(ctx, instanceId, consumerGroup)
if err != nil {
return err
}

if rocketmqConsumerGroup == nil {
d.SetId("")
log.Printf("[WARN]%s resource `TrocketRocketmqConsumerGroup` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}

_ = d.Set("instance_id", instanceId)
_ = d.Set("consumer_group", consumerGroup)

if rocketmqConsumerGroup.MaxRetryTimes != nil {
_ = d.Set("max_retry_times", rocketmqConsumerGroup.MaxRetryTimes)
}

if rocketmqConsumerGroup.ConsumeEnable != nil {
_ = d.Set("consume_enable", rocketmqConsumerGroup.ConsumeEnable)
}

if rocketmqConsumerGroup.ConsumeMessageOrderly != nil {
_ = d.Set("consume_message_orderly", rocketmqConsumerGroup.ConsumeMessageOrderly)
}

if rocketmqConsumerGroup.Remark != nil {
_ = d.Set("remark", rocketmqConsumerGroup.Remark)
}

return nil
}

func resourceTencentCloudTrocketRocketmqConsumerGroupUpdate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.update")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

request := trocket.NewModifyConsumerGroupRequest()

idSplit := strings.Split(d.Id(), FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", d.Id())
}
instanceId := idSplit[0]
consumerGroup := idSplit[1]

request.InstanceId = &instanceId
request.ConsumerGroup = &consumerGroup

needChange := false

mutableArgs := []string{"max_retry_times", "consume_enable", "consume_message_orderly", "remark"}

for _, v := range mutableArgs {
if d.HasChange(v) {
needChange = true
}
}

if needChange {
if v, ok := d.GetOkExists("max_retry_times"); ok {
request.MaxRetryTimes = helper.IntInt64(v.(int))
}

if v, ok := d.GetOkExists("consume_enable"); ok {
request.ConsumeEnable = helper.Bool(v.(bool))
}

if v, ok := d.GetOkExists("consume_message_orderly"); ok {
request.ConsumeMessageOrderly = helper.Bool(v.(bool))
}

if v, ok := d.GetOk("remark"); ok {
request.Remark = helper.String(v.(string))
}

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTrocketClient().ModifyConsumerGroup(request)
if e != nil {
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s update trocket rocketmqConsumerGroup failed, reason:%+v", logId, err)
return err
}
}

return resourceTencentCloudTrocketRocketmqConsumerGroupRead(d, meta)
}

func resourceTencentCloudTrocketRocketmqConsumerGroupDelete(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.delete")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

service := TrocketService{client: meta.(*TencentCloudClient).apiV3Conn}
idSplit := strings.Split(d.Id(), FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", d.Id())
}
instanceId := idSplit[0]
consumerGroup := idSplit[1]

if err := service.DeleteTrocketRocketmqConsumerGroupById(ctx, instanceId, consumerGroup); err != nil {
return err
}

return nil
}
Loading

0 comments on commit f9bf320

Please sign in to comment.