Skip to content

Commit

Permalink
Add projects (#1956)
Browse files Browse the repository at this point in the history
* Add list projects

* Add project flag to list vclusters
  • Loading branch information
rmweir authored Jul 19, 2024
1 parent f321469 commit fd1445a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (cmd *ListCmd) Run(cobraCmd *cobra.Command) error {
return fmt.Errorf("parse driver type: %w", err)
}
if driverType == config.PlatformDriver {
return cli.ListPlatform(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
return cli.ListPlatform(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log, "")
}

return cli.ListHelm(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
Expand Down
7 changes: 4 additions & 3 deletions cmd/vclusterctl/cmd/platform/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package list

import (
"github.com/loft-sh/api/v4/pkg/product"

"github.com/loft-sh/vcluster/pkg/cli/flags"
pdefaults "github.com/loft-sh/vcluster/pkg/platform/defaults"
"github.com/spf13/cobra"
)

// NewListCmd creates a new cobra command
func NewListCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
func NewListCmd(globalFlags *flags.GlobalFlags, defaults *pdefaults.Defaults) *cobra.Command {
description := product.ReplaceWithHeader("list", "")
listCmd := &cobra.Command{
Use: "list",
Expand All @@ -18,9 +18,10 @@ func NewListCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
}

listCmd.AddCommand(newClustersCmd(globalFlags))
listCmd.AddCommand(newProjectsCmd(globalFlags))
listCmd.AddCommand(newSharedSecretsCmd(globalFlags))
listCmd.AddCommand(newTeamsCmd(globalFlags))
listCmd.AddCommand(newVClustersCmd(globalFlags))
listCmd.AddCommand(newVClustersCmd(globalFlags, defaults))
listCmd.AddCommand(newNamespacesCmd(globalFlags))
return listCmd
}
75 changes: 75 additions & 0 deletions cmd/vclusterctl/cmd/platform/list/projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package list

import (
"context"

"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/log/table"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ProjectsCmd holds the login cmd flags
type ProjectsCmd struct {
*flags.GlobalFlags

log log.Logger
}

// newProjectsCmd creates a new spaces command
func newProjectsCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ProjectsCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
}
description := product.ReplaceWithHeader("list projects", `
List the vcluster platform projects you have access to
Example:
vcluster platform list projects
########################################################
`)
projectsCmd := &cobra.Command{
Use: "projects",
Short: product.Replace("Lists the loft projects you have access to"),
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, _ []string) error {
return cmd.RunProjects(cobraCmd.Context())
},
}

return projectsCmd
}

// RunProjects executes the functionality
func (cmd *ProjectsCmd) RunProjects(ctx context.Context) error {
platformClient, err := platform.InitClientFromConfig(ctx, cmd.LoadedConfig(cmd.log))
if err != nil {
return err
}

managementClient, err := platformClient.Management()
if err != nil {
return err
}

projectList, err := managementClient.Loft().ManagementV1().Projects().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}

header := []string{
"Project",
}
projects := make([][]string, len(projectList.Items))
for i, project := range projectList.Items {
projects[i] = []string{project.Name}
}

table.PrintTable(cmd.log, header, projects)
return nil
}
10 changes: 7 additions & 3 deletions cmd/vclusterctl/cmd/platform/list/vclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/flags"
pdefaults "github.com/loft-sh/vcluster/pkg/platform/defaults"
"github.com/spf13/cobra"
)

Expand All @@ -14,11 +15,12 @@ type VClustersCmd struct {
*flags.GlobalFlags
cli.ListOptions

log log.Logger
log log.Logger
Project string
}

// newVClustersCmd creates a new command
func newVClustersCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
func newVClustersCmd(globalFlags *flags.GlobalFlags, defaults *pdefaults.Defaults) *cobra.Command {
cmd := &VClustersCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
Expand All @@ -42,11 +44,13 @@ vcluster platform list vclusters
},
}

p, _ := defaults.Get(pdefaults.KeyProject, "")
cobraCmd.Flags().StringVarP(&cmd.Project, "project", "p", p, "The project to use")
cobraCmd.Flags().StringVar(&cmd.Output, "output", "table", "Choose the format of the output. [table|json]")

return cobraCmd
}

func (cmd *VClustersCmd) Run(ctx context.Context) error {
return cli.ListPlatform(ctx, &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
return cli.ListPlatform(ctx, &cmd.ListOptions, cmd.GlobalFlags, cmd.log, cmd.Project)
}
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewPlatformCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
platformCmd.AddCommand(NewAccessKeyCmd(globalFlags))
platformCmd.AddCommand(get.NewGetCmd(globalFlags, defaults))
platformCmd.AddCommand(connect.NewConnectCmd(globalFlags, defaults))
platformCmd.AddCommand(list.NewListCmd(globalFlags))
platformCmd.AddCommand(list.NewListCmd(globalFlags, defaults))
platformCmd.AddCommand(set.NewSetCmd(globalFlags, defaults))
platformCmd.AddCommand(backup.NewBackupCmd(globalFlags))
platformCmd.AddCommand(wakeup.NewWakeupCmd(globalFlags, defaults))
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/list_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.GlobalFlags, logger log.Logger) error {
func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.GlobalFlags, logger log.Logger, projectName string) error {
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return err
Expand All @@ -27,7 +27,7 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
return err
}

proVClusters, err := platform.ListVClusters(ctx, platformClient, "", "")
proVClusters, err := platform.ListVClusters(ctx, platformClient, "", projectName)
if err != nil {
return err
}
Expand Down

0 comments on commit fd1445a

Please sign in to comment.