mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #11033 from ioito/automated-cherry-pick-of-#11032-upstream-release-3.7
Automated cherry pick of #11032: fix(region): aliyun vpc endpoint
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user