|
| 1 | +/* |
| 2 | +Provides a CBS storage set attachment resource. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cbs_storage_set_attachment" "attachment" { |
| 8 | + storage_id = "disk-kdt0sq6m" |
| 9 | + instance_id = "ins-jqlegd42" |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "time" |
| 21 | + |
| 22 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 26 | + cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312" |
| 27 | +) |
| 28 | + |
| 29 | +func resourceTencentCloudCbsStorageSetAttachment() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Create: resourceTencentCloudCbsStorageSetAttachmentCreate, |
| 32 | + Read: resourceTencentCloudCbsStorageSetAttachmentRead, |
| 33 | + Delete: resourceTencentCloudCbsStorageSetAttachmentDelete, |
| 34 | + Importer: &schema.ResourceImporter{ |
| 35 | + State: schema.ImportStatePassthrough, |
| 36 | + }, |
| 37 | + |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "storage_id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + Description: "ID of the mounted CBS.", |
| 44 | + }, |
| 45 | + "instance_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + Description: "ID of the CVM instance.", |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func resourceTencentCloudCbsStorageSetAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 56 | + defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.create")() |
| 57 | + |
| 58 | + logId := getLogId(contextNil) |
| 59 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 60 | + |
| 61 | + storageId := d.Get("storage_id").(string) |
| 62 | + instanceId := d.Get("instance_id").(string) |
| 63 | + |
| 64 | + cbsService := CbsService{ |
| 65 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 66 | + } |
| 67 | + |
| 68 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 69 | + e := cbsService.AttachDisk(ctx, storageId, instanceId) |
| 70 | + if e != nil { |
| 71 | + ee, ok := e.(*sdkErrors.TencentCloudSDKError) |
| 72 | + if ok && IsContains(CVM_RETRYABLE_ERROR, ee.Code) { |
| 73 | + time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试 |
| 74 | + return resource.RetryableError(fmt.Errorf("cbs attach error: %s, retrying", ee.Error())) |
| 75 | + } |
| 76 | + return resource.NonRetryableError(ee) |
| 77 | + } |
| 78 | + return nil |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + log.Printf("[CRITAL]%s cbs storage attach failed, reason:%s\n ", logId, err.Error()) |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(storageId) |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTencentCloudCbsStorageSetAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.read")() |
| 92 | + defer inconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + logId := getLogId(contextNil) |
| 95 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 96 | + |
| 97 | + storageId := d.Id() |
| 98 | + cbsService := CbsService{ |
| 99 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 100 | + } |
| 101 | + |
| 102 | + var storage *cbs.Disk |
| 103 | + var errRet error |
| 104 | + storage, errRet = cbsService.DescribeDiskById(ctx, storageId) |
| 105 | + if errRet != nil { |
| 106 | + log.Printf("[CRITAL]%s describe cbs storage attach failed, reason:%s\n ", logId, errRet.Error()) |
| 107 | + return errRet |
| 108 | + } |
| 109 | + |
| 110 | + if storage == nil || !*storage.Attached { |
| 111 | + d.SetId("") |
| 112 | + return nil |
| 113 | + } |
| 114 | + _ = d.Set("storage_id", storage.DiskId) |
| 115 | + _ = d.Set("instance_id", storage.InstanceId) |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceTencentCloudCbsStorageSetAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 121 | + defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.delete")() |
| 122 | + |
| 123 | + logId := getLogId(contextNil) |
| 124 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 125 | + |
| 126 | + cbsService := CbsService{ |
| 127 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 128 | + } |
| 129 | + |
| 130 | + storageId := d.Id() |
| 131 | + instanceId := d.Get("instance_id").(string) |
| 132 | + |
| 133 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 134 | + errRet := cbsService.DetachDisk(ctx, storageId, instanceId) |
| 135 | + if errRet != nil { |
| 136 | + log.Printf("[CRITAL][detach disk]%s api[%s] fail, reason[%s]\n", |
| 137 | + logId, "detach", errRet.Error()) |
| 138 | + e, ok := errRet.(*sdkErrors.TencentCloudSDKError) |
| 139 | + if ok && IsContains(CVM_RETRYABLE_ERROR, e.Code) { |
| 140 | + time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试 |
| 141 | + return resource.RetryableError(fmt.Errorf("[detach]disk detach error: %s, retrying", e.Error())) |
| 142 | + } |
| 143 | + return resource.NonRetryableError(errRet) |
| 144 | + } |
| 145 | + return nil |
| 146 | + }) |
| 147 | + if err != nil { |
| 148 | + log.Printf("[CRITAL]%s cbs storage detach failed, reason:%s\n ", logId, err.Error()) |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
0 commit comments