Skip to content

Commit

Permalink
Bugfix: Add backoff retry to CDN domain and ddoscoo domain data sourc…
Browse files Browse the repository at this point in the history
…es. (#31)
  • Loading branch information
styumyum authored Sep 20, 2023
1 parent ef02809 commit 71bf325
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 57 deletions.
59 changes: 30 additions & 29 deletions alicloud/data_source_cdn_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package alicloud

import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -134,43 +136,42 @@ func (d *cdnDomainDataSource) Read(ctx context.Context, req datasource.ReadReque
return
}

var cdnDomains *alicloudCdnClient.DescribeCdnDomainDetailResponse
var err error
describeCdnDomainDetailRequest := &alicloudCdnClient.DescribeCdnDomainDetailRequest{
DomainName: tea.String(domainName),
}
runtime := &util.RuntimeOptions{}
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()

var cdnDomains *alicloudCdnClient.DescribeCdnDomainDetailResponse
var err error
describeCdnDomain := func() (err error) {
runtime := &util.RuntimeOptions{}

cdnDomains, err = d.client.DescribeCdnDomainDetailWithOptions(describeCdnDomainDetailRequest, runtime)
if err != nil {
return err
}

return nil
}()

if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else if *_t.Code == *tea.String("InvalidDomain.NotFound") {
return nil
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
return
}

_, err := util.AssertAsString(error.Message)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Query CDN Domains",
err.Error(),
)
return
}
// Retry backoff
reconnectBackoff := backoff.NewExponentialBackOff()
reconnectBackoff.MaxElapsedTime = 30 * time.Second
err = backoff.Retry(describeCdnDomain, reconnectBackoff)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Describe CDN Domain",
err.Error(),
)
return
}

if cdnDomains.String() != "{}" {
Expand Down
55 changes: 27 additions & 28 deletions alicloud/data_source_ddoscoo_domain_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package alicloud

import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -127,43 +129,40 @@ func (d *ddoscooDomainResourcesDataSource) Read(ctx context.Context, req datasou
return
}

var antiddosCooWebRules *alicloudAntiddosClient.DescribeWebRulesResponse
var err error
describeWebRulesRequest := &alicloudAntiddosClient.DescribeWebRulesRequest{
Domain: tea.String(domainName),
PageSize: tea.Int32(10),
}
runtime := &util.RuntimeOptions{}
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()

var antiddosCooWebRules *alicloudAntiddosClient.DescribeWebRulesResponse
var err error
describeWebRules := func() (err error) {
runtime := &util.RuntimeOptions{}

antiddosCooWebRules, err = d.client.DescribeWebRulesWithOptions(describeWebRulesRequest, runtime)
if err != nil {
return err
}
return nil
}()

if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
return
}

_, err := util.AssertAsString(error.Message)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to query AntiDDoS web rules",
err.Error(),
)
return
}
reconnectBackoff := backoff.NewExponentialBackOff()
reconnectBackoff.MaxElapsedTime = 30 * time.Second
err = backoff.Retry(describeWebRules, reconnectBackoff)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Describe Antiddos Web Rule.",
err.Error(),
)
return
}

if antiddosCooWebRules.String() != "{}" && *antiddosCooWebRules.Body.TotalCount > int64(0) {
Expand Down

0 comments on commit 71bf325

Please sign in to comment.