|
| 1 | +package tpulsar |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 7 | + svctdmq "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdmq" |
| 8 | + svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc" |
| 9 | + |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 16 | +) |
| 17 | + |
| 18 | +func ResourceTencentCloudTdmqTopicWithFullId() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Create: resourceTencentCloudTdmqTopicWithFullIdCreate, |
| 21 | + Read: resourceTencentCloudTdmqTopicWithFullIdRead, |
| 22 | + Update: resourceTencentCloudTdmqTopicWithFullIdUpdate, |
| 23 | + Delete: resourceTencentCloudTdmqTopicWithFullIdDelete, |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + State: schema.ImportStatePassthrough, |
| 26 | + }, |
| 27 | + |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "environ_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + Description: "The name of tdmq namespace.", |
| 34 | + }, |
| 35 | + "topic_name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ForceNew: true, |
| 39 | + Description: "The name of topic to be created.", |
| 40 | + }, |
| 41 | + "partitions": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Required: true, |
| 44 | + Description: "The partitions of topic.", |
| 45 | + }, |
| 46 | + "topic_type": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Optional: true, |
| 49 | + Computed: true, |
| 50 | + Deprecated: "This input will be gradually discarded and can be switched to PulsarTopicType parameter 0: Normal message; 1: Global sequential messages; 2: Local sequential messages; 3: Retrying queue; 4: Dead letter queue.", |
| 51 | + Description: "The type of topic.", |
| 52 | + }, |
| 53 | + "cluster_id": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + Description: "The Dedicated Cluster Id.", |
| 57 | + }, |
| 58 | + "pulsar_topic_type": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Optional: true, |
| 61 | + Computed: true, |
| 62 | + ConflictsWith: []string{"topic_type"}, |
| 63 | + Description: "Pulsar Topic Type 0: Non-persistent non-partitioned 1: Non-persistent partitioned 2: Persistent non-partitioned 3: Persistent partitioned.", |
| 64 | + }, |
| 65 | + "remark": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + Description: "Description of the namespace.", |
| 69 | + }, |
| 70 | + |
| 71 | + //compute |
| 72 | + "create_time": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Creation time of resource.", |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func resourceTencentCloudTdmqTopicWithFullIdCreate(d *schema.ResourceData, meta interface{}) error { |
| 82 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_topic_with_full_id.create")() |
| 83 | + |
| 84 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 85 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 86 | + |
| 87 | + tdmqService := svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 88 | + |
| 89 | + var ( |
| 90 | + environId string |
| 91 | + topicName string |
| 92 | + partitions uint64 |
| 93 | + topicType int64 |
| 94 | + remark string |
| 95 | + clusterId string |
| 96 | + pulsarTopicType int64 |
| 97 | + ) |
| 98 | + if temp, ok := d.GetOk("environ_id"); ok { |
| 99 | + environId = temp.(string) |
| 100 | + if len(environId) < 1 { |
| 101 | + return fmt.Errorf("environ_id should be not empty string") |
| 102 | + } |
| 103 | + } |
| 104 | + if temp, ok := d.GetOk("topic_name"); ok { |
| 105 | + topicName = temp.(string) |
| 106 | + if len(topicName) < 1 { |
| 107 | + return fmt.Errorf("topic_name should be not empty string") |
| 108 | + } |
| 109 | + } |
| 110 | + partitions = uint64(d.Get("partitions").(int)) |
| 111 | + if temp, ok := d.GetOk("remark"); ok { |
| 112 | + remark = temp.(string) |
| 113 | + } |
| 114 | + if temp, ok := d.GetOk("cluster_id"); ok { |
| 115 | + clusterId = temp.(string) |
| 116 | + } |
| 117 | + |
| 118 | + if v, ok := d.GetOkExists("pulsar_topic_type"); ok { |
| 119 | + pulsarTopicType = int64(v.(int)) |
| 120 | + } else { |
| 121 | + pulsarTopicType = svctdmq.NonePulsarTopicType |
| 122 | + if v, ok := d.GetOkExists("topic_type"); ok { |
| 123 | + topicType = int64(v.(int)) |
| 124 | + } else { |
| 125 | + topicType = svctdmq.NoneTopicType |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + err := tdmqService.CreateTdmqTopic(ctx, environId, topicName, partitions, topicType, remark, clusterId, pulsarTopicType) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + d.SetId(clusterId + tccommon.FILED_SP + environId + tccommon.FILED_SP + topicName) |
| 134 | + |
| 135 | + return resourceTencentCloudTdmqTopicWithFullIdRead(d, meta) |
| 136 | +} |
| 137 | + |
| 138 | +func resourceTencentCloudTdmqTopicWithFullIdRead(d *schema.ResourceData, meta interface{}) error { |
| 139 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_topic_with_full_id.read")() |
| 140 | + defer tccommon.InconsistentCheck(d, meta)() |
| 141 | + |
| 142 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 143 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 144 | + |
| 145 | + items := strings.Split(d.Id(), tccommon.FILED_SP) |
| 146 | + if len(items) != 3 { |
| 147 | + return fmt.Errorf("invalid ID %s", d.Id()) |
| 148 | + } |
| 149 | + clusterId := items[0] |
| 150 | + environId := items[1] |
| 151 | + topicName := items[2] |
| 152 | + |
| 153 | + tdmqService := svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 154 | + |
| 155 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 156 | + info, has, e := tdmqService.DescribeTdmqTopicById(ctx, environId, topicName, clusterId) |
| 157 | + if e != nil { |
| 158 | + return tccommon.RetryError(e) |
| 159 | + } |
| 160 | + if !has { |
| 161 | + d.SetId("") |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + _ = d.Set("cluster_id", clusterId) |
| 166 | + _ = d.Set("environ_id", environId) |
| 167 | + _ = d.Set("topic_name", topicName) |
| 168 | + _ = d.Set("partitions", info.Partitions) |
| 169 | + _ = d.Set("topic_type", info.TopicType) |
| 170 | + _ = d.Set("pulsar_topic_type", info.PulsarTopicType) |
| 171 | + _ = d.Set("remark", info.Remark) |
| 172 | + _ = d.Set("create_time", info.CreateTime) |
| 173 | + return nil |
| 174 | + }) |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +func resourceTencentCloudTdmqTopicWithFullIdUpdate(d *schema.ResourceData, meta interface{}) error { |
| 182 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_topic_with_full_id.update")() |
| 183 | + |
| 184 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 185 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 186 | + |
| 187 | + if d.HasChange("topic_type") { |
| 188 | + return fmt.Errorf("`topic_type` do not support change now.") |
| 189 | + } |
| 190 | + |
| 191 | + items := strings.Split(d.Id(), tccommon.FILED_SP) |
| 192 | + if len(items) != 3 { |
| 193 | + return fmt.Errorf("invalid ID %s", d.Id()) |
| 194 | + } |
| 195 | + clusterId := items[0] |
| 196 | + environId := items[1] |
| 197 | + topicName := items[2] |
| 198 | + |
| 199 | + service := svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 200 | + |
| 201 | + var ( |
| 202 | + partitions uint64 |
| 203 | + remark string |
| 204 | + ) |
| 205 | + old, now := d.GetChange("partitions") |
| 206 | + if d.HasChange("partitions") { |
| 207 | + partitions = uint64(now.(int)) |
| 208 | + } else { |
| 209 | + partitions = uint64(old.(int)) |
| 210 | + } |
| 211 | + |
| 212 | + old, now = d.GetChange("remark") |
| 213 | + if d.HasChange("remark") { |
| 214 | + remark = now.(string) |
| 215 | + } else { |
| 216 | + remark = old.(string) |
| 217 | + } |
| 218 | + |
| 219 | + d.Partial(true) |
| 220 | + |
| 221 | + if err := service.ModifyTdmqTopicAttribute(ctx, environId, topicName, |
| 222 | + partitions, remark, clusterId); err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + d.Partial(false) |
| 226 | + return resourceTencentCloudTdmqTopicWithFullIdRead(d, meta) |
| 227 | +} |
| 228 | + |
| 229 | +func resourceTencentCloudTdmqTopicWithFullIdDelete(d *schema.ResourceData, meta interface{}) error { |
| 230 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_topic_with_full_id.delete")() |
| 231 | + |
| 232 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 233 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 234 | + |
| 235 | + service := svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 236 | + |
| 237 | + items := strings.Split(d.Id(), tccommon.FILED_SP) |
| 238 | + if len(items) != 3 { |
| 239 | + return fmt.Errorf("invalid ID %s", d.Id()) |
| 240 | + } |
| 241 | + clusterId := items[0] |
| 242 | + environId := items[1] |
| 243 | + topicName := items[2] |
| 244 | + |
| 245 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 246 | + if err := service.DeleteTdmqTopic(ctx, environId, topicName, clusterId); err != nil { |
| 247 | + if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok { |
| 248 | + if sdkErr.Code == svcvpc.VPCNotFound { |
| 249 | + return nil |
| 250 | + } |
| 251 | + } |
| 252 | + return resource.RetryableError(err) |
| 253 | + } |
| 254 | + return nil |
| 255 | + }) |
| 256 | + |
| 257 | + return err |
| 258 | +} |
0 commit comments