-
Notifications
You must be signed in to change notification settings - Fork 141
feat/tdmq #2451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat/tdmq #2451
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdea8e1
add
SevenEarth 2637dd1
add
SevenEarth 70ab3fe
feat/tdmq
SevenEarth 5f75d0e
Merge branch 'master' of github.com:tencentcloudstack/terraform-provi…
SevenEarth 063b88d
Update resource_tc_tdmq_subscription.go
andrew-tx 9fd25a5
Update resource_tc_tdmq_subscription.go
andrew-tx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
tencentcloud_tdmq_subscription | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 249 additions & 0 deletions
249
tencentcloud/services/tpulsar/resource_tc_tdmq_subscription.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
createPolicyStatus bool | ||
deletePolicyStatus 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)) | ||
createPolicyStatus = v.(bool) | ||
|
||
if v, ok = d.GetOkExists("auto_delete_policy_topic"); ok { | ||
if createPolicyStatus == false && v.(bool) == true { | ||
return errors.New("If `auto_create_policy_topic` is false, Can't set `auto_delete_policy_topic` param.") | ||
} else { | ||
if v.(bool) { | ||
deletePolicyStatus = "true" | ||
} else { | ||
deletePolicyStatus = "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 == false { | ||
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, deletePolicyStatus}, 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] | ||
deletePolicyStatus := 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 deletePolicyStatus == "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] | ||
deletePolicyStatus := idSplit[4] | ||
|
||
if err := service.DeleteTdmqSubscriptionById(ctx, clusterId, environmentId, topicName, subscriptionName); err != nil { | ||
return err | ||
} | ||
|
||
if deletePolicyStatus == "true" { | ||
// Delete Topics | ||
if err := service.DeleteTdmqTopicsAttachmentById(ctx, environmentId, topicName, subscriptionName, clusterId); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createPolicyStatus 为啥不叫 autoCreatePolicyTopic
deletePolicyStatus 为啥不叫 autoDeletePolicyTopic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已变更