From b5a45e8535df15e6e595736bda7bd1105fb9f3c2 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Tue, 3 Aug 2021 10:27:32 +0800 Subject: [PATCH] fix(region): mongodb sync fix --- pkg/compute/models/mongodb.go | 11 +++++++---- pkg/multicloud/aliyun/mongodb.go | 27 ++++++++++++++++++++------ pkg/multicloud/aliyun/shell/mongodb.go | 8 ++++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/pkg/compute/models/mongodb.go b/pkg/compute/models/mongodb.go index bc7a707d77..5fb36546b7 100644 --- a/pkg/compute/models/mongodb.go +++ b/pkg/compute/models/mongodb.go @@ -17,6 +17,7 @@ package models import ( "context" "fmt" + "strings" "time" "yunion.io/x/jsonutils" @@ -510,11 +511,13 @@ func (self *SCloudregion) newFromCloudMongoDB(ctx context.Context, userCred mccl ins.ReplicationNum = ext.GetReplicationNum() if zoneId := ext.GetZoneId(); len(zoneId) > 0 { - _zone, err := db.FetchByExternalId(ZoneManager, zoneId) - if err != nil { - return nil, errors.Wrapf(err, "") + zones, _ := self.GetZones() + for _, zone := range zones { + if strings.HasSuffix(zone.ExternalId, zoneId) { + ins.ZoneId = zone.Id + break + } } - ins.ZoneId = _zone.GetId() } createdAt := ext.GetCreatedAt() diff --git a/pkg/multicloud/aliyun/mongodb.go b/pkg/multicloud/aliyun/mongodb.go index bb3e5a4d22..52b46e609f 100644 --- a/pkg/multicloud/aliyun/mongodb.go +++ b/pkg/multicloud/aliyun/mongodb.go @@ -171,11 +171,11 @@ func (self *SMongoDB) GetNetworkId() string { } func (self *SMongoDB) GetZoneId() string { - if strings.Contains(self.ZoneId, ",") { + if !strings.Contains(self.ZoneId, ",") { return self.ZoneId } - if info := strings.Split(self.ZoneId, "-"); len(info) == 3 { - return strings.Join([]string{info[0], info[1], string(info[2][strings.Index(info[2], ",")-1])}, "-") + if index := strings.Index(self.ZoneId, ",") - 1; index > 0 { + return fmt.Sprintf("%s-%s", self.region.RegionId, string(self.ZoneId[index])) } return "" } @@ -237,10 +237,10 @@ func (self *SMongoDB) GetVmemSizeMb() int { return 0 } -func (self *SRegion) GetICloudMongoDBs() ([]cloudprovider.ICloudMongoDB, error) { +func (self *SRegion) GetMongoDBsByType(mongoType string) ([]SMongoDB, error) { dbs := []SMongoDB{} for { - part, total, err := self.GetMongoDBs(100, len(dbs)/100) + part, total, err := self.GetMongoDBs(mongoType, 100, len(dbs)/100) if err != nil { return nil, errors.Wrapf(err, "GetMongoDB") } @@ -249,6 +249,18 @@ func (self *SRegion) GetICloudMongoDBs() ([]cloudprovider.ICloudMongoDB, error) break } } + return dbs, nil +} + +func (self *SRegion) GetICloudMongoDBs() ([]cloudprovider.ICloudMongoDB, error) { + dbs := []SMongoDB{} + for _, mongoType := range []string{"sharding", "replicate", "serverless"} { + part, err := self.GetMongoDBsByType(mongoType) + if err != nil { + return nil, err + } + dbs = append(dbs, part...) + } ret := []cloudprovider.ICloudMongoDB{} for i := range dbs { dbs[i].region = self @@ -257,7 +269,7 @@ func (self *SRegion) GetICloudMongoDBs() ([]cloudprovider.ICloudMongoDB, error) return ret, nil } -func (self *SRegion) GetMongoDBs(pageSize int, pageNum int) ([]SMongoDB, int, error) { +func (self *SRegion) GetMongoDBs(mongoType string, pageSize int, pageNum int) ([]SMongoDB, int, error) { if pageSize < 1 || pageSize > 100 { pageSize = 100 } @@ -269,6 +281,9 @@ func (self *SRegion) GetMongoDBs(pageSize int, pageNum int) ([]SMongoDB, int, er "PageSize": fmt.Sprintf("%d", pageSize), "PageNumber": fmt.Sprintf("%d", pageNum), } + if len(mongoType) > 0 { + params["DBInstanceType"] = mongoType + } resp, err := self.mongodbRequest("DescribeDBInstances", params) if err != nil { return nil, 0, errors.Wrapf(err, "DescribeDBInstances") diff --git a/pkg/multicloud/aliyun/shell/mongodb.go b/pkg/multicloud/aliyun/shell/mongodb.go index ff5740ea48..4d37945c5c 100644 --- a/pkg/multicloud/aliyun/shell/mongodb.go +++ b/pkg/multicloud/aliyun/shell/mongodb.go @@ -25,12 +25,12 @@ import ( func init() { type MongoDBListOptions struct { - Id string - Offset int - Limit int + MongoType string `choices:"sharding|replicate|serverless"` + Offset int + Limit int } shellutils.R(&MongoDBListOptions{}, "mongodb-list", "List mongodb", func(cli *aliyun.SRegion, args *MongoDBListOptions) error { - dbs, _, err := cli.GetMongoDBs(args.Limit, args.Offset) + dbs, _, err := cli.GetMongoDBs(args.MongoType, args.Limit, args.Offset) if err != nil { return err }