Skip to content

Commit

Permalink
Update govultr and add support for reserved IP label updates (#272)
Browse files Browse the repository at this point in the history
* Add update command to reserved IP

* Fix label flag for IP update

* Add docs to reserved IP commands

* Bump govultr to v2.17.2
  • Loading branch information
optik-aper committed Jun 14, 2022
1 parent e12ca3f commit ec74b56
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 26 deletions.
170 changes: 149 additions & 21 deletions cmd/reservedIP.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,97 @@ import (
"github.com/vultr/vultr-cli/v2/cmd/printer"
)

var (
reservedIPLong = `Get all available commands for reserved IPs`
reservedIPExample = `
# Full example
vultr-cli reserved-ip
# Shortened with aliased commands
vultr-cli rip
`

reservedIPCreateLong = `Create a reserved IP on your Vultr account`
reservedIPCreateExample = `
# Full Example
vultr-cli reserved-ip create --region="yto" --type="v4" --label="new IP"
# Shortened with alias commands
vultr-cli rip c -r="yto" -t="v4" -l="new IP"
`

reservedIPGetLong = `Get info for a reserved IP on your Vultr account`
reservedIPGetExample = `
# Full example
vultr-cli reserved-ip get 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5
# Shortened with alias commands
vultr-cli rip g 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5
`

reservedIPListLong = `List all reserved IPs on your Vultr account`
reservedIPListExample = `
# Full example
vultr-cli reserved-ip list
# Shortened with alias commands
vultr-cli rip l
`

reservedIPAttachLong = `Attach a reserved IP to an instance on your Vultr account`
reservedIPAttachExample = `
# Full example
vultr-cli reserved-ip attach 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5 --instance-id="2b9bf5fb-1644-4e0a-b706-1116ab64d783"
# Shortened with alias commands
vultr-cli rip a 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5 -i="2b9bf5fb-1644-4e0a-b706-1116ab64d783"
`

reservedIPDetachLong = `Detach a reserved IP from an instance on your Vultr account`
reservedIPDetachExample = `
# Full example
vultr-cli reserved-ip detach 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5
# Shortened with alias commands
vultr-cli rip d 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5
`

reservedIPConvertLong = `Convert an instance IP to a reserved IP on your Vultr account`
reservedIPConvertExample = `
# Full example
vultr-cli reserved-ip convert --ip="192.0.2.123" --label="new label converted"
# Shortened with alias commands
vultr-cli rip v -i="192.0.2.123" -l="new label converted"
`

reservedIPUpdateLong = `Update a reserved IP on your Vultr account`
reservedIPUpdateExample = `
# Full example
vultr-cli reserved-ip update 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5 --label="new label"
# Shortened with alias commands
vultr-cli rip u 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5 -l="new label"
`

reservedIPDeleteLong = `Delete a reserved IP from your Vultr account`
reservedIPDeleteExample = `
# Full example
vultr-cli reserved-ip delete 6a31648d-ebfa-4d43-9a00-9c9f0e5048f5
`
)

// ReservedIP represents the reservedip command
func ReservedIP() *cobra.Command {
reservedIPCmd := &cobra.Command{
Use: "reserved-ip",
Aliases: []string{"rip"},
Short: "reserved-ip lets you interact with reserved-ip ",
Long: ``,
Long: reservedIPLong,
Example: reservedIPExample,
}

reservedIPCmd.AddCommand(reservedIPGet, reservedIPList, reservedIPDelete, reservedIPAttach, reservedIPDetach, reservedIPConvert, reservedIPCreate)
reservedIPCmd.AddCommand(reservedIPGet, reservedIPList, reservedIPDelete, reservedIPAttach, reservedIPDetach, reservedIPConvert, reservedIPCreate, reservedIPUpdate)

// List
reservedIPList.Flags().StringP("cursor", "c", "", "(optional) Cursor for paging.")
Expand All @@ -56,13 +137,19 @@ func ReservedIP() *cobra.Command {
reservedIPCreate.MarkFlagRequired("type")
reservedIPCreate.Flags().StringP("label", "l", "", "label")

// Update
reservedIPUpdate.Flags().StringP("label", "l", "", "label")
reservedIPUpdate.MarkFlagRequired("label")

return reservedIPCmd
}

var reservedIPGet = &cobra.Command{
Use: "get <reservedIPID",
Short: "get a reserved IP",
Long: ``,
Use: "get <reservedIPID>",
Short: "get a reserved IP",
Long: reservedIPGetLong,
Example: reservedIPGetExample,
Aliases: []string{"g"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a reservedIP ID")
Expand All @@ -82,9 +169,11 @@ var reservedIPGet = &cobra.Command{
}

var reservedIPList = &cobra.Command{
Use: "list",
Short: "list all reserved IPs",
Long: ``,
Use: "list",
Short: "list all reserved IPs",
Long: reservedIPListLong,
Example: reservedIPListExample,
Aliases: []string{"l"},
Run: func(cmd *cobra.Command, args []string) {
options := getPaging(cmd)
rip, meta, err := client.ReservedIP.List(context.Background(), options)
Expand All @@ -100,8 +189,9 @@ var reservedIPList = &cobra.Command{
var reservedIPDelete = &cobra.Command{
Use: "delete <reservedIPID>",
Short: "delete a reserved ip",
Long: reservedIPDeleteLong,
Example: reservedIPDeleteExample,
Aliases: []string{"destroy"},
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a reservedIP ID")
Expand All @@ -120,9 +210,11 @@ var reservedIPDelete = &cobra.Command{
}

var reservedIPAttach = &cobra.Command{
Use: "attach <reservedIPID>",
Short: "attach a reservedIP to an instance",
Long: ``,
Use: "attach <reservedIPID>",
Short: "attach a reservedIP to an instance",
Long: reservedIPAttachLong,
Example: reservedIPAttachExample,
Aliases: []string{"a"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a reservedIP ID")
Expand All @@ -142,9 +234,11 @@ var reservedIPAttach = &cobra.Command{
}

var reservedIPDetach = &cobra.Command{
Use: "detach <reservedIPID>",
Short: "detach a reservedIP to an instance",
Long: ``,
Use: "detach <reservedIPID>",
Short: "detach a reservedIP to an instance",
Long: reservedIPDetachLong,
Example: reservedIPDetachExample,
Aliases: []string{"d"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a reservedIP ID")
Expand All @@ -163,9 +257,11 @@ var reservedIPDetach = &cobra.Command{
}

var reservedIPConvert = &cobra.Command{
Use: "convert ",
Short: "convert IP address to reservedIP",
Long: ``,
Use: "convert ",
Short: "convert IP address to reservedIP",
Long: reservedIPConvertLong,
Example: reservedIPConvertExample,
Aliases: []string{"v"},
Run: func(cmd *cobra.Command, args []string) {
ip, _ := cmd.Flags().GetString("ip")
label, _ := cmd.Flags().GetString("label")
Expand All @@ -185,9 +281,11 @@ var reservedIPConvert = &cobra.Command{
}

var reservedIPCreate = &cobra.Command{
Use: "create ",
Short: "create reservedIP",
Long: ``,
Use: "create ",
Short: "create reservedIP",
Long: reservedIPCreateLong,
Example: reservedIPCreateExample,
Aliases: []string{"c"},
Run: func(cmd *cobra.Command, args []string) {
region, _ := cmd.Flags().GetString("region")
ipType, _ := cmd.Flags().GetString("type")
Expand All @@ -208,3 +306,33 @@ var reservedIPCreate = &cobra.Command{
printer.ReservedIP(r)
},
}

var reservedIPUpdate = &cobra.Command{
Use: "update <reservedIPID>",
Short: "update reservedIP",
Long: reservedIPUpdateLong,
Example: reservedIPUpdateExample,
Aliases: []string{"u"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a reserved IP ID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
label, _ := cmd.Flags().GetString("label")
ip := args[0]

options := &govultr.ReservedIPUpdateReq{
Label: govultr.StringToStringPtr(label),
}

r, err := client.ReservedIP.Update(context.Background(), ip, options)
if err != nil {
fmt.Printf("error updating reserved IPs : %v\n", err)
os.Exit(1)
}

printer.ReservedIP(r)
},
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
github.com/vultr/govultr/v2 v2.17.1
github.com/vultr/govultr/v2 v2.17.2
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/vultr/govultr/v2 v2.17.1 h1:UBmotwA0mkGtyJMakUF9jhLH/W3mN5wfGRn543i/BCA=
github.com/vultr/govultr/v2 v2.17.1/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/vultr/govultr/v2 v2.17.2 h1:gej/rwr91Puc/tgh+j33p/BLR16UrIPnSr+AIwYWZQs=
github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/vultr/govultr/v2/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/vultr/govultr/v2/govultr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/vultr/govultr/v2/reserved_ip.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ github.com/spf13/viper/internal/encoding/yaml
# github.com/subosito/gotenv v1.3.0
## explicit; go 1.18
github.com/subosito/gotenv
# github.com/vultr/govultr/v2 v2.17.1
# github.com/vultr/govultr/v2 v2.17.2
## explicit; go 1.17
github.com/vultr/govultr/v2
# golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2
Expand Down

0 comments on commit ec74b56

Please sign in to comment.