mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-29 03:51:54 +08:00
Merge pull request #4 from yunionio/feature/lzx-aliyuncli
CLI: aliyuncli added
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/yunionio/log"
|
||||
"github.com/yunionio/structarg"
|
||||
"github.com/yunionio/onecloud/pkg/util/aliyun"
|
||||
"github.com/yunionio/onecloud/pkg/util/aliyun/shell"
|
||||
)
|
||||
|
||||
type BaseOptions struct {
|
||||
Help bool `help:"Show help"`
|
||||
AccessKey string `help:"Access key" default:"$ALIYUN_ACCESS_KEY"`
|
||||
Secret string `help:"Secret" default:"$ALIYUN_SECRET"`
|
||||
RegionId string `help:"RegionId" default:"$ALIYUN_REGION"`
|
||||
SUBCOMMAND string `help:"aliyuncli subcommand" subcommand:"true"`
|
||||
}
|
||||
|
||||
func getSubcommandParser() (*structarg.ArgumentParser, error) {
|
||||
parse, e := structarg.NewArgumentParser(&BaseOptions{},
|
||||
"aliyuncli",
|
||||
"Command-line interface to aliyun API.",
|
||||
`See "aliyuncli help COMMAND" for help on a specific command.`)
|
||||
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
subcmd := parse.GetSubcommand()
|
||||
if subcmd == nil {
|
||||
return nil, fmt.Errorf("No subcommand argument.")
|
||||
}
|
||||
type HelpOptions struct {
|
||||
SUBCOMMAND string `help:"sub-command name"`
|
||||
}
|
||||
shell.R(&HelpOptions{}, "help", "Show help of a subcommand", func(args *HelpOptions) error {
|
||||
helpstr, e := subcmd.SubHelpString(args.SUBCOMMAND)
|
||||
if e != nil {
|
||||
return e
|
||||
} else {
|
||||
fmt.Print(helpstr)
|
||||
return nil
|
||||
}
|
||||
})
|
||||
for _, v := range shell.CommandTable {
|
||||
_, e := subcmd.AddSubParser(v.Options, v.Command, v.Desc, v.Callback)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
}
|
||||
return parse, nil
|
||||
}
|
||||
|
||||
func showErrorAndExit(e error) {
|
||||
log.Errorf("%s", e)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func newClient(options *BaseOptions) (*aliyun.SRegion, error) {
|
||||
if len(options.AccessKey) == 0 {
|
||||
return nil, fmt.Errorf("Missing accessKey")
|
||||
}
|
||||
|
||||
if len(options.Secret) == 0 {
|
||||
return nil, fmt.Errorf("Missing secret")
|
||||
}
|
||||
|
||||
cli, err := aliyun.NewAliyunClient("", "", options.AccessKey, options.Secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
region := cli.GetRegion(options.RegionId)
|
||||
if region == nil {
|
||||
return nil, fmt.Errorf("No such region %s", options.RegionId)
|
||||
}
|
||||
|
||||
return region, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
parser, e := getSubcommandParser()
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
e = parser.ParseArgs(os.Args[1:], false)
|
||||
options := parser.Options().(*BaseOptions)
|
||||
|
||||
if options.Help {
|
||||
fmt.Print(parser.HelpString())
|
||||
} else {
|
||||
subcmd := parser.GetSubcommand()
|
||||
subparser := subcmd.GetSubParser()
|
||||
if e != nil {
|
||||
if subparser != nil {
|
||||
fmt.Print(subparser.Usage())
|
||||
} else {
|
||||
fmt.Print(parser.Usage())
|
||||
}
|
||||
showErrorAndExit(e)
|
||||
} else {
|
||||
suboptions := subparser.Options()
|
||||
if options.SUBCOMMAND == "help" {
|
||||
e = subcmd.Invoke(suboptions)
|
||||
} else {
|
||||
var region *aliyun.SRegion
|
||||
region, e = newClient(options)
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
e = subcmd.Invoke(region, suboptions)
|
||||
}
|
||||
if e != nil {
|
||||
showErrorAndExit(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,9 +88,14 @@ func (self *SAliyunClient) fetchRegions() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/*func (self *SAliyunClient) GetRegions() []SRegion {
|
||||
return self.regions
|
||||
}*/
|
||||
func (self *SAliyunClient) GetRegions() []SRegion {
|
||||
regions := make([]SRegion, len(self.iregions))
|
||||
for i := 0; i < len(regions); i += 1 {
|
||||
region := self.iregions[i].(*SRegion)
|
||||
regions[i] = *region
|
||||
}
|
||||
return regions
|
||||
}
|
||||
|
||||
func (self *SAliyunClient) GetIRegions() []cloudprovider.ICloudRegion {
|
||||
return self.iregions
|
||||
|
||||
+14
-14
@@ -22,9 +22,9 @@ type SRegion struct {
|
||||
RegionId string
|
||||
LocalName string
|
||||
|
||||
_izones []cloudprovider.ICloudZone
|
||||
izones []cloudprovider.ICloudZone
|
||||
|
||||
_ivpcs []cloudprovider.ICloudVpc
|
||||
ivpcs []cloudprovider.ICloudVpc
|
||||
|
||||
storageCache *SStoragecache
|
||||
|
||||
@@ -116,14 +116,14 @@ func (self *SRegion) Refresh() error {
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
|
||||
if self._izones == nil {
|
||||
if self.izones == nil {
|
||||
var err error
|
||||
err = self.fetchInfrastructure()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return self._izones, nil
|
||||
return self.izones, nil
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
|
||||
@@ -166,11 +166,11 @@ func (self *SRegion) _fetchZones(chargeType InstanceChargeType, spotStrategy Spo
|
||||
return err
|
||||
}
|
||||
|
||||
self._izones = make([]cloudprovider.ICloudZone, len(zones))
|
||||
self.izones = make([]cloudprovider.ICloudZone, len(zones))
|
||||
|
||||
for i := 0; i < len(zones); i += 1 {
|
||||
zones[i].region = self
|
||||
self._izones[i] = &zones[i]
|
||||
self.izones[i] = &zones[i]
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -191,13 +191,13 @@ func (self *SRegion) getZoneById(id string) (*SZone, error) {
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
|
||||
if self._ivpcs == nil {
|
||||
if self.ivpcs == nil {
|
||||
err := self.fetchInfrastructure()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return self._ivpcs, nil
|
||||
return self.ivpcs, nil
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
|
||||
@@ -225,10 +225,10 @@ func (self *SRegion) fetchIVpcs() error {
|
||||
break
|
||||
}
|
||||
}
|
||||
self._ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs))
|
||||
self.ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs))
|
||||
for i := 0; i < len(vpcs); i += 1 {
|
||||
vpcs[i].region = self
|
||||
self._ivpcs[i] = &vpcs[i]
|
||||
self.ivpcs[i] = &vpcs[i]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -242,10 +242,10 @@ func (self *SRegion) fetchInfrastructure() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < len(self._ivpcs); i += 1 {
|
||||
for j := 0; j < len(self._izones); j += 1 {
|
||||
zone := self._izones[j].(*SZone)
|
||||
vpc := self._ivpcs[i].(*SVpc)
|
||||
for i := 0; i < len(self.ivpcs); i += 1 {
|
||||
for j := 0; j < len(self.izones); j += 1 {
|
||||
zone := self.izones[j].(*SZone)
|
||||
vpc := self.ivpcs[i].(*SVpc)
|
||||
wire := SWire{zone: zone, vpc: vpc}
|
||||
zone.addWire(&wire)
|
||||
vpc.addWire(&wire)
|
||||
|
||||
@@ -10,7 +10,7 @@ func init() {
|
||||
Offset int `help:"page offset"`
|
||||
}
|
||||
R(&VSwitchListOptions{}, "vswitch-list", "List vswitches", func(cli *aliyun.SRegion, args *VSwitchListOptions) error {
|
||||
vswitches, total, e := cli.GetVSwitches(nil, args.Offset, args.Limit)
|
||||
vswitches, total, e := cli.GetVSwitches(nil, "", args.Offset, args.Limit)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user