yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/dbinstance_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 apsara
    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/multicloud"
    24  )
    25  
    26  type SDBInstanceDatabase struct {
    27  	multicloud.SDBInstanceDatabaseBase
    28  	ApsaraTags
    29  	instance *SDBInstance
    30  
    31  	CharacterSetName string
    32  	DBDescription    string
    33  	DBInstanceId     string
    34  	DBName           string
    35  	DBStatus         string
    36  	Engine           string
    37  }
    38  
    39  func (database *SDBInstanceDatabase) GetId() string {
    40  	return database.DBName
    41  }
    42  
    43  func (database *SDBInstanceDatabase) GetGlobalId() string {
    44  	return database.DBName
    45  }
    46  
    47  func (database *SDBInstanceDatabase) GetName() string {
    48  	return database.DBName
    49  }
    50  
    51  func (database *SDBInstanceDatabase) GetStatus() string {
    52  	switch database.DBStatus {
    53  	case "Creating":
    54  		return api.DBINSTANCE_DATABASE_CREATING
    55  	case "Running":
    56  		return api.DBINSTANCE_DATABASE_RUNNING
    57  	case "Deleting":
    58  		return api.DBINSTANCE_DATABASE_DELETING
    59  	}
    60  	return database.DBStatus
    61  }
    62  
    63  func (database *SDBInstanceDatabase) GetCharacterSet() string {
    64  	return database.CharacterSetName
    65  }
    66  
    67  func (database *SDBInstanceDatabase) Delete() error {
    68  	return database.instance.region.DeleteDBInstanceDatabase(database.DBInstanceId, database.DBName)
    69  }
    70  
    71  func (region *SRegion) DeleteDBInstanceDatabase(instanceId string, dbName string) error {
    72  	params := map[string]string{
    73  		"DBInstanceId": instanceId,
    74  		"DBName":       dbName,
    75  	}
    76  
    77  	_, err := region.rdsRequest("DeleteDatabase", params)
    78  	return err
    79  }
    80  
    81  func (region *SRegion) CreateDBInstanceDatabae(instanceId, characterSet, dbName, desc string) error {
    82  	params := map[string]string{
    83  		"DBInstanceId":     instanceId,
    84  		"DBName":           dbName,
    85  		"CharacterSetName": characterSet,
    86  		"DBDescription":    desc,
    87  	}
    88  
    89  	_, err := region.rdsRequest("CreateDatabase", params)
    90  	return err
    91  
    92  }
    93  
    94  func (region *SRegion) GetDBInstanceDatabases(instanceId, dbName string, offset int, limit int) ([]SDBInstanceDatabase, int, error) {
    95  	if limit > 500 || limit <= 0 {
    96  		limit = 500
    97  	}
    98  	params := map[string]string{
    99  		"RegionId":     region.RegionId,
   100  		"PageSize":     fmt.Sprintf("%d", limit),
   101  		"PageNumber":   fmt.Sprintf("%d", (offset/limit)+1),
   102  		"DBInstanceId": instanceId,
   103  	}
   104  	if len(dbName) > 0 {
   105  		params["DBName"] = dbName
   106  	}
   107  	body, err := region.rdsRequest("DescribeDatabases", params)
   108  	if err != nil {
   109  		return nil, 0, errors.Wrap(err, "DescribeDatabases")
   110  	}
   111  	databases := []SDBInstanceDatabase{}
   112  	err = body.Unmarshal(&databases, "Databases", "Database")
   113  	if err != nil {
   114  		return nil, 0, errors.Wrap(err, "Unmarshal")
   115  	}
   116  	total, _ := body.Int("TotalRecordCount")
   117  	return databases, int(total), nil
   118  }