fix(region): mongodb sync fix

This commit is contained in:
Qu Xuan
2021-08-03 11:00:46 +08:00
parent 5548a00f2d
commit b5a45e8535
3 changed files with 32 additions and 14 deletions
+7 -4
View File
@@ -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()
+21 -6
View File
@@ -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")
+4 -4
View File
@@ -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
}