Skip to content

Commit

Permalink
resource/alicloud_cloud_storage_gateway_gateway_cache_disk: Added the…
Browse files Browse the repository at this point in the history
… field performance_level; Supported cache_disk_category set to cloud_essd
  • Loading branch information
MrWolong authored and ChenHanZhang committed Jul 4, 2024
1 parent 21439f6 commit e3f0592
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 141 deletions.
2 changes: 1 addition & 1 deletion alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
154 changes: 99 additions & 55 deletions alicloud/resource_alicloud_cloud_storage_gateway_gateway_cache_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{}
Expand All @@ -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()
Expand All @@ -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))
}
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
}
Loading

0 comments on commit e3f0592

Please sign in to comment.