|
| 1 | +/* |
| 2 | +Provides a resource to create CBS set. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cbs_storage_set" "storage" { |
| 8 | + disk_count = 10 |
| 9 | + storage_name = "mystorage" |
| 10 | + storage_type = "CLOUD_SSD" |
| 11 | + storage_size = 100 |
| 12 | + availability_zone = "ap-guangzhou-3" |
| 13 | + project_id = 0 |
| 14 | + encrypt = false |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +*/ |
| 19 | +package tencentcloud |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "strconv" |
| 26 | + "time" |
| 27 | + |
| 28 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 32 | + cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudCbsStorageSet() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudCbsStorageSetCreate, |
| 39 | + Read: resourceTencentCloudCbsStorageSetRead, |
| 40 | + Update: resourceTencentCloudCbsStorageSetUpdate, |
| 41 | + Delete: resourceTencentCloudCbsStorageSetDelete, |
| 42 | + |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "storage_type": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + Description: "Type of CBS medium. Valid values: CLOUD_PREMIUM, CLOUD_SSD, CLOUD_TSSD and CLOUD_HSSD.", |
| 49 | + }, |
| 50 | + "storage_size": { |
| 51 | + Type: schema.TypeInt, |
| 52 | + Required: true, |
| 53 | + ValidateFunc: validateIntegerInRange(10, 16000), |
| 54 | + Description: "Volume of CBS, and unit is GB. If storage type is `CLOUD_SSD`, the size range is [100, 16000], and the others are [10-16000].", |
| 55 | + }, |
| 56 | + "disk_count": { |
| 57 | + Type: schema.TypeInt, |
| 58 | + Optional: true, |
| 59 | + Description: "The number of disks to be purchased. Default 1.", |
| 60 | + }, |
| 61 | + "charge_type": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + Default: CBS_CHARGE_TYPE_POSTPAID, |
| 65 | + ValidateFunc: validateAllowedStringValue(CBS_CHARGE_TYPE), |
| 66 | + Description: "The charge type of CBS instance. Only support `POSTPAID_BY_HOUR`.", |
| 67 | + }, |
| 68 | + "availability_zone": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Required: true, |
| 71 | + ForceNew: true, |
| 72 | + Description: "The available zone that the CBS instance locates at.", |
| 73 | + }, |
| 74 | + "storage_name": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + ValidateFunc: validateStringLengthInRange(2, 60), |
| 78 | + Description: "Name of CBS. The maximum length can not exceed 60 bytes.", |
| 79 | + }, |
| 80 | + "snapshot_id": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + Computed: true, |
| 84 | + Description: "ID of the snapshot. If specified, created the CBS by this snapshot.", |
| 85 | + }, |
| 86 | + "project_id": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Optional: true, |
| 89 | + Default: 0, |
| 90 | + Description: "ID of the project to which the instance belongs.", |
| 91 | + }, |
| 92 | + "encrypt": { |
| 93 | + Type: schema.TypeBool, |
| 94 | + Optional: true, |
| 95 | + ForceNew: true, |
| 96 | + Description: "Indicates whether CBS is encrypted.", |
| 97 | + }, |
| 98 | + "throughput_performance": { |
| 99 | + Type: schema.TypeInt, |
| 100 | + Optional: true, |
| 101 | + Default: 0, |
| 102 | + Description: "Add extra performance to the data disk. Only works when disk type is `CLOUD_TSSD` or `CLOUD_HSSD`.", |
| 103 | + }, |
| 104 | + // computed |
| 105 | + "storage_status": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "Status of CBS. Valid values: UNATTACHED, ATTACHING, ATTACHED, DETACHING, EXPANDING, ROLLBACKING, TORECYCLE and DUMPING.", |
| 109 | + }, |
| 110 | + "attached": { |
| 111 | + Type: schema.TypeBool, |
| 112 | + Computed: true, |
| 113 | + Description: "Indicates whether the CBS is mounted the CVM.", |
| 114 | + }, |
| 115 | + "disk_ids": { |
| 116 | + Type: schema.TypeList, |
| 117 | + Computed: true, |
| 118 | + Elem: &schema.Schema{ |
| 119 | + Type: schema.TypeString, |
| 120 | + }, |
| 121 | + Description: "disk id list.", |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func resourceTencentCloudCbsStorageSetCreate(d *schema.ResourceData, meta interface{}) error { |
| 128 | + defer logElapsed("resource.tencentcloud_cbs_storage_set.create")() |
| 129 | + |
| 130 | + logId := getLogId(contextNil) |
| 131 | + |
| 132 | + var diskCount int |
| 133 | + |
| 134 | + request := cbs.NewCreateDisksRequest() |
| 135 | + request.DiskName = helper.String(d.Get("storage_name").(string)) |
| 136 | + request.DiskType = helper.String(d.Get("storage_type").(string)) |
| 137 | + request.DiskSize = helper.IntUint64(d.Get("storage_size").(int)) |
| 138 | + if v, ok := d.GetOk("disk_count"); ok { |
| 139 | + diskCount = v.(int) |
| 140 | + request.DiskCount = helper.Uint64(uint64(diskCount)) |
| 141 | + } |
| 142 | + request.Placement = &cbs.Placement{ |
| 143 | + Zone: helper.String(d.Get("availability_zone").(string)), |
| 144 | + } |
| 145 | + if v, ok := d.GetOk("project_id"); ok { |
| 146 | + request.Placement.ProjectId = helper.IntUint64(v.(int)) |
| 147 | + } |
| 148 | + if v, ok := d.GetOk("snapshot_id"); ok { |
| 149 | + request.SnapshotId = helper.String(v.(string)) |
| 150 | + } |
| 151 | + if _, ok := d.GetOk("encrypt"); ok { |
| 152 | + request.Encrypt = helper.String("ENCRYPT") |
| 153 | + } |
| 154 | + |
| 155 | + if v, ok := d.GetOk("throughput_performance"); ok { |
| 156 | + request.ThroughputPerformance = helper.IntUint64(v.(int)) |
| 157 | + } |
| 158 | + |
| 159 | + chargeType := d.Get("charge_type").(string) |
| 160 | + |
| 161 | + request.DiskChargeType = &chargeType |
| 162 | + |
| 163 | + storageIds := make([]*string, 0) |
| 164 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 165 | + response, e := meta.(*TencentCloudClient).apiV3Conn.UseCbsClient().CreateDisks(request) |
| 166 | + if e != nil { |
| 167 | + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", |
| 168 | + logId, request.GetAction(), request.ToJsonString(), e.Error()) |
| 169 | + |
| 170 | + ee, ok := e.(*sdkErrors.TencentCloudSDKError) |
| 171 | + if ok && IsContains(CVM_RETRYABLE_ERROR, ee.Code) { |
| 172 | + time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试 |
| 173 | + return resource.RetryableError(fmt.Errorf("cbs create error: %s, retrying", e.Error())) |
| 174 | + } |
| 175 | + return resource.NonRetryableError(e) |
| 176 | + } |
| 177 | + |
| 178 | + if len(response.Response.DiskIdSet) < diskCount { |
| 179 | + err := fmt.Errorf("number of instances is less than %s", strconv.Itoa(diskCount)) |
| 180 | + return resource.NonRetryableError(err) |
| 181 | + } |
| 182 | + |
| 183 | + storageIds = response.Response.DiskIdSet |
| 184 | + return nil |
| 185 | + }) |
| 186 | + if err != nil { |
| 187 | + log.Printf("[CRITAL]%s create cbs failed, reason:%s\n ", logId, err.Error()) |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + d.Set("disk_ids", storageIds) |
| 192 | + d.SetId(helper.StrListToStr(storageIds)) |
| 193 | + |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +func resourceTencentCloudCbsStorageSetRead(d *schema.ResourceData, meta interface{}) error { |
| 198 | + defer logElapsed("resource.tencentcloud_cbs_storage_set.read")() |
| 199 | + defer inconsistentCheck(d, meta)() |
| 200 | + |
| 201 | + logId := getLogId(contextNil) |
| 202 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 203 | + |
| 204 | + storageId := d.Id() |
| 205 | + cbsService := CbsService{ |
| 206 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 207 | + } |
| 208 | + |
| 209 | + var storageSet []*cbs.Disk |
| 210 | + var errRet error |
| 211 | + |
| 212 | + storageSet, errRet = cbsService.DescribeDiskSetByIds(ctx, storageId) |
| 213 | + if errRet != nil { |
| 214 | + return errRet |
| 215 | + } |
| 216 | + if storageSet == nil { |
| 217 | + d.SetId("") |
| 218 | + return nil |
| 219 | + } |
| 220 | + |
| 221 | + storage := storageSet[0] |
| 222 | + |
| 223 | + _ = d.Set("storage_type", storage.DiskType) |
| 224 | + _ = d.Set("storage_size", storage.DiskSize) |
| 225 | + _ = d.Set("availability_zone", storage.Placement.Zone) |
| 226 | + _ = d.Set("storage_name", d.Get("storage_name")) |
| 227 | + _ = d.Set("project_id", storage.Placement.ProjectId) |
| 228 | + _ = d.Set("encrypt", storage.Encrypt) |
| 229 | + _ = d.Set("storage_status", storage.DiskState) |
| 230 | + _ = d.Set("attached", storage.Attached) |
| 231 | + _ = d.Set("charge_type", storage.DiskChargeType) |
| 232 | + _ = d.Set("throughput_performance", storage.ThroughputPerformance) |
| 233 | + |
| 234 | + return nil |
| 235 | +} |
| 236 | + |
| 237 | +func resourceTencentCloudCbsStorageSetUpdate(d *schema.ResourceData, meta interface{}) error { |
| 238 | + defer logElapsed("resource.tencentcloud_cbs_storage_set.update")() |
| 239 | + |
| 240 | + return fmt.Errorf("`tencentcloud_cbs_storage_set` do not support change now.") |
| 241 | +} |
| 242 | + |
| 243 | +func resourceTencentCloudCbsStorageSetDelete(d *schema.ResourceData, meta interface{}) error { |
| 244 | + defer logElapsed("resource.tencentcloud_cbs_storage_set.delete")() |
| 245 | + |
| 246 | + logId := getLogId(contextNil) |
| 247 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 248 | + |
| 249 | + storageId := d.Id() |
| 250 | + |
| 251 | + cbsService := CbsService{ |
| 252 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 253 | + } |
| 254 | + |
| 255 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 256 | + e := cbsService.DeleteDiskSetByIds(ctx, storageId) |
| 257 | + if e != nil { |
| 258 | + log.Printf("[CRITAL][first delete]%s api[%s] fail, reason[%s]\n", |
| 259 | + logId, "delete", e.Error()) |
| 260 | + ee, ok := e.(*sdkErrors.TencentCloudSDKError) |
| 261 | + if ok && IsContains(CVM_RETRYABLE_ERROR, ee.Code) { |
| 262 | + time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试 |
| 263 | + return resource.RetryableError(fmt.Errorf("[first delete]cvm delete error: %s, retrying", ee.Error())) |
| 264 | + } |
| 265 | + return resource.NonRetryableError(e) |
| 266 | + } |
| 267 | + return nil |
| 268 | + }) |
| 269 | + |
| 270 | + if err != nil { |
| 271 | + log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error()) |
| 272 | + return err |
| 273 | + } |
| 274 | + |
| 275 | + return nil |
| 276 | +} |
0 commit comments