yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/rds_mysql_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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SMySQLInstanceDatabase struct {
    28  	rds *SMySQLInstance
    29  	multicloud.SResourceBase
    30  	QcloudTags
    31  
    32  	CharacterSet string
    33  	DatabaseName string
    34  }
    35  
    36  func (self *SMySQLInstanceDatabase) GetStatus() string {
    37  	return api.DBINSTANCE_DATABASE_RUNNING
    38  }
    39  
    40  func (self *SMySQLInstanceDatabase) GetId() string {
    41  	return self.DatabaseName
    42  }
    43  
    44  func (self *SMySQLInstanceDatabase) GetName() string {
    45  	return self.DatabaseName
    46  }
    47  
    48  func (self *SMySQLInstanceDatabase) GetGlobalId() string {
    49  	return self.DatabaseName
    50  }
    51  
    52  func (self *SMySQLInstanceDatabase) GetCharacterSet() string {
    53  	return self.CharacterSet
    54  }
    55  
    56  func (self *SMySQLInstanceDatabase) Delete() error {
    57  	return cloudprovider.ErrNotSupported
    58  }
    59  
    60  func (self *SRegion) DescribeMySQLDatabases(instanceId string, offset, limit int) ([]SMySQLInstanceDatabase, int, error) {
    61  	if limit < 1 || limit > 100 {
    62  		limit = 100
    63  	}
    64  	params := map[string]string{
    65  		"Offset":     fmt.Sprintf("%d", offset),
    66  		"Limit":      fmt.Sprintf("%d", limit),
    67  		"InstanceId": instanceId,
    68  	}
    69  	resp, err := self.cdbRequest("DescribeDatabases", params)
    70  	if err != nil {
    71  		return nil, 0, errors.Wrapf(err, "DescribeDatabases")
    72  	}
    73  	databases := []SMySQLInstanceDatabase{}
    74  	err = resp.Unmarshal(&databases, "DatabaseList")
    75  	if err != nil {
    76  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
    77  	}
    78  	totalCount, _ := resp.Float("TotalCount")
    79  	return databases, int(totalCount), nil
    80  }
    81  
    82  func (rds *SMySQLInstance) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
    83  	ret := []cloudprovider.ICloudDBInstanceDatabase{}
    84  	for {
    85  		part, total, err := rds.region.DescribeMySQLDatabases(rds.InstanceId, len(ret), 100)
    86  		if err != nil {
    87  			return nil, errors.Wrapf(err, "DescribeMySQLDatabases")
    88  		}
    89  		for i := range part {
    90  			part[i].rds = rds
    91  			ret = append(ret, &part[i])
    92  		}
    93  		if len(ret) >= total || len(part) == 0 {
    94  			break
    95  		}
    96  	}
    97  	return ret, nil
    98  }