Skip to content

Commit 1a16afd

Browse files
authored
update govultr and add image_id support (#169)
1 parent a781115 commit 1a16afd

File tree

15 files changed

+152
-12
lines changed

15 files changed

+152
-12
lines changed

cmd/bareMetal.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func BareMetal() *cobra.Command {
3636

3737
bareMetalCmd.AddCommand(
3838
BareMetalApp(),
39+
BareMetalImage(),
3940
bareMetalBandwidth,
4041
bareMetalCreate,
4142
bareMetalDelete,
@@ -64,6 +65,7 @@ func BareMetal() *cobra.Command {
6465
bareMetalCreate.Flags().StringP("label", "l", "", "(optional) The label to assign to the server.")
6566
bareMetalCreate.Flags().StringSliceP("ssh", "k", []string{}, "(optional) Comma separated list of SSH key IDs that will be added to the server.")
6667
bareMetalCreate.Flags().IntP("app", "a", 0, "(optional) ID of the application that will be installed on the server.")
68+
bareMetalCreate.Flags().StringP("image", "", "", "(optional) Image ID of the application that will be installed on the server.")
6769
bareMetalCreate.Flags().StringP("userdata", "u", "", "(optional) A generic data store, which some provisioning tools and cloud operating systems use as a configuration file.")
6870
bareMetalCreate.Flags().StringP("notify", "n", "", "(optional) Whether an activation email will be sent when the server is ready. Possible values: 'yes', 'no'. Defaults to 'yes'.")
6971
bareMetalCreate.Flags().StringP("hostname", "m", "", "(optional) The hostname to assign to the server.")
@@ -103,6 +105,7 @@ var bareMetalCreate = &cobra.Command{
103105
tag, _ := cmd.Flags().GetString("tag")
104106
ripv4, _ := cmd.Flags().GetString("ripv4")
105107
pxe, _ := cmd.Flags().GetBool("persistent_pxe")
108+
image, _ := cmd.Flags().GetString("image")
106109

107110
options := &govultr.BareMetalCreate{
108111
StartupScriptID: script,
@@ -116,6 +119,7 @@ var bareMetalCreate = &cobra.Command{
116119
OsID: osID,
117120
Region: region,
118121
AppID: app,
122+
ImageID: image,
119123
PersistentPxe: govultr.BoolToBoolPtr(pxe),
120124
}
121125

@@ -131,7 +135,7 @@ var bareMetalCreate = &cobra.Command{
131135
options.EnableIPv6 = govultr.BoolToBoolPtr(true)
132136
}
133137

134-
osOptions := map[string]interface{}{"app_id": app, "snapshot_id": snapshot, "os_id": osID}
138+
osOptions := map[string]interface{}{"app_id": app, "snapshot_id": snapshot, "os_id": osID, "image_id": image}
135139
osOption, err := optionCheckBM(osOptions)
136140

137141
if err != nil {
@@ -141,7 +145,7 @@ var bareMetalCreate = &cobra.Command{
141145

142146
// If no osOptions were selected and osID has a real value then set the osOptions to os_id
143147
if osOption == "" && osID == 0 {
144-
fmt.Printf("error creating bare metal server : an app, snapshot, or os ID must be provided\n")
148+
fmt.Printf("error creating bare metal server : an app, image, snapshot, or os ID must be provided\n")
145149
os.Exit(1)
146150
}
147151

cmd/bareMetalImage.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright © 2019 The Vultr-cli Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"context"
19+
"errors"
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
"github.com/vultr/govultr/v2"
25+
)
26+
27+
// BareMetalImage represents the baremetal image commands
28+
func BareMetalImage() *cobra.Command {
29+
bareMetalImageCmd := &cobra.Command{
30+
Use: "image",
31+
Short: "image is used to access bare metal server image commands",
32+
Aliases: []string{"i"},
33+
}
34+
35+
bareMetalImageCmd.AddCommand(bareMetalImageChange, bareMetalAppChangeList)
36+
37+
return bareMetalImageCmd
38+
}
39+
40+
var bareMetalImageChange = &cobra.Command{
41+
Use: "change <bareMetalID> <imageID>",
42+
Short: "Change a bare metal server's application",
43+
Aliases: []string{"c"},
44+
Args: func(cmd *cobra.Command, args []string) error {
45+
if len(args) < 2 {
46+
return errors.New("please provide a bareMetalID and imageID")
47+
}
48+
return nil
49+
},
50+
Run: func(cmd *cobra.Command, args []string) {
51+
imageID := args[1]
52+
options := &govultr.BareMetalUpdate{
53+
ImageID: imageID,
54+
}
55+
56+
_, err := client.BareMetalServer.Update(context.TODO(), args[0], options)
57+
if err != nil {
58+
fmt.Printf("%v\n", err)
59+
os.Exit(1)
60+
}
61+
62+
fmt.Println("bare metal server's application changed")
63+
},
64+
}

cmd/instance.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func Instance() *cobra.Command {
6969
instanceCreate.Flags().StringArrayP("ssh-keys", "s", []string{}, "ssh keys you want to assign to the instance")
7070
instanceCreate.Flags().BoolP("auto-backup", "b", false, "enable auto backups | true or false")
7171
instanceCreate.Flags().IntP("app", "a", 0, "application ID you want this instance to have")
72+
instanceCreate.Flags().StringP("image", "", "", "(optional) image ID of the application that will be installed on the server.")
7273
instanceCreate.Flags().StringP("userdata", "u", "", "plain text userdata you want to give this instance which the CLI will base64 encode")
7374
instanceCreate.Flags().BoolP("notify", "n", true, "notify when instance has been created | true or false")
7475
instanceCreate.Flags().BoolP("ddos", "d", false, "enable ddos protection | true or false")
@@ -109,6 +110,17 @@ func Instance() *cobra.Command {
109110
appUpdate.MarkFlagRequired("app")
110111
instanceCmd.AddCommand(appCMD)
111112

113+
// Sub commands for Image
114+
imageCMD := &cobra.Command{
115+
Use: "image",
116+
Short: "update image for an instance",
117+
Long: ``,
118+
}
119+
imageCMD.AddCommand(imageUpdate, appUpdateList)
120+
imageUpdate.Flags().StringP("image", "", "", "application image ID you wish to use")
121+
imageUpdate.MarkFlagRequired("image")
122+
instanceCmd.AddCommand(imageCMD)
123+
112124
// Sub commands for Backup
113125
backupCMD := &cobra.Command{
114126
Use: "backup",
@@ -538,6 +550,33 @@ var osUpdateList = &cobra.Command{
538550
},
539551
}
540552

553+
var imageUpdate = &cobra.Command{
554+
Use: "change <instanceID>",
555+
Short: "changes application",
556+
Long: ``,
557+
Args: func(cmd *cobra.Command, args []string) error {
558+
if len(args) < 1 {
559+
return errors.New("please provide an instanceID")
560+
}
561+
return nil
562+
},
563+
Run: func(cmd *cobra.Command, args []string) {
564+
id := args[0]
565+
imageID, _ := cmd.Flags().GetString("image")
566+
567+
options := &govultr.InstanceUpdateReq{
568+
ImageID: imageID,
569+
}
570+
571+
if err := client.Instance.Update(context.TODO(), id, options); err != nil {
572+
fmt.Printf("error updating application : %v\n", err)
573+
os.Exit(1)
574+
}
575+
576+
fmt.Println("Updated Application")
577+
},
578+
}
579+
541580
var appUpdate = &cobra.Command{
542581
Use: "change <instanceID>",
543582
Short: "changes application",
@@ -992,6 +1031,7 @@ var instanceCreate = &cobra.Command{
9921031
ssh, _ := cmd.Flags().GetStringArray("ssh-keys")
9931032
backup, _ := cmd.Flags().GetBool("auto-backup")
9941033
app, _ := cmd.Flags().GetInt("app")
1034+
image, _ := cmd.Flags().GetString("image")
9951035
userData, _ := cmd.Flags().GetString("userdata")
9961036
notify, _ := cmd.Flags().GetBool("notify")
9971037
ddos, _ := cmd.Flags().GetBool("ddos")
@@ -1000,7 +1040,7 @@ var instanceCreate = &cobra.Command{
10001040
tag, _ := cmd.Flags().GetString("tag")
10011041
fwg, _ := cmd.Flags().GetString("firewall-group")
10021042

1003-
osOptions := map[string]interface{}{"iso_id": iso, "os_id": osID, "app_id": app, "snapshot_id": snapshot}
1043+
osOptions := map[string]interface{}{"iso_id": iso, "os_id": osID, "app_id": app, "snapshot_id": snapshot, "image_id": image}
10041044

10051045
if iso != "" {
10061046
osOptions["iso_id"] = iso
@@ -1033,13 +1073,14 @@ var instanceCreate = &cobra.Command{
10331073
ActivationEmail: govultr.BoolToBoolPtr(false),
10341074
Backups: "disabled",
10351075
EnablePrivateNetwork: govultr.BoolToBoolPtr(false),
1076+
ImageID: image,
10361077
}
10371078

10381079
// If no osOptions were selected and osID has a real value then set the osOptions to os_id
10391080
if osOption == "os_id" && osID != 0 {
10401081
opt.OsID = osID
10411082
} else if osOption == "" && osID == 0 {
1042-
fmt.Printf("error creating instance: an os_id, snapshot_id, iso_id, or app_id must be provided\n")
1083+
fmt.Printf("error creating instance: an os_id, image_id, snapshot_id, iso_id, or app_id must be provided\n")
10431084
os.Exit(1)
10441085
}
10451086

cmd/printer/application.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
func Application(apps []govultr.Application, meta *govultr.Meta) {
8-
col := columns{"ID", "NAME", "SHORT NAME", "DEPLOY NAME"}
8+
col := columns{"ID", "NAME", "SHORT NAME", "DEPLOY NAME", "TYPE", "VENDOR", "IMAGE ID"}
99
display(col)
1010
for _, a := range apps {
11-
display(columns{a.ID, a.Name, a.ShortName, a.DeployName})
11+
display(columns{a.ID, a.Name, a.ShortName, a.DeployName, a.Type, a.Vendor, a.ImageID})
1212
}
1313

1414
Meta(meta)

cmd/printer/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func OsList(os []govultr.OS) {
8787
}
8888

8989
func AppList(app []govultr.Application) {
90-
col := columns{"ID", "NAME", "SHORT NAME", "DEPLOY NAME"}
90+
col := columns{"ID", "NAME", "SHORT NAME", "DEPLOY NAME", "TYPE", "VENDOR", "IMAGE ID"}
9191
display(col)
9292
for _, a := range app {
93-
display(columns{a.ID, a.Name, a.ShortName, a.DeployName})
93+
display(columns{a.ID, a.Name, a.ShortName, a.DeployName, a.Type, a.Vendor, a.ImageID})
9494
}
9595
flush()
9696
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module github.com/vultr/vultr-cli
33
go 1.16
44

55
require (
6+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
67
github.com/spf13/cobra v1.2.1
78
github.com/spf13/viper v1.8.1
8-
github.com/vultr/govultr/v2 v2.6.0
9+
github.com/vultr/govultr/v2 v2.7.1
910
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
1011
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyN
148148
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
149149
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
150150
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
151+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
152+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
151153
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
152154
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
153155
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
@@ -246,6 +248,10 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
246248
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
247249
github.com/vultr/govultr/v2 v2.6.0 h1:Ev6oShSx5E0/lIapdSDdVJbOCQdKFJkcNqK2mKI68dI=
248250
github.com/vultr/govultr/v2 v2.6.0/go.mod h1:BvOhVe6/ZpjwcoL6/unkdQshmbS9VGbowI4QT+3DGVU=
251+
github.com/vultr/govultr/v2 v2.7.0 h1:Dl0PytGptFhR8S282plgt0Q3m6j/I4l+KA/JUpEanQc=
252+
github.com/vultr/govultr/v2 v2.7.0/go.mod h1:BvOhVe6/ZpjwcoL6/unkdQshmbS9VGbowI4QT+3DGVU=
253+
github.com/vultr/govultr/v2 v2.7.1 h1:uF9ERet++Gb+7Cqs3p1P6b6yebeaZqVd7t5P2uZCaJU=
254+
github.com/vultr/govultr/v2 v2.7.1/go.mod h1:BvOhVe6/ZpjwcoL6/unkdQshmbS9VGbowI4QT+3DGVU=
249255
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
250256
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
251257
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-cleanhttp/go.mod

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/vultr/govultr/v2/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)