|
| 1 | +/* |
| 2 | +Provide a resource to create a TDMQ instance. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_tdmq_instance" "foo" { |
| 8 | + cluster_name = "example" |
| 9 | + remark = "this is description." |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +Tdmq instance can be imported, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +$ terraform import tencentcloud_tdmq_instance.test tdmq_id |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 29 | +) |
| 30 | + |
| 31 | +func resourceTencentCloudTdmqInstance() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Create: resourceTencentCloudTdmqCreate, |
| 34 | + Read: resourceTencentCloudTdmqRead, |
| 35 | + Update: resourceTencentCloudTdmqUpdate, |
| 36 | + Delete: resourceTencentCloudTdmqDelete, |
| 37 | + Importer: &schema.ResourceImporter{ |
| 38 | + State: schema.ImportStatePassthrough, |
| 39 | + }, |
| 40 | + |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "cluster_name": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + Description: "The name of tdmq cluster to be created.", |
| 46 | + }, |
| 47 | + "bind_cluster_id": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Optional: true, |
| 50 | + Description: "The Dedicated Cluster Id.", |
| 51 | + }, |
| 52 | + "remark": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + Description: "Description of the tdmq cluster.", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceTencentCloudTdmqCreate(d *schema.ResourceData, meta interface{}) error { |
| 62 | + defer logElapsed("resource.tencentcloud_tdmq_instance.create")() |
| 63 | + |
| 64 | + logId := getLogId(contextNil) |
| 65 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 66 | + |
| 67 | + tdmqService := TdmqService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 68 | + |
| 69 | + var ( |
| 70 | + clusterName string |
| 71 | + bindClusterId uint64 |
| 72 | + remark string |
| 73 | + ) |
| 74 | + if temp, ok := d.GetOk("cluster_name"); ok { |
| 75 | + clusterName = temp.(string) |
| 76 | + if len(clusterName) < 1 { |
| 77 | + return fmt.Errorf("cluster_name should be not empty string") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + bindClusterId = uint64(d.Get("bind_cluster_id").(int)) |
| 82 | + |
| 83 | + if temp, ok := d.GetOk("remark"); ok { |
| 84 | + remark = temp.(string) |
| 85 | + } |
| 86 | + |
| 87 | + clusterId, err := tdmqService.CreateTdmqInstance(ctx, clusterName, bindClusterId, remark) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + d.SetId(clusterId) |
| 92 | + |
| 93 | + return resourceTencentCloudTdmqRead(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceTencentCloudTdmqRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + defer logElapsed("resource.tencentcloud_tdmq_instance.read")() |
| 98 | + defer inconsistentCheck(d, meta)() |
| 99 | + |
| 100 | + logId := getLogId(contextNil) |
| 101 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 102 | + |
| 103 | + id := d.Id() |
| 104 | + |
| 105 | + tdmqService := TdmqService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 106 | + |
| 107 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 108 | + info, has, e := tdmqService.DescribeTdmqInstanceById(ctx, id) |
| 109 | + if e != nil { |
| 110 | + return retryError(e) |
| 111 | + } |
| 112 | + if !has { |
| 113 | + d.SetId("") |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + _ = d.Set("cluster_name", info.ClusterName) |
| 118 | + _ = d.Set("remark", info.Remark) |
| 119 | + return nil |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func resourceTencentCloudTdmqUpdate(d *schema.ResourceData, meta interface{}) error { |
| 128 | + defer logElapsed("resource.tencentcloud_tdmq_instance.update")() |
| 129 | + |
| 130 | + logId := getLogId(contextNil) |
| 131 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 132 | + |
| 133 | + id := d.Id() |
| 134 | + |
| 135 | + service := TdmqService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 136 | + |
| 137 | + var ( |
| 138 | + clusterName string |
| 139 | + remark string |
| 140 | + ) |
| 141 | + old, now := d.GetChange("cluster_name") |
| 142 | + if d.HasChange("cluster_name") { |
| 143 | + clusterName = now.(string) |
| 144 | + } else { |
| 145 | + clusterName = old.(string) |
| 146 | + } |
| 147 | + |
| 148 | + old, now = d.GetChange("remark") |
| 149 | + if d.HasChange("remark") { |
| 150 | + remark = now.(string) |
| 151 | + } else { |
| 152 | + remark = old.(string) |
| 153 | + } |
| 154 | + |
| 155 | + d.Partial(true) |
| 156 | + |
| 157 | + if err := service.ModifyTdmqInstanceAttribute(ctx, id, clusterName, remark); err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + d.SetPartial("cluster_name") |
| 161 | + d.SetPartial("remark") |
| 162 | + |
| 163 | + d.Partial(false) |
| 164 | + return resourceTencentCloudTdmqRead(d, meta) |
| 165 | +} |
| 166 | + |
| 167 | +func resourceTencentCloudTdmqDelete(d *schema.ResourceData, meta interface{}) error { |
| 168 | + defer logElapsed("resource.tencentcloud_tdmq_instance.delete")() |
| 169 | + |
| 170 | + logId := getLogId(contextNil) |
| 171 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 172 | + |
| 173 | + service := TdmqService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 174 | + |
| 175 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 176 | + if err := service.DeleteTdmqInstance(ctx, d.Id()); err != nil { |
| 177 | + if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok { |
| 178 | + if sdkErr.Code == VPCNotFound { |
| 179 | + return nil |
| 180 | + } |
| 181 | + } |
| 182 | + return resource.RetryableError(err) |
| 183 | + } |
| 184 | + return nil |
| 185 | + }) |
| 186 | + |
| 187 | + return err |
| 188 | +} |
0 commit comments