diff --git a/alicloud/provider.go b/alicloud/provider.go index e54b748e7539..016940f919d8 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1296,7 +1296,7 @@ func Provider() terraform.ResourceProvider { "alicloud_ga_bandwidth_package": resourceAliCloudGaBandwidthPackage(), "alicloud_ga_endpoint_group": resourceAliCloudGaEndpointGroup(), "alicloud_brain_industrial_pid_organization": resourceAlicloudBrainIndustrialPidOrganization(), - "alicloud_ga_bandwidth_package_attachment": resourceAlicloudGaBandwidthPackageAttachment(), + "alicloud_ga_bandwidth_package_attachment": resourceAliCloudGaBandwidthPackageAttachment(), "alicloud_ga_ip_set": resourceAliCloudGaIpSet(), "alicloud_ga_forwarding_rule": resourceAliCloudGaForwardingRule(), "alicloud_eipanycast_anycast_eip_address": resourceAliCloudEipanycastAnycastEipAddress(), diff --git a/alicloud/resource_alicloud_ga_bandwidth_package_attachment.go b/alicloud/resource_alicloud_ga_bandwidth_package_attachment.go index 117c666e34b8..f014adeb6213 100644 --- a/alicloud/resource_alicloud_ga_bandwidth_package_attachment.go +++ b/alicloud/resource_alicloud_ga_bandwidth_package_attachment.go @@ -12,12 +12,12 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -func resourceAlicloudGaBandwidthPackageAttachment() *schema.Resource { +func resourceAliCloudGaBandwidthPackageAttachment() *schema.Resource { return &schema.Resource{ - Create: resourceAlicloudGaBandwidthPackageAttachmentCreate, - Read: resourceAlicloudGaBandwidthPackageAttachmentRead, - Update: resourceAlicloudGaBandwidthPackageAttachmentUpdate, - Delete: resourceAlicloudGaBandwidthPackageAttachmentDelete, + Create: resourceAliCloudGaBandwidthPackageAttachmentCreate, + Read: resourceAliCloudGaBandwidthPackageAttachmentRead, + Update: resourceAliCloudGaBandwidthPackageAttachmentUpdate, + Delete: resourceAliCloudGaBandwidthPackageAttachmentDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -39,9 +39,7 @@ func resourceAlicloudGaBandwidthPackageAttachment() *schema.Resource { "accelerators": { Type: schema.TypeList, Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, + Elem: &schema.Schema{Type: schema.TypeString}, }, "status": { Type: schema.TypeString, @@ -51,7 +49,7 @@ func resourceAlicloudGaBandwidthPackageAttachment() *schema.Resource { } } -func resourceAlicloudGaBandwidthPackageAttachmentCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudGaBandwidthPackageAttachmentCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) gaService := GaService{client} var response map[string]interface{} @@ -62,12 +60,15 @@ func resourceAlicloudGaBandwidthPackageAttachmentCreate(d *schema.ResourceData, return WrapError(err) } + request["RegionId"] = client.RegionId request["AcceleratorId"] = d.Get("accelerator_id") request["BandwidthPackageId"] = d.Get("bandwidth_package_id") - request["RegionId"] = client.RegionId + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutCreate)), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) if err != nil { if IsExpectedErrors(err, []string{"StateError.BandwidthPackage", "StateError.Accelerator", "GreaterThanGa.IpSetBandwidth", "BandwidthIllegal.BandwidthPackage", "NotExist.BasicBandwidthPackage"}) || NeedRetry(err) { wait() @@ -83,52 +84,58 @@ func resourceAlicloudGaBandwidthPackageAttachmentCreate(d *schema.ResourceData, return WrapErrorf(err, DefaultErrorMsg, "alicloud_ga_bandwidth_package_attachment", action, AlibabaCloudSdkGoERROR) } - d.SetId(fmt.Sprint(request["AcceleratorId"], ":", response["BandwidthPackageId"])) - stateConf := BuildStateConf([]string{}, []string{"active"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, gaService.GaBandwidthPackageAttachmentStateRefreshFunc(d.Id(), []string{})) + d.SetId(fmt.Sprintf("%v:%v", request["AcceleratorId"], response["BandwidthPackageId"])) + + stateConf := BuildStateConf([]string{}, []string{"binded"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, gaService.GaBandwidthPackageAttachmentStateRefreshFunc(d.Id(), []string{})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } - return resourceAlicloudGaBandwidthPackageAttachmentRead(d, meta) + return resourceAliCloudGaBandwidthPackageAttachmentRead(d, meta) } -func resourceAlicloudGaBandwidthPackageAttachmentRead(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudGaBandwidthPackageAttachmentRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) gaService := GaService{client} + if !strings.Contains(d.Id(), ":") { - d.SetId(fmt.Sprint(d.Get("accelerator_id"), ":", d.Id())) + d.SetId(fmt.Sprintf("%v:%v", d.Get("accelerator_id"), d.Id())) } + object, err := gaService.DescribeGaBandwidthPackageAttachment(d.Id()) if err != nil { - if NotFoundError(err) { + if !d.IsNewResource() && NotFoundError(err) { log.Printf("[DEBUG] Resource alicloud_ga_bandwidth_package_attachment gaService.DescribeGaBandwidthPackageAttachment Failed!!! %s", err) d.SetId("") return nil } return WrapError(err) } + parts, err := ParseResourceId(d.Id(), 2) if err != nil { return WrapError(err) } - d.Set("bandwidth_package_id", parts[1]) d.Set("accelerator_id", parts[0]) + d.Set("bandwidth_package_id", parts[1]) d.Set("accelerators", []string{parts[0]}) d.Set("status", object["State"]) return nil } -func resourceAlicloudGaBandwidthPackageAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudGaBandwidthPackageAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) gaService := GaService{client} var response map[string]interface{} update := false + parts, err := ParseResourceId(d.Id(), 2) if err != nil { return WrapError(err) } + request := map[string]interface{}{ "RegionId": client.RegionId, "AcceleratorId": parts[0], @@ -150,9 +157,11 @@ func resourceAlicloudGaBandwidthPackageAttachmentUpdate(d *schema.ResourceData, return WrapError(err) } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) if err != nil { if IsExpectedErrors(err, []string{"StateError.BandwidthPackage", "StateError.Accelerator", "GreaterThanGa.IpSetBandwidth", "BandwidthIllegal.BandwidthPackage", "NotExist.BasicBandwidthPackage"}) || NeedRetry(err) { wait() @@ -170,41 +179,46 @@ func resourceAlicloudGaBandwidthPackageAttachmentUpdate(d *schema.ResourceData, d.SetId(fmt.Sprintf("%v:%v", parts[0], request["BandwidthPackageId"])) - stateConf := BuildStateConf([]string{}, []string{"active"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, gaService.GaBandwidthPackageAttachmentStateRefreshFunc(d.Id(), []string{})) + stateConf := BuildStateConf([]string{}, []string{"binded"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, gaService.GaBandwidthPackageAttachmentStateRefreshFunc(d.Id(), []string{})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } - } - return resourceAlicloudGaBandwidthPackageAttachmentRead(d, meta) + return resourceAliCloudGaBandwidthPackageAttachmentRead(d, meta) } -func resourceAlicloudGaBandwidthPackageAttachmentDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudGaBandwidthPackageAttachmentDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) gaService := GaService{client} - if !strings.Contains(d.Id(), ":") { - d.SetId(fmt.Sprint(d.Get("accelerator_id"), ":", d.Id())) - } - parts, err := ParseResourceId(d.Id(), 2) - if err != nil { - return WrapError(err) - } action := "BandwidthPackageRemoveAccelerator" var response map[string]interface{} + conn, err := client.NewGaplusClient() if err != nil { return WrapError(err) } + + if !strings.Contains(d.Id(), ":") { + d.SetId(fmt.Sprintf("%v:%v", d.Get("accelerator_id"), d.Id())) + } + + parts, err := ParseResourceId(d.Id(), 2) + if err != nil { + return WrapError(err) + } + request := map[string]interface{}{ + "RegionId": client.RegionId, + "AcceleratorId": parts[0], "BandwidthPackageId": parts[1], } - request["AcceleratorId"] = parts[0] - request["RegionId"] = client.RegionId + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 3*time.Second) err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutDelete)), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{}) + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) if err != nil { if IsExpectedErrors(err, []string{"StateError.BandwidthPackage", "StateError.Accelerator", "BindExist.CrossDomain", "Exist.EndpointGroup", "Exist.IpSet", "BandwidthPackageCannotUnbind.HasCrossRegion", "BandwidthPackageCannotUnbind.IpSet", "BandwidthPackageCannotUnbind.EndpointGroup"}) || NeedRetry(err) { wait() @@ -217,7 +231,7 @@ func resourceAlicloudGaBandwidthPackageAttachmentDelete(d *schema.ResourceData, addDebug(action, response, request) if err != nil { - if IsExpectedErrors(err, []string{"NotExist.BandwidthPackage", "Exist.EndpointGroup"}) { + if IsExpectedErrors(err, []string{"NotExist.BandwidthPackage", "Exist.EndpointGroup"}) || NotFoundError(err) { return nil } return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) diff --git a/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go b/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go index 53f9efad396a..22af2b82f326 100644 --- a/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go +++ b/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go @@ -2,23 +2,26 @@ package alicloud import ( "fmt" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "testing" "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccAlicloudGaBandwidthPackageAttachment_basic(t *testing.T) { +func TestAccAliCloudGaBandwidthPackageAttachment_basic0(t *testing.T) { var v map[string]interface{} checkoutSupportedRegions(t, true, connectivity.GaSupportRegions) resourceId := "alicloud_ga_bandwidth_package_attachment.default" - ra := resourceAttrInit(resourceId, AlicloudGaBandwidthPackageAttachmentMap) + ra := resourceAttrInit(resourceId, AliCloudGaBandwidthPackageAttachmentMap0) rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { return &GaService{testAccProvider.Meta().(*connectivity.AliyunClient)} }, "DescribeGaBandwidthPackageAttachment") rac := resourceAttrCheckInit(rc, ra) testAccCheck := rac.resourceAttrMapUpdateSet() - testAccConfig := resourceTestAccConfigFunc(resourceId, "", AlicloudGaBandwidthPackageAttachmentBasicDependence) + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testAcc%sAliCloudGaBandwidthPackageAttachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudGaBandwidthPackageAttachmentBasicDependence0) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -29,7 +32,7 @@ func TestAccAlicloudGaBandwidthPackageAttachment_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "accelerator_id": "${local.accelerator_id}", + "accelerator_id": "${data.alicloud_ga_accelerators.default.ids.0}", "bandwidth_package_id": "${alicloud_ga_bandwidth_package.default.id}", }), Check: resource.ComposeTestCheckFunc( @@ -58,33 +61,156 @@ func TestAccAlicloudGaBandwidthPackageAttachment_basic(t *testing.T) { }) } -var AlicloudGaBandwidthPackageAttachmentMap = map[string]string{ +func TestAccAliCloudGaBandwidthPackageAttachment_basic0_twin(t *testing.T) { + var v map[string]interface{} + checkoutSupportedRegions(t, true, connectivity.GaSupportRegions) + resourceId := "alicloud_ga_bandwidth_package_attachment.default" + ra := resourceAttrInit(resourceId, AliCloudGaBandwidthPackageAttachmentMap0) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &GaService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeGaBandwidthPackageAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testAcc%sAliCloudGaBandwidthPackageAttachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudGaBandwidthPackageAttachmentBasicDependence0) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "accelerator_id": "${data.alicloud_ga_accelerators.default.ids.0}", + "bandwidth_package_id": "${alicloud_ga_bandwidth_package.default.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "accelerator_id": CHECKSET, + "bandwidth_package_id": CHECKSET, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAliCloudGaBandwidthPackageAttachment_basic1(t *testing.T) { + var v map[string]interface{} + checkoutSupportedRegions(t, true, connectivity.GaSupportRegions) + resourceId := "alicloud_ga_bandwidth_package_attachment.default" + ra := resourceAttrInit(resourceId, AliCloudGaBandwidthPackageAttachmentMap0) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &GaService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeGaBandwidthPackageAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testAcc%sAliCloudGaBandwidthPackageAttachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudGaBandwidthPackageAttachmentBasicDependence1) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "accelerator_id": "${data.alicloud_ga_basic_accelerators.default.ids.0}", + "bandwidth_package_id": "${alicloud_ga_bandwidth_package.default.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "accelerator_id": CHECKSET, + "bandwidth_package_id": CHECKSET, + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "bandwidth_package_id": "${alicloud_ga_bandwidth_package.update.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "bandwidth_package_id": CHECKSET, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAliCloudGaBandwidthPackageAttachment_basic1_twin(t *testing.T) { + var v map[string]interface{} + checkoutSupportedRegions(t, true, connectivity.GaSupportRegions) + resourceId := "alicloud_ga_bandwidth_package_attachment.default" + ra := resourceAttrInit(resourceId, AliCloudGaBandwidthPackageAttachmentMap0) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &GaService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeGaBandwidthPackageAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testAcc%sAliCloudGaBandwidthPackageAttachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudGaBandwidthPackageAttachmentBasicDependence1) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "accelerator_id": "${data.alicloud_ga_basic_accelerators.default.ids.0}", + "bandwidth_package_id": "${alicloud_ga_bandwidth_package.default.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "accelerator_id": CHECKSET, + "bandwidth_package_id": CHECKSET, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +var AliCloudGaBandwidthPackageAttachmentMap0 = map[string]string{ "accelerators.#": CHECKSET, - "status": "active", + "status": CHECKSET, } -func AlicloudGaBandwidthPackageAttachmentBasicDependence(name string) string { +func AliCloudGaBandwidthPackageAttachmentBasicDependence0(name string) string { return fmt.Sprintf(` variable "name" { default = "%s" } - locals { - accelerator_id = length(data.alicloud_ga_accelerators.default.accelerators) > 0 ? data.alicloud_ga_accelerators.default.accelerators.0.id : alicloud_ga_accelerator.default.0.id - } - data "alicloud_ga_accelerators" "default" { - status = "active" - bandwidth_billing_type = "BandwidthPackage" - } - - resource "alicloud_ga_accelerator" "default" { - count = length(data.alicloud_ga_accelerators.default.accelerators) > 0 ? 0 : 1 - duration = 1 - spec = "1" - accelerator_name = var.name - auto_use_coupon = true - description = var.name + status = "active" + bandwidth_billing_type = "BandwidthPackage" } resource "alicloud_ga_bandwidth_package" "default" { @@ -110,3 +236,31 @@ func AlicloudGaBandwidthPackageAttachmentBasicDependence(name string) string { } `, name) } + +func AliCloudGaBandwidthPackageAttachmentBasicDependence1(name string) string { + return fmt.Sprintf(` + variable "name" { + default = "%s" + } + + data "alicloud_ga_basic_accelerators" "default" { + status = "active" + } + + resource "alicloud_ga_bandwidth_package" "default" { + bandwidth = 20 + type = "Basic" + bandwidth_type = "Basic" + duration = 1 + auto_pay = true + } + + resource "alicloud_ga_bandwidth_package" "update" { + bandwidth = 20 + type = "Basic" + bandwidth_type = "Basic" + duration = 1 + auto_pay = true + } +`, name) +} diff --git a/alicloud/service_alicloud_ga.go b/alicloud/service_alicloud_ga.go index 1e1a2e5f7256..d35ed09a141e 100644 --- a/alicloud/service_alicloud_ga.go +++ b/alicloud/service_alicloud_ga.go @@ -146,32 +146,48 @@ func (s *GaService) GaListenerStateRefreshFunc(id string, failStates []string) r func (s *GaService) DescribeGaBandwidthPackage(id string) (object map[string]interface{}, err error) { var response map[string]interface{} + action := "DescribeBandwidthPackage" + conn, err := s.client.NewGaplusClient() if err != nil { return nil, WrapError(err) } - action := "DescribeBandwidthPackage" + request := map[string]interface{}{ "RegionId": s.client.RegionId, "BandwidthPackageId": id, } + runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(5*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { if IsExpectedErrors(err, []string{"NotExist.BandwidthPackage"}) { - err = WrapErrorf(Error(GetNotFoundMessage("GaBandwidthPackage", id)), NotFoundMsg, ProviderERROR) - return object, err + return object, WrapErrorf(Error(GetNotFoundMessage("Ga:BandwidthPackage", id)), NotFoundWithResponse, response) } - err = WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) - return object, err + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) } - addDebug(action, response, request) + v, err := jsonpath.Get("$", response) if err != nil { return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$", response) } + object = v.(map[string]interface{}) + return object, nil } @@ -187,11 +203,12 @@ func (s *GaService) GaBandwidthPackageStateRefreshFunc(id string, failStates []s } for _, failState := range failStates { - if object["State"].(string) == failState { - return object, object["State"].(string), WrapError(Error(FailedToReachTargetStatus, object["State"].(string))) + if fmt.Sprint(object["State"]) == failState { + return object, fmt.Sprint(object["State"]), WrapError(Error(FailedToReachTargetStatus, fmt.Sprint(object["State"]))) } } - return object, object["State"].(string), nil + + return object, fmt.Sprint(object["State"]), nil } } @@ -372,49 +389,37 @@ func (s *GaService) DescribeGaIpSet(id string) (object map[string]interface{}, e } func (s *GaService) DescribeGaBandwidthPackageAttachment(id string) (object map[string]interface{}, err error) { - var response map[string]interface{} parts, err := ParseResourceId(id, 2) if err != nil { return nil, WrapError(err) } - conn, err := s.client.NewGaplusClient() + + object, err = s.DescribeGaBandwidthPackage(fmt.Sprint(parts[1])) if err != nil { - return nil, WrapError(err) - } - action := "DescribeAccelerator" - request := map[string]interface{}{ - "RegionId": s.client.RegionId, - "AcceleratorId": parts[0], + return object, WrapError(err) } - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime) - if err != nil { - if IsExpectedErrors(err, []string{"UnknownError"}) { - err = WrapErrorf(Error(GetNotFoundMessage("GaBandwidthPackageAttachment", id)), NotFoundMsg, ProviderERROR) - return object, err + + idExist := false + if accelerators, ok := object["Accelerators"]; ok { + acceleratorList := accelerators.([]interface{}) + for _, accelerator := range acceleratorList { + if accelerator == fmt.Sprint(parts[0]) { + idExist = true + return object, nil + } } - err = WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) - return object, err } - addDebug(action, response, request) - v, err := jsonpath.Get("$", response) - if err != nil { - return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$", response) - } - object = v.(map[string]interface{}) - basic, exist1 := object["BasicBandwidthPackage"] - cross, exist2 := object["CrossDomainBandwidthPackage"] - if (exist1 && basic.(map[string]interface{})["InstanceId"] == parts[1]) || (exist2 && cross.(map[string]interface{})["InstanceId"] == parts[1]) { - return object, nil + if !idExist { + return object, WrapErrorf(Error(GetNotFoundMessage("Ga:BandwidthPackageAttachment", id)), NotFoundMsg, ProviderERROR, fmt.Sprint(object["RequestId"])) } - return object, WrapErrorf(Error(GetNotFoundMessage("GaBandwidthPackageAttachment", id)), NotFoundMsg, ProviderERROR) + + return object, nil } -func (s *GaService) GaEndpointGroupStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { +func (s *GaService) GaBandwidthPackageAttachmentStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { return func() (interface{}, string, error) { - object, err := s.DescribeGaEndpointGroup(id) + object, err := s.DescribeGaBandwidthPackageAttachment(id) if err != nil { if NotFoundError(err) { // Set this to nil as if we didn't find anything. @@ -424,17 +429,18 @@ func (s *GaService) GaEndpointGroupStateRefreshFunc(id string, failStates []stri } for _, failState := range failStates { - if object["State"].(string) == failState { - return object, object["State"].(string), WrapError(Error(FailedToReachTargetStatus, object["State"].(string))) + if fmt.Sprint(object["State"]) == failState { + return object, fmt.Sprint(object["State"]), WrapError(Error(FailedToReachTargetStatus, fmt.Sprint(object["State"]))) } } - return object, object["State"].(string), nil + + return object, fmt.Sprint(object["State"]), nil } } -func (s *GaService) GaForwardingRuleStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { +func (s *GaService) GaEndpointGroupStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { return func() (interface{}, string, error) { - object, err := s.DescribeGaForwardingRule(id) + object, err := s.DescribeGaEndpointGroup(id) if err != nil { if NotFoundError(err) { // Set this to nil as if we didn't find anything. @@ -444,18 +450,17 @@ func (s *GaService) GaForwardingRuleStateRefreshFunc(id string, failStates []str } for _, failState := range failStates { - if object["ForwardingRuleStatus"].(string) == failState { - return object, object["ForwardingRuleStatus"].(string), WrapError(Error(FailedToReachTargetStatus, object["ForwardingRuleStatus"].(string))) + if object["State"].(string) == failState { + return object, object["State"].(string), WrapError(Error(FailedToReachTargetStatus, object["State"].(string))) } } - - return object, object["ForwardingRuleStatus"].(string), nil + return object, object["State"].(string), nil } } -func (s *GaService) GaBandwidthPackageAttachmentStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { +func (s *GaService) GaForwardingRuleStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc { return func() (interface{}, string, error) { - object, err := s.DescribeGaBandwidthPackageAttachment(id) + object, err := s.DescribeGaForwardingRule(id) if err != nil { if NotFoundError(err) { // Set this to nil as if we didn't find anything. @@ -465,11 +470,12 @@ func (s *GaService) GaBandwidthPackageAttachmentStateRefreshFunc(id string, fail } for _, failState := range failStates { - if object["State"].(string) == failState { - return object, object["State"].(string), WrapError(Error(FailedToReachTargetStatus, object["State"].(string))) + if object["ForwardingRuleStatus"].(string) == failState { + return object, object["ForwardingRuleStatus"].(string), WrapError(Error(FailedToReachTargetStatus, object["ForwardingRuleStatus"].(string))) } } - return object, object["State"].(string), nil + + return object, object["ForwardingRuleStatus"].(string), nil } } diff --git a/website/docs/r/ga_bandwidth_package_attachment.html.markdown b/website/docs/r/ga_bandwidth_package_attachment.html.markdown index 13826771ee9a..3daa1574259b 100644 --- a/website/docs/r/ga_bandwidth_package_attachment.html.markdown +++ b/website/docs/r/ga_bandwidth_package_attachment.html.markdown @@ -20,24 +20,24 @@ For information about Global Accelerator (GA) Bandwidth Package Attachment and h Basic Usage ```terraform -resource "alicloud_ga_accelerator" "example" { +resource "alicloud_ga_accelerator" "default" { duration = 1 auto_use_coupon = true spec = "1" } -resource "alicloud_ga_bandwidth_package" "example" { - bandwidth = 20 +resource "alicloud_ga_bandwidth_package" "default" { + bandwidth = 100 type = "Basic" bandwidth_type = "Basic" - duration = 1 - auto_pay = true + payment_type = "PayAsYouGo" + billing_type = "PayBy95" ratio = 30 } -resource "alicloud_ga_bandwidth_package_attachment" "example" { - accelerator_id = alicloud_ga_accelerator.example.id - bandwidth_package_id = alicloud_ga_bandwidth_package.example.id +resource "alicloud_ga_bandwidth_package_attachment" "default" { + accelerator_id = alicloud_ga_accelerator.default.id + bandwidth_package_id = alicloud_ga_bandwidth_package.default.id } ``` @@ -45,14 +45,15 @@ resource "alicloud_ga_bandwidth_package_attachment" "example" { The following arguments are supported: -* `accelerator_id` - (Required, ForceNew) The ID of the Global Accelerator instance from which you want to disassociate the bandwidth plan. -* `bandwidth_package_id` - (Required) The ID of the bandwidth plan to disassociate. **NOTE:** From version 1.192.0, `bandwidth_package_id` can be modified. +* `accelerator_id` - (Required, ForceNew) The ID of the Global Accelerator instance. +* `bandwidth_package_id` - (Required) The ID of the Bandwidth Package. **NOTE:** From version 1.192.0, `bandwidth_package_id` can be modified. ## Attributes Reference The following attributes are exported: -* `id` - The resource ID in terraform of Bandwidth Package Attachment. Value as `:`. Before version 1.120.0, the value is ``. +* `id` - The resource ID in terraform of Bandwidth Package Attachment. It formats as `:`. +-> **NOTE:** Before provider version 1.120.0, it formats as ``. * `accelerators` - Accelerators bound with current Bandwidth Package. * `status` - State of Bandwidth Package. @@ -66,8 +67,8 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/d ## Import -Ga Bandwidth Package Attachment can be imported using the id. Format to `:`, e.g. +Ga Bandwidth Package Attachment can be imported using the id, e.g. ```shell -$ terraform import alicloud_ga_bandwidth_package_attachment.example your_accelerator_id:your_bandwidth_package_id +$ terraform import alicloud_ga_bandwidth_package_attachment.example : ```