Skip to content

Commit 760b1cc

Browse files
authored
Add and remove models from collections (#396)
1 parent 713686c commit 760b1cc

File tree

9 files changed

+172
-3
lines changed

9 files changed

+172
-3
lines changed

cli/cmd/collection_addmodel.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pkg/errors"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitCollectionAddModel(parent *cobra.Command) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "add-model",
13+
Short: "add a model to a collection",
14+
Long: `add a model to a collection`,
15+
RunE: r.addModelToCollection,
16+
SilenceUsage: true,
17+
}
18+
parent.AddCommand(cmd)
19+
20+
cmd.Flags().StringVar(&r.args.modelCollectionAddModelName, "model-name", "", "The name of the model")
21+
cmd.MarkFlagRequired("model-id")
22+
cmd.Flags().StringVar(&r.args.modelCollectionAddModelCollectionID, "collection-id", "", "The ID of the collection")
23+
cmd.MarkFlagRequired("collection-id")
24+
25+
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
26+
27+
return cmd
28+
}
29+
30+
func (r *runners) addModelToCollection(_ *cobra.Command, args []string) error {
31+
err := r.kotsAPI.UpdateModelsInCollection(r.args.modelCollectionAddModelCollectionID, []string{r.args.modelCollectionAddModelName}, nil)
32+
if err != nil {
33+
return errors.Wrap(err, "list model collections")
34+
}
35+
36+
fmt.Fprintf(r.w, "Model %s has been added to collection %s\n", r.args.modelCollectionAddModelName, r.args.modelCollectionAddModelCollectionID)
37+
return nil
38+
}

cli/cmd/collection_removemodel.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pkg/errors"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitCollectionRemoveModel(parent *cobra.Command) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "remove-model",
13+
Short: "remove a model from a collection",
14+
Long: `remove a model from a collection`,
15+
RunE: r.removeModelFromCollection,
16+
SilenceUsage: true,
17+
}
18+
parent.AddCommand(cmd)
19+
20+
cmd.Flags().StringVar(&r.args.modelCollectionRmModelName, "model-name", "", "The name of the model")
21+
cmd.MarkFlagRequired("model-id")
22+
cmd.Flags().StringVar(&r.args.modelCollectionRmModelCollectionID, "collection-id", "", "The ID of the collection")
23+
cmd.MarkFlagRequired("collection-id")
24+
25+
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
26+
27+
return cmd
28+
}
29+
30+
func (r *runners) removeModelFromCollection(_ *cobra.Command, args []string) error {
31+
err := r.kotsAPI.UpdateModelsInCollection(r.args.modelCollectionRmModelCollectionID, nil, []string{r.args.modelCollectionRmModelName})
32+
if err != nil {
33+
return errors.Wrap(err, "list model collections")
34+
}
35+
36+
fmt.Fprintf(r.w, "Model %s has been removed from collection %s\n", r.args.modelCollectionRmModelName, r.args.modelCollectionRmModelCollectionID)
37+
return nil
38+
}

cli/cmd/model_ls.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/replicatedhq/replicated/cli/print"
45
"github.com/spf13/cobra"
56
)
67

@@ -19,5 +20,10 @@ func (r *runners) InitModelList(parent *cobra.Command) *cobra.Command {
1920
}
2021

2122
func (r *runners) listModels(_ *cobra.Command, args []string) error {
22-
return nil
23+
models, err := r.kotsAPI.ListModels()
24+
if err != nil {
25+
return err
26+
}
27+
28+
return print.Models(r.outputFormat, r.w, models)
2329
}

cli/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
220220
runCmds.InitCollectionList(collectionCmd)
221221
runCmds.InitCollectionCreate(collectionCmd)
222222
runCmds.InitCollectionRemove(collectionCmd)
223+
runCmds.InitCollectionAddModel(collectionCmd)
224+
runCmds.InitCollectionRemoveModel(collectionCmd)
223225

224226
clusterCmd := runCmds.InitClusterCommand(runCmds.rootCmd)
225227
runCmds.InitClusterCreate(clusterCmd)

cli/cmd/runner.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,12 @@ type runnerArgs struct {
242242
removeClusterNames []string
243243
removeClusterDryRun bool
244244

245-
modelCollectionCreateName string
246-
modelPushName string
245+
modelCollectionCreateName string
246+
modelPushName string
247+
modelCollectionAddModelName string
248+
modelCollectionAddModelCollectionID string
249+
modelCollectionRmModelName string
250+
modelCollectionRmModelCollectionID string
247251

248252
lsAppVersion string
249253
lsVersionsClusterKubernetesDistribution string

cli/print/ai-models.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,25 @@ func ModelCollections(outputFormat string, w *tabwriter.Writer, collections []ty
3030

3131
return w.Flush()
3232
}
33+
34+
var modelsTmplSrc = `NAME
35+
{{ range . -}}
36+
{{ .Name }}
37+
{{ end }}`
38+
39+
var modelsTmpl = template.Must(template.New("registries").Funcs(funcs).Parse(modelsTmplSrc))
40+
41+
func Models(outputFormat string, w *tabwriter.Writer, models []types.Model) error {
42+
if outputFormat == "table" {
43+
if err := modelsTmpl.Execute(w, models); err != nil {
44+
return err
45+
}
46+
} else if outputFormat == "json" {
47+
mAsByte, _ := json.MarshalIndent(models, "", " ")
48+
if _, err := fmt.Fprintln(w, string(mAsByte)); err != nil {
49+
return err
50+
}
51+
}
52+
53+
return w.Flush()
54+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package kotsclient
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
type updateModelsInCollectionRequest struct {
11+
AddModels []string `json:"add_models"`
12+
RemoveModels []string `json:"remove_models"`
13+
}
14+
15+
func (c *VendorV3Client) UpdateModelsInCollection(collectionID string, modelsToAdd []string, modelsToRemove []string) error {
16+
var reqBody = updateModelsInCollectionRequest{
17+
AddModels: modelsToAdd,
18+
RemoveModels: modelsToRemove,
19+
}
20+
21+
err := c.DoJSON("PATCH", fmt.Sprintf("/v3/models/collection/%s/models", collectionID), http.StatusOK, reqBody, nil)
22+
if err != nil {
23+
return errors.Wrap(err, "update models in collection")
24+
}
25+
26+
return nil
27+
}

pkg/kotsclient/model_list.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package kotsclient
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/pkg/errors"
7+
"github.com/replicatedhq/replicated/pkg/types"
8+
)
9+
10+
type modelsResponse struct {
11+
Models []types.Model `json:"models"`
12+
}
13+
14+
func (c *VendorV3Client) ListModels() ([]types.Model, error) {
15+
var response = modelsResponse{}
16+
17+
err := c.DoJSON("GET", "/v3/models", http.StatusOK, nil, &response)
18+
if err != nil {
19+
return nil, errors.Wrap(err, "list models")
20+
}
21+
22+
results := make([]types.Model, 0)
23+
results = append(results, response.Models...)
24+
25+
return results, nil
26+
}

pkg/types/ai-models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ type ModelCollection struct {
88
CreatedAt time.Time `json:"createdAt"`
99
UpdatedAt time.Time `json:"updatedAt"`
1010
}
11+
12+
type Model struct {
13+
Name string `json:"name"`
14+
CreatedAt time.Time `json:"createdAt"`
15+
UpdatedAt time.Time `json:"updatedAt"`
16+
}

0 commit comments

Comments
 (0)