Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

feature: support filtering by workspace provider when listing environ… #282

Merged
merged 6 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feature: support filtering by workspace provider when listing environ…
…ments
  • Loading branch information
Faris Huskovic committed Mar 11, 2021
commit e9874ea9c9210774d63cec94daf2b56f9191ac37
14 changes: 14 additions & 0 deletions ci/integration/envs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ func TestEnvsCLI(t *testing.T) {
tcli.StdoutMatches(regexp.QuoteMeta(name)),
)

// filter by provider that does not exist should fail
doesntExist := randString(10)
c.Run(ctx, fmt.Sprintf("coder envs ls --provider %s", doesntExist)).Assert(t,
tcli.Error(),
tcli.StderrMatches(regexp.QuoteMeta(fmt.Sprintf("fatal: no environments found for workspace provider %q", doesntExist))),
)

// filter by provider that does exist should succeed
var envs []coder.Environment
c.Run(ctx, "coder envs ls --provider built-in").Assert(t,
tcli.Success(),
tcli.StdoutJSONUnmarshal(&envs),
)

var env coder.Environment
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
tcli.Success(),
Expand Down
8 changes: 8 additions & 0 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,11 @@ func (c *DefaultClient) EnvironmentByID(ctx context.Context, id string) (*Enviro
}
return &env, nil
}

func (c *DefaultClient) EnvironmentsByWorkspaceProvider(ctx context.Context, wpID string) ([]Environment, error) {
var envs []Environment
if err := c.requestBody(ctx, http.MethodGet, "/api/private/resource-pools/"+wpID+"/environments/", nil, &envs); err != nil {
return nil, err
}
return envs, nil
}
3 changes: 3 additions & 0 deletions coder-sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ type Client interface {
// EnvironmentByID get the details of an environment by its id.
EnvironmentByID(ctx context.Context, id string) (*Environment, error)

// EnvironmentsByWorkspaceProvider returns environments that belong to a particular workspace provider.
EnvironmentsByWorkspaceProvider(ctx context.Context, wpID string) ([]Environment, error)

// ImportImage creates a new image and optionally a new registry.
ImportImage(ctx context.Context, req ImportImageReq) (*Image, error)

Expand Down
27 changes: 27 additions & 0 deletions internal/cmd/ceapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,30 @@ func getUserOrgs(ctx context.Context, client coder.Client, email string) ([]code
}
return lookupUserOrgs(u, orgs), nil
}

func getProviderByName(ctx context.Context, client coder.Client, wpName string) (*coder.KubernetesProvider, error) {
providers, err := client.WorkspaceProviders(ctx)
if err != nil {
return nil, err
}

for _, provider := range providers.Kubernetes {
if provider.Name == wpName {
return &provider, nil
}
}
return nil, xerrors.Errorf("workspace provider %q not found", wpName)
}

func getEnvsByProvider(ctx context.Context, client coder.Client, wpName string) ([]coder.Environment, error) {
wp, err := getProviderByName(ctx, client, wpName)
if err != nil {
return nil, err
}

envs, err := client.EnvironmentsByWorkspaceProvider(ctx, wp.ID)
if err != nil {
return nil, err
}
return envs, nil
}
8 changes: 8 additions & 0 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func lsEnvsCommand() *cobra.Command {
var (
outputFmt string
user string
provider string
)

cmd := &cobra.Command{
Expand All @@ -67,6 +68,12 @@ func lsEnvsCommand() *cobra.Command {
if err != nil {
return err
}
if provider != "" {
envs, err = getEnvsByProvider(ctx, client, provider)
if err != nil {
return err
}
}
if len(envs) < 1 {
clog.LogInfo("no environments found")
envs = []coder.Environment{} // ensures that json output still marshals
Expand Down Expand Up @@ -94,6 +101,7 @@ func lsEnvsCommand() *cobra.Command {

cmd.Flags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
cmd.Flags().StringVarP(&provider, "provider", "p", "", "Filter environments by a particular workspace provider name.")

return cmd
}
Expand Down