diff --git a/alicloud/provider.go b/alicloud/provider.go index e54b748e7539..664ecd4fc73e 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1479,7 +1479,7 @@ func Provider() terraform.ResourceProvider { "alicloud_pvtz_rule_attachment": resourceAlicloudPvtzRuleAttachment(), "alicloud_simple_application_server_snapshot": resourceAlicloudSimpleApplicationServerSnapshot(), "alicloud_simple_application_server_custom_image": resourceAlicloudSimpleApplicationServerCustomImage(), - "alicloud_cloud_storage_gateway_gateway_cache_disk": resourceAlicloudCloudStorageGatewayGatewayCacheDisk(), + "alicloud_cloud_storage_gateway_gateway_cache_disk": resourceAliCloudCloudStorageGatewayGatewayCacheDisk(), "alicloud_cloud_storage_gateway_gateway_logging": resourceAlicloudCloudStorageGatewayGatewayLogging(), "alicloud_cloud_storage_gateway_gateway_block_volume": resourceAlicloudCloudStorageGatewayGatewayBlockVolume(), "alicloud_direct_mail_tag": resourceAlicloudDirectMailTag(), diff --git a/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk.go b/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk.go index 8ffdb75ebe07..02111434342b 100644 --- a/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk.go +++ b/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk.go @@ -9,40 +9,45 @@ import ( "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) -func resourceAlicloudCloudStorageGatewayGatewayCacheDisk() *schema.Resource { +func resourceAliCloudCloudStorageGatewayGatewayCacheDisk() *schema.Resource { return &schema.Resource{ - Create: resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate, - Read: resourceAlicloudCloudStorageGatewayGatewayCacheDiskRead, - Update: resourceAlicloudCloudStorageGatewayGatewayCacheDiskUpdate, - Delete: resourceAlicloudCloudStorageGatewayGatewayCacheDiskDelete, + Create: resourceAliCloudCloudStorageGatewayGatewayCacheDiskCreate, + Read: resourceAliCloudCloudStorageGatewayGatewayCacheDiskRead, + Update: resourceAliCloudCloudStorageGatewayGatewayCacheDiskUpdate, + Delete: resourceAliCloudCloudStorageGatewayGatewayCacheDiskDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ + "gateway_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "cache_disk_size_in_gb": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: IntBetween(20, 32768), + }, "cache_disk_category": { Type: schema.TypeString, Optional: true, - Computed: true, ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{"cloud_efficiency", "cloud_ssd"}, false), + Computed: true, + ValidateFunc: StringInSlice([]string{"cloud_efficiency", "cloud_ssd", "cloud_essd"}, false), }, - "cache_disk_size_in_gb": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validation.IntBetween(20, 32768), + "performance_level": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: StringInSlice([]string{"PL1", "PL2", "PL3"}, false), }, "cache_id": { Type: schema.TypeString, Computed: true, }, - "gateway_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, "local_file_path": { Type: schema.TypeString, Computed: true, @@ -55,7 +60,7 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDisk() *schema.Resource { } } -func resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudCloudStorageGatewayGatewayCacheDiskCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) sgwService := SgwService{client} var response map[string]interface{} @@ -65,14 +70,23 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(d *schema.Resourc if err != nil { return WrapError(err) } + + request["GatewayId"] = d.Get("gateway_id") + request["CacheDiskSizeInGB"] = d.Get("cache_disk_size_in_gb") + if v, ok := d.GetOk("cache_disk_category"); ok { request["CacheDiskCategory"] = v } - request["CacheDiskSizeInGB"] = d.Get("cache_disk_size_in_gb") - request["GatewayId"] = d.Get("gateway_id") + + if v, ok := d.GetOk("performance_level"); ok { + request["PerformanceLevel"] = v + } + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) - err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutCreate)), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &runtime) if err != nil { if NeedRetry(err) { wait() @@ -83,9 +97,11 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(d *schema.Resourc return nil }) addDebug(action, response, request) + if err != nil { return WrapErrorf(err, DefaultErrorMsg, "alicloud_cloud_storage_gateway_gateway_cache_disk", action, AlibabaCloudSdkGoERROR) } + if fmt.Sprint(response["Success"]) == "false" { return WrapError(fmt.Errorf("%s failed, response: %v", action, response)) } @@ -97,68 +113,82 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(d *schema.Resourc task, err := sgwService.DescribeTasks(fmt.Sprint(request["GatewayId"]), fmt.Sprint(response["TaskId"])) if err != nil { - return nil + return WrapError(err) } - object, err := sgwService.DescribeCloudStorageGatewayGatewayCacheDisk(fmt.Sprint(request["GatewayId"], ":", task["RelatedResourceId"], ":")) + + object, err := sgwService.DescribeCloudStorageGatewayGatewayCacheDisk(fmt.Sprintf("%v:%v:", request["GatewayId"], task["RelatedResourceId"])) if err != nil { - if NotFoundError(err) { - log.Printf("[DEBUG] Resource alicloud_cloud_storage_gateway_gateway_cache_disk sgwService.DescribeCloudStorageGatewayGatewayCacheDisk Failed!!! %s", err) - d.SetId("") - return nil - } return WrapError(err) } - d.SetId(fmt.Sprint(request["GatewayId"], ":", task["RelatedResourceId"], ":", object["LocalFilePath"])) + d.SetId(fmt.Sprintf("%v:%v:%v", request["GatewayId"], task["RelatedResourceId"], object["LocalFilePath"])) - return resourceAlicloudCloudStorageGatewayGatewayCacheDiskRead(d, meta) + return resourceAliCloudCloudStorageGatewayGatewayCacheDiskRead(d, meta) } -func resourceAlicloudCloudStorageGatewayGatewayCacheDiskRead(d *schema.ResourceData, meta interface{}) error { + +func resourceAliCloudCloudStorageGatewayGatewayCacheDiskRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) sgwService := SgwService{client} + object, err := sgwService.DescribeCloudStorageGatewayGatewayCacheDisk(d.Id()) if err != nil { - if NotFoundError(err) { + if !d.IsNewResource() && NotFoundError(err) { log.Printf("[DEBUG] Resource alicloud_cloud_storage_gateway_gateway_cache_disk sgwService.DescribeCloudStorageGatewayGatewayCacheDisk Failed!!! %s", err) d.SetId("") return nil } return WrapError(err) } + parts, err := ParseResourceId(d.Id(), 3) if err != nil { return WrapError(err) } + d.Set("gateway_id", parts[0]) - d.Set("local_file_path", object["LocalFilePath"]) - d.Set("cache_disk_category", object["CacheType"]) d.Set("cache_disk_size_in_gb", formatInt(object["SizeInGB"])) + d.Set("cache_disk_category", object["CacheType"]) + d.Set("performance_level", object["PerformanceLevel"]) d.Set("cache_id", object["CacheId"]) + d.Set("local_file_path", object["LocalFilePath"]) d.Set("status", formatInt(object["ExpireStatus"])) + return nil } -func resourceAlicloudCloudStorageGatewayGatewayCacheDiskUpdate(d *schema.ResourceData, meta interface{}) error { + +func resourceAliCloudCloudStorageGatewayGatewayCacheDiskUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) - conn, err := client.NewHcsSgwClient() - if err != nil { - return WrapError(err) - } sgwService := SgwService{client} var response map[string]interface{} + update := false + parts, err := ParseResourceId(d.Id(), 3) if err != nil { return WrapError(err) } + + request := map[string]interface{}{ + "GatewayId": parts[0], + "LocalFilePath": parts[2], + } + if d.HasChange("cache_disk_size_in_gb") { - request := map[string]interface{}{ - "GatewayId": parts[0], - "LocalFilePath": parts[2], - "NewSizeInGB": d.Get("cache_disk_size_in_gb"), - } + update = true + } + request["NewSizeInGB"] = d.Get("cache_disk_size_in_gb") + + if update { action := "ExpandCacheDisk" + conn, err := client.NewHcsSgwClient() + if err != nil { + return WrapError(err) + } + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) - err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &runtime) if err != nil { if NeedRetry(err) { wait() @@ -169,41 +199,51 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDiskUpdate(d *schema.Resourc return nil }) addDebug(action, response, request) + if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } + if fmt.Sprint(response["Success"]) == "false" { return WrapError(fmt.Errorf("%s failed, response: %v", action, response)) } + stateConf := BuildStateConf([]string{}, []string{"task.state.completed"}, d.Timeout(schema.TimeoutCreate), 1*time.Second, sgwService.CloudStorageGatewayTaskStateRefreshFunc(fmt.Sprint(request["GatewayId"]), fmt.Sprint(response["TaskId"]), []string{"task.state.failed"})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } } - return resourceAlicloudCloudStorageGatewayGatewayCacheDiskRead(d, meta) + return resourceAliCloudCloudStorageGatewayGatewayCacheDiskRead(d, meta) } -func resourceAlicloudCloudStorageGatewayGatewayCacheDiskDelete(d *schema.ResourceData, meta interface{}) error { + +func resourceAliCloudCloudStorageGatewayGatewayCacheDiskDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) sgwService := SgwService{client} - parts, err := ParseResourceId(d.Id(), 3) - if err != nil { - return WrapError(err) - } action := "DeleteGatewayCacheDisk" var response map[string]interface{} + conn, err := client.NewHcsSgwClient() if err != nil { return WrapError(err) } + + parts, err := ParseResourceId(d.Id(), 3) + if err != nil { + return WrapError(err) + } + request := map[string]interface{}{ "GatewayId": parts[0], "CacheId": parts[1], "LocalFilePath": parts[2], } + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) - err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutDelete)), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-05-11"), StringPointer("AK"), nil, request, &runtime) if err != nil { if NeedRetry(err) { wait() @@ -214,15 +254,19 @@ func resourceAlicloudCloudStorageGatewayGatewayCacheDiskDelete(d *schema.Resourc return nil }) addDebug(action, response, request) + if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } + if fmt.Sprint(response["Success"]) == "false" { return WrapError(fmt.Errorf("%s failed, response: %v", action, response)) } + stateConf := BuildStateConf([]string{}, []string{"task.state.completed"}, d.Timeout(schema.TimeoutCreate), 1*time.Second, sgwService.CloudStorageGatewayTaskStateRefreshFunc(fmt.Sprint(request["GatewayId"]), fmt.Sprint(response["TaskId"]), []string{"task.state.failed"})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } + return nil } diff --git a/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk_test.go b/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk_test.go index f1ab1e7a6775..b3dd49274e81 100644 --- a/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk_test.go +++ b/alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk_test.go @@ -19,18 +19,18 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccAlicloudCloudStorageGatewayGatewayCacheDisk_basic0(t *testing.T) { +func TestAccAliCloudCloudStorageGatewayGatewayCacheDisk_basic0(t *testing.T) { var v map[string]interface{} resourceId := "alicloud_cloud_storage_gateway_gateway_cache_disk.default" - ra := resourceAttrInit(resourceId, AlicloudCloudStorageGatewayGatewayCacheDiskMap0) + ra := resourceAttrInit(resourceId, AliCloudCloudStorageGatewayGatewayCacheDiskMap0) rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { return &SgwService{testAccProvider.Meta().(*connectivity.AliyunClient)} }, "DescribeCloudStorageGatewayGatewayCacheDisk") rac := resourceAttrCheckInit(rc, ra) testAccCheck := rac.resourceAttrMapUpdateSet() rand := acctest.RandIntRange(10000, 99999) - name := fmt.Sprintf("tf-testacc-cloudstoragegatewaygatewaycachedisk%d", rand) - testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudCloudStorageGatewayGatewayCacheDiskBasicDependence0) + name := fmt.Sprintf("tf-testacc-CloudStorageGatewayGatewayCacheDisk%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudCloudStorageGatewayGatewayCacheDiskBasicDependence0) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -41,13 +41,11 @@ func TestAccAlicloudCloudStorageGatewayGatewayCacheDisk_basic0(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "cache_disk_category": "cloud_efficiency", "gateway_id": "${alicloud_cloud_storage_gateway_gateway.default.id}", "cache_disk_size_in_gb": "50", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "cache_disk_category": "cloud_efficiency", "gateway_id": CHECKSET, "cache_disk_size_in_gb": "50", }), @@ -72,64 +70,100 @@ func TestAccAlicloudCloudStorageGatewayGatewayCacheDisk_basic0(t *testing.T) { }) } -var AlicloudCloudStorageGatewayGatewayCacheDiskMap0 = map[string]string{ +func TestAccAliCloudCloudStorageGatewayGatewayCacheDisk_basic0_twin(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_cloud_storage_gateway_gateway_cache_disk.default" + ra := resourceAttrInit(resourceId, AliCloudCloudStorageGatewayGatewayCacheDiskMap0) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &SgwService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeCloudStorageGatewayGatewayCacheDisk") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc-CloudStorageGatewayGatewayCacheDisk%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudCloudStorageGatewayGatewayCacheDiskBasicDependence0) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "gateway_id": "${alicloud_cloud_storage_gateway_gateway.default.id}", + "cache_disk_size_in_gb": "50", + "cache_disk_category": "cloud_essd", + "performance_level": "PL1", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "gateway_id": CHECKSET, + "cache_disk_size_in_gb": "50", + "cache_disk_category": "cloud_essd", + "performance_level": "PL1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +var AliCloudCloudStorageGatewayGatewayCacheDiskMap0 = map[string]string{ "cache_disk_category": CHECKSET, - "gateway_id": CHECKSET, + "cache_id": CHECKSET, "local_file_path": CHECKSET, "status": CHECKSET, - "cache_id": CHECKSET, } -func AlicloudCloudStorageGatewayGatewayCacheDiskBasicDependence0(name string) string { +func AliCloudCloudStorageGatewayGatewayCacheDiskBasicDependence0(name string) string { return fmt.Sprintf(` -variable "name" { - default = "%s" -} - -data "alicloud_cloud_storage_gateway_stocks" "default" { - gateway_class = "Standard" -} + variable "name" { + default = "%s" + } -data "alicloud_vpcs" "default" { - name_regex = "^default-NODELETING$" -} -data "alicloud_vswitches" "default" { - vpc_id = data.alicloud_vpcs.default.ids.0 - zone_id = data.alicloud_cloud_storage_gateway_stocks.default.stocks.0.zone_id -} + data "alicloud_cloud_storage_gateway_stocks" "default" { + gateway_class = "Standard" + } -resource "alicloud_vswitch" "vswitch" { - count = length(data.alicloud_vswitches.default.ids) > 0 ? 0 : 1 - vpc_id = data.alicloud_vpcs.default.ids.0 - cidr_block = cidrsubnet(data.alicloud_vpcs.default.vpcs[0].cidr_block, 8, 8) - zone_id = data.alicloud_cloud_storage_gateway_stocks.default.stocks.0.zone_id - vswitch_name = var.name -} + resource "alicloud_vpc" "default" { + vpc_name = var.name + cidr_block = "172.16.0.0/16" + } -locals { - vswitch_id = length(data.alicloud_vswitches.default.ids) > 0 ? data.alicloud_vswitches.default.ids[0] : concat(alicloud_vswitch.vswitch.*.id, [""])[0] -} + resource "alicloud_vswitch" "default" { + vpc_id = alicloud_vpc.default.id + cidr_block = "172.16.0.0/24" + zone_id = data.alicloud_cloud_storage_gateway_stocks.default.stocks.0.zone_id + vswitch_name = var.name + } -resource "alicloud_cloud_storage_gateway_storage_bundle" "default" { - storage_bundle_name = var.name -} + resource "alicloud_cloud_storage_gateway_storage_bundle" "default" { + storage_bundle_name = var.name + } -resource "alicloud_cloud_storage_gateway_gateway" "default" { - description = "tf-acctestDesalone" - gateway_class = "Standard" - type = "File" - payment_type = "PayAsYouGo" - vswitch_id = local.vswitch_id - release_after_expiration = true - public_network_bandwidth = 10 - storage_bundle_id = alicloud_cloud_storage_gateway_storage_bundle.default.id - location = "Cloud" - gateway_name = var.name -} + resource "alicloud_cloud_storage_gateway_gateway" "default" { + description = var.name + gateway_class = "Standard" + type = "File" + payment_type = "PayAsYouGo" + vswitch_id = alicloud_vswitch.default.id + release_after_expiration = true + public_network_bandwidth = 10 + storage_bundle_id = alicloud_cloud_storage_gateway_storage_bundle.default.id + location = "Cloud" + gateway_name = var.name + } `, name) } -func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { +func TestUnitAliCloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { p := Provider().(*schema.Provider).ResourcesMap dInit, _ := schema.InternalMap(p["alicloud_cloud_storage_gateway_gateway_cache_disk"].Schema).Data(nil, nil) dExisted, _ := schema.InternalMap(p["alicloud_cloud_storage_gateway_gateway_cache_disk"].Schema).Data(nil, nil) @@ -228,7 +262,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { StatusCode: tea.Int(400), } }) - err = resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(dInit, rawClient) + err = resourceAliCloudCloudStorageGatewayGatewayCacheDiskCreate(dInit, rawClient) patches.Reset() assert.NotNil(t, err) ReadMockResponseDiff := map[string]interface{}{ @@ -261,7 +295,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { } return ReadMockResponse, nil }) - err := resourceAlicloudCloudStorageGatewayGatewayCacheDiskCreate(dInit, rawClient) + err := resourceAliCloudCloudStorageGatewayGatewayCacheDiskCreate(dInit, rawClient) patches.Reset() switch errorCode { case "NonRetryableError": @@ -286,7 +320,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { Message: String("loadEndpoint error"), } }) - err = resourceAlicloudCloudStorageGatewayGatewayCacheDiskUpdate(dExisted, rawClient) + err = resourceAliCloudCloudStorageGatewayGatewayCacheDiskUpdate(dExisted, rawClient) patches.Reset() assert.NotNil(t, err) // ExpandCacheDisk @@ -326,7 +360,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { } return ReadMockResponse, nil }) - err := resourceAlicloudCloudStorageGatewayGatewayCacheDiskUpdate(dExisted, rawClient) + err := resourceAliCloudCloudStorageGatewayGatewayCacheDiskUpdate(dExisted, rawClient) patches.Reset() switch errorCode { case "NonRetryableError": @@ -370,7 +404,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { } return ReadMockResponse, nil }) - err := resourceAlicloudCloudStorageGatewayGatewayCacheDiskRead(dExisted, rawClient) + err := resourceAliCloudCloudStorageGatewayGatewayCacheDiskRead(dExisted, rawClient) patches.Reset() switch errorCode { case "NonRetryableError": @@ -388,7 +422,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { StatusCode: tea.Int(400), } }) - err = resourceAlicloudCloudStorageGatewayGatewayCacheDiskDelete(dExisted, rawClient) + err = resourceAliCloudCloudStorageGatewayGatewayCacheDiskDelete(dExisted, rawClient) patches.Reset() assert.NotNil(t, err) attributesDiff = map[string]interface{}{} @@ -427,7 +461,7 @@ func TestUnitAlicloudCloudStorageGatewayGatewayCacheDisk(t *testing.T) { } return ReadMockResponse, nil }) - err := resourceAlicloudCloudStorageGatewayGatewayCacheDiskDelete(dExisted, rawClient) + err := resourceAliCloudCloudStorageGatewayGatewayCacheDiskDelete(dExisted, rawClient) patches.Reset() switch errorCode { case "NonRetryableError": diff --git a/alicloud/service_alicloud_sgw.go b/alicloud/service_alicloud_sgw.go index e3064db5f27e..a32fde713f65 100644 --- a/alicloud/service_alicloud_sgw.go +++ b/alicloud/service_alicloud_sgw.go @@ -283,19 +283,22 @@ func (s *SgwService) CloudStorageGatewayTaskStateRefreshFunc(id, taskId string, func (s *SgwService) DescribeCloudStorageGatewayGatewayCacheDisk(id string) (object map[string]interface{}, err error) { var response map[string]interface{} + action := "DescribeGatewayCaches" + conn, err := s.client.NewHcsSgwClient() if err != nil { return nil, WrapError(err) } - action := "DescribeGatewayCaches" + parts, err := ParseResourceId(id, 3) if err != nil { - err = WrapError(err) - return + return nil, WrapError(err) } + request := map[string]interface{}{ "GatewayId": parts[0], } + idExist := false runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) @@ -312,31 +315,38 @@ func (s *SgwService) DescribeCloudStorageGatewayGatewayCacheDisk(id string) (obj return nil }) addDebug(action, response, request) + if err != nil { if IsExpectedErrors(err, []string{"GatewayNotExist"}) { - return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway:Gateway:CacheDisk", id)), NotFoundMsg, ProviderERROR, fmt.Sprint(response["RequestId"])) + return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway:GatewayCacheDisk", id)), NotFoundWithResponse, response) } return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) } + if fmt.Sprint(response["Success"]) == "false" { return object, WrapError(fmt.Errorf("%s failed, response: %v", action, response)) } - v, err := jsonpath.Get("$.Caches.Cache", response) + + resp, err := jsonpath.Get("$.Caches.Cache", response) if err != nil { return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.Caches.Cache", response) } - if len(v.([]interface{})) < 1 { - return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway", id)), NotFoundWithResponse, response) + + if v, ok := resp.([]interface{}); !ok || len(v) < 1 { + return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway:GatewayCacheDisk", id)), NotFoundWithResponse, response) } - for _, v := range v.([]interface{}) { + + for _, v := range resp.([]interface{}) { if fmt.Sprint(v.(map[string]interface{})["CacheId"]) == parts[1] { idExist = true return v.(map[string]interface{}), nil } } + if !idExist { - return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway", id)), NotFoundWithResponse, response) + return object, WrapErrorf(Error(GetNotFoundMessage("CloudStorageGateway:GatewayCacheDisk", id)), NotFoundWithResponse, response) } + return object, nil } diff --git a/website/docs/r/cloud_storage_gateway_gateway_cache_disk.html.markdown b/website/docs/r/cloud_storage_gateway_gateway_cache_disk.html.markdown index c0869cd480ea..37250df2af0e 100644 --- a/website/docs/r/cloud_storage_gateway_gateway_cache_disk.html.markdown +++ b/website/docs/r/cloud_storage_gateway_gateway_cache_disk.html.markdown @@ -24,43 +24,42 @@ variable "name" { default = "tf-example" } -resource "random_uuid" "default" { -} -resource "alicloud_cloud_storage_gateway_storage_bundle" "default" { - storage_bundle_name = substr("tf-example-${replace(random_uuid.default.result, "-", "")}", 0, 16) +data "alicloud_cloud_storage_gateway_stocks" "default" { + gateway_class = "Standard" } resource "alicloud_vpc" "default" { vpc_name = var.name - cidr_block = "172.16.0.0/12" -} -data "alicloud_cloud_storage_gateway_stocks" "default" { - gateway_class = "Standard" + cidr_block = "172.16.0.0/16" } + resource "alicloud_vswitch" "default" { vpc_id = alicloud_vpc.default.id - cidr_block = "172.16.0.0/21" + cidr_block = "172.16.0.0/24" zone_id = data.alicloud_cloud_storage_gateway_stocks.default.stocks.0.zone_id vswitch_name = var.name } +resource "alicloud_cloud_storage_gateway_storage_bundle" "default" { + storage_bundle_name = var.name +} + resource "alicloud_cloud_storage_gateway_gateway" "default" { - gateway_name = var.name description = var.name gateway_class = "Standard" type = "File" payment_type = "PayAsYouGo" vswitch_id = alicloud_vswitch.default.id release_after_expiration = true - public_network_bandwidth = 40 storage_bundle_id = alicloud_cloud_storage_gateway_storage_bundle.default.id location = "Cloud" + gateway_name = var.name } resource "alicloud_cloud_storage_gateway_gateway_cache_disk" "default" { - cache_disk_category = "cloud_efficiency" gateway_id = alicloud_cloud_storage_gateway_gateway.default.id cache_disk_size_in_gb = 50 + cache_disk_category = "cloud_efficiency" } ``` @@ -68,18 +67,19 @@ resource "alicloud_cloud_storage_gateway_gateway_cache_disk" "default" { The following arguments are supported: -* `cache_disk_category` - (Optional, ForceNew) The cache disk type. Valid values: `cloud_efficiency`, `cloud_ssd`. -* `cache_disk_size_in_gb` - (Required) size of the cache disk. Unit: `GB`. The upper limit of the basic gateway cache disk is `1` TB (`1024` GB), that of the standard gateway is `2` TB (`2048` GB), and that of other gateway cache disks is `32` TB (`32768` GB). The lower limit for the file gateway cache disk capacity is `40` GB, and the lower limit for the block gateway cache disk capacity is `20` GB. * `gateway_id` - (Required, ForceNew) The ID of the gateway. +* `cache_disk_size_in_gb` - (Required, Int) The capacity of the cache disk. +* `cache_disk_category` - (Optional, ForceNew) The type of the cache disk. Valid values: `cloud_efficiency`, `cloud_ssd`, `cloud_essd`. **NOTE:** From version 1.226.1, `cache_disk_category` can be set to `cloud_essd`. +* `performance_level` - (Optional, ForceNew, Available since v1.226.1) The performance level (PL) of the Enterprise SSD (ESSD). Valid values: `PL1`, `PL2`, `PL3`. **NOTE:** If `cache_disk_category` is set to `cloud_essd`, `performance_level` is required. ## Attributes Reference The following attributes are exported: -* `id` - The resource ID of Gateway Cache Disk. The value formats as `::`. -* `local_file_path` - The cache disk inside the device name. -* `status` - The status of the resource. Valid values: `0`, `1`, `2`. `0`: Normal. `1`: Is about to expire. `2`: Has expired. -* `cache_id` - The ID of the cache. +* `id` - The resource ID in terraform of Gateway Cache Disk. It formats as `::`. +* `cache_id` - The ID of the cache disk. +* `local_file_path` - The path of the cache disk. +* `status` - The status of the Gateway Cache Disk. ## Import @@ -87,4 +87,4 @@ Cloud Storage Gateway Gateway Cache Disk can be imported using the id, e.g. ```shell $ terraform import alicloud_cloud_storage_gateway_gateway_cache_disk.example :: -``` \ No newline at end of file +```