@@ -317,6 +317,37 @@ func (me *GaapService) DeleteCertificate(ctx context.Context, id string) error {
317317 return nil
318318}
319319
320+ func waitTaskReady (ctx context.Context , client * gaap.Client , reqeustId string ) error {
321+ logId := tccommon .GetLogId (ctx )
322+
323+ describeRequest := gaap .NewDescribeTaskStatusRequest ()
324+ describeRequest .TaskId = helper .String (reqeustId )
325+
326+ err := resource .Retry (2 * tccommon .WriteRetryTimeout , func () * resource.RetryError {
327+ ratelimit .Check (describeRequest .GetAction ())
328+ response , err := client .DescribeTaskStatus (describeRequest )
329+ if err != nil {
330+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" ,
331+ logId , describeRequest .GetAction (), describeRequest .ToJsonString (), err )
332+ return tccommon .RetryError (err )
333+ }
334+ // 任务状态:RUNNING,FAIL,SUCCESS
335+ status := * response .Response .Status
336+ if status == "SUCCESS" {
337+ return nil
338+ } else if status == "FAIL" {
339+ return resource .NonRetryableError (fmt .Errorf ("Task[%s] failed" , reqeustId ))
340+ } else {
341+ return resource .RetryableError (fmt .Errorf ("Task[%s] status: %s" , reqeustId , status ))
342+ }
343+ })
344+ if err != nil {
345+ log .Printf ("[CRITAL]%s task failed, reason: %v" , logId , err )
346+ return err
347+ }
348+ return nil
349+ }
350+
320351func (me * GaapService ) CreateProxy (
321352 ctx context.Context ,
322353 name , accessRegion , realserverRegion string ,
@@ -326,7 +357,7 @@ func (me *GaapService) CreateProxy(
326357) (id string , err error ) {
327358 logId := tccommon .GetLogId (ctx )
328359 client := me .client .UseGaapClient ()
329-
360+ var createProxyRequestId string
330361 createRequest := gaap .NewCreateProxyRequest ()
331362 createRequest .ProxyName = & name
332363 createRequest .ProjectId = helper .IntInt64 (projectId )
@@ -362,57 +393,15 @@ func (me *GaapService) CreateProxy(
362393 }
363394
364395 id = * response .Response .InstanceId
396+ createProxyRequestId = * response .Response .RequestId
365397 return nil
366398 }); err != nil {
367399 log .Printf ("[CRITAL]%s create proxy failed, reason: %v" , logId , err )
368400 return "" , err
369401 }
370402
371- describeRequest := gaap .NewDescribeProxiesRequest ()
372- describeRequest .ProxyIds = []* string {& id }
373-
374- if err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
375- ratelimit .Check (describeRequest .GetAction ())
376-
377- response , err := client .DescribeProxies (describeRequest )
378- if err != nil {
379- log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" ,
380- logId , describeRequest .GetAction (), describeRequest .ToJsonString (), err )
381- return tccommon .RetryError (err )
382- }
383-
384- proxies := response .Response .ProxySet
385-
386- switch len (proxies ) {
387- case 0 :
388- err := errors .New ("read no proxy" )
389- log .Printf ("[DEBUG]%s %v" , logId , err )
390- return resource .RetryableError (err )
391-
392- default :
393- err := errors .New ("return more than 1 proxy" )
394- log .Printf ("[DEBUG]%s %v" , logId , err )
395- return resource .NonRetryableError (err )
396-
397- case 1 :
398- }
399-
400- proxy := proxies [0 ]
401- if proxy .Status == nil {
402- err := errors .New ("proxy status is nil" )
403- log .Printf ("[CRITAL]%s %v" , logId , err )
404- return resource .NonRetryableError (err )
405- }
406-
407- if * proxy .Status != GAAP_PROXY_RUNNING {
408- err := errors .New ("proxy is still creating" )
409- log .Printf ("[DEBUG]%s %v" , logId , err )
410- return resource .RetryableError (err )
411- }
412-
413- return nil
414- }); err != nil {
415- log .Printf ("[CRITAL]%s create proxy failed, reason: %v" , logId , err )
403+ time .Sleep (3 * time .Second )
404+ if err := waitTaskReady (ctx , client , createProxyRequestId ); err != nil {
416405 return "" , err
417406 }
418407
@@ -717,7 +706,7 @@ func (me *GaapService) ModifyProxyProjectId(ctx context.Context, id string, proj
717706func (me * GaapService ) ModifyProxyConfiguration (ctx context.Context , id string , bandwidth , concurrent * int ) error {
718707 logId := tccommon .GetLogId (ctx )
719708 client := me .client .UseGaapClient ()
720-
709+ var modifyProxyRequestId string
721710 modifyRequest := gaap .NewModifyProxyConfigurationRequest ()
722711 modifyRequest .ProxyId = & id
723712 if bandwidth != nil {
@@ -731,56 +720,21 @@ func (me *GaapService) ModifyProxyConfiguration(ctx context.Context, id string,
731720 ratelimit .Check (modifyRequest .GetAction ())
732721 modifyRequest .ClientToken = helper .String (helper .BuildToken ())
733722
734- if _ , err := client .ModifyProxyConfiguration (modifyRequest ); err != nil {
723+ response , err := client .ModifyProxyConfiguration (modifyRequest )
724+ if err != nil {
735725 log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" ,
736726 logId , modifyRequest .GetAction (), modifyRequest .ToJsonString (), err )
737727 return tccommon .RetryError (err )
738728 }
729+ modifyProxyRequestId = * response .Response .RequestId
739730 return nil
740731 }); err != nil {
741732 log .Printf ("[CRITAL]%s modify proxy configuration failed, reason: %v" , logId , err )
742733 return err
743734 }
744735
745- time .Sleep (5 * time .Second )
746-
747- describeRequest := gaap .NewDescribeProxiesRequest ()
748- describeRequest .ProxyIds = []* string {& id }
749-
750- if err := resource .Retry (3 * tccommon .ReadRetryTimeout , func () * resource.RetryError {
751- ratelimit .Check (describeRequest .GetAction ())
752-
753- response , err := client .DescribeProxies (describeRequest )
754- if err != nil {
755- log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]" ,
756- logId , describeRequest .GetAction (), describeRequest .ToJsonString (), err )
757- return tccommon .RetryError (err )
758- }
759-
760- proxies := response .Response .ProxySet
761-
762- if len (proxies ) != 1 {
763- err := fmt .Errorf ("api[%s] read %d proxies" , describeRequest .GetAction (), len (proxies ))
764- log .Printf ("[CRITAL]%s %v" , logId , err )
765- return resource .NonRetryableError (err )
766- }
767-
768- proxy := proxies [0 ]
769- if proxy .Status == nil {
770- err := fmt .Errorf ("api[%s] proxy status is nil" , describeRequest .GetAction ())
771- log .Printf ("[CRITAL]%s %v" , logId , err )
772- return resource .NonRetryableError (err )
773- }
774-
775- if * proxy .Status == GAAP_PROXY_ADJUSTING {
776- err := errors .New ("proxy is still modifying" )
777- log .Printf ("[DEBUG]%s %v" , logId , err )
778- return resource .RetryableError (err )
779- }
780-
781- return nil
782- }); err != nil {
783- log .Printf ("[CRITAL]%s modify proxy configuration failed, reason: %v" , logId , err )
736+ time .Sleep (3 * time .Second )
737+ if err := waitTaskReady (ctx , client , modifyProxyRequestId ); err != nil {
784738 return err
785739 }
786740
0 commit comments