yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/rds_database.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/apis"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/client"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/models"
    23  
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SDBInstanceDatabase struct {
    32  	multicloud.SDBInstanceDatabaseBase
    33  	JdcloudTags
    34  
    35  	rds *SDBInstance
    36  	models.Database
    37  }
    38  
    39  func (self *SDBInstanceDatabase) GetCharacterSet() string {
    40  	return self.CharacterSetName
    41  }
    42  
    43  func (self *SDBInstanceDatabase) GetGlobalId() string {
    44  	return self.DbName
    45  }
    46  
    47  func (self *SDBInstanceDatabase) GetId() string {
    48  	return self.DbName
    49  }
    50  
    51  func (self *SDBInstanceDatabase) GetName() string {
    52  	return self.DbName
    53  }
    54  
    55  func (self *SDBInstanceDatabase) GetStatus() string {
    56  	return api.DBINSTANCE_DATABASE_RUNNING
    57  }
    58  
    59  func (self *SRegion) GetDBInstanceDatabases(id string, pageNumber, pageSize int) ([]SDBInstanceDatabase, int, error) {
    60  
    61  	req := apis.NewDescribeDatabasesRequestWithAllParams(self.ID, id, nil, &pageNumber, &pageSize)
    62  	client := client.NewRdsClient(self.getCredential())
    63  	client.Logger = Logger{}
    64  	resp, err := client.DescribeDatabases(req)
    65  	if err != nil {
    66  		return nil, 0, errors.Wrapf(err, "DescribeDatabases")
    67  	}
    68  	if resp.Error.Code >= 400 {
    69  		err = fmt.Errorf(resp.Error.Message)
    70  		return nil, 0, err
    71  	}
    72  	total := resp.Result.TotalCount
    73  	ret := []SDBInstanceDatabase{}
    74  	for i := range resp.Result.Databases {
    75  		ret = append(ret, SDBInstanceDatabase{
    76  			Database: resp.Result.Databases[i],
    77  		})
    78  	}
    79  	return ret, total, nil
    80  }
    81  
    82  func (self *SDBInstance) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
    83  	dbs := []SDBInstanceDatabase{}
    84  	n := 1
    85  	for {
    86  		part, total, err := self.region.GetDBInstanceDatabases(self.InstanceId, n, 100)
    87  		if err != nil {
    88  			return nil, errors.Wrapf(err, "GetDBInstanceDatabases")
    89  		}
    90  		dbs = append(dbs, part...)
    91  		if len(dbs) >= total {
    92  			break
    93  		}
    94  		n++
    95  	}
    96  	ret := []cloudprovider.ICloudDBInstanceDatabase{}
    97  	for i := range dbs {
    98  		dbs[i].rds = self
    99  		ret = append(ret, &dbs[i])
   100  	}
   101  	return ret, nil
   102  }