Skip to content

Commit 68abbb1

Browse files
committed
Add support for Vultr Serverless Inference subscriptions
1 parent 75534dd commit 68abbb1

File tree

3 files changed

+422
-0
lines changed

3 files changed

+422
-0
lines changed

cmd/inference/inference.go

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
// Package inference is used by the CLI to control serverless inference subscriptions
2+
package inference
3+
4+
import (
5+
"errors"
6+
"fmt"
7+
"os"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/vultr/govultr/v3"
11+
"github.com/vultr/vultr-cli/v3/cmd/printer"
12+
"github.com/vultr/vultr-cli/v3/cmd/utils"
13+
"github.com/vultr/vultr-cli/v3/pkg/cli"
14+
)
15+
16+
var (
17+
long = `Get commands available to inference`
18+
example = `
19+
# Full example
20+
vultr-cli inference
21+
`
22+
listLong = `Get all serverless inference subscriptions on your Vultr account`
23+
listExample = `
24+
# Full example
25+
vultr-cli inference list
26+
`
27+
createLong = `Create a new Serverless Inference subscription with specified label`
28+
createExample = `
29+
# Full example
30+
vultr-cli inference create --label="example-inference"
31+
`
32+
updateLong = `Updates a Serverless Inference subscription with the supplied information`
33+
updateExample = `
34+
# Full example
35+
vultr-cli inference update --label="example-inference-updated"
36+
`
37+
)
38+
39+
// NewCmdInference provides the CLI command for inference functions
40+
func NewCmdInference(base *cli.Base) *cobra.Command { //nolint:funlen,gocyclo
41+
o := &options{Base: base}
42+
43+
cmd := &cobra.Command{
44+
Use: "inference",
45+
Short: "Commands to manage serverless inference",
46+
Long: long,
47+
Example: example,
48+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
49+
utils.SetOptions(o.Base, cmd, args)
50+
if !o.Base.HasAuth {
51+
return errors.New(utils.APIKeyError)
52+
}
53+
return nil
54+
},
55+
}
56+
57+
// List
58+
list := &cobra.Command{
59+
Use: "list",
60+
Short: "List inference subscriptions",
61+
Aliases: []string{"l"},
62+
Long: listLong,
63+
Example: listExample,
64+
RunE: func(cmd *cobra.Command, args []string) error {
65+
inferenceSubs, err := o.list()
66+
if err != nil {
67+
return fmt.Errorf("error retrieving inference list : %v", err)
68+
}
69+
70+
data := &InferenceSubsPrinter{InferenceSubs: inferenceSubs}
71+
o.Base.Printer.Display(data, nil)
72+
73+
return nil
74+
},
75+
}
76+
77+
// Get
78+
get := &cobra.Command{
79+
Use: "get <Inference ID>",
80+
Short: "Retrieve an inference subscription",
81+
Aliases: []string{"g"},
82+
Args: func(cmd *cobra.Command, args []string) error {
83+
if len(args) < 1 {
84+
return errors.New("please provide an inference ID")
85+
}
86+
return nil
87+
},
88+
RunE: func(cmd *cobra.Command, args []string) error {
89+
inferenceSub, err := o.get()
90+
if err != nil {
91+
return fmt.Errorf("error retrieving inference subscription : %v", err)
92+
}
93+
94+
data := &InferenceSubPrinter{InferenceSub: inferenceSub}
95+
o.Base.Printer.Display(data, nil)
96+
97+
return nil
98+
},
99+
}
100+
101+
// Create
102+
create := &cobra.Command{
103+
Use: "create",
104+
Short: "Create inference subscription",
105+
Aliases: []string{"c"},
106+
Long: createLong,
107+
Example: createExample,
108+
RunE: func(cmd *cobra.Command, args []string) error {
109+
label, errLa := cmd.Flags().GetString("label")
110+
if errLa != nil {
111+
return fmt.Errorf("error parsing flag 'label' for inference create : %v", errLa)
112+
}
113+
114+
o.CreateUpdateReq = &govultr.InferenceCreateUpdateReq{
115+
Label: label,
116+
}
117+
118+
inferenceSub, err := o.create()
119+
if err != nil {
120+
return fmt.Errorf("error creating inference subscription : %v", err)
121+
}
122+
123+
data := &InferenceSubPrinter{InferenceSub: inferenceSub}
124+
o.Base.Printer.Display(data, nil)
125+
126+
return nil
127+
},
128+
}
129+
130+
create.Flags().StringP("label", "l", "", "label for the new inference subscription")
131+
if err := create.MarkFlagRequired("label"); err != nil {
132+
fmt.Printf("error marking inference create 'label' flag required: %v", err)
133+
os.Exit(1)
134+
}
135+
136+
// Update
137+
update := &cobra.Command{
138+
Use: "update <inference ID>",
139+
Short: "Update an inference subscription",
140+
Aliases: []string{"u"},
141+
Long: updateLong,
142+
Example: updateExample,
143+
Args: func(cmd *cobra.Command, args []string) error {
144+
if len(args) < 1 {
145+
return errors.New("please provide an inference ID")
146+
}
147+
return nil
148+
},
149+
RunE: func(cmd *cobra.Command, args []string) error {
150+
label, errLa := cmd.Flags().GetString("label")
151+
if errLa != nil {
152+
return fmt.Errorf("error parsing flag 'label' for inference update : %v", errLa)
153+
}
154+
155+
o.CreateUpdateReq = &govultr.InferenceCreateUpdateReq{}
156+
157+
if cmd.Flags().Changed("label") {
158+
o.CreateUpdateReq.Label = label
159+
}
160+
161+
inferenceSub, err := o.update()
162+
if err != nil {
163+
return fmt.Errorf("error updating inference : %v", err)
164+
}
165+
166+
data := &InferenceSubPrinter{InferenceSub: inferenceSub}
167+
o.Base.Printer.Display(data, nil)
168+
169+
return nil
170+
},
171+
}
172+
173+
update.Flags().StringP("label", "l", "", "label for the inference subscription")
174+
175+
// Delete
176+
del := &cobra.Command{
177+
Use: "delete <inference ID>",
178+
Short: "Delete an inference subscription",
179+
Aliases: []string{"destroy", "d"},
180+
Args: func(cmd *cobra.Command, args []string) error {
181+
if len(args) < 1 {
182+
return errors.New("please provide an inference ID")
183+
}
184+
return nil
185+
},
186+
RunE: func(cmd *cobra.Command, args []string) error {
187+
if err := o.del(); err != nil {
188+
return fmt.Errorf("error deleting inference subscription : %v", err)
189+
}
190+
191+
o.Base.Printer.Display(printer.Info("Inference subscription has been deleted"), nil)
192+
193+
return nil
194+
},
195+
}
196+
197+
// Usage
198+
usage := &cobra.Command{
199+
Use: "usage",
200+
Short: "Commands to display inference subscription usage information",
201+
}
202+
203+
// Usage Get
204+
usageGet := &cobra.Command{
205+
Use: "get <inference ID>",
206+
Short: "Get inference subscription usage",
207+
Args: func(cmd *cobra.Command, args []string) error {
208+
if len(args) != 1 {
209+
return errors.New("please provide an inference ID")
210+
}
211+
return nil
212+
},
213+
RunE: func(cmd *cobra.Command, args []string) error {
214+
us, err := o.getUsage()
215+
if err != nil {
216+
return fmt.Errorf("error retrieving inference subscription usage : %v", err)
217+
}
218+
219+
data := &UsagePrinter{Usage: us}
220+
o.Base.Printer.Display(data, nil)
221+
222+
return nil
223+
},
224+
}
225+
226+
usage.AddCommand(
227+
usageGet,
228+
)
229+
230+
cmd.AddCommand(
231+
list,
232+
get,
233+
create,
234+
update,
235+
del,
236+
usage,
237+
)
238+
239+
return cmd
240+
}
241+
242+
type options struct {
243+
Base *cli.Base
244+
CreateUpdateReq *govultr.InferenceCreateUpdateReq
245+
}
246+
247+
func (o *options) list() ([]govultr.Inference, error) {
248+
inferenceSubs, _, err := o.Base.Client.Inference.List(o.Base.Context)
249+
return inferenceSubs, err
250+
}
251+
252+
func (o *options) get() (*govultr.Inference, error) {
253+
inferenceSub, _, err := o.Base.Client.Inference.Get(o.Base.Context, o.Base.Args[0])
254+
return inferenceSub, err
255+
}
256+
257+
func (o *options) create() (*govultr.Inference, error) {
258+
inferenceSub, _, err := o.Base.Client.Inference.Create(o.Base.Context, o.CreateUpdateReq)
259+
return inferenceSub, err
260+
}
261+
262+
func (o *options) update() (*govultr.Inference, error) {
263+
inferenceSub, _, err := o.Base.Client.Inference.Update(o.Base.Context, o.Base.Args[0], o.CreateUpdateReq)
264+
return inferenceSub, err
265+
}
266+
267+
func (o *options) del() error {
268+
return o.Base.Client.Inference.Delete(o.Base.Context, o.Base.Args[0])
269+
}
270+
271+
func (o *options) getUsage() (*govultr.InferenceUsage, error) {
272+
usage, _, err := o.Base.Client.Inference.GetUsage(o.Base.Context, o.Base.Args[0])
273+
return usage, err
274+
}

0 commit comments

Comments
 (0)