|
| 1 | +/* |
| 2 | +Provides a resource to create a trocket rocketmq_consumer_group |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" { |
| 8 | + instance_type = "EXPERIMENT" |
| 9 | + name = "test" |
| 10 | + sku_code = "experiment_500" |
| 11 | + remark = "test" |
| 12 | + vpc_id = "vpc-xxxxxx" |
| 13 | + subnet_id = "subnet-xxxxx" |
| 14 | + tags = { |
| 15 | + tag_key = "rocketmq" |
| 16 | + tag_value = "5.x" |
| 17 | + } |
| 18 | +} |
| 19 | +
|
| 20 | +resource "tencentcloud_trocket_rocketmq_consumer_group" "rocketmq_consumer_group" { |
| 21 | + instance_id = tencentcloud_trocket_rocketmq_instance.rocketmq_instance.id |
| 22 | + consumer_group = "test_consumer_group" |
| 23 | + max_retry_times = 20 |
| 24 | + consume_enable = false |
| 25 | + consume_message_orderly = true |
| 26 | + remark = "test for terraform" |
| 27 | +} |
| 28 | +``` |
| 29 | +
|
| 30 | +Import |
| 31 | +
|
| 32 | +trocket rocketmq_consumer_group can be imported using the id, e.g. |
| 33 | +
|
| 34 | +``` |
| 35 | +terraform import tencentcloud_trocket_rocketmq_consumer_group.rocketmq_consumer_group instanceId#consumerGroup |
| 36 | +``` |
| 37 | +*/ |
| 38 | +package tencentcloud |
| 39 | + |
| 40 | +import ( |
| 41 | + "context" |
| 42 | + "fmt" |
| 43 | + "log" |
| 44 | + "strings" |
| 45 | + |
| 46 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 47 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 48 | + trocket "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket/v20230308" |
| 49 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 50 | +) |
| 51 | + |
| 52 | +func resourceTencentCloudTrocketRocketmqConsumerGroup() *schema.Resource { |
| 53 | + return &schema.Resource{ |
| 54 | + Create: resourceTencentCloudTrocketRocketmqConsumerGroupCreate, |
| 55 | + Read: resourceTencentCloudTrocketRocketmqConsumerGroupRead, |
| 56 | + Update: resourceTencentCloudTrocketRocketmqConsumerGroupUpdate, |
| 57 | + Delete: resourceTencentCloudTrocketRocketmqConsumerGroupDelete, |
| 58 | + Importer: &schema.ResourceImporter{ |
| 59 | + State: schema.ImportStatePassthrough, |
| 60 | + }, |
| 61 | + Schema: map[string]*schema.Schema{ |
| 62 | + "instance_id": { |
| 63 | + Required: true, |
| 64 | + Type: schema.TypeString, |
| 65 | + ForceNew: true, |
| 66 | + Description: "Instance ID.", |
| 67 | + }, |
| 68 | + |
| 69 | + "consumer_group": { |
| 70 | + Required: true, |
| 71 | + Type: schema.TypeString, |
| 72 | + ForceNew: true, |
| 73 | + Description: "Name of consumer group.", |
| 74 | + }, |
| 75 | + |
| 76 | + "max_retry_times": { |
| 77 | + Required: true, |
| 78 | + Type: schema.TypeInt, |
| 79 | + Description: "Max retry times.", |
| 80 | + }, |
| 81 | + |
| 82 | + "consume_enable": { |
| 83 | + Required: true, |
| 84 | + Type: schema.TypeBool, |
| 85 | + Description: "Whether to enable consumption.", |
| 86 | + }, |
| 87 | + |
| 88 | + "consume_message_orderly": { |
| 89 | + Required: true, |
| 90 | + Type: schema.TypeBool, |
| 91 | + Description: "`true`: Sequential delivery, `false`: Concurrent delivery.", |
| 92 | + }, |
| 93 | + |
| 94 | + "remark": { |
| 95 | + Optional: true, |
| 96 | + Type: schema.TypeString, |
| 97 | + Description: "remark.", |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func resourceTencentCloudTrocketRocketmqConsumerGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 104 | + defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.create")() |
| 105 | + defer inconsistentCheck(d, meta)() |
| 106 | + |
| 107 | + logId := getLogId(contextNil) |
| 108 | + |
| 109 | + var ( |
| 110 | + request = trocket.NewCreateConsumerGroupRequest() |
| 111 | + response = trocket.NewCreateConsumerGroupResponse() |
| 112 | + instanceId string |
| 113 | + consumerGroup string |
| 114 | + ) |
| 115 | + if v, ok := d.GetOk("instance_id"); ok { |
| 116 | + request.InstanceId = helper.String(v.(string)) |
| 117 | + } |
| 118 | + |
| 119 | + if v, ok := d.GetOk("consumer_group"); ok { |
| 120 | + request.ConsumerGroup = helper.String(v.(string)) |
| 121 | + } |
| 122 | + |
| 123 | + if v, ok := d.GetOkExists("max_retry_times"); ok { |
| 124 | + request.MaxRetryTimes = helper.IntInt64(v.(int)) |
| 125 | + } |
| 126 | + |
| 127 | + if v, ok := d.GetOkExists("consume_enable"); ok { |
| 128 | + request.ConsumeEnable = helper.Bool(v.(bool)) |
| 129 | + } |
| 130 | + |
| 131 | + if v, ok := d.GetOkExists("consume_message_orderly"); ok { |
| 132 | + request.ConsumeMessageOrderly = helper.Bool(v.(bool)) |
| 133 | + } |
| 134 | + |
| 135 | + if v, ok := d.GetOk("remark"); ok { |
| 136 | + request.Remark = helper.String(v.(string)) |
| 137 | + } |
| 138 | + |
| 139 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 140 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTrocketClient().CreateConsumerGroup(request) |
| 141 | + if e != nil { |
| 142 | + return retryError(e) |
| 143 | + } else { |
| 144 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 145 | + } |
| 146 | + response = result |
| 147 | + return nil |
| 148 | + }) |
| 149 | + if err != nil { |
| 150 | + log.Printf("[CRITAL]%s create trocket rocketmqConsumerGroup failed, reason:%+v", logId, err) |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + instanceId = *response.Response.InstanceId |
| 155 | + consumerGroup = *response.Response.ConsumerGroup |
| 156 | + d.SetId(instanceId + FILED_SP + consumerGroup) |
| 157 | + |
| 158 | + return resourceTencentCloudTrocketRocketmqConsumerGroupRead(d, meta) |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudTrocketRocketmqConsumerGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.read")() |
| 163 | + defer inconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + logId := getLogId(contextNil) |
| 166 | + |
| 167 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 168 | + |
| 169 | + service := TrocketService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 170 | + |
| 171 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 172 | + if len(idSplit) != 2 { |
| 173 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 174 | + } |
| 175 | + instanceId := idSplit[0] |
| 176 | + consumerGroup := idSplit[1] |
| 177 | + |
| 178 | + rocketmqConsumerGroup, err := service.DescribeTrocketRocketmqConsumerGroupById(ctx, instanceId, consumerGroup) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + if rocketmqConsumerGroup == nil { |
| 184 | + d.SetId("") |
| 185 | + log.Printf("[WARN]%s resource `TrocketRocketmqConsumerGroup` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 186 | + return nil |
| 187 | + } |
| 188 | + |
| 189 | + _ = d.Set("instance_id", instanceId) |
| 190 | + _ = d.Set("consumer_group", consumerGroup) |
| 191 | + |
| 192 | + if rocketmqConsumerGroup.MaxRetryTimes != nil { |
| 193 | + _ = d.Set("max_retry_times", rocketmqConsumerGroup.MaxRetryTimes) |
| 194 | + } |
| 195 | + |
| 196 | + if rocketmqConsumerGroup.ConsumeEnable != nil { |
| 197 | + _ = d.Set("consume_enable", rocketmqConsumerGroup.ConsumeEnable) |
| 198 | + } |
| 199 | + |
| 200 | + if rocketmqConsumerGroup.ConsumeMessageOrderly != nil { |
| 201 | + _ = d.Set("consume_message_orderly", rocketmqConsumerGroup.ConsumeMessageOrderly) |
| 202 | + } |
| 203 | + |
| 204 | + if rocketmqConsumerGroup.Remark != nil { |
| 205 | + _ = d.Set("remark", rocketmqConsumerGroup.Remark) |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func resourceTencentCloudTrocketRocketmqConsumerGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 212 | + defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.update")() |
| 213 | + defer inconsistentCheck(d, meta)() |
| 214 | + |
| 215 | + logId := getLogId(contextNil) |
| 216 | + |
| 217 | + request := trocket.NewModifyConsumerGroupRequest() |
| 218 | + |
| 219 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 220 | + if len(idSplit) != 2 { |
| 221 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 222 | + } |
| 223 | + instanceId := idSplit[0] |
| 224 | + consumerGroup := idSplit[1] |
| 225 | + |
| 226 | + request.InstanceId = &instanceId |
| 227 | + request.ConsumerGroup = &consumerGroup |
| 228 | + |
| 229 | + needChange := false |
| 230 | + |
| 231 | + mutableArgs := []string{"max_retry_times", "consume_enable", "consume_message_orderly", "remark"} |
| 232 | + |
| 233 | + for _, v := range mutableArgs { |
| 234 | + if d.HasChange(v) { |
| 235 | + needChange = true |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + if needChange { |
| 240 | + if v, ok := d.GetOkExists("max_retry_times"); ok { |
| 241 | + request.MaxRetryTimes = helper.IntInt64(v.(int)) |
| 242 | + } |
| 243 | + |
| 244 | + if v, ok := d.GetOkExists("consume_enable"); ok { |
| 245 | + request.ConsumeEnable = helper.Bool(v.(bool)) |
| 246 | + } |
| 247 | + |
| 248 | + if v, ok := d.GetOkExists("consume_message_orderly"); ok { |
| 249 | + request.ConsumeMessageOrderly = helper.Bool(v.(bool)) |
| 250 | + } |
| 251 | + |
| 252 | + if v, ok := d.GetOk("remark"); ok { |
| 253 | + request.Remark = helper.String(v.(string)) |
| 254 | + } |
| 255 | + |
| 256 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 257 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTrocketClient().ModifyConsumerGroup(request) |
| 258 | + if e != nil { |
| 259 | + return retryError(e) |
| 260 | + } else { |
| 261 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 262 | + } |
| 263 | + return nil |
| 264 | + }) |
| 265 | + if err != nil { |
| 266 | + log.Printf("[CRITAL]%s update trocket rocketmqConsumerGroup failed, reason:%+v", logId, err) |
| 267 | + return err |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return resourceTencentCloudTrocketRocketmqConsumerGroupRead(d, meta) |
| 272 | +} |
| 273 | + |
| 274 | +func resourceTencentCloudTrocketRocketmqConsumerGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 275 | + defer logElapsed("resource.tencentcloud_trocket_rocketmq_consumer_group.delete")() |
| 276 | + defer inconsistentCheck(d, meta)() |
| 277 | + |
| 278 | + logId := getLogId(contextNil) |
| 279 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 280 | + |
| 281 | + service := TrocketService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 282 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 283 | + if len(idSplit) != 2 { |
| 284 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 285 | + } |
| 286 | + instanceId := idSplit[0] |
| 287 | + consumerGroup := idSplit[1] |
| 288 | + |
| 289 | + if err := service.DeleteTrocketRocketmqConsumerGroupById(ctx, instanceId, consumerGroup); err != nil { |
| 290 | + return err |
| 291 | + } |
| 292 | + |
| 293 | + return nil |
| 294 | +} |
0 commit comments