Skip to content

Commit

Permalink
fix: 阿里云资源组同步
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Apr 21, 2020
1 parent d9575b2 commit f5ed692
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 23 deletions.
33 changes: 28 additions & 5 deletions pkg/multicloud/aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (

ALIYUN_RAM_API_VERSION = "2015-05-01"
ALIYUN_API_VERION_RDS = "2014-08-15"
ALIYUN_RM_API_VERSION = "2020-03-31"
)

type AliyunClientConfig struct {
Expand Down Expand Up @@ -209,6 +210,14 @@ func (self *SAliyunClient) getDefaultClient() (*sdk.Client, error) {
return client, err
}

func (self *SAliyunClient) rmRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
cli, err := self.getDefaultClient()
if err != nil {
return nil, err
}
return jsonRequest(cli, "resourcemanager.aliyuncs.com", ALIYUN_RM_API_VERSION, apiName, params, self.debug)
}

func (self *SAliyunClient) ecsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
cli, err := self.getDefaultClient()
if err != nil {
Expand Down Expand Up @@ -423,11 +432,25 @@ func (self *SAliyunClient) GetIStorageById(id string) (cloudprovider.ICloudStora
return nil, cloudprovider.ErrNotFound
}

func (region *SAliyunClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
// 阿里云并未公布资源组的api地址
// params := map[string]string{}
// body, err := region.ecsRequest("ListResourceGroups", params)
return nil, cloudprovider.ErrNotImplemented
func (self *SAliyunClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
pageSize, pageNumber := 50, 1
resourceGroups := []SResourceGroup{}
for {
parts, total, err := self.GetResourceGroups(pageNumber, pageSize)
if err != nil {
return nil, errors.Wrap(err, "GetResourceGroups")
}
resourceGroups = append(resourceGroups, parts...)
if len(resourceGroups) >= total {
break
}
pageNumber += 1
}
ret := []cloudprovider.ICloudProject{}
for i := range resourceGroups {
ret = append(ret, &resourceGroups[i])
}
return ret, nil
}

func (region *SAliyunClient) GetCapabilities() []string {
Expand Down
4 changes: 4 additions & 0 deletions pkg/multicloud/aliyun/dbinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ func (region *SRegion) RecoveryDBInstanceFromBackup(instanceId string, backupId

}

func (rds *SDBInstance) GetProjectId() string {
return rds.ResourceGroupId
}

func (rds *SDBInstance) CreateDatabase(conf *cloudprovider.SDBInstanceDatabaseCreateConfig) error {
return rds.region.CreateDBInstanceDatabae(rds.DBInstanceId, conf.CharacterSet, conf.Name, conf.Description)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/aliyun/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,5 @@ func (self *SRegion) rebuildDisk(diskId string) error {
}

func (self *SDisk) GetProjectId() string {
return ""
return self.ResourceGroupId
}
7 changes: 4 additions & 3 deletions pkg/multicloud/aliyun/eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ type SEipAddress struct {

OperationLocks string

ChargeType TChargeType
ExpiredTime time.Time
ChargeType TChargeType
ExpiredTime time.Time
ResourceGroupId string
}

func (self *SEipAddress) GetId() string {
Expand Down Expand Up @@ -378,5 +379,5 @@ func (region *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
}

func (self *SEipAddress) GetProjectId() string {
return ""
return self.ResourceGroupId
}
5 changes: 5 additions & 0 deletions pkg/multicloud/aliyun/elasticcache_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type SElasticcache struct {
NodeType string `json:"NodeType"`
CapacityMB int `json:"Capacity"`
Connections int64 `json:"Connections"`
ResourceGroupId string `json:"ResourceGroupId"`
}

type SElasticcacheAttribute struct {
Expand Down Expand Up @@ -851,6 +852,10 @@ func (self *SElasticcache) UpdateAuthMode(noPwdAccess bool) error {
return nil
}

func (self *SElasticcache) GetProjectId() string {
return self.ResourceGroupId
}

func (self *SElasticcache) GetAuthMode() string {
attribute, err := self.GetAttribute()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/aliyun/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ func (region *SRegion) RenewInstance(instanceId string, bc billing.SBillingCycle
}

func (self *SInstance) GetProjectId() string {
return ""
return self.ResourceGroupId
}

func (self *SInstance) GetError() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/aliyun/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,5 @@ func (lb *SLoadbalancer) GetILoadBalancerListeners() ([]cloudprovider.ICloudLoad
}

func (lb *SLoadbalancer) GetProjectId() string {
return ""
return lb.ResourceGroupId
}
52 changes: 50 additions & 2 deletions pkg/multicloud/aliyun/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,61 @@

package aliyun

import "time"
import (
"fmt"
"time"

"yunion.io/x/onecloud/pkg/multicloud"
"yunion.io/x/pkg/errors"
)

type SResourceGroup struct {
multicloud.SResourceBase

type SProject struct {
Status string
AccountId string
DisplayName string
Id string
CreateDate time.Time
Name string
}

func (self *SResourceGroup) GetGlobalId() string {
return self.Id
}

func (self *SResourceGroup) GetId() string {
return self.Id
}

func (self *SResourceGroup) GetName() string {
return self.Name
}

func (self *SResourceGroup) GetStatus() string {
return ""
}

func (self *SAliyunClient) GetResourceGroups(pageNumber int, pageSize int) ([]SResourceGroup, int, error) {
if pageSize <= 0 || pageSize > 100 {
pageSize = 10
}
if pageNumber <= 0 {
pageNumber = 1
}
params := map[string]string{
"PageNumber": fmt.Sprintf("%d", pageNumber),
"PageSize": fmt.Sprintf("%d", pageSize),
}
resp, err := self.rmRequest("ListResourceGroups", params)
if err != nil {
return nil, 0, errors.Wrap(err, "rmRequest.ListResourceGroups")
}
groups := []SResourceGroup{}
err = resp.Unmarshal(&groups, "ResourceGroups", "ResourceGroup")
if err != nil {
return nil, 0, errors.Wrap(err, "resp.Unmarshal")
}
total, _ := resp.Int("TotalCount")
return groups, int(total), nil
}
35 changes: 35 additions & 0 deletions pkg/multicloud/aliyun/shell/resourcegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shell

import (
"yunion.io/x/onecloud/pkg/multicloud/aliyun"
"yunion.io/x/onecloud/pkg/util/shellutils"
)

func init() {
type ResourceGroupListOptions struct {
PageSize int
PageNumber int
}
shellutils.R(&ResourceGroupListOptions{}, "resource-group-list", "List resource group", func(cli *aliyun.SRegion, args *ResourceGroupListOptions) error {
groups, _, err := cli.GetClient().GetResourceGroups(args.PageNumber, args.PageSize)
if err != nil {
return err
}
printList(groups, 0, 0, 0, nil)
return nil
})
}
19 changes: 10 additions & 9 deletions pkg/multicloud/aliyun/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ const (
type SSnapshot struct {
region *SRegion

Progress string
SnapshotId string
SnapshotName string
SourceDiskId string
SourceDiskSize int32
SourceDiskType string
Status SnapshotStatusType
Usage string
Progress string
SnapshotId string
SnapshotName string
SourceDiskId string
SourceDiskSize int32
SourceDiskType string
Status SnapshotStatusType
Usage string
ResourceGroupId string
}

func (self *SSnapshot) GetId() string {
Expand Down Expand Up @@ -200,7 +201,7 @@ func (self *SRegion) DeleteSnapshot(snapshotId string) error {
}

func (self *SSnapshot) GetProjectId() string {
return ""
return self.ResourceGroupId
}

// If snapshot linked images can't be delete
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/aliyun/vswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@ func (vsw *SVSwitch) dissociateWithSNAT() error {
}

func (self *SVSwitch) GetProjectId() string {
return ""
return self.ResourceGroupId
}

0 comments on commit f5ed692

Please sign in to comment.