From ebe35df358a15af2bddb03c1af0d2aa980eae4a8 Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Wed, 10 May 2023 16:39:40 -0400 Subject: [PATCH 1/3] Initial work on VPC attach/detach for instances --- cmd/instance.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cmd/instance.go b/cmd/instance.go index 343d5833..96299f00 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -314,6 +314,14 @@ func Instance() *cobra.Command { setUserData.Flags().StringP("userdata", "d", "/dev/stdin", "file to read userdata from") instanceCmd.AddCommand(userdataCmd) + vpcCmd := &cobra.Command{ + Use: "vpc", + Short: "commands to handle vpc on an instance", + Long: ``, + } + vpcCmd.AddCommand(attachVPC, detachVPC) + instanceCmd.AddCommand(vpcCmd) + return instanceCmd } @@ -1313,6 +1321,40 @@ var getUserData = &cobra.Command{ }, } +var attachVPC = &cobra.Command{ + Use: "attach ", + Short: "Attach a VPC to an instance", + 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) { + if err := client.Instance.AttachVPC(context.TODO(), args[0], "TODO"); err != nil { + fmt.Printf("error attaching VPC : %v\n", err) + os.Exit(1) + } + }, +} + +var detachVPC = &cobra.Command{ + Use: "detach ", + Short: "Detach a VPC from an instance", + 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) { + if err := client.Instance.DetachVPC(context.TODO(), args[0], "TODO"); err != nil { + fmt.Printf("error detaching VPC : %v\n", err) + os.Exit(1) + } + }, +} + func optionCheck(options map[string]interface{}) (string, error) { var result []string for k, v := range options { From 6cefe0002f0a07fe892c2f31f99b24c8322f8a82 Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Wed, 10 May 2023 17:00:22 -0400 Subject: [PATCH 2/3] Add flags for VPC ID on attach/detach --- cmd/instance.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/instance.go b/cmd/instance.go index 96299f00..492ef558 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -319,7 +319,9 @@ func Instance() *cobra.Command { Short: "commands to handle vpc on an instance", Long: ``, } - vpcCmd.AddCommand(attachVPC, detachVPC) + vpcCmd.AddCommand(vpcAttach, vpcDetach) + vpcAttach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC you wish to attach") + vpcDetach.Flags().StringP("vpc-id", "v", "", "the ID of the VPC you wish to detach") instanceCmd.AddCommand(vpcCmd) return instanceCmd @@ -1321,7 +1323,7 @@ var getUserData = &cobra.Command{ }, } -var attachVPC = &cobra.Command{ +var vpcAttach = &cobra.Command{ Use: "attach ", Short: "Attach a VPC to an instance", Args: func(cmd *cobra.Command, args []string) error { @@ -1331,14 +1333,18 @@ var attachVPC = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - if err := client.Instance.AttachVPC(context.TODO(), args[0], "TODO"); err != nil { + id := args[0] + vpcID, _ := cmd.Flags().GetString("vpc-id") + if err := client.Instance.AttachVPC(context.TODO(), id, vpcID); err != nil { fmt.Printf("error attaching VPC : %v\n", err) os.Exit(1) } + + fmt.Println("VPC has been attached") }, } -var detachVPC = &cobra.Command{ +var vpcDetach = &cobra.Command{ Use: "detach ", Short: "Detach a VPC from an instance", Args: func(cmd *cobra.Command, args []string) error { @@ -1348,10 +1354,13 @@ var detachVPC = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - if err := client.Instance.DetachVPC(context.TODO(), args[0], "TODO"); err != nil { + id := args[0] + vpcID, _ := cmd.Flags().GetString("vpc-id") + if err := client.Instance.DetachVPC(context.TODO(), id, vpcID); err != nil { fmt.Printf("error detaching VPC : %v\n", err) os.Exit(1) } + fmt.Println("VPC has been detached") }, } From d7cf9256271134e0fae263a88854cbcabc5f51ab Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Wed, 10 May 2023 17:10:02 -0400 Subject: [PATCH 3/3] Add additional help for instance VPC commands --- cmd/instance.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/instance.go b/cmd/instance.go index 492ef558..df44e5dd 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -61,6 +61,17 @@ var ( # Shortened example with aliases vultr-cli instance tags -t="example-tag-1,example-tag-2" ` + instanceVPCAttachLong = `Attaches an existing VPC to the specified instance` + instanceVPCAttachExample = ` + # Full example + vultr-cli instance vpc attach --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294" + ` + + instanceVPCDetachLong = `Detaches an existing VPC from the specified instance` + instanceVPCDetachExample = ` + # Full example + vultr-cli instance vpc detach --vpc-id="2126b7d9-5e2a-491e-8840-838aa6b5f294" + ` ) // Instance represents the instance command @@ -1324,8 +1335,10 @@ var getUserData = &cobra.Command{ } var vpcAttach = &cobra.Command{ - Use: "attach ", - Short: "Attach a VPC to an instance", + Use: "attach ", + Short: "Attach a VPC to an instance", + Long: instanceVPCAttachLong, + Example: instanceVPCAttachExample, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { return errors.New("please provide an instance ID") @@ -1345,8 +1358,10 @@ var vpcAttach = &cobra.Command{ } var vpcDetach = &cobra.Command{ - Use: "detach ", - Short: "Detach a VPC from an instance", + Use: "detach ", + Short: "Detach a VPC from an instance", + Long: instanceVPCDetachLong, + Example: instanceVPCDetachExample, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { return errors.New("please provide an instance ID")