From 7f69b0fc96ea1525367b341cb50d055b142fc1e8 Mon Sep 17 00:00:00 2001 From: ioito Date: Thu, 28 Apr 2022 14:42:19 +0800 Subject: [PATCH] fix(region): qcloud account read only --- pkg/multicloud/qcloud/instance.go | 42 +++------- pkg/multicloud/qcloud/monitor.go | 2 +- pkg/multicloud/qcloud/network.go | 2 +- pkg/multicloud/qcloud/networkinterface.go | 17 ++-- pkg/multicloud/qcloud/qcloud.go | 83 ++++++++----------- .../qcloud/shell/networkinterface.go | 11 +-- pkg/multicloud/qcloud/tags.go | 2 +- 7 files changed, 68 insertions(+), 91 deletions(-) diff --git a/pkg/multicloud/qcloud/instance.go b/pkg/multicloud/qcloud/instance.go index 25b14b3ce9..c4cf3de66a 100644 --- a/pkg/multicloud/qcloud/instance.go +++ b/pkg/multicloud/qcloud/instance.go @@ -21,9 +21,7 @@ import ( "strings" "time" - qcommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" sdkerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" - qvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -271,12 +269,9 @@ func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) { } func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) { - var ( - classic = self.VirtualPrivateCloud.VpcId == "" - region = self.host.zone.region - - nics []cloudprovider.ICloudNic - ) + classic := self.VirtualPrivateCloud.VpcId == "" + region := self.host.zone.region + ret := []cloudprovider.ICloudNic{} if classic { for _, ipAddr := range self.PrivateIpAddresses { nic := SInstanceNic{ @@ -284,39 +279,28 @@ func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) { ipAddr: ipAddr, classic: true, } - nics = append(nics, &nic) + ret = append(ret, &nic) } } - client, err := region.client.getVpcClient(region.GetId()) + nics, _, err := region.GetNetworkInterfaces(nil, self.InstanceId, "", 0, 10) if err != nil { - return nil, err + return nil, errors.Wrapf(err, "GetNetworkInterfaces") } - req := qvpc.NewDescribeNetworkInterfacesRequest() - req.Filters = []*qvpc.Filter{ - { - Values: qcommon.StringPtrs([]string{self.InstanceId}), - Name: qcommon.StringPtr("attachment.instance-id"), - }, - } - resp, err := client.DescribeNetworkInterfaces(req) - if err != nil { - return nil, err - } - for _, networkInterface := range resp.Response.NetworkInterfaceSet { + for _, networkInterface := range nics { nic := &SInstanceNic{ instance: self, - id: String(networkInterface.NetworkInterfaceId), - macAddr: strings.ToLower(String(networkInterface.MacAddress)), + id: String(&networkInterface.NetworkInterfaceId), + macAddr: strings.ToLower(networkInterface.MacAddress), classic: classic, } for _, addr := range networkInterface.PrivateIpAddressSet { - if Bool(addr.Primary) { - nic.ipAddr = String(addr.PrivateIpAddress) + if addr.Primary { + nic.ipAddr = addr.PrivateIpAddress } } - nics = append(nics, nic) + ret = append(ret, nic) } - return nics, nil + return ret, nil } func (self *SInstance) GetVcpuCount() int { diff --git a/pkg/multicloud/qcloud/monitor.go b/pkg/multicloud/qcloud/monitor.go index fa676964e4..98c30939c0 100644 --- a/pkg/multicloud/qcloud/monitor.go +++ b/pkg/multicloud/qcloud/monitor.go @@ -74,7 +74,7 @@ type SBatchQueryMetricDataInput struct { func (r *SRegion) metricsRequest(action string, params map[string]string) (jsonutils.JSONObject, error) { client := r.GetClient() - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } diff --git a/pkg/multicloud/qcloud/network.go b/pkg/multicloud/qcloud/network.go index f7b8b03c00..a28296cdaf 100644 --- a/pkg/multicloud/qcloud/network.go +++ b/pkg/multicloud/qcloud/network.go @@ -79,7 +79,7 @@ func (self *SRegion) DeleteNetwork(networkId string) error { interfaces := []SNetworkInterface{} for { - _interfaces, total, err := self.GetNetworkInterfaces([]string{}, networkId, len(interfaces), 50) + _interfaces, total, err := self.GetNetworkInterfaces([]string{}, "", networkId, len(interfaces), 50) if err != nil { return errors.Wrapf(err, "DeleteNetwork.GetNetworkInterfaces") } diff --git a/pkg/multicloud/qcloud/networkinterface.go b/pkg/multicloud/qcloud/networkinterface.go index 1e6f118b7b..50db0a3740 100644 --- a/pkg/multicloud/qcloud/networkinterface.go +++ b/pkg/multicloud/qcloud/networkinterface.go @@ -130,7 +130,7 @@ func (nic *SNetworkInterface) GetICloudInterfaceAddresses() ([]cloudprovider.ICl func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) { interfaces := []SNetworkInterface{} for { - parts, total, err := region.GetNetworkInterfaces([]string{}, "", len(interfaces), 50) + parts, total, err := region.GetNetworkInterfaces([]string{}, "", "", len(interfaces), 50) if err != nil { return nil, err } @@ -149,7 +149,7 @@ func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInt return ret, nil } -func (region *SRegion) GetNetworkInterfaces(interfaceIds []string, subnetId string, offset int, limit int) ([]SNetworkInterface, int, error) { +func (region *SRegion) GetNetworkInterfaces(nicIds []string, instanceId, subnetId string, offset int, limit int) ([]SNetworkInterface, int, error) { if limit > 50 || limit <= 0 { limit = 50 } @@ -157,13 +157,20 @@ func (region *SRegion) GetNetworkInterfaces(interfaceIds []string, subnetId stri params["Limit"] = fmt.Sprintf("%d", limit) params["Offset"] = fmt.Sprintf("%d", offset) - for idx, interfaceId := range interfaceIds { + for idx, interfaceId := range nicIds { params[fmt.Sprintf("NetworkInterfaceIds.%d", idx)] = interfaceId } + idx := 0 if len(subnetId) > 0 { - params["Filters.0.Name"] = "subnet-id" - params["Filters.0.Values.0"] = subnetId + params[fmt.Sprintf("Filters.%d.Name", idx)] = "subnet-id" + params[fmt.Sprintf("Filters.%d.Values.0", idx)] = subnetId + idx++ + } + if len(instanceId) > 0 { + params[fmt.Sprintf("Filters.%d.Name", idx)] = "attachment.instance-id" + params[fmt.Sprintf("Filters.%d.Values.0", idx)] = instanceId + idx++ } body, err := region.vpcRequest("DescribeNetworkInterfaces", params) if err != nil { diff --git a/pkg/multicloud/qcloud/qcloud.go b/pkg/multicloud/qcloud/qcloud.go index 9b130f6b24..1fd23bfaed 100644 --- a/pkg/multicloud/qcloud/qcloud.go +++ b/pkg/multicloud/qcloud/qcloud.go @@ -30,7 +30,6 @@ import ( sdkerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" tchttp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" - qvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "github.com/tencentyun/cos-go-sdk-v5" "github.com/tencentyun/cos-go-sdk-v5/debug" @@ -441,9 +440,6 @@ func (r *QcloudResponse) GetResponse() *interface{} { func _jsonRequest(client *common.Client, domain string, version string, apiName string, params map[string]string, updateFun func(string, string), debug bool, retry bool) (jsonutils.JSONObject, error) { req := &tchttp.BaseRequest{} - if region, ok := params["Region"]; ok { - client = client.Init(region) - } _profile := profile.NewClientProfile() _profile.SignMethod = common.SHA256 client.WithProfile(_profile) @@ -475,9 +471,6 @@ func _jsonRequest(client *common.Client, domain string, version string, apiName // todo: 添加自定义response参数 func _phpJsonRequest(client *common.Client, resp qcloudResponse, domain string, path string, version string, apiName string, params map[string]string, updateFunc func(string, string), debug bool) (jsonutils.JSONObject, error) { req := &phpJsonRequest{Path: path} - if region, ok := params["Region"]; ok { - client = client.Init(region) - } _profile := profile.NewClientProfile() _profile.SignMethod = common.SHA256 client.WithProfile(_profile) @@ -600,8 +593,14 @@ func (client *SQcloudClient) GetRegions() []SRegion { return regions } -func (client *SQcloudClient) getDefaultClient() (*common.Client, error) { - return client.getSdkClient(QCLOUD_DEFAULT_REGION) +func (client *SQcloudClient) getDefaultClient(params map[string]string) (*common.Client, error) { + regionId := QCLOUD_DEFAULT_REGION + if len(params) > 0 { + if region, ok := params["Region"]; ok { + regionId = region + } + } + return client.getSdkClient(regionId) } func (client *SQcloudClient) getSdkClient(regionId string) (*common.Client, error) { @@ -641,22 +640,8 @@ func (client *SQcloudClient) getSdkClient(regionId string) (*common.Client, erro return cli, nil } -func (client *SQcloudClient) getVpcClient(regionId string) (*qvpc.Client, error) { - cli, err := client.getSdkClient(regionId) - if err != nil { - return nil, err - } - vpcClient := &qvpc.Client{ - Client: *cli, - } - cpf := profile.NewClientProfile() - cpf.HttpProfile.Endpoint = apiDomainByRegion("vpc", regionId) - vpcClient.WithProfile(cpf) - return vpcClient, nil -} - func (client *SQcloudClient) tkeRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -664,7 +649,7 @@ func (client *SQcloudClient) tkeRequest(apiName string, params map[string]string } func (client *SQcloudClient) vpcRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -672,7 +657,7 @@ func (client *SQcloudClient) vpcRequest(apiName string, params map[string]string } func (client *SQcloudClient) auditRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -680,7 +665,7 @@ func (client *SQcloudClient) auditRequest(apiName string, params map[string]stri } func (client *SQcloudClient) cbsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -688,7 +673,7 @@ func (client *SQcloudClient) cbsRequest(apiName string, params map[string]string } func (client *SQcloudClient) accountRequestRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -696,7 +681,7 @@ func (client *SQcloudClient) accountRequestRequest(apiName string, params map[st } func (client *SQcloudClient) clbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -704,7 +689,7 @@ func (client *SQcloudClient) clbRequest(apiName string, params map[string]string } func (client *SQcloudClient) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -712,7 +697,7 @@ func (client *SQcloudClient) lbRequest(apiName string, params map[string]string) } func (client *SQcloudClient) cdbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -720,7 +705,7 @@ func (client *SQcloudClient) cdbRequest(apiName string, params map[string]string } func (client *SQcloudClient) esRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -729,7 +714,7 @@ func (client *SQcloudClient) esRequest(apiName string, params map[string]string) } func (client *SQcloudClient) kafkaRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -738,7 +723,7 @@ func (client *SQcloudClient) kafkaRequest(apiName string, params map[string]stri } func (client *SQcloudClient) redisRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -747,7 +732,7 @@ func (client *SQcloudClient) redisRequest(apiName string, params map[string]stri } func (client *SQcloudClient) dcdbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -756,7 +741,7 @@ func (client *SQcloudClient) dcdbRequest(apiName string, params map[string]strin } func (client *SQcloudClient) mongodbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -765,7 +750,7 @@ func (client *SQcloudClient) mongodbRequest(apiName string, params map[string]st } func (client *SQcloudClient) memcachedRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -774,7 +759,7 @@ func (client *SQcloudClient) memcachedRequest(apiName string, params map[string] } func (client *SQcloudClient) mariadbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -782,7 +767,7 @@ func (client *SQcloudClient) mariadbRequest(apiName string, params map[string]st } func (client *SQcloudClient) postgresRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -790,7 +775,7 @@ func (client *SQcloudClient) postgresRequest(apiName string, params map[string]s } func (client *SQcloudClient) sqlserverRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -799,7 +784,7 @@ func (client *SQcloudClient) sqlserverRequest(apiName string, params map[string] // deprecated func (client *SQcloudClient) wssRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -807,7 +792,7 @@ func (client *SQcloudClient) wssRequest(apiName string, params map[string]string } func (client *SQcloudClient) sslRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -815,7 +800,7 @@ func (client *SQcloudClient) sslRequest(apiName string, params map[string]string } func (client *SQcloudClient) dnsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -823,7 +808,7 @@ func (client *SQcloudClient) dnsRequest(apiName string, params map[string]string } func (client *SQcloudClient) vpc2017Request(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -831,7 +816,7 @@ func (client *SQcloudClient) vpc2017Request(apiName string, params map[string]st } func (client *SQcloudClient) billingRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -839,7 +824,7 @@ func (client *SQcloudClient) billingRequest(apiName string, params map[string]st } func (client *SQcloudClient) camRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -847,7 +832,7 @@ func (client *SQcloudClient) camRequest(apiName string, params map[string]string } func (client *SQcloudClient) cdnRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -855,7 +840,7 @@ func (client *SQcloudClient) cdnRequest(apiName string, params map[string]string } func (client *SQcloudClient) stsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } @@ -863,7 +848,7 @@ func (client *SQcloudClient) stsRequest(apiName string, params map[string]string } func (client *SQcloudClient) jsonRequest(apiName string, params map[string]string, retry bool) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err } diff --git a/pkg/multicloud/qcloud/shell/networkinterface.go b/pkg/multicloud/qcloud/shell/networkinterface.go index 283a6ea538..6d5bbbbeee 100644 --- a/pkg/multicloud/qcloud/shell/networkinterface.go +++ b/pkg/multicloud/qcloud/shell/networkinterface.go @@ -21,13 +21,14 @@ import ( func init() { type NetworkInterfaceListOptions struct { - Subnet string - Ids []string - Limit int `help:"page size"` - Offset int `help:"page offset"` + Subnet string + Ids []string + InstanceId string + Limit int `help:"page size"` + Offset int `help:"page offset"` } shellutils.R(&NetworkInterfaceListOptions{}, "network-interface-list", "List network interfaces", func(cli *qcloud.SRegion, args *NetworkInterfaceListOptions) error { - interfaces, total, err := cli.GetNetworkInterfaces(args.Ids, args.Subnet, args.Offset, args.Limit) + interfaces, total, err := cli.GetNetworkInterfaces(args.Ids, args.InstanceId, args.Subnet, args.Offset, args.Limit) if err != nil { return err } diff --git a/pkg/multicloud/qcloud/tags.go b/pkg/multicloud/qcloud/tags.go index 826bae7bca..305a6e2ecc 100644 --- a/pkg/multicloud/qcloud/tags.go +++ b/pkg/multicloud/qcloud/tags.go @@ -35,7 +35,7 @@ func tagRequest(client *common.Client, apiName string, params map[string]string, } func (client *SQcloudClient) tagRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { - cli, err := client.getDefaultClient() + cli, err := client.getDefaultClient(params) if err != nil { return nil, err }