yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/shell/dbinstance.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 shell
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/cloudmux/pkg/multicloud/apsara"
    22  	"yunion.io/x/onecloud/pkg/util/shellutils"
    23  )
    24  
    25  func init() {
    26  	type DBInstanceListOptions struct {
    27  		Id     []string `help:"IDs of instances to show"`
    28  		Limit  int      `help:"page size"`
    29  		Offset int      `help:"page offset"`
    30  	}
    31  	shellutils.R(&DBInstanceListOptions{}, "dbinstance-list", "List dbintances", func(cli *apsara.SRegion, args *DBInstanceListOptions) error {
    32  		instances, total, e := cli.GetDBInstances(args.Id, args.Offset, args.Limit)
    33  		if e != nil {
    34  			return e
    35  		}
    36  		printList(instances, total, args.Offset, args.Limit, []string{})
    37  		return nil
    38  	})
    39  
    40  	type DBInstanceIdOptions struct {
    41  		ID string `help:"ID of instances to show"`
    42  	}
    43  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-show", "Show dbintance", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    44  		instance, err := cli.GetDBInstanceDetail(args.ID)
    45  		if err != nil {
    46  			return err
    47  		}
    48  		printObject(instance)
    49  		return nil
    50  	})
    51  
    52  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-open-public-connection", "Open dbintance public connection", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    53  		return cli.OpenPublicConnection(args.ID)
    54  	})
    55  
    56  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-close-public-connection", "Close dbintance public connection", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    57  		return cli.ClosePublicConnection(args.ID)
    58  	})
    59  
    60  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-delete", "Delete dbintance", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    61  		return cli.DeleteDBInstance(args.ID)
    62  	})
    63  
    64  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-restart", "Restart dbintance", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    65  		return cli.RebootDBInstance(args.ID)
    66  	})
    67  
    68  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-network-list", "Show dbintance network info", func(cli *apsara.SRegion, args *DBInstanceIdOptions) error {
    69  		networks, err := cli.GetDBInstanceNetInfo(args.ID)
    70  		if err != nil {
    71  			return err
    72  		}
    73  		printList(networks, 0, 0, 0, []string{})
    74  		return nil
    75  	})
    76  
    77  	type DBInstanceRecoveryOptions struct {
    78  		ID        string
    79  		TARGET    string
    80  		BACKUP    string
    81  		Databases []string
    82  	}
    83  
    84  	shellutils.R(&DBInstanceRecoveryOptions{}, "dbinstance-recovery", "Recovery dbintance from backup", func(cli *apsara.SRegion, args *DBInstanceRecoveryOptions) error {
    85  		databases := map[string]string{}
    86  		for _, database := range args.Databases {
    87  			if len(database) > 0 {
    88  				dbInfo := strings.Split(database, ":")
    89  				if len(dbInfo) == 1 {
    90  					databases[dbInfo[0]] = dbInfo[0]
    91  
    92  				} else if len(dbInfo) == 2 {
    93  					databases[dbInfo[0]] = dbInfo[1]
    94  				} else {
    95  					return fmt.Errorf("Invalid dbinfo: %s", database)
    96  				}
    97  			}
    98  		}
    99  		return cli.RecoveryDBInstanceFromBackup(args.ID, args.TARGET, args.BACKUP, databases)
   100  	})
   101  
   102  }