diff --git a/alicloud/provider.go b/alicloud/provider.go index 0fb61d2a2bb3..4e013e20dda3 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1271,7 +1271,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..42f6c4d427d6 100644 --- a/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go +++ b/alicloud/resource_alicloud_ga_bandwidth_package_attachment_test.go @@ -60,7 +60,7 @@ func TestAccAlicloudGaBandwidthPackageAttachment_basic(t *testing.T) { var AlicloudGaBandwidthPackageAttachmentMap = map[string]string{ "accelerators.#": CHECKSET, - "status": "active", + "status": CHECKSET, } func AlicloudGaBandwidthPackageAttachmentBasicDependence(name string) string { diff --git a/alicloud/service_alicloud_ga.go b/alicloud/service_alicloud_ga.go index e6c54665146e..1d988eb5b3e1 100644 --- a/alicloud/service_alicloud_ga.go +++ b/alicloud/service_alicloud_ga.go @@ -344,48 +344,74 @@ 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) + action := "DescribeBandwidthPackage" + + conn, err := s.client.NewGaplusClient() if err != nil { return nil, WrapError(err) } - conn, err := s.client.NewGaplusClient() + + parts, err := ParseResourceId(id, 2) if err != nil { return nil, WrapError(err) } - action := "DescribeAccelerator" + request := map[string]interface{}{ - "RegionId": s.client.RegionId, - "AcceleratorId": parts[0], + "RegionId": s.client.RegionId, + "BandwidthPackageId": parts[1], } + + idExist := false 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, 3*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{"UnknownError"}) { - err = WrapErrorf(Error(GetNotFoundMessage("GaBandwidthPackageAttachment", id)), NotFoundMsg, ProviderERROR) - return object, err + if IsExpectedErrors(err, []string{"NotExist.BandwidthPackage"}) { + return object, WrapErrorf(Error(GetNotFoundMessage("Ga:BandwidthPackageAttachment", id)), NotFoundMsg, ProviderERROR, fmt.Sprint(response["RequestId"])) } - 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{}) - 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 accelerators, ok := object["Accelerators"]; ok { + acceleratorList := accelerators.([]interface{}) + for _, accelerator := range acceleratorList { + if accelerator == fmt.Sprint(parts[0]) { + idExist = true + return object, nil + } + } + } + + if !idExist { + return object, WrapErrorf(Error(GetNotFoundMessage("Ga:BandwidthPackageAttachment", id)), NotFoundWithResponse, response) } - 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. @@ -395,17 +421,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. @@ -415,17 +442,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. @@ -435,11 +462,11 @@ 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 } }