diff --git a/.changelog/2451.txt b/.changelog/2451.txt new file mode 100644 index 0000000000..0ecb490e92 --- /dev/null +++ b/.changelog/2451.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +tencentcloud_tdmq_subscription +``` \ No newline at end of file diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 1835b20e2a..1b83e25a95 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -1463,12 +1463,12 @@ func Provider() *schema.Provider { "tencentcloud_tdmq_topic": tpulsar.ResourceTencentCloudTdmqTopic(), "tencentcloud_tdmq_role": tpulsar.ResourceTencentCloudTdmqRole(), "tencentcloud_tdmq_namespace_role_attachment": tpulsar.ResourceTencentCloudTdmqNamespaceRoleAttachment(), - "tencentcloud_tdmq_subscription_attachment": tpulsar.ResourceTencentCloudTdmqSubscriptionAttachment(), "tencentcloud_tdmq_rabbitmq_user": trabbit.ResourceTencentCloudTdmqRabbitmqUser(), "tencentcloud_tdmq_rabbitmq_virtual_host": trabbit.ResourceTencentCloudTdmqRabbitmqVirtualHost(), "tencentcloud_tdmq_rabbitmq_vip_instance": trabbit.ResourceTencentCloudTdmqRabbitmqVipInstance(), "tencentcloud_tdmq_send_rocketmq_message": trocket.ResourceTencentCloudTdmqSendRocketmqMessage(), "tencentcloud_tdmq_professional_cluster": tpulsar.ResourceTencentCloudTdmqProfessionalCluster(), + "tencentcloud_tdmq_subscription": tpulsar.ResourceTencentCloudTdmqSubscription(), "tencentcloud_cos_bucket_policy": cos.ResourceTencentCloudCosBucketPolicy(), "tencentcloud_cos_bucket_domain_certificate_attachment": cos.ResourceTencentCloudCosBucketDomainCertificateAttachment(), "tencentcloud_cos_bucket_inventory": cos.ResourceTencentCloudCosBucketInventory(), diff --git a/tencentcloud/provider.md b/tencentcloud/provider.md index 51842e94c0..032f5b627e 100644 --- a/tencentcloud/provider.md +++ b/tencentcloud/provider.md @@ -683,6 +683,7 @@ TDMQ for Pulsar(tpulsar) tencentcloud_tdmq_topic tencentcloud_tdmq_role tencentcloud_tdmq_namespace_role_attachment + tencentcloud_tdmq_subscription TencentDB for MongoDB(mongodb) Data Source diff --git a/tencentcloud/services/tdmq/service_tencentcloud_tdmq.go b/tencentcloud/services/tdmq/service_tencentcloud_tdmq.go index e70ec97f97..29256b51be 100644 --- a/tencentcloud/services/tdmq/service_tencentcloud_tdmq.go +++ b/tencentcloud/services/tdmq/service_tencentcloud_tdmq.go @@ -920,6 +920,39 @@ func (me *TdmqService) DescribeTdmqSubscriptionAttachmentById(ctx context.Contex return } +func (me *TdmqService) DescribeTdmqSubscriptionById(ctx context.Context, clusterId, environmentId, topicName, subscriptionName string) (subscriptionAttachment *tdmq.Subscription, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := tdmq.NewDescribeSubscriptionsRequest() + request.ClusterId = &clusterId + request.EnvironmentId = &environmentId + request.TopicName = &topicName + request.SubscriptionName = &subscriptionName + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTdmqClient().DescribeSubscriptions(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if *response.Response.TotalCount == 0 { + return + } + + subscriptionAttachment = response.Response.SubscriptionSets[0] + return +} + func (me *TdmqService) GetTdmqTopicsAttachmentById(ctx context.Context, environmentId, Topic, subscriptionName, clusterId string) (has bool, errRet error) { logId := tccommon.GetLogId(ctx) @@ -1032,6 +1065,39 @@ func (me *TdmqService) DeleteTdmqSubscriptionAttachmentById(ctx context.Context, return } +func (me *TdmqService) DeleteTdmqSubscriptionById(ctx context.Context, clusterId, environmentId, topicName, subscriptionName string) (errRet error) { + logId := tccommon.GetLogId(ctx) + + request := tdmq.NewDeleteSubscriptionsRequest() + request.SubscriptionTopicSets = []*tdmq.SubscriptionTopic{ + { + EnvironmentId: &environmentId, + TopicName: &topicName, + SubscriptionName: &subscriptionName, + }, + } + request.ClusterId = &clusterId + request.EnvironmentId = &environmentId + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseTdmqClient().DeleteSubscriptions(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + return +} + func (me *TdmqService) DescribeTdmqDeadLetterSourceQueueByFilter(ctx context.Context, param map[string]interface{}) (deadLetterSourceQueue []*tdmq.CmqDeadLetterSource, errRet error) { var ( logId = tccommon.GetLogId(ctx) diff --git a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.go b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.go new file mode 100644 index 0000000000..fdd9d45c1f --- /dev/null +++ b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.go @@ -0,0 +1,249 @@ +package tpulsar + +import ( + "context" + "errors" + "fmt" + "log" + "strings" + + svctdmq "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdmq" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudTdmqSubscription() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudTdmqSubscriptionCreate, + Read: resourceTencentCloudTdmqSubscriptionRead, + Delete: resourceTencentCloudTdmqSubscriptionDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "cluster_id": { + Required: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Pulsar cluster ID.", + }, + "environment_id": { + Required: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Environment (namespace) name.", + }, + "topic_name": { + Required: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Topic name.", + }, + "subscription_name": { + Required: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Subscriber name, which can contain up to 128 characters.", + }, + "remark": { + Optional: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Remarks (up to 128 characters).", + }, + "auto_create_policy_topic": { + Optional: true, + ForceNew: true, + Type: schema.TypeBool, + Default: false, + Description: "Whether to automatically create a dead letter topic and a retry letter topic. true: yes; false: no(default value).", + }, + "auto_delete_policy_topic": { + Optional: true, + ForceNew: true, + Type: schema.TypeBool, + Default: false, + 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.", + }, + }, + } +} + +func resourceTencentCloudTdmqSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.create")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + request = tdmq.NewCreateSubscriptionRequest() + clusterId string + environmentId string + topicName string + subscriptionName string + autoCreatePolicyTopic bool + autoDeletePolicyTopic string + ) + + if v, ok := d.GetOk("cluster_id"); ok { + request.ClusterId = helper.String(v.(string)) + clusterId = v.(string) + } + + if v, ok := d.GetOk("environment_id"); ok { + request.EnvironmentId = helper.String(v.(string)) + environmentId = v.(string) + } + + if v, ok := d.GetOk("topic_name"); ok { + request.TopicName = helper.String(v.(string)) + topicName = v.(string) + } + + if v, ok := d.GetOk("subscription_name"); ok { + request.SubscriptionName = helper.String(v.(string)) + subscriptionName = v.(string) + } + + if v, ok := d.GetOk("remark"); ok { + request.Remark = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("auto_create_policy_topic"); ok { + request.AutoCreatePolicyTopic = helper.Bool(v.(bool)) + autoCreatePolicyTopic = v.(bool) + + if v, ok = d.GetOkExists("auto_delete_policy_topic"); ok { + if !autoCreatePolicyTopic && v.(bool) { + return errors.New("If `auto_create_policy_topic` is false, Can't set `auto_delete_policy_topic` param.") + } else { + if v.(bool) { + autoDeletePolicyTopic = "true" + } else { + autoDeletePolicyTopic = "false" + } + } + } + } + + request.IsIdempotent = helper.Bool(false) + + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTdmqClient().CreateSubscription(request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + if result == nil || !*result.Response.Result { + e = fmt.Errorf("create tdmq subscription failed") + return resource.NonRetryableError(e) + } + + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s create tdmq subscription failed, reason:%+v", logId, err) + return err + } + + d.SetId(strings.Join([]string{clusterId, environmentId, topicName, subscriptionName, autoDeletePolicyTopic}, tccommon.FILED_SP)) + + return resourceTencentCloudTdmqSubscriptionRead(d, meta) +} + +func resourceTencentCloudTdmqSubscriptionRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.read")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) + ) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + + if len(idSplit) != 5 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + + clusterId := idSplit[0] + environmentId := idSplit[1] + topicName := idSplit[2] + subscriptionName := idSplit[3] + autoDeletePolicyTopic := idSplit[4] + + subscription, err := service.DescribeTdmqSubscriptionById(ctx, clusterId, environmentId, topicName, subscriptionName) + if err != nil { + return err + } + + if subscription == nil { + d.SetId("") + log.Printf("[WARN]%s resource `TdmqSubscription` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + + _ = d.Set("environment_id", subscription.EnvironmentId) + _ = d.Set("topic_name", subscription.TopicName) + _ = d.Set("subscription_name", subscriptionName) + _ = d.Set("cluster_id", clusterId) + _ = d.Set("remark", subscription.Remark) + + if autoDeletePolicyTopic == "true" { + _ = d.Set("auto_delete_policy_topic", true) + } else { + _ = d.Set("auto_delete_policy_topic", false) + } + + // Get Topics Status For auto_create_policy_topic + has, err := service.GetTdmqTopicsAttachmentById(ctx, environmentId, topicName, subscriptionName, clusterId) + if err != nil { + return err + } + + _ = d.Set("auto_create_policy_topic", has) + return nil +} + +func resourceTencentCloudTdmqSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) + ) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 5 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + + clusterId := idSplit[0] + environmentId := idSplit[1] + topicName := idSplit[2] + subscriptionName := idSplit[3] + autoDeletePolicyTopic := idSplit[4] + + if err := service.DeleteTdmqSubscriptionById(ctx, clusterId, environmentId, topicName, subscriptionName); err != nil { + return err + } + + if autoDeletePolicyTopic == "true" { + // Delete Topics + if err := service.DeleteTdmqTopicsAttachmentById(ctx, environmentId, topicName, subscriptionName, clusterId); err != nil { + return err + } + } + + return nil +} diff --git a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.md b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.md new file mode 100644 index 0000000000..b6b45bc3ed --- /dev/null +++ b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.md @@ -0,0 +1,51 @@ +Provides a resource to create a tdmq subscription + +Example Usage + +```hcl +resource "tencentcloud_tdmq_instance" "example" { + cluster_name = "tf_example" + remark = "remark." + tags = { + "createdBy" = "terraform" + } +} + +resource "tencentcloud_tdmq_namespace" "example" { + environ_name = "tf_example" + msg_ttl = 300 + cluster_id = tencentcloud_tdmq_instance.example.id + retention_policy { + time_in_minutes = 60 + size_in_mb = 10 + } + remark = "remark." +} + +resource "tencentcloud_tdmq_topic" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environ_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = "tf-example-topic" + partitions = 1 + pulsar_topic_type = 3 + remark = "remark." +} + +resource "tencentcloud_tdmq_subscription" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environment_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = tencentcloud_tdmq_topic.example.topic_name + subscription_name = "tf-example-subscription" + remark = "remark." + auto_create_policy_topic = true + auto_delete_policy_topic = true +} +``` + +Import + +tdmq subscription can be imported using the id, e.g. + +``` +terraform import tencentcloud_tdmq_subscription.example pulsar-q4k5898krpqj#tf_example#tf-example-topic#tf-example-subscription#true +``` diff --git a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment.go b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment.go deleted file mode 100644 index 3010c59e20..0000000000 --- a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment.go +++ /dev/null @@ -1,297 +0,0 @@ -/* -Provides a resource to create a tdmq subscription_attachment - -# Example Usage - -```hcl - - resource "tencentcloud_tdmq_instance" "example" { - cluster_name = "tf_example" - remark = "remark." - tags = { - "createdBy" = "terraform" - } - } - - resource "tencentcloud_tdmq_namespace" "example" { - environ_name = "tf_example" - msg_ttl = 300 - cluster_id = tencentcloud_tdmq_instance.example.id - retention_policy { - time_in_minutes = 60 - size_in_mb = 10 - } - remark = "remark." - } - - resource "tencentcloud_tdmq_topic" "example" { - environ_id = tencentcloud_tdmq_namespace.example.environ_name - cluster_id = tencentcloud_tdmq_instance.example.id - topic_name = "tf-example-topic" - partitions = 6 - pulsar_topic_type = 3 - remark = "remark." - } - - resource "tencentcloud_tdmq_subscription_attachment" "example" { - environment_id = tencentcloud_tdmq_namespace.example.environ_name - cluster_id = tencentcloud_tdmq_instance.example.id - topic_name = tencentcloud_tdmq_topic.example.topic_name - subscription_name = "tf-example-subcription" - remark = "remark." - auto_create_policy_topic = true - } - -``` - -# Import - -tdmq subscription_attachment can be imported using the id, e.g. - -``` -terraform import tencentcloud_tdmq_subscription_attachment.subscription_attachment subscription_attachment_id -``` -*/ -package tpulsar - -import ( - tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" - svctdmq "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdmq" - - "context" - "fmt" - "log" - "strings" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217" - - "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" -) - -func ResourceTencentCloudTdmqSubscriptionAttachment() *schema.Resource { - return &schema.Resource{ - Create: resourceTencentCloudTdmqSubscriptionAttachmentCreate, - Read: resourceTencentCloudTdmqSubscriptionAttachmentRead, - Update: resourceTencentCloudTdmqSubscriptionAttachmentUpdate, - Delete: resourceTencentCloudTdmqSubscriptionAttachmentDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - Schema: map[string]*schema.Schema{ - "environment_id": { - Required: true, - ForceNew: true, - Type: schema.TypeString, - Description: "Environment (namespace) name.", - }, - "topic_name": { - Required: true, - ForceNew: true, - Type: schema.TypeString, - Description: "topic name.", - }, - "subscription_name": { - Required: true, - ForceNew: true, - Type: schema.TypeString, - Description: "Subscriber name, no more than 128 characters.", - }, - "remark": { - Optional: true, - ForceNew: true, - Type: schema.TypeString, - Description: "Remarks, within 128 characters.", - }, - "cluster_id": { - Optional: true, - ForceNew: true, - Type: schema.TypeString, - Description: "ID of the Pulsar cluster.", - }, - "auto_create_policy_topic": { - Optional: true, - Type: schema.TypeBool, - Description: "Whether to automatically create dead letters and retry topics, True means to create, False means not to create, the default is to automatically create dead letters and retry topics.", - }, - }, - } -} - -func resourceTencentCloudTdmqSubscriptionAttachmentCreate(d *schema.ResourceData, meta interface{}) error { - defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription_attachment.create")() - defer tccommon.InconsistentCheck(d, meta)() - - var ( - logId = tccommon.GetLogId(tccommon.ContextNil) - request = tdmq.NewCreateSubscriptionRequest() - environmentId string - Topic string - subscriptionName string - clusterId string - autoCreatePolicyTopic bool - ) - - if v, ok := d.GetOk("environment_id"); ok { - request.EnvironmentId = helper.String(v.(string)) - environmentId = v.(string) - } - - if v, ok := d.GetOk("topic_name"); ok { - request.TopicName = helper.String(v.(string)) - Topic = v.(string) - } - - if v, ok := d.GetOk("subscription_name"); ok { - request.SubscriptionName = helper.String(v.(string)) - subscriptionName = v.(string) - } - - if v, ok := d.GetOk("is_idempotent"); ok { - request.IsIdempotent = helper.Bool(v.(bool)) - } - - if v, ok := d.GetOk("remark"); ok { - request.Remark = helper.String(v.(string)) - } - - if v, ok := d.GetOk("cluster_id"); ok { - request.ClusterId = helper.String(v.(string)) - clusterId = v.(string) - } - - if v, ok := d.GetOk("auto_create_policy_topic"); ok { - request.AutoCreatePolicyTopic = helper.Bool(v.(bool)) - autoCreatePolicyTopic = v.(bool) - } - - var isIdempotent = false - request.IsIdempotent = &isIdempotent - request.AutoCreatePolicyTopic = &autoCreatePolicyTopic - - err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { - result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTdmqClient().CreateSubscription(request) - if e != nil { - return tccommon.RetryError(e) - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } - - return nil - }) - - if err != nil { - log.Printf("[CRITAL]%s create tdmq subscriptionAttachment failed, reason:%+v", logId, err) - return err - } - - d.SetId(strings.Join([]string{environmentId, Topic, subscriptionName, clusterId}, tccommon.FILED_SP)) - - return resourceTencentCloudTdmqSubscriptionAttachmentRead(d, meta) -} - -func resourceTencentCloudTdmqSubscriptionAttachmentRead(d *schema.ResourceData, meta interface{}) error { - defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription_attachment.read")() - defer tccommon.InconsistentCheck(d, meta)() - - var ( - logId = tccommon.GetLogId(tccommon.ContextNil) - ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) - ) - - idSplit := strings.Split(d.Id(), tccommon.FILED_SP) - if len(idSplit) != 4 { - return fmt.Errorf("id is broken, id is %s", d.Id()) - } - environmentId := idSplit[0] - Topic := idSplit[1] - subscriptionName := idSplit[2] - clusterId := idSplit[3] - - subscriptionAttachment, err := service.DescribeTdmqSubscriptionAttachmentById(ctx, environmentId, Topic, subscriptionName, clusterId) - if err != nil { - return err - } - - if subscriptionAttachment == nil { - d.SetId("") - log.Printf("[WARN]%s resource `TdmqSubscriptionAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) - return nil - } - - if subscriptionAttachment.EnvironmentId != nil { - _ = d.Set("environment_id", subscriptionAttachment.EnvironmentId) - } - - if subscriptionAttachment.TopicName != nil { - _ = d.Set("topic_name", subscriptionAttachment.TopicName) - } - - if subscriptionAttachment.SubscriptionName != nil { - _ = d.Set("subscription_name", subscriptionAttachment.SubscriptionName) - } - - if subscriptionAttachment.Remark != nil { - _ = d.Set("remark", subscriptionAttachment.Remark) - } - - _ = d.Set("cluster_id", clusterId) - - // Get Topics Status For auto_create_policy_topic - has, err := service.GetTdmqTopicsAttachmentById(ctx, environmentId, Topic, subscriptionName, clusterId) - if err != nil { - return err - } - - _ = d.Set("auto_create_policy_topic", has) - - return nil -} - -func resourceTencentCloudTdmqSubscriptionAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { - defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription_attachment.update")() - defer tccommon.InconsistentCheck(d, meta)() - - return nil -} - -func resourceTencentCloudTdmqSubscriptionAttachmentDelete(d *schema.ResourceData, meta interface{}) error { - defer tccommon.LogElapsed("resource.tencentcloud_tdmq_subscription_attachment.delete")() - defer tccommon.InconsistentCheck(d, meta)() - - var ( - logId = tccommon.GetLogId(tccommon.ContextNil) - ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - service = svctdmq.NewTdmqService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) - autoCreatePolicyTopic bool - ) - - idSplit := strings.Split(d.Id(), tccommon.FILED_SP) - if len(idSplit) != 4 { - return fmt.Errorf("id is broken, id is %s", d.Id()) - } - - environmentId := idSplit[0] - Topic := idSplit[1] - subscriptionName := idSplit[2] - clusterId := idSplit[3] - - // Delete Subscription - if err := service.DeleteTdmqSubscriptionAttachmentById(ctx, environmentId, Topic, subscriptionName, clusterId); err != nil { - return err - } - - if v, ok := d.GetOk("auto_create_policy_topic"); ok { - autoCreatePolicyTopic = v.(bool) - if autoCreatePolicyTopic { - // Delete Topics - if err := service.DeleteTdmqTopicsAttachmentById(ctx, environmentId, Topic, subscriptionName, clusterId); err != nil { - return err - } - } - } - - return nil -} diff --git a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment_test.go b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment_test.go deleted file mode 100644 index e620f0a395..0000000000 --- a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_attachment_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package tpulsar_test - -import ( - "context" - "fmt" - "strings" - "testing" - - tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" - tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" - svctdmq "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tdmq" - - sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" -) - -// go test -i; go test -test.run TestAccTencentCloudTdmqSubscriptionAttachmentResource_basic -v -func TestAccTencentCloudTdmqSubscriptionAttachmentResource_basic(t *testing.T) { - t.Parallel() - resource.Test(t, resource.TestCase{ - PreCheck: func() { - tcacctest.AccPreCheck(t) - }, - CheckDestroy: testAccCheckTdmqSubscriptionAttachmentDestroy, - Providers: tcacctest.AccProviders, - Steps: []resource.TestStep{ - { - Config: testAccTdmqSubscriptionAttachment, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("tencentcloud_tdmq_subscription_attachment.example", "id"), - ), - }, - { - ResourceName: "tencentcloud_tdmq_subscription_attachment.example", - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func testAccCheckTdmqSubscriptionAttachmentDestroy(s *terraform.State) error { - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - service := svctdmq.NewTdmqService(tcacctest.AccProvider.Meta().(tccommon.ProviderMeta).GetAPIV3Conn()) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "tencentcloud_tdmq_subscription_attachment" { - continue - } - - idSplit := strings.Split(rs.Primary.ID, tccommon.FILED_SP) - if len(idSplit) != 4 { - return fmt.Errorf("id is broken,%s", rs.Primary.ID) - } - - environmentId := idSplit[0] - Topic := idSplit[1] - subscriptionName := idSplit[2] - clusterId := idSplit[3] - - response, err := service.DescribeTdmqSubscriptionAttachmentById(ctx, environmentId, Topic, subscriptionName, clusterId) - - if err != nil { - if sdkerr, ok := err.(*sdkErrors.TencentCloudSDKError); ok { - if sdkerr.Code == "ResourceNotFound.Cluster" { - return nil - } - } - return err - } - - if response != nil { - return fmt.Errorf("tdmq subscription attachment still exist, id: %v", rs.Primary.ID) - } - } - - return nil -} - -const testAccTdmqSubscriptionAttachment = ` -resource "tencentcloud_tdmq_instance" "example" { - cluster_name = "tf_example" - remark = "remark." - tags = { - "createdBy" = "terraform" - } -} - -resource "tencentcloud_tdmq_namespace" "example" { - environ_name = "tf_example" - msg_ttl = 300 - cluster_id = tencentcloud_tdmq_instance.example.id - retention_policy { - time_in_minutes = 60 - size_in_mb = 10 - } - remark = "remark." -} - -resource "tencentcloud_tdmq_topic" "example" { - environ_id = tencentcloud_tdmq_namespace.example.environ_name - cluster_id = tencentcloud_tdmq_instance.example.id - topic_name = "tf-example-topic" - partitions = 1 - pulsar_topic_type = 3 - remark = "remark." -} - -resource "tencentcloud_tdmq_subscription_attachment" "example" { - environment_id = tencentcloud_tdmq_namespace.example.environ_name - cluster_id = tencentcloud_tdmq_instance.example.id - topic_name = tencentcloud_tdmq_topic.example.topic_name - subscription_name = "tf-example-subcription" - remark = "remark." - auto_create_policy_topic = true -} -` diff --git a/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_test.go b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_test.go new file mode 100644 index 0000000000..2cc52ed55f --- /dev/null +++ b/tencentcloud/services/tpulsar/resource_tc_tdmq_subscription_test.go @@ -0,0 +1,78 @@ +package tpulsar_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +// go test -i; go test -test.run TestAccTencentCloudTdmqSubscriptionResource_basic -v +func TestAccTencentCloudTdmqSubscriptionResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTdmqSubscription, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_tdmq_subscription.example", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_tdmq_subscription.example", "cluster_id"), + resource.TestCheckResourceAttrSet("tencentcloud_tdmq_subscription.example", "environment_id"), + resource.TestCheckResourceAttrSet("tencentcloud_tdmq_subscription.example", "topic_name"), + resource.TestCheckResourceAttr("tencentcloud_tdmq_subscription.example", "subscription_name", "tf-example-subscription"), + resource.TestCheckResourceAttr("tencentcloud_tdmq_subscription.example", "remark", "remark."), + ), + }, + { + ResourceName: "tencentcloud_tdmq_subscription.example", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +const testAccTdmqSubscription = ` +resource "tencentcloud_tdmq_instance" "example" { + cluster_name = "tf_example" + remark = "remark." + tags = { + "createdBy" = "terraform" + } +} + +resource "tencentcloud_tdmq_namespace" "example" { + environ_name = "tf_example" + msg_ttl = 300 + cluster_id = tencentcloud_tdmq_instance.example.id + retention_policy { + time_in_minutes = 60 + size_in_mb = 10 + } + remark = "remark." +} + +resource "tencentcloud_tdmq_topic" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environ_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = "tf-example-topic" + partitions = 1 + pulsar_topic_type = 3 + remark = "remark." +} + +resource "tencentcloud_tdmq_subscription" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environment_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = tencentcloud_tdmq_topic.example.topic_name + subscription_name = "tf-example-subscription" + remark = "remark." + auto_create_policy_topic = true + auto_delete_policy_topic = true +} +` diff --git a/website/docs/r/tdmq_subscription.html.markdown b/website/docs/r/tdmq_subscription.html.markdown new file mode 100644 index 0000000000..621fae848c --- /dev/null +++ b/website/docs/r/tdmq_subscription.html.markdown @@ -0,0 +1,83 @@ +--- +subcategory: "TDMQ for Pulsar(tpulsar)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_tdmq_subscription" +sidebar_current: "docs-tencentcloud-resource-tdmq_subscription" +description: |- + Provides a resource to create a tdmq subscription +--- + +# tencentcloud_tdmq_subscription + +Provides a resource to create a tdmq subscription + +## Example Usage + +```hcl +resource "tencentcloud_tdmq_instance" "example" { + cluster_name = "tf_example" + remark = "remark." + tags = { + "createdBy" = "terraform" + } +} + +resource "tencentcloud_tdmq_namespace" "example" { + environ_name = "tf_example" + msg_ttl = 300 + cluster_id = tencentcloud_tdmq_instance.example.id + retention_policy { + time_in_minutes = 60 + size_in_mb = 10 + } + remark = "remark." +} + +resource "tencentcloud_tdmq_topic" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environ_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = "tf-example-topic" + partitions = 1 + pulsar_topic_type = 3 + remark = "remark." +} + +resource "tencentcloud_tdmq_subscription" "example" { + cluster_id = tencentcloud_tdmq_instance.example.id + environment_id = tencentcloud_tdmq_namespace.example.environ_name + topic_name = tencentcloud_tdmq_topic.example.topic_name + subscription_name = "tf-example-subscription" + remark = "remark." + auto_create_policy_topic = true + auto_delete_policy_topic = true +} +``` + +## Argument Reference + +The following arguments are supported: + +* `cluster_id` - (Required, String, ForceNew) Pulsar cluster ID. +* `environment_id` - (Required, String, ForceNew) Environment (namespace) name. +* `subscription_name` - (Required, String, ForceNew) Subscriber name, which can contain up to 128 characters. +* `topic_name` - (Required, String, ForceNew) Topic name. +* `auto_create_policy_topic` - (Optional, Bool, ForceNew) Whether to automatically create a dead letter topic and a retry letter topic. true: yes; false: no(default value). +* `auto_delete_policy_topic` - (Optional, Bool, ForceNew) 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. +* `remark` - (Optional, String, ForceNew) Remarks (up to 128 characters). + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + +## Import + +tdmq subscription can be imported using the id, e.g. + +``` +terraform import tencentcloud_tdmq_subscription.example pulsar-q4k5898krpqj#tf_example#tf-example-topic#tf-example-subscription#true +``` + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 5ddf1e8206..6c43490716 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -3788,6 +3788,9 @@
  • tencentcloud_tdmq_role
  • +
  • + tencentcloud_tdmq_subscription +
  • tencentcloud_tdmq_topic