Skip to content

Commit

Permalink
Joshd/sc 113095/api cli changes for vms (#414)
Browse files Browse the repository at this point in the history
* vm list
* Adding vm versions
* Adding vm rm
* Adding VM create
* Added vm update ttl
* adding group ls for vm
  • Loading branch information
jdewinne authored Oct 1, 2024
1 parent a58508d commit be946a1
Show file tree
Hide file tree
Showing 25 changed files with 1,262 additions and 20 deletions.
6 changes: 3 additions & 3 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r *runners) InitClusterCommand(parent *cobra.Command) *cobra.Command {
return cmd
}

func (r *runners) initClient() error {
func (r *runners) initClusterClient() error {
if apiToken == "" {
creds, err := credentials.GetCurrentCredentials()
if err != nil {
Expand All @@ -40,7 +40,7 @@ func (r *runners) initClient() error {
}

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

func (r *runners) completeClusterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := r.initClient()
err := r.initClusterClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "parse tags")
}

nodeGroups, err := parseNodeGroups(r.args.createClusterNodeGroups)
nodeGroups, err := parseClusterNodeGroups(r.args.createClusterNodeGroups)
if err != nil {
return errors.Wrap(err, "parse node groups")
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func waitForCluster(kotsRestClient *kotsclient.VendorV3Client, id string, durati
}
}

func parseNodeGroups(nodeGroups []string) ([]kotsclient.NodeGroup, error) {
func parseClusterNodeGroups(nodeGroups []string) ([]kotsclient.NodeGroup, error) {
parsedNodeGroups := []kotsclient.NodeGroup{}
for _, nodeGroup := range nodeGroups {
field := strings.Split(nodeGroup, ",")
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cluster_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Test_parseNodeGroups(t *testing.T) {
},
}
for _, tt := range tests {
got, err := parseNodeGroups(tt.args.nodeGroups)
got, err := parseClusterNodeGroups(tt.args.nodeGroups)
if (err != nil) != tt.wantErr {
t.Errorf("%q. parseNodeGroups() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/cluster_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *runners) InitClusterRemove(parent *cobra.Command) *cobra.Command {
Long: `Removes a cluster immediately.
You can specify the --all flag to terminate all clusters.`,
RunE: r.removeCluster,
RunE: r.removeClusters,
ValidArgsFunction: r.completeClusterIDs,
}
parent.AddCommand(cmd)
Expand All @@ -31,7 +31,7 @@ You can specify the --all flag to terminate all clusters.`,
return cmd
}

func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
func (r *runners) removeClusters(_ *cobra.Command, args []string) error {
if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) == 0 && len(r.args.removeClusterTags) == 0 {
return errors.New("One of ID, --all, --name or --tag flag required")
} else if len(args) > 0 && (r.args.removeClusterAll || len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
Expand All @@ -50,7 +50,7 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
for _, cluster := range clusters {
for _, name := range r.args.removeClusterNames {
if cluster.Name == name {
err := remove(r, cluster.ID)
err := removeCuster(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
Expand All @@ -74,7 +74,7 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
for _, tag := range tags {
for _, clusterTag := range cluster.Tags {
if clusterTag.Key == tag.Key && clusterTag.Value == tag.Value {
err := remove(r, cluster.ID)
err := removeCuster(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
Expand All @@ -91,15 +91,15 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "list clusters")
}
for _, cluster := range clusters {
err := remove(r, cluster.ID)
err := removeCuster(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}

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

func remove(r *runners, clusterID string) error {
func removeCuster(r *runners, clusterID string) error {
if r.args.removeClusterDryRun {
fmt.Printf("would remove cluster %s\n", clusterID)
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/cluster_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func (r *runners) InitClusterUpdateCommand(parent *cobra.Command) *cobra.Command
}
parent.AddCommand(cmd)

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

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

return cmd
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/cluster_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (r *runners) InitClusterVersions(parent *cobra.Command) *cobra.Command {
}
parent.AddCommand(cmd)

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

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

if r.args.lsVersionsClusterKubernetesDistribution != "" {
if r.args.lsVersionsDistribution != "" {
var filteredCV []*types.ClusterVersion
for _, cluster := range cv {
if cluster.Name == r.args.lsVersionsClusterKubernetesDistribution {
if cluster.Name == r.args.lsVersionsDistribution {
filteredCV = append(filteredCV, cluster)
break
}
Expand Down
13 changes: 13 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
runCmds.InitClusterUpdateTTL(clusterUpdateCmd)
runCmds.InitClusterUpdateNodegroup(clusterUpdateCmd)

vmCmd := runCmds.InitVMCommand(runCmds.rootCmd)
runCmds.InitVMCreate(vmCmd)
runCmds.InitVMList(vmCmd)
runCmds.InitVMVersions(vmCmd)
runCmds.InitVMRemove(vmCmd)

vmNodeGroupCmd := runCmds.InitVMGroup(vmCmd)
runCmds.InitVMGroupList(vmNodeGroupCmd)

vmUpdateCmd := runCmds.InitVMUpdateCommand(vmCmd)
runCmds.InitVMUpdateTTL(vmUpdateCmd)

runCmds.InitLoginCommand(runCmds.rootCmd)
runCmds.InitLogoutCommand(runCmds.rootCmd)

Expand Down Expand Up @@ -339,6 +351,7 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
appCmd.PersistentPreRunE = preRunSetupAPIs
registryCmd.PersistentPreRunE = preRunSetupAPIs
clusterCmd.PersistentPreRunE = preRunSetupAPIs
vmCmd.PersistentPreRunE = preRunSetupAPIs
apiCmd.PersistentPreRunE = preRunSetupAPIs
modelCmd.PersistentPreRunE = preRunSetupAPIs

Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ type runnerArgs struct {
modelCollectionRmModelName string
modelCollectionRmModelCollectionID string

lsAppVersion string
lsVersionsClusterKubernetesDistribution string
lsAppVersion string
lsVersionsDistribution string

lsClusterShowTerminated bool
lsClusterStartTime string
Expand Down
71 changes: 71 additions & 0 deletions cli/cmd/vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"github.com/replicatedhq/replicated/pkg/credentials"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/spf13/cobra"
)

func (r *runners) InitVMCommand(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "vm",
Short: "Manage test vms",
Long: ``,
}
parent.AddCommand(cmd)

return cmd
}

func (r *runners) initVMClient() error {
if apiToken == "" {
creds, err := credentials.GetCurrentCredentials()
if err != nil {
return err
}

apiToken = creds.APIToken
}

httpClient := platformclient.NewHTTPClient(platformOrigin, apiToken)
kotsAPI := &kotsclient.VendorV3Client{HTTPClient: *httpClient}
r.kotsAPI = kotsAPI
return nil
}

func (r *runners) completeVMIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := r.initVMClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var vmIDs []string
vms, err := r.kotsAPI.ListVMs(false, nil, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

for _, vm := range vms {
vmIDs = append(vmIDs, vm.ID)
}
return vmIDs, cobra.ShellCompDirectiveNoFileComp
}

func (r *runners) completeVMNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := r.initVMClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var vmNames []string
vms, err := r.kotsAPI.ListVMs(false, nil, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

for _, vm := range vms {
vmNames = append(vmNames, vm.Name)
}
return vmNames, cobra.ShellCompDirectiveNoFileComp
}
Loading

0 comments on commit be946a1

Please sign in to comment.