From c2fbd90bc6c9e8a17aa461a33c8b57f0d78590ad Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Tue, 11 May 2021 15:19:36 +0800 Subject: [PATCH] fix(region): aliyun vpc endpoint --- pkg/multicloud/aliyun/aliyun.go | 37 ++++++++++++++++++++++++++++++ pkg/multicloud/aliyun/region.go | 4 ++-- pkg/multicloud/aliyun/shell/vpc.go | 15 ++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/multicloud/aliyun/aliyun.go b/pkg/multicloud/aliyun/aliyun.go index 37917a6090..991c9d2e87 100644 --- a/pkg/multicloud/aliyun/aliyun.go +++ b/pkg/multicloud/aliyun/aliyun.go @@ -124,6 +124,7 @@ type SAliyunClient struct { ownerName string nasEndpoints map[string]string + vpcEndpoints map[string]string iregions []cloudprovider.ICloudRegion iBuckets []cloudprovider.ICloudBucket @@ -133,6 +134,7 @@ func NewAliyunClient(cfg *AliyunClientConfig) (*SAliyunClient, error) { client := SAliyunClient{ AliyunClientConfig: cfg, nasEndpoints: map[string]string{}, + vpcEndpoints: map[string]string{}, } err := client.fetchRegions() if err != nil { @@ -288,6 +290,41 @@ func (self *SAliyunClient) getDefaultClient() (*sdk.Client, error) { return client, err } +func (self *SAliyunClient) getVpcEndpoint(regionId string) string { + err := self.fetchVpcEndpoints() + if err != nil { + return "vpc.aliyuncs.com" + } + ep, ok := self.vpcEndpoints[regionId] + if ok && len(ep) > 0 { + return ep + } + return "vpc.aliyuncs.com" +} + +func (self *SAliyunClient) fetchVpcEndpoints() error { + if len(self.vpcEndpoints) > 0 { + return nil + } + client, err := self.getDefaultClient() + if err != nil { + return errors.Wrapf(err, "getDefaultClient") + } + resp, err := jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, "DescribeRegions", nil, self.debug) + if err != nil { + return errors.Wrapf(err, "DescribeRegions") + } + regions := []SRegion{} + err = resp.Unmarshal(®ions, "Regions", "Region") + if err != nil { + return errors.Wrapf(err, "resp.Unmarshal") + } + for _, region := range regions { + self.vpcEndpoints[region.RegionId] = region.RegionEndpoint + } + return nil +} + func (self *SAliyunClient) getSdkClient(regionId string) (*sdk.Client, error) { transport := httputils.GetAdaptiveTransport(true) transport.Proxy = self.cpcfg.ProxyFunc diff --git a/pkg/multicloud/aliyun/region.go b/pkg/multicloud/aliyun/region.go index 25ce7e2c20..5b8f7fc6fe 100644 --- a/pkg/multicloud/aliyun/region.go +++ b/pkg/multicloud/aliyun/region.go @@ -149,8 +149,8 @@ func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonut if err != nil { return nil, err } - - return jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, action, params, self.client.debug) + endpoint := self.GetClient().getVpcEndpoint(self.RegionId) + return jsonRequest(client, endpoint, ALIYUN_API_VERSION_VPC, action, params, self.client.debug) } func (self *SRegion) nasRequest(action string, params map[string]string) (jsonutils.JSONObject, error) { diff --git a/pkg/multicloud/aliyun/shell/vpc.go b/pkg/multicloud/aliyun/shell/vpc.go index f611913c5f..7ed23aafb5 100644 --- a/pkg/multicloud/aliyun/shell/vpc.go +++ b/pkg/multicloud/aliyun/shell/vpc.go @@ -33,6 +33,21 @@ func init() { return nil }) + type VpcCreateOptions struct { + Name string + Desc string + CIDR string + } + + shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *aliyun.SRegion, args *VpcCreateOptions) error { + vpc, err := cli.CreateIVpc(args.Name, args.Desc, args.CIDR) + if err != nil { + return err + } + printObject(vpc) + return nil + }) + type VpcOptions struct { ID string `help:"VPC id"` }