Skip to content

Commit be946a1

Browse files
authored
Joshd/sc 113095/api cli changes for vms (#414)
* vm list * Adding vm versions * Adding vm rm * Adding VM create * Added vm update ttl * adding group ls for vm
1 parent a58508d commit be946a1

25 files changed

+1262
-20
lines changed

cli/cmd/cluster.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *runners) InitClusterCommand(parent *cobra.Command) *cobra.Command {
2323
return cmd
2424
}
2525

26-
func (r *runners) initClient() error {
26+
func (r *runners) initClusterClient() error {
2727
if apiToken == "" {
2828
creds, err := credentials.GetCurrentCredentials()
2929
if err != nil {
@@ -40,7 +40,7 @@ func (r *runners) initClient() error {
4040
}
4141

4242
func (r *runners) completeClusterIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
43-
err := r.initClient()
43+
err := r.initClusterClient()
4444
if err != nil {
4545
return nil, cobra.ShellCompDirectiveNoFileComp
4646
}
@@ -58,7 +58,7 @@ func (r *runners) completeClusterIDs(cmd *cobra.Command, args []string, toComple
5858
}
5959

6060
func (r *runners) completeClusterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
61-
err := r.initClient()
61+
err := r.initClusterClient()
6262
if err != nil {
6363
return nil, cobra.ShellCompDirectiveNoFileComp
6464
}

cli/cmd/cluster_create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
6363
return errors.Wrap(err, "parse tags")
6464
}
6565

66-
nodeGroups, err := parseNodeGroups(r.args.createClusterNodeGroups)
66+
nodeGroups, err := parseClusterNodeGroups(r.args.createClusterNodeGroups)
6767
if err != nil {
6868
return errors.Wrap(err, "parse node groups")
6969
}
@@ -182,7 +182,7 @@ func waitForCluster(kotsRestClient *kotsclient.VendorV3Client, id string, durati
182182
}
183183
}
184184

185-
func parseNodeGroups(nodeGroups []string) ([]kotsclient.NodeGroup, error) {
185+
func parseClusterNodeGroups(nodeGroups []string) ([]kotsclient.NodeGroup, error) {
186186
parsedNodeGroups := []kotsclient.NodeGroup{}
187187
for _, nodeGroup := range nodeGroups {
188188
field := strings.Split(nodeGroup, ",")

cli/cmd/cluster_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func Test_parseNodeGroups(t *testing.T) {
120120
},
121121
}
122122
for _, tt := range tests {
123-
got, err := parseNodeGroups(tt.args.nodeGroups)
123+
got, err := parseClusterNodeGroups(tt.args.nodeGroups)
124124
if (err != nil) != tt.wantErr {
125125
t.Errorf("%q. parseNodeGroups() error = %v, wantErr %v", tt.name, err, tt.wantErr)
126126
continue

cli/cmd/cluster_rm.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (r *runners) InitClusterRemove(parent *cobra.Command) *cobra.Command {
1515
Long: `Removes a cluster immediately.
1616
1717
You can specify the --all flag to terminate all clusters.`,
18-
RunE: r.removeCluster,
18+
RunE: r.removeClusters,
1919
ValidArgsFunction: r.completeClusterIDs,
2020
}
2121
parent.AddCommand(cmd)
@@ -31,7 +31,7 @@ You can specify the --all flag to terminate all clusters.`,
3131
return cmd
3232
}
3333

34-
func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
34+
func (r *runners) removeClusters(_ *cobra.Command, args []string) error {
3535
if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) == 0 && len(r.args.removeClusterTags) == 0 {
3636
return errors.New("One of ID, --all, --name or --tag flag required")
3737
} else if len(args) > 0 && (r.args.removeClusterAll || len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
@@ -50,7 +50,7 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
5050
for _, cluster := range clusters {
5151
for _, name := range r.args.removeClusterNames {
5252
if cluster.Name == name {
53-
err := remove(r, cluster.ID)
53+
err := removeCuster(r, cluster.ID)
5454
if err != nil {
5555
return errors.Wrap(err, "remove cluster")
5656
}
@@ -74,7 +74,7 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
7474
for _, tag := range tags {
7575
for _, clusterTag := range cluster.Tags {
7676
if clusterTag.Key == tag.Key && clusterTag.Value == tag.Value {
77-
err := remove(r, cluster.ID)
77+
err := removeCuster(r, cluster.ID)
7878
if err != nil {
7979
return errors.Wrap(err, "remove cluster")
8080
}
@@ -91,15 +91,15 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
9191
return errors.Wrap(err, "list clusters")
9292
}
9393
for _, cluster := range clusters {
94-
err := remove(r, cluster.ID)
94+
err := removeCuster(r, cluster.ID)
9595
if err != nil {
9696
return errors.Wrap(err, "remove cluster")
9797
}
9898
}
9999
}
100100

101101
for _, arg := range args {
102-
err := remove(r, arg)
102+
err := removeCuster(r, arg)
103103
if err != nil {
104104
return errors.Wrap(err, "remove cluster")
105105
}
@@ -108,7 +108,7 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
108108
return nil
109109
}
110110

111-
func remove(r *runners, clusterID string) error {
111+
func removeCuster(r *runners, clusterID string) error {
112112
if r.args.removeClusterDryRun {
113113
fmt.Printf("would remove cluster %s\n", clusterID)
114114
return nil

cli/cmd/cluster_update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func (r *runners) InitClusterUpdateCommand(parent *cobra.Command) *cobra.Command
1414
}
1515
parent.AddCommand(cmd)
1616

17-
cmd.PersistentFlags().StringVar(&r.args.updateClusterName, "name", "", "Name of the cluster to update TTL.")
17+
cmd.PersistentFlags().StringVar(&r.args.updateClusterName, "name", "", "Name of the cluster to update.")
1818
cmd.RegisterFlagCompletionFunc("name", r.completeClusterNames)
1919

20-
cmd.PersistentFlags().StringVar(&r.args.updateClusterID, "id", "", "id of the cluster to update TTL (when name is not provided)")
20+
cmd.PersistentFlags().StringVar(&r.args.updateClusterID, "id", "", "id of the cluster to update (when name is not provided)")
2121
cmd.RegisterFlagCompletionFunc("id", r.completeClusterIDs)
2222

2323
return cmd

cli/cmd/cluster_versions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (r *runners) InitClusterVersions(parent *cobra.Command) *cobra.Command {
1717
}
1818
parent.AddCommand(cmd)
1919

20-
cmd.Flags().StringVar(&r.args.lsVersionsClusterKubernetesDistribution, "distribution", "", "Kubernetes distribution to filter by.")
20+
cmd.Flags().StringVar(&r.args.lsVersionsDistribution, "distribution", "", "Kubernetes distribution to filter by.")
2121
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
2222

2323
return cmd
@@ -31,10 +31,10 @@ func (r *runners) listClusterVersions(_ *cobra.Command, args []string) error {
3131
return errors.Wrap(err, "list cluster versions")
3232
}
3333

34-
if r.args.lsVersionsClusterKubernetesDistribution != "" {
34+
if r.args.lsVersionsDistribution != "" {
3535
var filteredCV []*types.ClusterVersion
3636
for _, cluster := range cv {
37-
if cluster.Name == r.args.lsVersionsClusterKubernetesDistribution {
37+
if cluster.Name == r.args.lsVersionsDistribution {
3838
filteredCV = append(filteredCV, cluster)
3939
break
4040
}

cli/cmd/root.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
256256
runCmds.InitClusterUpdateTTL(clusterUpdateCmd)
257257
runCmds.InitClusterUpdateNodegroup(clusterUpdateCmd)
258258

259+
vmCmd := runCmds.InitVMCommand(runCmds.rootCmd)
260+
runCmds.InitVMCreate(vmCmd)
261+
runCmds.InitVMList(vmCmd)
262+
runCmds.InitVMVersions(vmCmd)
263+
runCmds.InitVMRemove(vmCmd)
264+
265+
vmNodeGroupCmd := runCmds.InitVMGroup(vmCmd)
266+
runCmds.InitVMGroupList(vmNodeGroupCmd)
267+
268+
vmUpdateCmd := runCmds.InitVMUpdateCommand(vmCmd)
269+
runCmds.InitVMUpdateTTL(vmUpdateCmd)
270+
259271
runCmds.InitLoginCommand(runCmds.rootCmd)
260272
runCmds.InitLogoutCommand(runCmds.rootCmd)
261273

@@ -339,6 +351,7 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
339351
appCmd.PersistentPreRunE = preRunSetupAPIs
340352
registryCmd.PersistentPreRunE = preRunSetupAPIs
341353
clusterCmd.PersistentPreRunE = preRunSetupAPIs
354+
vmCmd.PersistentPreRunE = preRunSetupAPIs
342355
apiCmd.PersistentPreRunE = preRunSetupAPIs
343356
modelCmd.PersistentPreRunE = preRunSetupAPIs
344357

cli/cmd/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ type runnerArgs struct {
257257
modelCollectionRmModelName string
258258
modelCollectionRmModelCollectionID string
259259

260-
lsAppVersion string
261-
lsVersionsClusterKubernetesDistribution string
260+
lsAppVersion string
261+
lsVersionsDistribution string
262262

263263
lsClusterShowTerminated bool
264264
lsClusterStartTime string

cli/cmd/vm.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cmd
2+
3+
import (
4+
"github.com/replicatedhq/replicated/pkg/credentials"
5+
"github.com/replicatedhq/replicated/pkg/kotsclient"
6+
"github.com/replicatedhq/replicated/pkg/platformclient"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitVMCommand(parent *cobra.Command) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "vm",
13+
Short: "Manage test vms",
14+
Long: ``,
15+
}
16+
parent.AddCommand(cmd)
17+
18+
return cmd
19+
}
20+
21+
func (r *runners) initVMClient() error {
22+
if apiToken == "" {
23+
creds, err := credentials.GetCurrentCredentials()
24+
if err != nil {
25+
return err
26+
}
27+
28+
apiToken = creds.APIToken
29+
}
30+
31+
httpClient := platformclient.NewHTTPClient(platformOrigin, apiToken)
32+
kotsAPI := &kotsclient.VendorV3Client{HTTPClient: *httpClient}
33+
r.kotsAPI = kotsAPI
34+
return nil
35+
}
36+
37+
func (r *runners) completeVMIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
38+
err := r.initVMClient()
39+
if err != nil {
40+
return nil, cobra.ShellCompDirectiveNoFileComp
41+
}
42+
43+
var vmIDs []string
44+
vms, err := r.kotsAPI.ListVMs(false, nil, nil)
45+
if err != nil {
46+
return nil, cobra.ShellCompDirectiveNoFileComp
47+
}
48+
49+
for _, vm := range vms {
50+
vmIDs = append(vmIDs, vm.ID)
51+
}
52+
return vmIDs, cobra.ShellCompDirectiveNoFileComp
53+
}
54+
55+
func (r *runners) completeVMNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
56+
err := r.initVMClient()
57+
if err != nil {
58+
return nil, cobra.ShellCompDirectiveNoFileComp
59+
}
60+
61+
var vmNames []string
62+
vms, err := r.kotsAPI.ListVMs(false, nil, nil)
63+
if err != nil {
64+
return nil, cobra.ShellCompDirectiveNoFileComp
65+
}
66+
67+
for _, vm := range vms {
68+
vmNames = append(vmNames, vm.Name)
69+
}
70+
return vmNames, cobra.ShellCompDirectiveNoFileComp
71+
}

0 commit comments

Comments
 (0)