yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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/aliyun"
    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 *aliyun.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 *aliyun.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-secgroup-list", "List dbintance secgroup", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    53  		secgroupIds, err := cli.GetRdsSecgroupIds(args.ID)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		fmt.Println(secgroupIds)
    58  		return nil
    59  	})
    60  
    61  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-open-public-connection", "Open dbintance public connection", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    62  		return cli.OpenPublicConnection(args.ID)
    63  	})
    64  
    65  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-close-public-connection", "Close dbintance public connection", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    66  		return cli.ClosePublicConnection(args.ID)
    67  	})
    68  
    69  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-delete", "Delete dbintance", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    70  		return cli.DeleteDBInstance(args.ID)
    71  	})
    72  
    73  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-restart", "Restart dbintance", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    74  		return cli.RebootDBInstance(args.ID)
    75  	})
    76  
    77  	shellutils.R(&DBInstanceIdOptions{}, "dbinstance-network-list", "Show dbintance network info", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
    78  		networks, err := cli.GetDBInstanceNetInfo(args.ID)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		printList(networks, 0, 0, 0, []string{})
    83  		return nil
    84  	})
    85  
    86  	type DBInstanceRecoveryOptions struct {
    87  		ID        string
    88  		TARGET    string
    89  		BACKUP    string
    90  		Databases []string
    91  	}
    92  
    93  	shellutils.R(&DBInstanceRecoveryOptions{}, "dbinstance-recovery", "Recovery dbintance from backup", func(cli *aliyun.SRegion, args *DBInstanceRecoveryOptions) error {
    94  		databases := map[string]string{}
    95  		for _, database := range args.Databases {
    96  			if len(database) > 0 {
    97  				dbInfo := strings.Split(database, ":")
    98  				if len(dbInfo) == 1 {
    99  					databases[dbInfo[0]] = dbInfo[0]
   100  
   101  				} else if len(dbInfo) == 2 {
   102  					databases[dbInfo[0]] = dbInfo[1]
   103  				} else {
   104  					return fmt.Errorf("Invalid dbinfo: %s", database)
   105  				}
   106  			}
   107  		}
   108  		return cli.RecoveryDBInstanceFromBackup(args.ID, args.TARGET, args.BACKUP, databases)
   109  	})
   110  
   111  }