|
| 1 | +/* |
| 2 | +Provides a resource to create a ckafka datahub_topic |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_user_info" "user" {} |
| 8 | +
|
| 9 | +resource "tencentcloud_ckafka_datahub_topic" "datahub_topic" { |
| 10 | + name = format("%s-tf", data.tencentcloud_user_info.user.app_id) |
| 11 | + partition_num = 20 |
| 12 | + retention_ms = 60000 |
| 13 | + note = "for test" |
| 14 | + tags = { |
| 15 | + "createdBy" = "terraform" |
| 16 | + } |
| 17 | +} |
| 18 | +``` |
| 19 | +
|
| 20 | +Import |
| 21 | +
|
| 22 | +ckafka datahub_topic can be imported using the id, e.g. |
| 23 | +
|
| 24 | +``` |
| 25 | +terraform import tencentcloud_ckafka_datahub_topic.datahub_topic datahub_topic_name |
| 26 | +``` |
| 27 | +*/ |
| 28 | +package tencentcloud |
| 29 | + |
| 30 | +import ( |
| 31 | + "context" |
| 32 | + "fmt" |
| 33 | + "log" |
| 34 | + |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 37 | + ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" |
| 38 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudCkafkaDatahubTopic() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Create: resourceTencentCloudCkafkaDatahubTopicCreate, |
| 44 | + Read: resourceTencentCloudCkafkaDatahubTopicRead, |
| 45 | + Update: resourceTencentCloudCkafkaDatahubTopicUpdate, |
| 46 | + Delete: resourceTencentCloudCkafkaDatahubTopicDelete, |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "name": { |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "Name, start with appid, which is a string of no more than 128 characters, must start with a letter, and the rest can contain letters, numbers, and dashes (-).", |
| 56 | + }, |
| 57 | + |
| 58 | + "partition_num": { |
| 59 | + Required: true, |
| 60 | + Type: schema.TypeInt, |
| 61 | + Description: "Number of Partitions, greater than 0.", |
| 62 | + }, |
| 63 | + |
| 64 | + "retention_ms": { |
| 65 | + Required: true, |
| 66 | + Type: schema.TypeInt, |
| 67 | + Description: "Message retention time, in ms, the current minimum value is 60000 ms.", |
| 68 | + }, |
| 69 | + |
| 70 | + "note": { |
| 71 | + Optional: true, |
| 72 | + Type: schema.TypeString, |
| 73 | + Description: "Subject note, which is a string of no more than 64 characters, must start with a letter, and the rest can contain letters, numbers and dashes (-).", |
| 74 | + }, |
| 75 | + |
| 76 | + "tags": { |
| 77 | + Type: schema.TypeMap, |
| 78 | + Optional: true, |
| 79 | + Description: "Tags of dataHub topic.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func resourceTencentCloudCkafkaDatahubTopicCreate(d *schema.ResourceData, meta interface{}) error { |
| 86 | + defer logElapsed("resource.tencentcloud_ckafka_datahub_topic.create")() |
| 87 | + defer inconsistentCheck(d, meta)() |
| 88 | + |
| 89 | + logId := getLogId(contextNil) |
| 90 | + |
| 91 | + var ( |
| 92 | + request = ckafka.NewCreateDatahubTopicRequest() |
| 93 | + topicName string |
| 94 | + ) |
| 95 | + if v, ok := d.GetOk("name"); ok { |
| 96 | + topicName = v.(string) |
| 97 | + request.Name = helper.String(v.(string)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOkExists("partition_num"); ok { |
| 101 | + request.PartitionNum = helper.IntInt64(v.(int)) |
| 102 | + } |
| 103 | + |
| 104 | + if v, ok := d.GetOkExists("retention_ms"); ok { |
| 105 | + request.RetentionMs = helper.IntInt64(v.(int)) |
| 106 | + } |
| 107 | + |
| 108 | + if v, ok := d.GetOk("note"); ok { |
| 109 | + request.Note = helper.String(v.(string)) |
| 110 | + } |
| 111 | + |
| 112 | + if v := helper.GetTags(d, "tags"); len(v) > 0 { |
| 113 | + for tagKey, tagValue := range v { |
| 114 | + tagInfo := ckafka.Tag{ |
| 115 | + TagKey: helper.String(tagKey), |
| 116 | + TagValue: helper.String(tagValue), |
| 117 | + } |
| 118 | + request.Tags = append(request.Tags, &tagInfo) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 123 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCkafkaClient().CreateDatahubTopic(request) |
| 124 | + if e != nil { |
| 125 | + return retryError(e) |
| 126 | + } else { |
| 127 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 128 | + } |
| 129 | + return nil |
| 130 | + }) |
| 131 | + if err != nil { |
| 132 | + log.Printf("[CRITAL]%s create ckafka datahubTopic failed, reason:%+v", logId, err) |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + d.SetId(topicName) |
| 137 | + |
| 138 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 139 | + |
| 140 | + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { |
| 141 | + tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 142 | + region := meta.(*TencentCloudClient).apiV3Conn.Region |
| 143 | + resourceName := fmt.Sprintf("qcs::ckafka:%s:uin/:dipTopic/%s", region, topicName) |
| 144 | + if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return resourceTencentCloudCkafkaDatahubTopicRead(d, meta) |
| 150 | +} |
| 151 | + |
| 152 | +func resourceTencentCloudCkafkaDatahubTopicRead(d *schema.ResourceData, meta interface{}) error { |
| 153 | + defer logElapsed("resource.tencentcloud_ckafka_datahub_topic.read")() |
| 154 | + defer inconsistentCheck(d, meta)() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + |
| 158 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 159 | + |
| 160 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 161 | + |
| 162 | + topicName := d.Id() |
| 163 | + |
| 164 | + datahubTopic, err := service.DescribeCkafkaDatahubTopicById(ctx, topicName) |
| 165 | + if err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + if datahubTopic == nil { |
| 170 | + d.SetId("") |
| 171 | + log.Printf("[WARN]%s resource `CkafkaDatahubTopic` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 172 | + return nil |
| 173 | + } |
| 174 | + |
| 175 | + if datahubTopic.Name != nil { |
| 176 | + _ = d.Set("name", datahubTopic.Name) |
| 177 | + } |
| 178 | + |
| 179 | + if datahubTopic.PartitionNum != nil { |
| 180 | + _ = d.Set("partition_num", datahubTopic.PartitionNum) |
| 181 | + } |
| 182 | + |
| 183 | + if datahubTopic.RetentionMs != nil { |
| 184 | + _ = d.Set("retention_ms", datahubTopic.RetentionMs) |
| 185 | + } |
| 186 | + |
| 187 | + if datahubTopic.Note != nil { |
| 188 | + _ = d.Set("note", datahubTopic.Note) |
| 189 | + } |
| 190 | + |
| 191 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 192 | + tagService := &TagService{client: tcClient} |
| 193 | + tags, err := tagService.DescribeResourceTags(ctx, "ckafka", "dipTopic", tcClient.Region, d.Id()) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + _ = d.Set("tags", tags) |
| 198 | + |
| 199 | + return nil |
| 200 | +} |
| 201 | + |
| 202 | +func resourceTencentCloudCkafkaDatahubTopicUpdate(d *schema.ResourceData, meta interface{}) error { |
| 203 | + defer logElapsed("resource.tencentcloud_ckafka_datahub_topic.update")() |
| 204 | + defer inconsistentCheck(d, meta)() |
| 205 | + |
| 206 | + logId := getLogId(contextNil) |
| 207 | + |
| 208 | + request := ckafka.NewModifyDatahubTopicRequest() |
| 209 | + |
| 210 | + topicName := d.Id() |
| 211 | + |
| 212 | + request.Name = &topicName |
| 213 | + |
| 214 | + immutableArgs := []string{"partition_num"} |
| 215 | + |
| 216 | + for _, v := range immutableArgs { |
| 217 | + if d.HasChange(v) { |
| 218 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + needChange := false |
| 223 | + mutableArgs := []string{"retention_ms", "note"} |
| 224 | + |
| 225 | + for _, v := range mutableArgs { |
| 226 | + if d.HasChange(v) { |
| 227 | + needChange = true |
| 228 | + break |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + if needChange { |
| 233 | + |
| 234 | + if v, ok := d.GetOkExists("retention_ms"); ok { |
| 235 | + request.RetentionMs = helper.IntInt64(v.(int)) |
| 236 | + } |
| 237 | + |
| 238 | + if v, ok := d.GetOk("note"); ok { |
| 239 | + request.Note = helper.String(v.(string)) |
| 240 | + } |
| 241 | + |
| 242 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 243 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCkafkaClient().ModifyDatahubTopic(request) |
| 244 | + if e != nil { |
| 245 | + return retryError(e) |
| 246 | + } else { |
| 247 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 248 | + } |
| 249 | + return nil |
| 250 | + }) |
| 251 | + if err != nil { |
| 252 | + log.Printf("[CRITAL]%s update ckafka datahubTopic failed, reason:%+v", logId, err) |
| 253 | + return err |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 258 | + |
| 259 | + if d.HasChange("tags") { |
| 260 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 261 | + tagService := &TagService{client: tcClient} |
| 262 | + oldTags, newTags := d.GetChange("tags") |
| 263 | + replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{})) |
| 264 | + resourceName := BuildTagResourceName("ckafka", "dipTopic", tcClient.Region, d.Id()) |
| 265 | + if err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err != nil { |
| 266 | + return err |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + return resourceTencentCloudCkafkaDatahubTopicRead(d, meta) |
| 271 | +} |
| 272 | + |
| 273 | +func resourceTencentCloudCkafkaDatahubTopicDelete(d *schema.ResourceData, meta interface{}) error { |
| 274 | + defer logElapsed("resource.tencentcloud_ckafka_datahub_topic.delete")() |
| 275 | + defer inconsistentCheck(d, meta)() |
| 276 | + |
| 277 | + logId := getLogId(contextNil) |
| 278 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 279 | + |
| 280 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 281 | + topicName := d.Id() |
| 282 | + |
| 283 | + if err := service.DeleteCkafkaDatahubTopicById(ctx, topicName); err != nil { |
| 284 | + return err |
| 285 | + } |
| 286 | + |
| 287 | + return nil |
| 288 | +} |
0 commit comments