Skip to content

Commit

Permalink
Add support for VPC 2.0 (#335)
Browse files Browse the repository at this point in the history
* Add main vpc2 commands and printers

* Add support for instance VPC2 endpoints

* Add support for bare metal VPC2 endpoints

* Add README reference for VPC2

* Update example description for VPC2
  • Loading branch information
christhemorse authored Aug 14, 2023
1 parent b589ffc commit e1e3bb7
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Available Commands:
user user commands
version Display current version of Vultr-cli
vpc Interact with VPCs
vpc2 Interact with VPC 2.0 networks

Flags:
--config string config file (default is $HOME/.vultr-cli.yaml) (default "#HOME/.vultr-cli.yaml")
Expand Down
97 changes: 97 additions & 0 deletions cmd/bareMetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ var (
# Shortened example with aliases
vultr-cli bm tags <bareMetalID> -t="tag-1,tag-2"
`

bareMetalVPC2AttachLong = `Attaches an existing VPC 2.0 network to the specified bare metal server`
bareMetalVPC2AttachExample = `
# Full example
vultr-cli bare-metal vpc2 attach <bareMetalID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`
bareMetalVPC2DetachLong = `Detaches an existing VPC 2.0 network from the specified bare metal server`
bareMetalVPC2DetachExample = `
# Full example
vultr-cli bare-metal vpc2 detach <bareMetalID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`
)

// BareMetal represents the baremetal commands
Expand Down Expand Up @@ -115,6 +126,17 @@ func BareMetal() *cobra.Command {
os.Exit(1)
}

vpc2Cmd := &cobra.Command{
Use: "vpc2",
Short: "commands to handle vpc 2.0 on a server",
Long: ``,
}
vpc2Cmd.AddCommand(bareMetalVPC2List, bareMetalVPC2Attach, bareMetalVPC2Detach)
bareMetalVPC2Attach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to attach")
bareMetalVPC2Attach.Flags().StringP("ip-address", "i", "", "the IP address to use for this server on the attached VPC 2.0 network")
bareMetalVPC2Detach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to detach")
bareMetalCmd.AddCommand(vpc2Cmd)

return bareMetalCmd
}

Expand Down Expand Up @@ -473,3 +495,78 @@ func optionCheckBM(options map[string]interface{}) (string, error) {

return result[0], nil
}

var bareMetalVPC2List = &cobra.Command{
Use: "list <bareMetalID>",
Aliases: []string{"l"},
Short: "list all VPC 2.0 networks attached to a server",
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a bareMetalID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
s, _, err := client.BareMetalServer.ListVPC2Info(context.TODO(), id)
if err != nil {
fmt.Printf("error getting list of attached VPC 2.0 networks : %v\n", err)
os.Exit(1)
}

printer.BareMetalVPC2List(s)
},
}

var bareMetalVPC2Attach = &cobra.Command{
Use: "attach <bareMetalID>",
Short: "Attach a VPC 2.0 network to a server",
Long: bareMetalVPC2AttachLong,
Example: bareMetalVPC2AttachExample,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a bareMetalID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
vpcID, _ := cmd.Flags().GetString("vpc-id")
IPAddress, _ := cmd.Flags().GetString("ip-address")

opt := &govultr.AttachVPC2Req{
VPCID: vpcID,
IPAddress: &IPAddress,
}

if err := client.BareMetalServer.AttachVPC2(context.TODO(), id, opt); err != nil {
fmt.Printf("error attaching VPC 2.0 network : %v\n", err)
os.Exit(1)
}

fmt.Println("VPC 2.0 network has been attached")
},
}

var bareMetalVPC2Detach = &cobra.Command{
Use: "detach <bareMetalID>",
Short: "Detach a VPC 2.0 network from a server",
Long: bareMetalVPC2DetachLong,
Example: bareMetalVPC2DetachExample,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide a bareMetalID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
vpcID, _ := cmd.Flags().GetString("vpc-id")
if err := client.BareMetalServer.DetachVPC2(context.TODO(), id, vpcID); err != nil {
fmt.Printf("error detaching VPC 2.0 network : %v\n", err)
os.Exit(1)
}
fmt.Println("VPC 2.0 network has been detached")
},
}
4 changes: 2 additions & 2 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
# Full example with custom MySQL settings
vultr-cli database create --database-engine="mysql" --database-engine-version="8" --region="ewr" --plan="vultr-dbaas-startup-cc-1-55-2" --label="example-db" --mysql-slow-query-log="true" --mysql-long-query-time="2"
`
databaseUpdateLong = `Create a new Managed Database with specified plan, region, and database engine/version`
databaseUpdateLong = `Updates a Managed Database with the supplied information`
databaseUpdateExample = `
# Full example
vultr-cli database update --region="sea" --plan="vultr-dbaas-startup-cc-2-80-4"
Expand All @@ -49,7 +49,7 @@ var (
`
)

// Instance represents the instance command
// Database represents the database command
func Database() *cobra.Command {
databaseCmd := &cobra.Command{
Use: "database",
Expand Down
101 changes: 99 additions & 2 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,28 @@ var (
# Shortened example with aliases
vultr-cli instance tags <instanceID> -t="example-tag-1,example-tag-2"
`

instanceVPCAttachLong = `Attaches an existing VPC to the specified instance`
instanceVPCAttachExample = `
# Full example
vultr-cli instance vpc attach <instanceID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`

instanceVPCDetachLong = `Detaches an existing VPC from the specified instance`
instanceVPCDetachExample = `
# Full example
vultr-cli instance vpc detach <instanceID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`

instanceVPC2AttachLong = `Attaches an existing VPC 2.0 network to the specified instance`
instanceVPC2AttachExample = `
# Full example
vultr-cli instance vpc2 attach <instanceID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`
instanceVPC2DetachLong = `Detaches an existing VPC 2.0 network from the specified instance`
instanceVPC2DetachExample = `
# Full example
vultr-cli instance vpc2 detach <instanceID> --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294"
`
)

// Instance represents the instance command
Expand Down Expand Up @@ -335,6 +346,17 @@ func Instance() *cobra.Command {
vpcDetach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC you wish to detach")
instanceCmd.AddCommand(vpcCmd)

vpc2Cmd := &cobra.Command{
Use: "vpc2",
Short: "commands to handle vpc 2.0 on an instance",
Long: ``,
}
vpc2Cmd.AddCommand(instanceVPC2List, vpc2Attach, vpc2Detach)
vpc2Attach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to attach")
vpc2Attach.Flags().StringP("ip-address", "i", "", "the IP address to use for this instance on the attached VPC 2.0 network")
vpc2Detach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC 2.0 network you wish to detach")
instanceCmd.AddCommand(vpc2Cmd)

return instanceCmd
}

Expand Down Expand Up @@ -1272,7 +1294,6 @@ var instanceCreate = &cobra.Command{
opt.UserData = base64.StdEncoding.EncodeToString([]byte(userData))
}

//region, plan, osOpt, opt
instance, _, err := client.Instance.Create(context.TODO(), opt)
if err != nil {
fmt.Printf("error creating instance : %v\n", err)
Expand Down Expand Up @@ -1379,6 +1400,82 @@ var vpcDetach = &cobra.Command{
},
}

var instanceVPC2List = &cobra.Command{
Use: "list <instanceID>",
Aliases: []string{"l"},
Short: "list all VPC 2.0 networks attached to an instance",
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide an instance ID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
options := getPaging(cmd)
s, meta, _, err := client.Instance.ListVPC2Info(context.TODO(), id, options)
if err != nil {
fmt.Printf("error getting list of attached VPC 2.0 networks : %v\n", err)
os.Exit(1)
}

printer.InstanceVPC2List(s, meta)
},
}

var vpc2Attach = &cobra.Command{
Use: "attach <instanceID>",
Short: "Attach a VPC 2.0 network to an instance",
Long: instanceVPC2AttachLong,
Example: instanceVPC2AttachExample,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide an instance ID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
vpcID, _ := cmd.Flags().GetString("vpc-id")
IPAddress, _ := cmd.Flags().GetString("ip-address")

opt := &govultr.AttachVPC2Req{
VPCID: vpcID,
IPAddress: &IPAddress,
}

if err := client.Instance.AttachVPC2(context.TODO(), id, opt); err != nil {
fmt.Printf("error attaching VPC 2.0 network : %v\n", err)
os.Exit(1)
}

fmt.Println("VPC 2.0 network has been attached")
},
}

var vpc2Detach = &cobra.Command{
Use: "detach <instanceID>",
Short: "Detach a VPC 2.0 network from an instance",
Long: instanceVPC2DetachLong,
Example: instanceVPC2DetachExample,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide an instance ID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
vpcID, _ := cmd.Flags().GetString("vpc-id")
if err := client.Instance.DetachVPC2(context.TODO(), id, vpcID); err != nil {
fmt.Printf("error detaching VPC 2.0 network : %v\n", err)
os.Exit(1)
}
fmt.Println("VPC 2.0 network has been detached")
},
}

func optionCheck(options map[string]interface{}) (string, error) {
var result []string
for k, v := range options {
Expand Down
9 changes: 9 additions & 0 deletions cmd/printer/bareMetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ func BareMetalVNCUrl(vnc *govultr.VNCUrl) {
display(columns{vnc.URL})
flush()
}

// BareMetalVPC2List Generate a printer display of all VPC 2.0 networks attached to a given server
func BareMetalVPC2List(vpc2s []govultr.VPC2Info) {
display(columns{"ID", "MAC ADDRESS", "IP ADDRESS"})
for _, r := range vpc2s {
display(columns{r.ID, r.MacAddress, r.IPAddress})
}
flush()
}
11 changes: 11 additions & 0 deletions cmd/printer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ func ReverseIpv6(rip []govultr.ReverseIP) {
}
flush()
}

// InstanceVPC2List Generate a printer display of all VPC 2.0 networks attached to a given instance
func InstanceVPC2List(vpc2s []govultr.VPC2Info, meta *govultr.Meta) {
display(columns{"ID", "MAC ADDRESS", "IP ADDRESS"})
for _, r := range vpc2s {
display(columns{r.ID, r.MacAddress, r.IPAddress})
}

Meta(meta)
flush()
}
23 changes: 23 additions & 0 deletions cmd/printer/vpc2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package printer

import (
"github.com/vultr/govultr/v3"
)

// VPC2List Generates a printer display of all VPC 2.0 networks on the account
func VPC2List(vpc2s []govultr.VPC2, meta *govultr.Meta) {
display(columns{"ID", "DATE CREATED", "REGION", "DESCRIPTION", "IP BLOCK", "PREFIX LENGTH"})
for d := range vpc2s {
display(columns{vpc2s[d].ID, vpc2s[d].DateCreated, vpc2s[d].Region, vpc2s[d].Description, vpc2s[d].IPBlock, vpc2s[d].PrefixLength})
}

Meta(meta)
flush()
}

// VPC2 Generates a printer display of a given VPC 2.0 network
func VPC2(vpc2 *govultr.VPC2) {
display(columns{"ID", "DATE CREATED", "REGION", "DESCRIPTION", "IP BLOCK", "PREFIX LENGTH"})
display(columns{vpc2.ID, vpc2.DateCreated, vpc2.Region, vpc2.Description, vpc2.IPBlock, vpc2.PrefixLength})
flush()
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func init() {
rootCmd.AddCommand(SSHKey())
rootCmd.AddCommand(User())
rootCmd.AddCommand(VPC())
rootCmd.AddCommand(VPC2())
cobra.OnInitialize(initConfig)
}

Expand Down
Loading

0 comments on commit e1e3bb7

Please sign in to comment.