Skip to content

Commit

Permalink
create and update proxy using DescribeTaskStatus checks (#2738)
Browse files Browse the repository at this point in the history
Co-authored-by: mikatong <[email protected]>
  • Loading branch information
tongyiming and mikatong authored Jul 24, 2024
1 parent dfba122 commit f497569
Show file tree
Hide file tree
Showing 8 changed files with 1,886 additions and 1,593 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.860
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.962
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.964
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.967
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
Expand All @@ -61,7 +61,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb v1.0.760
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.811
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.777
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.771
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.967
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.729
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.0.777
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.963 h1:lUVC
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.963/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.964 h1:ET3EulYQvWrdD5FNwOP+196w5Vbniy/uRGucM5ILExQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.964/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.962 h1:H7V/7lpplT3r5zW5/W+agrsJC8MCA1/f6zWWrMZYykU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.962/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.950 h1:gsBG5scMDYykLzPSj9aXCTxVjWE5lt+y6VwNAROf7Fw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.950/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.961 h1:UqinjIsO+Y48BQYSxrD6aHQe8SD+Wsbvj+jU9V9VvWM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.961/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.967 h1:ui73H/2pKk2aDCxaBCLAeMB3JlNgdCkn0nx1x0pqvf0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.967/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860/go.mod h1:NZo1WplQcC314kMlCRUoy8NQju2BnolIJj7NAWgsuhY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624 h1:nEZqsoqt1pEoaP9JjkHQy3/H00suCfzlHW1qOm2nYD8=
Expand Down Expand Up @@ -994,6 +990,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.777 h1:G1eX/Dki
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.777/go.mod h1:ehbc9eaNknF/VU/kPTFLZbIUmkL06vQ/JYydu6yeXp4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.771 h1:hdd41TtqgVqdsq68NMEWTKRvJLRPN+Jae9kNvExvRdw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.771/go.mod h1:SzBYz5GyZvZlBHLhKKQ7X/1YaJu73ehlJRx4WK/V7hQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.967 h1:zV/M+XqJjSn5uqTd4gWsEFPhMOgEaRgDPUB5obel9Qk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.967/go.mod h1:d2Rx/0LqYx5fQbGkpen6D27CRKHvxdng8vSeKIJ5ZZU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563 h1:FoX+MK4vHThvPO6FbP5q98zD8S3n+d5+DbtK7skl++c=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1:uom4Nvi9W+Qkom0exYiJ9VWJjXwyxtPYTkKkaLMlfE0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.729 h1:WQpzm3x27Jo/5uh6/Yqe/gbJ7r2ui9hc9ST7Ml2eP9Y=
Expand Down
128 changes: 41 additions & 87 deletions tencentcloud/services/gaap/service_tencentcloud_gaap.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,37 @@ func (me *GaapService) DeleteCertificate(ctx context.Context, id string) error {
return nil
}

func waitTaskReady(ctx context.Context, client *gaap.Client, reqeustId string) error {
logId := tccommon.GetLogId(ctx)

describeRequest := gaap.NewDescribeTaskStatusRequest()
describeRequest.TaskId = helper.String(reqeustId)

err := resource.Retry(2*tccommon.WriteRetryTimeout, func() *resource.RetryError {
ratelimit.Check(describeRequest.GetAction())
response, err := client.DescribeTaskStatus(describeRequest)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]",
logId, describeRequest.GetAction(), describeRequest.ToJsonString(), err)
return tccommon.RetryError(err)
}
// 任务状态:RUNNING,FAIL,SUCCESS
status := *response.Response.Status
if status == "SUCCESS" {
return nil
} else if status == "FAIL" {
return resource.NonRetryableError(fmt.Errorf("Task[%s] failed", reqeustId))
} else {
return resource.RetryableError(fmt.Errorf("Task[%s] status: %s", reqeustId, status))
}
})
if err != nil {
log.Printf("[CRITAL]%s task failed, reason: %v", logId, err)
return err
}
return nil
}

func (me *GaapService) CreateProxy(
ctx context.Context,
name, accessRegion, realserverRegion string,
Expand All @@ -326,7 +357,7 @@ func (me *GaapService) CreateProxy(
) (id string, err error) {
logId := tccommon.GetLogId(ctx)
client := me.client.UseGaapClient()

var createProxyRequestId string
createRequest := gaap.NewCreateProxyRequest()
createRequest.ProxyName = &name
createRequest.ProjectId = helper.IntInt64(projectId)
Expand Down Expand Up @@ -362,57 +393,15 @@ func (me *GaapService) CreateProxy(
}

id = *response.Response.InstanceId
createProxyRequestId = *response.Response.RequestId
return nil
}); err != nil {
log.Printf("[CRITAL]%s create proxy failed, reason: %v", logId, err)
return "", err
}

describeRequest := gaap.NewDescribeProxiesRequest()
describeRequest.ProxyIds = []*string{&id}

if err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
ratelimit.Check(describeRequest.GetAction())

response, err := client.DescribeProxies(describeRequest)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]",
logId, describeRequest.GetAction(), describeRequest.ToJsonString(), err)
return tccommon.RetryError(err)
}

proxies := response.Response.ProxySet

switch len(proxies) {
case 0:
err := errors.New("read no proxy")
log.Printf("[DEBUG]%s %v", logId, err)
return resource.RetryableError(err)

default:
err := errors.New("return more than 1 proxy")
log.Printf("[DEBUG]%s %v", logId, err)
return resource.NonRetryableError(err)

case 1:
}

proxy := proxies[0]
if proxy.Status == nil {
err := errors.New("proxy status is nil")
log.Printf("[CRITAL]%s %v", logId, err)
return resource.NonRetryableError(err)
}

if *proxy.Status != GAAP_PROXY_RUNNING {
err := errors.New("proxy is still creating")
log.Printf("[DEBUG]%s %v", logId, err)
return resource.RetryableError(err)
}

return nil
}); err != nil {
log.Printf("[CRITAL]%s create proxy failed, reason: %v", logId, err)
time.Sleep(3 * time.Second)
if err := waitTaskReady(ctx, client, createProxyRequestId); err != nil {
return "", err
}

Expand Down Expand Up @@ -717,7 +706,7 @@ func (me *GaapService) ModifyProxyProjectId(ctx context.Context, id string, proj
func (me *GaapService) ModifyProxyConfiguration(ctx context.Context, id string, bandwidth, concurrent *int) error {
logId := tccommon.GetLogId(ctx)
client := me.client.UseGaapClient()

var modifyProxyRequestId string
modifyRequest := gaap.NewModifyProxyConfigurationRequest()
modifyRequest.ProxyId = &id
if bandwidth != nil {
Expand All @@ -731,56 +720,21 @@ func (me *GaapService) ModifyProxyConfiguration(ctx context.Context, id string,
ratelimit.Check(modifyRequest.GetAction())
modifyRequest.ClientToken = helper.String(helper.BuildToken())

if _, err := client.ModifyProxyConfiguration(modifyRequest); err != nil {
response, err := client.ModifyProxyConfiguration(modifyRequest)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]",
logId, modifyRequest.GetAction(), modifyRequest.ToJsonString(), err)
return tccommon.RetryError(err)
}
modifyProxyRequestId = *response.Response.RequestId
return nil
}); err != nil {
log.Printf("[CRITAL]%s modify proxy configuration failed, reason: %v", logId, err)
return err
}

time.Sleep(5 * time.Second)

describeRequest := gaap.NewDescribeProxiesRequest()
describeRequest.ProxyIds = []*string{&id}

if err := resource.Retry(3*tccommon.ReadRetryTimeout, func() *resource.RetryError {
ratelimit.Check(describeRequest.GetAction())

response, err := client.DescribeProxies(describeRequest)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]",
logId, describeRequest.GetAction(), describeRequest.ToJsonString(), err)
return tccommon.RetryError(err)
}

proxies := response.Response.ProxySet

if len(proxies) != 1 {
err := fmt.Errorf("api[%s] read %d proxies", describeRequest.GetAction(), len(proxies))
log.Printf("[CRITAL]%s %v", logId, err)
return resource.NonRetryableError(err)
}

proxy := proxies[0]
if proxy.Status == nil {
err := fmt.Errorf("api[%s] proxy status is nil", describeRequest.GetAction())
log.Printf("[CRITAL]%s %v", logId, err)
return resource.NonRetryableError(err)
}

if *proxy.Status == GAAP_PROXY_ADJUSTING {
err := errors.New("proxy is still modifying")
log.Printf("[DEBUG]%s %v", logId, err)
return resource.RetryableError(err)
}

return nil
}); err != nil {
log.Printf("[CRITAL]%s modify proxy configuration failed, reason: %v", logId, err)
time.Sleep(3 * time.Second)
if err := waitTaskReady(ctx, client, modifyProxyRequestId); err != nil {
return err
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f497569

Please sign in to comment.