diff --git a/common/common_volcengine_config.go b/common/common_volcengine_config.go index 9966ade8..69dde773 100644 --- a/common/common_volcengine_config.go +++ b/common/common_volcengine_config.go @@ -85,5 +85,5 @@ func (c *Config) Client() (*SdkClient, error) { func init() { InitLocks() - InitSyncLimit() + //InitSyncLimit() } diff --git a/common/common_volcengine_dispatcher.go b/common/common_volcengine_dispatcher.go index f35bd0e8..4ffca714 100644 --- a/common/common_volcengine_dispatcher.go +++ b/common/common_volcengine_dispatcher.go @@ -1,18 +1,15 @@ package common import ( - "context" "fmt" "time" re "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "golang.org/x/sync/semaphore" - "golang.org/x/time/rate" ) type Dispatcher struct { - rateInfo *RateInfo + //rateInfo *RateInfo } var defaultDispatcher *Dispatcher @@ -25,46 +22,32 @@ func DefaultDispatcher() *Dispatcher { return defaultDispatcher } -type RateInfo struct { - Create *Rate - Read *Rate - Update *Rate - Delete *Rate - Data *Rate -} -type Rate struct { - Limiter *rate.Limiter - Semaphore *semaphore.Weighted -} +func (d *Dispatcher) initDispatcher(resourceService ResourceService, resourceDate *schema.ResourceData, resource *schema.Resource) { -func NewRateLimitDispatcher(r *RateInfo) *Dispatcher { - return &Dispatcher{ - rateInfo: r, - } } func (d *Dispatcher) Create(resourceService ResourceService, resourceDate *schema.ResourceData, resource *schema.Resource) (err error) { - defer func() { - if d.rateInfo != nil && d.rateInfo.Create != nil && d.rateInfo.Create.Semaphore != nil { - d.rateInfo.Create.Semaphore.Release(1) - } - }() - if d.rateInfo != nil && d.rateInfo.Create != nil { - ctx := context.Background() - if d.rateInfo.Create.Limiter != nil { - err = d.rateInfo.Create.Limiter.Wait(ctx) - if err != nil { - return err - } - } - if d.rateInfo.Create.Semaphore != nil { - err = d.rateInfo.Create.Semaphore.Acquire(ctx, 1) - if err != nil { - return err - } - } - - } + //defer func() { + // if d.rateInfo != nil && d.rateInfo.Create != nil && d.rateInfo.Create.Semaphore != nil { + // d.rateInfo.Create.Semaphore.Release(1) + // } + //}() + //if d.rateInfo != nil && d.rateInfo.Create != nil { + // ctx := context.Background() + // if d.rateInfo.Create.Limiter != nil { + // err = d.rateInfo.Create.Limiter.Wait(ctx) + // if err != nil { + // return err + // } + // } + // if d.rateInfo.Create.Semaphore != nil { + // err = d.rateInfo.Create.Semaphore.Acquire(ctx, 1) + // if err != nil { + // return err + // } + // } + // + //} callbacks := resourceService.CreateResource(resourceDate, resource) var calls []SdkCall for _, callback := range callbacks { @@ -81,26 +64,26 @@ func (d *Dispatcher) Create(resourceService ResourceService, resourceDate *schem } func (d *Dispatcher) Update(resourceService ResourceService, resourceDate *schema.ResourceData, resource *schema.Resource) (err error) { - defer func() { - if d.rateInfo != nil && d.rateInfo.Update != nil && d.rateInfo.Update.Semaphore != nil { - d.rateInfo.Update.Semaphore.Release(1) - } - }() - if d.rateInfo != nil && d.rateInfo.Update != nil { - ctx := context.Background() - if d.rateInfo.Update.Limiter != nil { - err = d.rateInfo.Update.Limiter.Wait(ctx) - if err != nil { - return err - } - } - if d.rateInfo.Update.Semaphore != nil { - err = d.rateInfo.Update.Semaphore.Acquire(ctx, 1) - if err != nil { - return err - } - } - } + //defer func() { + // if d.rateInfo != nil && d.rateInfo.Update != nil && d.rateInfo.Update.Semaphore != nil { + // d.rateInfo.Update.Semaphore.Release(1) + // } + //}() + //if d.rateInfo != nil && d.rateInfo.Update != nil { + // ctx := context.Background() + // if d.rateInfo.Update.Limiter != nil { + // err = d.rateInfo.Update.Limiter.Wait(ctx) + // if err != nil { + // return err + // } + // } + // if d.rateInfo.Update.Semaphore != nil { + // err = d.rateInfo.Update.Semaphore.Acquire(ctx, 1) + // if err != nil { + // return err + // } + // } + //} var callbacks []Callback if projectUpdateEnabled, ok := resourceService.(ProjectUpdateEnabled); ok { projectUpdateCallback := NewProjectService(resourceService.GetClient()).ModifyProject(projectUpdateEnabled.ProjectTrn(), @@ -124,26 +107,26 @@ func (d *Dispatcher) Update(resourceService ResourceService, resourceDate *schem } func (d *Dispatcher) Read(resourceService ResourceService, resourceDate *schema.ResourceData, resource *schema.Resource) (err error) { - defer func() { - if d.rateInfo != nil && d.rateInfo.Read != nil && d.rateInfo.Read.Semaphore != nil { - d.rateInfo.Read.Semaphore.Release(1) - } - }() - if d.rateInfo != nil && d.rateInfo.Read != nil { - ctx := context.Background() - if d.rateInfo.Read.Limiter != nil { - err = d.rateInfo.Read.Limiter.Wait(ctx) - if err != nil { - return err - } - } - if d.rateInfo.Read.Semaphore != nil { - err = d.rateInfo.Read.Semaphore.Acquire(ctx, 1) - if err != nil { - return err - } - } - } + //defer func() { + // if d.rateInfo != nil && d.rateInfo.Read != nil && d.rateInfo.Read.Semaphore != nil { + // d.rateInfo.Read.Semaphore.Release(1) + // } + //}() + //if d.rateInfo != nil && d.rateInfo.Read != nil { + // ctx := context.Background() + // if d.rateInfo.Read.Limiter != nil { + // err = d.rateInfo.Read.Limiter.Wait(ctx) + // if err != nil { + // return err + // } + // } + // if d.rateInfo.Read.Semaphore != nil { + // err = d.rateInfo.Read.Semaphore.Acquire(ctx, 1) + // if err != nil { + // return err + // } + // } + //} var ( instance map[string]interface{} @@ -191,26 +174,26 @@ func (d *Dispatcher) Read(resourceService ResourceService, resourceDate *schema. } func (d *Dispatcher) Delete(resourceService ResourceService, resourceDate *schema.ResourceData, resource *schema.Resource) (err error) { - defer func() { - if d.rateInfo != nil && d.rateInfo.Delete != nil && d.rateInfo.Delete.Semaphore != nil { - d.rateInfo.Delete.Semaphore.Release(1) - } - }() - if d.rateInfo != nil && d.rateInfo.Delete != nil { - ctx := context.Background() - if d.rateInfo.Delete.Limiter != nil { - err = d.rateInfo.Delete.Limiter.Wait(ctx) - if err != nil { - return err - } - } - if d.rateInfo.Delete.Semaphore != nil { - err = d.rateInfo.Delete.Semaphore.Acquire(ctx, 1) - if err != nil { - return err - } - } - } + //defer func() { + // if d.rateInfo != nil && d.rateInfo.Delete != nil && d.rateInfo.Delete.Semaphore != nil { + // d.rateInfo.Delete.Semaphore.Release(1) + // } + //}() + //if d.rateInfo != nil && d.rateInfo.Delete != nil { + // ctx := context.Background() + // if d.rateInfo.Delete.Limiter != nil { + // err = d.rateInfo.Delete.Limiter.Wait(ctx) + // if err != nil { + // return err + // } + // } + // if d.rateInfo.Delete.Semaphore != nil { + // err = d.rateInfo.Delete.Semaphore.Acquire(ctx, 1) + // if err != nil { + // return err + // } + // } + //} var ( callbacks []Callback unsubscribeInfo *UnsubscribeInfo @@ -251,26 +234,26 @@ func (d *Dispatcher) Data(resourceService ResourceService, resourceDate *schema. condition map[string]interface{} collection []interface{} ) - defer func() { - if d.rateInfo != nil && d.rateInfo.Data != nil && d.rateInfo.Data.Semaphore != nil { - d.rateInfo.Data.Semaphore.Release(1) - } - }() - if d.rateInfo != nil && d.rateInfo.Data != nil { - ctx := context.Background() - if d.rateInfo.Data.Limiter != nil { - err = d.rateInfo.Data.Limiter.Wait(ctx) - if err != nil { - return err - } - } - if d.rateInfo.Data.Semaphore != nil { - err = d.rateInfo.Data.Semaphore.Acquire(ctx, 1) - if err != nil { - return err - } - } - } + //defer func() { + // if d.rateInfo != nil && d.rateInfo.Data != nil && d.rateInfo.Data.Semaphore != nil { + // d.rateInfo.Data.Semaphore.Release(1) + // } + //}() + //if d.rateInfo != nil && d.rateInfo.Data != nil { + // ctx := context.Background() + // if d.rateInfo.Data.Limiter != nil { + // err = d.rateInfo.Data.Limiter.Wait(ctx) + // if err != nil { + // return err + // } + // } + // if d.rateInfo.Data.Semaphore != nil { + // err = d.rateInfo.Data.Semaphore.Acquire(ctx, 1) + // if err != nil { + // return err + // } + // } + //} info = resourceService.DatasourceResources(resourceDate, resource) condition, err = DataSourceToRequest(resourceDate, resource, info) if err != nil { diff --git a/common/common_volcengine_limit.go b/common/common_volcengine_limit.go new file mode 100644 index 00000000..485835ab --- /dev/null +++ b/common/common_volcengine_limit.go @@ -0,0 +1,45 @@ +package common + +import ( + "fmt" + + "golang.org/x/sync/semaphore" + "golang.org/x/time/rate" +) + +var rateInfoMap map[string]*Rate + +func init() { + rateInfoMap = map[string]*Rate{ + "ecs.RunInstances.2020-04-01": { + Limiter: rate.NewLimiter(4, 10), + Semaphore: semaphore.NewWeighted(14), + }, + "ecs.DescribeInstances.2020-04-01": { + Limiter: rate.NewLimiter(4, 10), + Semaphore: semaphore.NewWeighted(14), + }, + "ecs.DeleteInstance.2020-04-01": { + Limiter: rate.NewLimiter(4, 10), + Semaphore: semaphore.NewWeighted(10), + }, + "vpc.DescribeNetworkInterfaces.2020-04-01": { + Limiter: rate.NewLimiter(4, 10), + Semaphore: semaphore.NewWeighted(10), + }, + "vpc.DescribeSubnets.2020-04-01": { + Limiter: rate.NewLimiter(4, 10), + Semaphore: semaphore.NewWeighted(10), + }, + } +} + +type Rate struct { + Limiter *rate.Limiter + Semaphore *semaphore.Weighted +} + +func GetRateInfoMap(svc, action, version string) *Rate { + key := fmt.Sprintf("%s.%s.%s", svc, action, version) + return rateInfoMap[key] +} diff --git a/common/common_volcengine_sync_limit.go b/common/common_volcengine_sync_limit.go index 5d9121dc..811c9f4c 100644 --- a/common/common_volcengine_sync_limit.go +++ b/common/common_volcengine_sync_limit.go @@ -1,21 +1,15 @@ package common -import ( - "context" - - "golang.org/x/sync/semaphore" -) - -var syncSemaphore *semaphore.Weighted - -func InitSyncLimit() { - syncSemaphore = semaphore.NewWeighted(10) -} - -func Acquire() { - _ = syncSemaphore.Acquire(context.Background(), 1) -} - -func Release() { - syncSemaphore.Release(1) -} +//var syncSemaphore *semaphore.Weighted +// +//func InitSyncLimit() { +// syncSemaphore = semaphore.NewWeighted(10) +//} +// +//func Acquire() { +// _ = syncSemaphore.Acquire(context.Background(), 1) +//} +// +//func Release() { +// syncSemaphore.Release(1) +//} diff --git a/common/common_volcengine_universal_client.go b/common/common_volcengine_universal_client.go index dcf3b736..54da8055 100644 --- a/common/common_volcengine_universal_client.go +++ b/common/common_volcengine_universal_client.go @@ -1,6 +1,7 @@ package common import ( + "context" "strings" "github.com/volcengine/volcengine-go-sdk/volcengine/client" @@ -107,6 +108,27 @@ func getContentType(m ContentType) string { } func (u *Universal) DoCall(info UniversalInfo, input *map[string]interface{}) (output *map[string]interface{}, err error) { + rate := GetRateInfoMap(info.ServiceName, info.Action, info.Version) + if rate == nil { + return u.doCall(info, input) + } + + // 开始限流 + ctx := context.Background() + if err = rate.Limiter.Wait(ctx); err != nil { + return nil, err + } + if err = rate.Semaphore.Acquire(ctx, 1); err != nil { + return nil, err + } + defer func() { + rate.Semaphore.Release(1) + }() + + return u.doCall(info, input) +} + +func (u *Universal) doCall(info UniversalInfo, input *map[string]interface{}) (output *map[string]interface{}, err error) { c := u.newTargetClient(info) op := &request.Operation{ HTTPMethod: u.getMethod(info.HttpMethod), diff --git a/common/common_volcengine_version.go b/common/common_volcengine_version.go index 8359a36d..1144af1c 100644 --- a/common/common_volcengine_version.go +++ b/common/common_volcengine_version.go @@ -2,5 +2,5 @@ package common const ( TerraformProviderName = "terraform-provider-volcengine" - TerraformProviderVersion = "0.0.104" + TerraformProviderVersion = "0.0.105" ) diff --git a/volcengine/ecs/ecs_instance/data_source_volcengine_ecs_instances.go b/volcengine/ecs/ecs_instance/data_source_volcengine_ecs_instances.go index d96e43a8..22b20107 100644 --- a/volcengine/ecs/ecs_instance/data_source_volcengine_ecs_instances.go +++ b/volcengine/ecs/ecs_instance/data_source_volcengine_ecs_instances.go @@ -338,5 +338,5 @@ func DataSourceVolcengineEcsInstances() *schema.Resource { func dataSourceVolcengineInstancesRead(d *schema.ResourceData, meta interface{}) error { ecsService := NewEcsService(meta.(*ve.SdkClient)) - return ve.NewRateLimitDispatcher(rateInfo).Data(ecsService, d, DataSourceVolcengineEcsInstances()) + return ve.DefaultDispatcher().Data(ecsService, d, DataSourceVolcengineEcsInstances()) } diff --git a/volcengine/ecs/ecs_instance/resource_volcengine_ecs_instance.go b/volcengine/ecs/ecs_instance/resource_volcengine_ecs_instance.go index 8398c069..edc98d89 100644 --- a/volcengine/ecs/ecs_instance/resource_volcengine_ecs_instance.go +++ b/volcengine/ecs/ecs_instance/resource_volcengine_ecs_instance.go @@ -382,7 +382,7 @@ func ResourceVolcengineEcsInstance() *schema.Resource { func resourceVolcengineEcsInstanceCreate(d *schema.ResourceData, meta interface{}) (err error) { instanceService := NewEcsService(meta.(*ve.SdkClient)) - err = ve.NewRateLimitDispatcher(rateInfo).Create(instanceService, d, ResourceVolcengineEcsInstance()) + err = ve.DefaultDispatcher().Create(instanceService, d, ResourceVolcengineEcsInstance()) if err != nil { return fmt.Errorf("error on creating ecs instance %q, %s", d.Id(), err) } @@ -391,7 +391,7 @@ func resourceVolcengineEcsInstanceCreate(d *schema.ResourceData, meta interface{ func resourceVolcengineEcsInstanceRead(d *schema.ResourceData, meta interface{}) (err error) { instanceService := NewEcsService(meta.(*ve.SdkClient)) - err = ve.NewRateLimitDispatcher(rateInfo).Read(instanceService, d, ResourceVolcengineEcsInstance()) + err = ve.DefaultDispatcher().Read(instanceService, d, ResourceVolcengineEcsInstance()) if err != nil { return fmt.Errorf("error on reading ecs instance %q, %s", d.Id(), err) } @@ -400,7 +400,7 @@ func resourceVolcengineEcsInstanceRead(d *schema.ResourceData, meta interface{}) func resourceVolcengineEcsInstanceUpdate(d *schema.ResourceData, meta interface{}) (err error) { instanceService := NewEcsService(meta.(*ve.SdkClient)) - err = ve.NewRateLimitDispatcher(rateInfo).Update(instanceService, d, ResourceVolcengineEcsInstance()) + err = ve.DefaultDispatcher().Update(instanceService, d, ResourceVolcengineEcsInstance()) if err != nil { return fmt.Errorf("error on updating ecs instance %q, %s", d.Id(), err) } @@ -409,7 +409,7 @@ func resourceVolcengineEcsInstanceUpdate(d *schema.ResourceData, meta interface{ func resourceVolcengineEcsInstanceDelete(d *schema.ResourceData, meta interface{}) (err error) { instanceService := NewEcsService(meta.(*ve.SdkClient)) - err = ve.NewRateLimitDispatcher(rateInfo).Delete(instanceService, d, ResourceVolcengineEcsInstance()) + err = ve.DefaultDispatcher().Delete(instanceService, d, ResourceVolcengineEcsInstance()) if err != nil { return fmt.Errorf("error on deleting ecs instance %q, %s", d.Id(), err) } diff --git a/volcengine/ecs/ecs_instance/service_volcengine_ecs_instance.go b/volcengine/ecs/ecs_instance/service_volcengine_ecs_instance.go index 3e113659..0625eeb9 100644 --- a/volcengine/ecs/ecs_instance/service_volcengine_ecs_instance.go +++ b/volcengine/ecs/ecs_instance/service_volcengine_ecs_instance.go @@ -18,37 +18,8 @@ import ( "github.com/volcengine/terraform-provider-volcengine/logger" "github.com/volcengine/terraform-provider-volcengine/volcengine/ecs/ecs_deployment_set_associate" "github.com/volcengine/terraform-provider-volcengine/volcengine/vpc/subnet" - "golang.org/x/sync/semaphore" - "golang.org/x/time/rate" ) -var rateInfo *ve.RateInfo - -func init() { - rateInfo = &ve.RateInfo{ - Create: &ve.Rate{ - Limiter: rate.NewLimiter(4, 10), - Semaphore: semaphore.NewWeighted(14), - }, - Update: &ve.Rate{ - Limiter: rate.NewLimiter(4, 10), - Semaphore: semaphore.NewWeighted(14), - }, - Read: &ve.Rate{ - Limiter: rate.NewLimiter(4, 10), - Semaphore: semaphore.NewWeighted(14), - }, - Delete: &ve.Rate{ - Limiter: rate.NewLimiter(4, 10), - Semaphore: semaphore.NewWeighted(14), - }, - Data: &ve.Rate{ - Limiter: rate.NewLimiter(4, 10), - Semaphore: semaphore.NewWeighted(14), - }, - } -} - type VolcengineEcsService struct { Client *ve.SdkClient SubnetService *subnet.VolcengineSubnetService @@ -76,16 +47,15 @@ func (s *VolcengineEcsService) ReadResources(condition map[string]interface{}) ( networkInterfaceId string ) data, err = ve.WithNextTokenQuery(condition, "MaxResults", "NextToken", 20, nil, func(m map[string]interface{}) ([]interface{}, string, error) { - ecs := s.Client.EcsClient action := "DescribeInstances" logger.Debug(logger.ReqFormat, action, condition) if condition == nil { - resp, err = ecs.DescribeInstancesCommon(nil) + resp, err = s.Client.UniversalClient.DoCall(getUniversalInfo(action), nil) if err != nil { return data, next, err } } else { - resp, err = ecs.DescribeInstancesCommon(&condition) + resp, err = s.Client.UniversalClient.DoCall(getUniversalInfo(action), &condition) if err != nil { return data, next, err } @@ -303,10 +273,8 @@ func (s *VolcengineEcsService) WithResourceResponseHandlers(ecs map[string]inter if _err := recover(); _err != nil { logger.Debug(logger.ReqFormat, "DescribeUserData", _err) } - ve.Release() wg.Done() }() - ve.Acquire() var ( userDataParam *map[string]interface{} userDataResp *map[string]interface{} @@ -315,7 +283,7 @@ func (s *VolcengineEcsService) WithResourceResponseHandlers(ecs map[string]inter userDataParam = &map[string]interface{}{ "InstanceId": instanceId, } - userDataResp, userDataErr = s.Client.EcsClient.DescribeUserDataCommon(userDataParam) + userDataResp, userDataErr = s.Client.UniversalClient.DoCall(getUniversalInfo("DescribeUserData"), userDataParam) if userDataErr != nil { return } @@ -331,10 +299,8 @@ func (s *VolcengineEcsService) WithResourceResponseHandlers(ecs map[string]inter if _err := recover(); _err != nil { logger.Debug(logger.ReqFormat, "DescribeNetworkInterfaces", _err) } - ve.Release() wg.Done() }() - ve.Acquire() var ( networkInterfaceParam *map[string]interface{} networkInterfaceResp *map[string]interface{} @@ -343,7 +309,7 @@ func (s *VolcengineEcsService) WithResourceResponseHandlers(ecs map[string]inter networkInterfaceParam = &map[string]interface{}{ "InstanceId": instanceId, } - networkInterfaceResp, networkInterfaceErr = s.Client.VpcClient.DescribeNetworkInterfacesCommon(networkInterfaceParam) + networkInterfaceResp, networkInterfaceErr = s.Client.UniversalClient.DoCall(getVpcUniversalInfo("DescribeNetworkInterfaces"), networkInterfaceParam) if networkInterfaceErr != nil { return } @@ -545,7 +511,7 @@ func (s *VolcengineEcsService) CreateResource(resourceData *schema.ResourceData, ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //创建ECS - return s.Client.EcsClient.RunInstancesCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("RunInstances"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { //注意 获取内容 这个地方不能是指针 需要转一次 @@ -665,7 +631,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, (*call.SdkParam)["ClientToken"] = uuid.New().String() logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //修改实例属性 - return s.Client.EcsClient.ModifyInstanceAttributeCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("ModifyInstanceAttribute"), call.SdkParam) }, Refresh: &ve.StateRefresh{ Target: []string{"RUNNING", "STOPPED"}, @@ -713,7 +679,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, (*call.SdkParam)["ClientToken"] = uuid.New().String() logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //修改实例计费方式 - return s.Client.EcsClient.ModifyInstanceChargeTypeCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("ModifyInstanceChargeType"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { return nil @@ -745,7 +711,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, }, ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) - return s.Client.VpcClient.ModifyNetworkInterfaceAttributesCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getVpcUniversalInfo("ModifyNetworkInterfaceAttributes"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { return nil @@ -830,7 +796,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, (*call.SdkParam)["ClientToken"] = uuid.New().String() logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //续费实例 - return s.Client.EcsClient.RenewInstanceCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("RenewInstance"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { return nil @@ -875,7 +841,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, (*call.SdkParam)["ClientToken"] = uuid.New().String() logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //修改实例规格 - return s.Client.EcsClient.ModifyInstanceSpecCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("ModifyInstanceSpec"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { return nil @@ -925,7 +891,7 @@ func (s *VolcengineEcsService) ModifyResource(resourceData *schema.ResourceData, }, ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) - return s.Client.EcsClient.ReplaceSystemVolumeCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("ReplaceSystemVolume"), call.SdkParam) }, AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { return nil @@ -997,7 +963,7 @@ func (s *VolcengineEcsService) RemoveResource(resourceData *schema.ResourceData, ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) //删除ECS - return s.Client.EcsClient.DeleteInstanceCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("DeleteInstance"), call.SdkParam) }, CallError: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall, baseErr error) error { //出现错误后重试 @@ -1184,7 +1150,7 @@ func (s *VolcengineEcsService) StartOrStopInstanceCallback(resourceData *schema. } callback.Call.ExecuteCall = func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) - return s.Client.EcsClient.StopInstanceCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("StopInstance"), call.SdkParam) } callback.Call.AfterCall = func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error { *flag = true @@ -1212,7 +1178,7 @@ func (s *VolcengineEcsService) StartOrStopInstanceCallback(resourceData *schema. } callback.Call.ExecuteCall = func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) { logger.Debug(logger.RespFormat, call.Action, call.SdkParam) - return s.Client.EcsClient.StartInstanceCommon(call.SdkParam) + return s.Client.UniversalClient.DoCall(getUniversalInfo("StartInstance"), call.SdkParam) } callback.Call.Refresh = &ve.StateRefresh{ Target: []string{"RUNNING"}, @@ -1250,10 +1216,8 @@ func (s *VolcengineEcsService) readInstanceTypes(sourceData []interface{}) (extr if e := recover(); e != nil { logger.Debug(logger.ReqFormat, action, e) } - ve.Release() wg.Done() }() - ve.Acquire() instanceTypeId, _err = ve.ObtainSdkValue("InstanceTypeId", instance) if _err != nil { @@ -1271,7 +1235,7 @@ func (s *VolcengineEcsService) readInstanceTypes(sourceData []interface{}) (extr "InstanceTypeIds.1": instanceTypeId, } logger.Debug(logger.ReqFormat, action, instanceTypeCondition) - resp, _err = s.Client.EcsClient.DescribeInstanceTypesCommon(&instanceTypeCondition) + resp, _err = s.Client.UniversalClient.DoCall(getUniversalInfo("DescribeInstanceTypes"), &instanceTypeCondition) if _err != nil { syncMap.Store(instanceTypeId, err) return @@ -1343,10 +1307,8 @@ func (s *VolcengineEcsService) readEbsVolumes(sourceData []interface{}) (extraDa if e := recover(); e != nil { logger.Debug(logger.ReqFormat, action, e) } - ve.Release() wg.Done() }() - ve.Acquire() instanceId, _err = ve.ObtainSdkValue("InstanceId", instance) if _err != nil { diff --git a/volcengine/vpc/subnet/service_volcengine_subnet.go b/volcengine/vpc/subnet/service_volcengine_subnet.go index 56133107..8b8877a1 100644 --- a/volcengine/vpc/subnet/service_volcengine_subnet.go +++ b/volcengine/vpc/subnet/service_volcengine_subnet.go @@ -36,16 +36,15 @@ func (s *VolcengineSubnetService) ReadResources(m map[string]interface{}) (data ok bool ) return ve.WithPageNumberQuery(m, "PageSize", "PageNumber", 20, 1, func(condition map[string]interface{}) ([]interface{}, error) { - vpcClient := s.Client.VpcClient action := "DescribeSubnets" logger.Debug(logger.ReqFormat, action, condition) if condition == nil { - resp, err = vpcClient.DescribeSubnetsCommon(nil) + resp, err = s.Client.UniversalClient.DoCall(getUniversalInfo(action), nil) if err != nil { return data, err } } else { - resp, err = vpcClient.DescribeSubnetsCommon(&condition) + resp, err = s.Client.UniversalClient.DoCall(getUniversalInfo(action), &condition) if err != nil { return data, err } @@ -315,3 +314,12 @@ func (s *VolcengineSubnetService) DatasourceResources(*schema.ResourceData, *sch func (s *VolcengineSubnetService) ReadResourceId(id string) string { return id } + +func getUniversalInfo(actionName string) ve.UniversalInfo { + return ve.UniversalInfo{ + ServiceName: "vpc", + Version: "2020-04-01", + HttpMethod: ve.GET, + Action: actionName, + } +}