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

Commit e9874ea

Browse files
author
Faris Huskovic
committed
feature: support filtering by workspace provider when listing environments
1 parent 94a596d commit e9874ea

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

ci/integration/envs_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ func TestEnvsCLI(t *testing.T) {
144144
tcli.StdoutMatches(regexp.QuoteMeta(name)),
145145
)
146146

147+
// filter by provider that does not exist should fail
148+
doesntExist := randString(10)
149+
c.Run(ctx, fmt.Sprintf("coder envs ls --provider %s", doesntExist)).Assert(t,
150+
tcli.Error(),
151+
tcli.StderrMatches(regexp.QuoteMeta(fmt.Sprintf("fatal: no environments found for workspace provider %q", doesntExist))),
152+
)
153+
154+
// filter by provider that does exist should succeed
155+
var envs []coder.Environment
156+
c.Run(ctx, "coder envs ls --provider built-in").Assert(t,
157+
tcli.Success(),
158+
tcli.StdoutJSONUnmarshal(&envs),
159+
)
160+
147161
var env coder.Environment
148162
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
149163
tcli.Success(),

coder-sdk/env.go

+8
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,11 @@ func (c *DefaultClient) EnvironmentByID(ctx context.Context, id string) (*Enviro
338338
}
339339
return &env, nil
340340
}
341+
342+
func (c *DefaultClient) EnvironmentsByWorkspaceProvider(ctx context.Context, wpID string) ([]Environment, error) {
343+
var envs []Environment
344+
if err := c.requestBody(ctx, http.MethodGet, "/api/private/resource-pools/"+wpID+"/environments/", nil, &envs); err != nil {
345+
return nil, err
346+
}
347+
return envs, nil
348+
}

coder-sdk/interface.go

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ type Client interface {
130130
// EnvironmentByID get the details of an environment by its id.
131131
EnvironmentByID(ctx context.Context, id string) (*Environment, error)
132132

133+
// EnvironmentsByWorkspaceProvider returns environments that belong to a particular workspace provider.
134+
EnvironmentsByWorkspaceProvider(ctx context.Context, wpID string) ([]Environment, error)
135+
133136
// ImportImage creates a new image and optionally a new registry.
134137
ImportImage(ctx context.Context, req ImportImageReq) (*Image, error)
135138

internal/cmd/ceapi.go

+27
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,30 @@ func getUserOrgs(ctx context.Context, client coder.Client, email string) ([]code
202202
}
203203
return lookupUserOrgs(u, orgs), nil
204204
}
205+
206+
func getProviderByName(ctx context.Context, client coder.Client, wpName string) (*coder.KubernetesProvider, error) {
207+
providers, err := client.WorkspaceProviders(ctx)
208+
if err != nil {
209+
return nil, err
210+
}
211+
212+
for _, provider := range providers.Kubernetes {
213+
if provider.Name == wpName {
214+
return &provider, nil
215+
}
216+
}
217+
return nil, xerrors.Errorf("workspace provider %q not found", wpName)
218+
}
219+
220+
func getEnvsByProvider(ctx context.Context, client coder.Client, wpName string) ([]coder.Environment, error) {
221+
wp, err := getProviderByName(ctx, client, wpName)
222+
if err != nil {
223+
return nil, err
224+
}
225+
226+
envs, err := client.EnvironmentsByWorkspaceProvider(ctx, wp.ID)
227+
if err != nil {
228+
return nil, err
229+
}
230+
return envs, nil
231+
}

internal/cmd/envs.go

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func lsEnvsCommand() *cobra.Command {
5151
var (
5252
outputFmt string
5353
user string
54+
provider string
5455
)
5556

5657
cmd := &cobra.Command{
@@ -67,6 +68,12 @@ func lsEnvsCommand() *cobra.Command {
6768
if err != nil {
6869
return err
6970
}
71+
if provider != "" {
72+
envs, err = getEnvsByProvider(ctx, client, provider)
73+
if err != nil {
74+
return err
75+
}
76+
}
7077
if len(envs) < 1 {
7178
clog.LogInfo("no environments found")
7279
envs = []coder.Environment{} // ensures that json output still marshals
@@ -94,6 +101,7 @@ func lsEnvsCommand() *cobra.Command {
94101

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

98106
return cmd
99107
}

0 commit comments

Comments
 (0)