From 73c81c9e29ad0c92690aa305be0d5b6b0978eea2 Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Thu, 16 Jan 2025 10:26:06 +0800 Subject: [PATCH] resource/alicloud_esa_rate_plan_instance: Support release instance. --- ...esource_alicloud_esa_rate_plan_instance.go | 54 ++++++++++++++++++- alicloud/service_alicloud_esa_v2.go | 12 +++++ .../r/esa_rate_plan_instance.html.markdown | 13 +---- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/alicloud/resource_alicloud_esa_rate_plan_instance.go b/alicloud/resource_alicloud_esa_rate_plan_instance.go index 00889c053268..dcc285d7a330 100644 --- a/alicloud/resource_alicloud_esa_rate_plan_instance.go +++ b/alicloud/resource_alicloud_esa_rate_plan_instance.go @@ -6,6 +6,7 @@ import ( "log" "time" + util "github.com/alibabacloud-go/tea-utils/service" "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -225,7 +226,58 @@ func resourceAliCloudEsaRatePlanInstanceUpdate(d *schema.ResourceData, meta inte } func resourceAliCloudEsaRatePlanInstanceDelete(d *schema.ResourceData, meta interface{}) error { - log.Printf("[WARN] Cannot destroy resource AliCloud Resource Rate Plan Instance. Terraform will remove this resource from the state file, however resources may remain.") + + client := meta.(*connectivity.AliyunClient) + action := "RefundInstance" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewBssopenapiClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + request["InstanceId"] = d.Id() + + request["ClientToken"] = buildClientToken(action) + + request["ProductCode"] = "dcdn" + request["ImmediatelyRelease"] = "0" + request["ProductType"] = "dcdn_dcdnserviceplan_public_cn" + if client.IsInternationalAccount() { + request["ProductType"] = "dcdn_dcdnserviceplan_public_intl" + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), query, request, &runtime) + request["ClientToken"] = buildClientToken(action) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + if !client.IsInternationalAccount() && IsExpectedErrors(err, []string{"NotApplicable"}) { + request["ProductCode"] = "dcdn" + request["ProductType"] = "dcdn_dcdnserviceplan_public_intl" + conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational) + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + if NotFoundError(err) { + return nil + } + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + return nil } diff --git a/alicloud/service_alicloud_esa_v2.go b/alicloud/service_alicloud_esa_v2.go index 2622e4950754..d62e8d3a6064 100644 --- a/alicloud/service_alicloud_esa_v2.go +++ b/alicloud/service_alicloud_esa_v2.go @@ -312,6 +312,11 @@ func (s *EsaServiceV2) DescribeEsaRatePlanInstance(id string) (object map[string return object, WrapErrorf(Error(GetNotFoundMessage("RatePlanInstance", id)), NotFoundMsg, response) } + currentStatus := v.([]interface{})[0].(map[string]interface{})["Status"] + if currentStatus == "offline" { + return object, WrapErrorf(Error(GetNotFoundMessage("RatePlanInstance", id)), NotFoundMsg, response) + } + return v.([]interface{})[0].(map[string]interface{}), nil } func (s *EsaServiceV2) DescribeDescribeRatePlanInstanceStatus(id string) (object map[string]interface{}, err error) { @@ -358,6 +363,13 @@ func (s *EsaServiceV2) EsaRatePlanInstanceStateRefreshFunc(id string, field stri v, err := jsonpath.Get(field, object) currentStatus := fmt.Sprint(v) + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + for _, failState := range failStates { if currentStatus == failState { return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) diff --git a/website/docs/r/esa_rate_plan_instance.html.markdown b/website/docs/r/esa_rate_plan_instance.html.markdown index d6d7ccc2f6d8..465b1f710367 100644 --- a/website/docs/r/esa_rate_plan_instance.html.markdown +++ b/website/docs/r/esa_rate_plan_instance.html.markdown @@ -20,12 +20,6 @@ For information about ESA Rate Plan Instance and how to use it, see [What is Rat Basic Usage -
- ```terraform variable "name" { default = "terraform-example" @@ -47,10 +41,6 @@ resource "alicloud_esa_rate_plan_instance" "default" { } ``` -### Deleting `alicloud_esa_rate_plan_instance` or removing it from your configuration - -Terraform cannot destroy resource `alicloud_esa_rate_plan_instance`. Terraform will remove this resource from the state file, however resources may remain. - ## Argument Reference The following arguments are supported: @@ -67,7 +57,7 @@ The following arguments are supported: global: global. overseas: Global (excluding Mainland China). -* `payment_type` - (Optional, ForceNew, Computed) The payment type of the resource, Valid vales: Subscription. +* `payment_type` - (Optional, ForceNew, Computed) The payment type of the resource * `period` - (Optional, Int) Purchase cycle (in months). * `plan_name` - (Optional) The plan name, which is obtained from the DescribeRatePlanPrice interface. * `type` - (Optional) Site access type: @@ -100,6 +90,7 @@ The following attributes are exported: The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: * `create` - (Defaults to 5 mins) Used when create the Rate Plan Instance. +* `delete` - (Defaults to 5 mins) Used when delete the Rate Plan Instance. * `update` - (Defaults to 5 mins) Used when update the Rate Plan Instance. ## Import