forked from yunionio/cloudmux
/
dbinstance.go
120 lines (103 loc) · 3.94 KB
/
dbinstance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shell
import (
"fmt"
"strings"
"github.com/nyl1001/pkg/util/shellutils"
"github.com/nyl1001/cloudmux/pkg/multicloud/aliyun"
)
func init() {
type DBInstanceListOptions struct {
Id []string `help:"IDs of instances to show"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&DBInstanceListOptions{}, "dbinstance-list", "List dbintances", func(cli *aliyun.SRegion, args *DBInstanceListOptions) error {
instances, total, e := cli.GetDBInstances(args.Id, args.Offset, args.Limit)
if e != nil {
return e
}
printList(instances, total, args.Offset, args.Limit, []string{})
return nil
})
type DBInstanceIdOptions struct {
ID string `help:"ID of instances to show"`
}
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-show", "Show dbintance", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
instance, err := cli.GetDBInstanceDetail(args.ID)
if err != nil {
return err
}
printObject(instance)
return nil
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-secgroup-list", "List dbintance secgroup", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
secgroupIds, err := cli.GetRdsSecgroupIds(args.ID)
if err != nil {
return err
}
fmt.Println(secgroupIds)
return nil
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-open-public-connection", "Open dbintance public connection", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
return cli.OpenPublicConnection(args.ID)
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-close-public-connection", "Close dbintance public connection", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
return cli.ClosePublicConnection(args.ID)
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-delete", "Delete dbintance", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
return cli.DeleteDBInstance(args.ID)
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-restart", "Restart dbintance", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
return cli.RebootDBInstance(args.ID)
})
shellutils.R(&DBInstanceIdOptions{}, "dbinstance-network-list", "Show dbintance network info", func(cli *aliyun.SRegion, args *DBInstanceIdOptions) error {
networks, err := cli.GetDBInstanceNetInfo(args.ID)
if err != nil {
return err
}
printList(networks, 0, 0, 0, []string{})
return nil
})
type DBInstanceRecoveryOptions struct {
ID string
TARGET string
BACKUP string
Databases []string
}
shellutils.R(&DBInstanceRecoveryOptions{}, "dbinstance-recovery", "Recovery dbintance from backup", func(cli *aliyun.SRegion, args *DBInstanceRecoveryOptions) error {
databases := map[string]string{}
for _, database := range args.Databases {
if len(database) > 0 {
dbInfo := strings.Split(database, ":")
if len(dbInfo) == 1 {
databases[dbInfo[0]] = dbInfo[0]
} else if len(dbInfo) == 2 {
databases[dbInfo[0]] = dbInfo[1]
} else {
return fmt.Errorf("Invalid dbinfo: %s", database)
}
}
}
return cli.RecoveryDBInstanceFromBackup(args.ID, args.TARGET, args.BACKUP, databases)
})
type DBInstanceSetNameOptions struct {
ID string
NAME string
}
shellutils.R(&DBInstanceSetNameOptions{}, "dbinstance-set-name", "Set dbintance name ", func(cli *aliyun.SRegion, args *DBInstanceSetNameOptions) error {
return cli.ModifyDBInstanceName(args.ID, args.NAME)
})
}