|
| 1 | +/* |
| 2 | +Provides a resource to create a CLB instance topic. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_clb_log_topic" "topic" { |
| 8 | + topic_name="clb-topic" |
| 9 | + partition_count=3 |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +CLB log topic can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +$ terraform import tencentcloud_clb_log_topic.topic lb-7a0t6zqb |
| 19 | +*/ |
| 20 | +package tencentcloud |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "log" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | +) |
| 29 | + |
| 30 | +var clsActionMu = &sync.Mutex{} |
| 31 | + |
| 32 | +func resourceTencentCloudClbLogTopic() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Create: resourceTencentCloudClbInstanceTopicCreate, |
| 35 | + Read: resourceTencentCloudClbInstanceTopicRead, |
| 36 | + Update: resourceTencentCloudClbInstanceTopicUpdate, |
| 37 | + Delete: resourceTencentCloudClbInstanceTopicDelete, |
| 38 | + Importer: &schema.ResourceImporter{ |
| 39 | + State: schema.ImportStatePassthrough, |
| 40 | + }, |
| 41 | + |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "topic_name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: "Log topic of CLB instance.", |
| 47 | + }, |
| 48 | + "partition_count": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Optional: true, |
| 51 | + ValidateFunc: validateIntegerInRange(1, 10), |
| 52 | + Description: "Topic partition count of CLB instance.(Default 1).", |
| 53 | + }, |
| 54 | + "limit": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + Description: "Fetch topic info pagination limit.", |
| 58 | + }, |
| 59 | + "offset": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Optional: true, |
| 62 | + Description: "Fetch topic info pagination offset.", |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func resourceTencentCloudClbInstanceTopicRead(d *schema.ResourceData, meta interface{}) error { |
| 69 | + clsActionMu.Lock() |
| 70 | + defer clsActionMu.Unlock() |
| 71 | + logId := getLogId(contextNil) |
| 72 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 73 | + var topicName string |
| 74 | + if value, ok := d.GetOk("topic_name"); ok { |
| 75 | + topicName = value.(string) |
| 76 | + } |
| 77 | + clsService := ClsService{ |
| 78 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 79 | + } |
| 80 | + res, ok := clsService.DescribeTopicsByTopicName(ctx, topicName) |
| 81 | + if ok != nil { |
| 82 | + return ok |
| 83 | + } |
| 84 | + _ = d.Set("logset_id", res.LogsetId) |
| 85 | + _ = d.Set("topic_id", res.TopicId) |
| 86 | + _ = d.Set("topic_name", res.TopicName) |
| 87 | + _ = d.Set("partition_count", res.PartitionCount) |
| 88 | + _ = d.Set("index", res.Index) |
| 89 | + _ = d.Set("create_time", res.CreateTime) |
| 90 | + _ = d.Set("status", res.Status) |
| 91 | + _ = d.Set("tags", res.Tags) |
| 92 | + _ = d.Set("auto_split", res.AutoSplit) |
| 93 | + _ = d.Set("max_split_partitions", res.MaxSplitPartitions) |
| 94 | + _ = d.Set("storage_type", res.StorageType) |
| 95 | + _ = d.Set("period", res.Period) |
| 96 | + return nil |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudClbInstanceTopicUpdate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func resourceTencentCloudClbInstanceTopicDelete(d *schema.ResourceData, meta interface{}) error { |
| 105 | + clsActionMu.Lock() |
| 106 | + defer clsActionMu.Unlock() |
| 107 | + logId := getLogId(contextNil) |
| 108 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 109 | + var topicName string |
| 110 | + if value, ok := d.GetOk("topic_name"); ok { |
| 111 | + topicName = value.(string) |
| 112 | + } |
| 113 | + clsService := ClsService{ |
| 114 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 115 | + } |
| 116 | + _, ok := clsService.DeleteTopicsByTopicName(ctx, topicName) |
| 117 | + if ok != nil { |
| 118 | + return ok |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceTencentCloudClbInstanceTopicCreate(d *schema.ResourceData, meta interface{}) error { |
| 124 | + logId := getLogId(contextNil) |
| 125 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 126 | + params := make(map[string]interface{}) |
| 127 | + if topicName, ok := d.GetOk("topic_name"); ok { |
| 128 | + params["topic_name"] = topicName |
| 129 | + } |
| 130 | + if partitionCount, ok := d.GetOk("partition_count"); ok { |
| 131 | + params["partition_count"] = partitionCount |
| 132 | + } |
| 133 | + clbService := ClbService{ |
| 134 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 135 | + } |
| 136 | + resp, err := clbService.CreateTopic(ctx, params) |
| 137 | + d.SetId(*resp.Response.TopicId) |
| 138 | + if err != nil { |
| 139 | + log.Printf("[CRITAL]%s create CLB topic failed, reason:%+v", logId, err) |
| 140 | + return err |
| 141 | + } |
| 142 | + return resourceTencentCloudClbInstanceTopicRead(d, meta) |
| 143 | +} |
0 commit comments