|
| 1 | +package tpulsar |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + |
| 10 | + svctdmq "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdmq" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 14 | + tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217" |
| 15 | + |
| 16 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 17 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 18 | +) |
| 19 | + |
| 20 | +func ResourceTencentCloudTdmqSubscription() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Create: resourceTencentCloudTdmqSubscriptionCreate, |
| 23 | + Read: resourceTencentCloudTdmqSubscriptionRead, |
| 24 | + Delete: resourceTencentCloudTdmqSubscriptionDelete, |
| 25 | + Importer: &schema.ResourceImporter{ |
| 26 | + State: schema.ImportStatePassthrough, |
| 27 | + }, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "cluster_id": { |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + Type: schema.TypeString, |
| 33 | + Description: "Pulsar cluster ID.", |
| 34 | + }, |
| 35 | + "environment_id": { |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "Environment (namespace) name.", |
| 40 | + }, |
| 41 | + "topic_name": { |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "Topic name.", |
| 46 | + }, |
| 47 | + "subscription_name": { |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + Type: schema.TypeString, |
| 51 | + Description: "Subscriber name, which can contain up to 128 characters.", |
| 52 | + }, |
| 53 | + "remark": { |
| 54 | + Optional: true, |
| 55 | + ForceNew: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Remarks (up to 128 characters).", |
| 58 | + }, |
| 59 | + "auto_create_policy_topic": { |
| 60 | + Optional: true, |
| 61 | + ForceNew: true, |
| 62 | + Type: schema.TypeBool, |
| 63 | + Default: false, |
| 64 | + Description: "Whether to automatically create a dead letter topic and a retry letter topic. true: yes; false: no(default value).", |
| 65 | + }, |
| 66 | + "auto_delete_policy_topic": { |
| 67 | + Optional: true, |
| 68 | + ForceNew: true, |
| 69 | + Type: schema.TypeBool, |
| 70 | + Default: false, |
| 71 | + Description: "Whether to automatically delete a dead letter topic and a retry letter topic. Setting is only allowed when `auto_create_policy_topic` is true. Default is false.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func resourceTencentCloudTdmqSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { |
| 78 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.create")() |
| 79 | + defer tccommon.InconsistentCheck(d, meta)() |
| 80 | + |
| 81 | + var ( |
| 82 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 83 | + request = tdmq.NewCreateSubscriptionRequest() |
| 84 | + clusterId string |
| 85 | + environmentId string |
| 86 | + topicName string |
| 87 | + subscriptionName string |
| 88 | + autoCreatePolicyTopic bool |
| 89 | + autoDeletePolicyTopic string |
| 90 | + ) |
| 91 | + |
| 92 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 93 | + request.ClusterId = helper.String(v.(string)) |
| 94 | + clusterId = v.(string) |
| 95 | + } |
| 96 | + |
| 97 | + if v, ok := d.GetOk("environment_id"); ok { |
| 98 | + request.EnvironmentId = helper.String(v.(string)) |
| 99 | + environmentId = v.(string) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("topic_name"); ok { |
| 103 | + request.TopicName = helper.String(v.(string)) |
| 104 | + topicName = v.(string) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOk("subscription_name"); ok { |
| 108 | + request.SubscriptionName = helper.String(v.(string)) |
| 109 | + subscriptionName = v.(string) |
| 110 | + } |
| 111 | + |
| 112 | + if v, ok := d.GetOk("remark"); ok { |
| 113 | + request.Remark = helper.String(v.(string)) |
| 114 | + } |
| 115 | + |
| 116 | + if v, ok := d.GetOkExists("auto_create_policy_topic"); ok { |
| 117 | + request.AutoCreatePolicyTopic = helper.Bool(v.(bool)) |
| 118 | + autoCreatePolicyTopic = v.(bool) |
| 119 | + |
| 120 | + if v, ok = d.GetOkExists("auto_delete_policy_topic"); ok { |
| 121 | + if !autoCreatePolicyTopic && v.(bool) { |
| 122 | + return errors.New("If `auto_create_policy_topic` is false, Can't set `auto_delete_policy_topic` param.") |
| 123 | + } else { |
| 124 | + if v.(bool) { |
| 125 | + autoDeletePolicyTopic = "true" |
| 126 | + } else { |
| 127 | + autoDeletePolicyTopic = "false" |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + request.IsIdempotent = helper.Bool(false) |
| 134 | + |
| 135 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 136 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTdmqClient().CreateSubscription(request) |
| 137 | + if e != nil { |
| 138 | + return tccommon.RetryError(e) |
| 139 | + } else { |
| 140 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 141 | + } |
| 142 | + |
| 143 | + if result == nil || !*result.Response.Result { |
| 144 | + e = fmt.Errorf("create tdmq subscription failed") |
| 145 | + return resource.NonRetryableError(e) |
| 146 | + } |
| 147 | + |
| 148 | + return nil |
| 149 | + }) |
| 150 | + |
| 151 | + if err != nil { |
| 152 | + log.Printf("[CRITAL]%s create tdmq subscription failed, reason:%+v", logId, err) |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + d.SetId(strings.Join([]string{clusterId, environmentId, topicName, subscriptionName, autoDeletePolicyTopic}, tccommon.FILED_SP)) |
| 157 | + |
| 158 | + return resourceTencentCloudTdmqSubscriptionRead(d, meta) |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudTdmqSubscriptionRead(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.read")() |
| 163 | + defer tccommon.InconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + var ( |
| 166 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 167 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 168 | + service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 169 | + ) |
| 170 | + |
| 171 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 172 | + |
| 173 | + if len(idSplit) != 5 { |
| 174 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 175 | + } |
| 176 | + |
| 177 | + clusterId := idSplit[0] |
| 178 | + environmentId := idSplit[1] |
| 179 | + topicName := idSplit[2] |
| 180 | + subscriptionName := idSplit[3] |
| 181 | + autoDeletePolicyTopic := idSplit[4] |
| 182 | + |
| 183 | + subscription, err := service.DescribeTdmqSubscriptionById(ctx, clusterId, environmentId, topicName, subscriptionName) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + if subscription == nil { |
| 189 | + d.SetId("") |
| 190 | + log.Printf("[WARN]%s resource `TdmqSubscription` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 191 | + return nil |
| 192 | + } |
| 193 | + |
| 194 | + _ = d.Set("environment_id", subscription.EnvironmentId) |
| 195 | + _ = d.Set("topic_name", subscription.TopicName) |
| 196 | + _ = d.Set("subscription_name", subscriptionName) |
| 197 | + _ = d.Set("cluster_id", clusterId) |
| 198 | + _ = d.Set("remark", subscription.Remark) |
| 199 | + |
| 200 | + if autoDeletePolicyTopic == "true" { |
| 201 | + _ = d.Set("auto_delete_policy_topic", true) |
| 202 | + } else { |
| 203 | + _ = d.Set("auto_delete_policy_topic", false) |
| 204 | + } |
| 205 | + |
| 206 | + // Get Topics Status For auto_create_policy_topic |
| 207 | + has, err := service.GetTdmqTopicsAttachmentById(ctx, environmentId, topicName, subscriptionName, clusterId) |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + _ = d.Set("auto_create_policy_topic", has) |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func resourceTencentCloudTdmqSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { |
| 217 | + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.delete")() |
| 218 | + defer tccommon.InconsistentCheck(d, meta)() |
| 219 | + |
| 220 | + var ( |
| 221 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 222 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 223 | + service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) |
| 224 | + ) |
| 225 | + |
| 226 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 227 | + if len(idSplit) != 5 { |
| 228 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 229 | + } |
| 230 | + |
| 231 | + clusterId := idSplit[0] |
| 232 | + environmentId := idSplit[1] |
| 233 | + topicName := idSplit[2] |
| 234 | + subscriptionName := idSplit[3] |
| 235 | + autoDeletePolicyTopic := idSplit[4] |
| 236 | + |
| 237 | + if err := service.DeleteTdmqSubscriptionById(ctx, clusterId, environmentId, topicName, subscriptionName); err != nil { |
| 238 | + return err |
| 239 | + } |
| 240 | + |
| 241 | + if autoDeletePolicyTopic == "true" { |
| 242 | + // Delete Topics |
| 243 | + if err := service.DeleteTdmqTopicsAttachmentById(ctx, environmentId, topicName, subscriptionName, clusterId); err != nil { |
| 244 | + return err |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + return nil |
| 249 | +} |
0 commit comments