Skip to content

Commit

Permalink
cleanup, prepare v1.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesClonk committed Dec 18, 2016
1 parent 492f900 commit f28dc15
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 133 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ export VULTR_API_KEY=87dFbC91rJjkL/18zJEQxS
* Run it
```sh
$ vultr version
Client version: v1.11
Client version: 1.12.0
Vultr API endpoint: https://api.vultr.com/
Vultr API version: v1
OS/Arch (client): linux/amd64
Expand All @@ -46,7 +46,7 @@ $ export VULTR_API_KEY=89dFbb91rGjkL/12zJEQxS
* Run it
```sh
$ vultr version
Client version: v1.11
Client version: 1.12.0
Vultr API endpoint: https://api.vultr.com/
Vultr API version: v1
OS/Arch (client): linux/amd64
Expand Down
23 changes: 10 additions & 13 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,16 @@ func (c *CLI) RegisterCommands() {
})
c.Command("scripts", "list all startup scripts on current account", scriptsList)

c.Command("reservedip", "modify reserved ip's", func(cmd *cli.Cmd) {
cmd.Command("attach", "attach a reserved IP to an existing subscription", reservedIpAttach)
cmd.Command("convert", "convert an existing IP on a subscription to a reserved IP", reservedIpConvert)
cmd.Command("create", "create a new reserved IP", func(cmd *cli.Cmd) {
cmd.Command("v4", "create ipv4", func(cmd *cli.Cmd) { reservedIpCreate(cmd, "v4") })
cmd.Command("v6", "create ipv6", func(cmd *cli.Cmd) { reservedIpCreate(cmd, "v6") })
})
cmd.Command("destroy", "remove a reserved IP from your account", reservedIpDestroy)
cmd.Command("detach", "detach a reserved IP from an existing subscription", reservedIpDetach)
cmd.Command("list", "list all the active reserved IPs on this account", reservedIpList)
})
c.Command("reservedips", "list all the active reserved IPs on this account", reservedIpList)

// reserved ips
c.Command("reservedip", "modify reserved IPs", func(cmd *cli.Cmd) {
cmd.Command("attach", "attach reserved IP to an existing virtual machine", reservedIpAttach)
cmd.Command("convert", "convert existing IP on a virtual machine to a reserved IP", reservedIpConvert)
cmd.Command("create", "create new reserved IP", reservedIpCreate)
cmd.Command("delete", "delete reserved IP from your account", reservedIpDestroy)
cmd.Command("detach", "detach reserved IP from an existing virtual machine", reservedIpDetach)
cmd.Command("list", "list all active reserved IPs on current account", reservedIpList)
})
c.Command("reservedips", "list all active reserved IPs on current account", reservedIpList)

// version
c.Command("version", "vultr CLI version", func(cmd *cli.Cmd) {
Expand Down
99 changes: 54 additions & 45 deletions cmd/commands_reservedip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,83 @@ import (
)

func reservedIpAttach(cmd *cli.Cmd) {
ip_address := cmd.StringArg("ip_address", "", "ip_address to attach")
attach_subid := cmd.StringArg("attach_SUBID", "", "subid to attach ip")
cmd.Spec = "SUBID IP_ADDRESS"

serverId := cmd.StringArg("SUBID", "", "SUBID of virtual machine to attach to (see <servers>)")
ip := cmd.StringArg("IP_ADDRESS", "", "IP address to attach (see <reservedips>)")

cmd.Action = func() {
err := GetClient().AttachReservedIp(*ip_address, *attach_subid)
if err != nil {
if err := GetClient().AttachReservedIp(*ip, *serverId); err != nil {
log.Fatal(err)
}
fmt.Printf("Attach IP to SUBID\n\n")
lengths := []int{40, 10}
tabsPrint(Columns{"ip_address", "attached_SUBID"}, lengths)
tabsPrint(Columns{*ip_address, *attach_subid}, lengths)
tabsFlush()
fmt.Println("Reserved IP attached")
}
}

func reservedIpConvert(cmd *cli.Cmd) {
cmd.Spec = "SUBID IPADDRESS"
subid := cmd.StringArg("SUBID", "", "subid convert to reverse")
ip_address := cmd.StringArg("IPADDRESS", "", "ipaddress to convert")
cmd.Spec = "SUBID IP_ADDRESS"

serverId := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
ip := cmd.StringArg("IP_ADDRESS", "", "IP address to convert to reserved IP")

cmd.Action = func() {
// fmt.Println("meno-0")
// fmt.Printf("meno-3 %v %v\n", *subid, *ip_address)
osubid, err := GetClient().ConvertReservedIp(*subid, *ip_address)
id, err := GetClient().ConvertReservedIp(*serverId, *ip)
if err != nil {
log.Fatal(err)
}
fmt.Printf("SUBIID to Attach IP\n\n")
lengths := []int{10, 40, 10}
tabsPrint(Columns{"SUBID", "ip_address", "attach_subid"}, lengths)
tabsPrint(Columns{*subid, *ip_address, osubid.SUBID}, lengths)

fmt.Printf("Reserved IP converted\n\n")
lengths := []int{12, 48, 12}
tabsPrint(Columns{"ID", "IP_ADDRESS", "ATTACHED_TO"}, lengths)
tabsPrint(Columns{id, *ip, *serverId}, lengths)
tabsFlush()
}
}

func reservedIpCreate(cmd *cli.Cmd, ip_type string) {
dcid := cmd.StringArg("DCID", "", "DCID Datacenter id")
func reservedIpCreate(cmd *cli.Cmd) {
cmd.Spec = "[-r -t]"

regionID := cmd.IntOpt("r region", 1, "Region (DCID)")
ipType := cmd.StringOpt("t type", "v4", "Type of new reserved IP (v4 or v6)")

cmd.Action = func() {
subid, err := GetClient().CreateReservedIp(*dcid, ip_type)
id, err := GetClient().CreateReservedIp(*regionID, *ipType)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Reserved IP created\n\n")
lengths := []int{10, 4}
tabsPrint(Columns{"SUBID", "TYPE"}, lengths)
tabsPrint(Columns{subid, ip_type}, lengths)
lengths := []int{12, 6, 10}
tabsPrint(Columns{"ID", "TYPE", "DCID"}, lengths)
tabsPrint(Columns{id, *ipType, *regionID}, lengths)
tabsFlush()
}
}

func reservedIpDestroy(cmd *cli.Cmd) {
subid := cmd.StringArg("SUBID", "", "SUBID of the ip")
cmd.Spec = "SUBID"

id := cmd.StringArg("SUBID", "", "SUBID of reserved IP (see <reservedips>)")

cmd.Action = func() {
err := GetClient().DestroyReservedIp(*subid)
if err != nil {
if err := GetClient().DestroyReservedIp(*id); err != nil {
log.Fatal(err)
}
fmt.Printf("Destroyed IP\n\n")
lengths := []int{10}
tabsPrint(Columns{"SUBID"}, lengths)
tabsPrint(Columns{*subid}, lengths)
tabsFlush()
fmt.Println("Reserved IP deleted")
}
}

func reservedIpDetach(cmd *cli.Cmd) {
ip_address := cmd.StringArg("ip_address", "", "ip_address to attach")
detach_subid := cmd.StringArg("detach_SUBID", "", "subid to detach ip")
cmd.Spec = "SUBID IP_ADDRESS"

serverId := cmd.StringArg("SUBID", "", "SUBID of virtual machine to detach from (see <servers>)")
ip := cmd.StringArg("IP_ADDRESS", "", "IP address to detach (see <reservedips>)")

cmd.Action = func() {
err := GetClient().DetachReservedIp(*ip_address, *detach_subid)
if err != nil {
if err := GetClient().DetachReservedIp(*ip, *serverId); err != nil {
log.Fatal(err)
}
fmt.Printf("Detach IP to SUBID\n\n")
lengths := []int{40, 10}
tabsPrint(Columns{"ip_address", "detached_SUBID"}, lengths)
tabsPrint(Columns{*ip_address, *detach_subid}, lengths)
tabsFlush()
fmt.Println("Reserved IP detached")
}
}

Expand All @@ -99,10 +99,19 @@ func reservedIpList(cmd *cli.Cmd) {
fmt.Println()
return
}
lengths := []int{10, 4, 4, 32, 4, 10, 10}
tabsPrint(Columns{"SUBID", "DCID", "ip_type", "subnet", "prefix", "label", "attached"}, lengths)

lengths := []int{12, 8, 8, 48, 6, 32, 12}
tabsPrint(Columns{"SUBID", "DCID", "IP_TYPE", "SUBNET", "SIZE", "LABEL", "ATTACHED_TO"}, lengths)
for _, ip := range ips {
tabsPrint(Columns{ip.SUBID, ip.DCID, ip.Ip_type, ip.Subnet, ip.Subnet_size, ip.Label, ip.Attached_SUBID}, lengths)
tabsPrint(Columns{
ip.ID,
ip.RegionID,
ip.IPType,
ip.Subnet,
ip.SubnetSize,
ip.Label,
ip.AttachedTo,
}, lengths)
}
tabsFlush()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BlockStorage struct {
}

// Implements json.Unmarshaller on BlockStorage.
// This is needed because the Vultr API is inconsistent in it's JSON responses for account info.
// This is needed because the Vultr API is inconsistent in it's JSON responses.
// Some fields can change type, from JSON number to JSON string and vice-versa.
func (b *BlockStorage) UnmarshalJSON(data []byte) (err error) {
if b == nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

const (
// Version of this libary
Version = "v1.11-mabels"
Version = "1.12.0"

// APIVersion of Vultr
APIVersion = "v1"
Expand Down
Loading

0 comments on commit f28dc15

Please sign in to comment.