From 71088ae56eb5fbbae9e8eb38c2da2272dcaba7fd Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 8 Jul 2024 16:30:37 -0500 Subject: [PATCH 1/4] chore: cli template list includes all templates Shows all accessible templates from all organizations --- cli/templatelist.go | 10 ++-------- cli/templatelist_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/cli/templatelist.go b/cli/templatelist.go index d014cdd6cef47..abd9a3600dd0f 100644 --- a/cli/templatelist.go +++ b/cli/templatelist.go @@ -11,7 +11,6 @@ import ( ) func (r *RootCmd) templateList() *serpent.Command { - orgContext := NewOrganizationContext() formatter := cliui.NewOutputFormatter( cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}), cliui.JSONFormat(), @@ -26,17 +25,13 @@ func (r *RootCmd) templateList() *serpent.Command { r.InitClient(client), ), Handler: func(inv *serpent.Invocation) error { - organization, err := orgContext.Selected(inv, client) - if err != nil { - return err - } - templates, err := client.TemplatesByOrganization(inv.Context(), organization.ID) + templates, err := client.Templates(inv.Context(), codersdk.TemplateFilter{}) if err != nil { return err } if len(templates) == 0 { - _, _ = fmt.Fprintf(inv.Stderr, "%s No templates found in %s! Create one:\n\n", Caret, color.HiWhiteString(organization.Name)) + _, _ = fmt.Fprintf(inv.Stderr, "%s No templates found! Create one:\n\n", Caret) _, _ = fmt.Fprintln(inv.Stderr, color.HiMagentaString(" $ coder templates push \n")) return nil } @@ -53,6 +48,5 @@ func (r *RootCmd) templateList() *serpent.Command { } formatter.AttachOptions(&cmd.Options) - orgContext.AttachOptions(cmd) return cmd } diff --git a/cli/templatelist_test.go b/cli/templatelist_test.go index 3ce91da91b75e..28ee69faa1e91 100644 --- a/cli/templatelist_test.go +++ b/cli/templatelist_test.go @@ -114,4 +114,39 @@ func TestTemplateList(t *testing.T) { pty.ExpectMatch(org.Name) pty.ExpectMatch("Create one:") }) + + t.Run("MultiOrg", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) + owner := coderdtest.CreateFirstUser(t, client) + + // Template in the first organization + firstVersion := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil) + _ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, firstVersion.ID) + _ = coderdtest.CreateTemplate(t, client, owner.OrganizationID, firstVersion.ID) + + secondOrg := coderdtest.CreateOrganization(t, client, coderdtest.CreateOrganizationOptions{ + IncludeProvisionerDaemon: true, + }) + secondVersion := coderdtest.CreateTemplateVersion(t, client, secondOrg.ID, nil) + _ = coderdtest.CreateTemplate(t, client, secondOrg.ID, secondVersion.ID) + + // Create a site wide template admin + templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin()) + + inv, root := clitest.New(t, "templates", "list", "--output=json") + clitest.SetupConfig(t, templateAdmin, root) + + ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong) + defer cancelFunc() + + out := bytes.NewBuffer(nil) + inv.Stdout = out + err := inv.WithContext(ctx).Run() + require.NoError(t, err) + + var templates []codersdk.Template + require.NoError(t, json.Unmarshal(out.Bytes(), &templates)) + require.Len(t, templates, 2) + }) } From d61eec0c4267d325529f0703b84718c2ea8637bd Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 9 Jul 2024 08:06:30 -0500 Subject: [PATCH 2/4] make gne --- docs/cli/templates_list.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/docs/cli/templates_list.md b/docs/cli/templates_list.md index 551b03ed9ffc1..24eb51fe64e6a 100644 --- a/docs/cli/templates_list.md +++ b/docs/cli/templates_list.md @@ -33,12 +33,3 @@ Columns to display in table output. Available columns: name, created at, last up | Default | table | Output format. Available formats: table, json. - -### -O, --org - -| | | -| ----------- | -------------------------------- | -| Type | string | -| Environment | $CODER_ORGANIZATION | - -Select which organization (uuid or name) to use. From ca2a8ded9a287a86aa3b54cf4abe783497ec4533 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 9 Jul 2024 08:23:39 -0500 Subject: [PATCH 3/4] fix unit test --- cli/templatelist_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cli/templatelist_test.go b/cli/templatelist_test.go index 28ee69faa1e91..5181720cc30b2 100644 --- a/cli/templatelist_test.go +++ b/cli/templatelist_test.go @@ -88,9 +88,6 @@ func TestTemplateList(t *testing.T) { client := coderdtest.New(t, &coderdtest.Options{}) owner := coderdtest.CreateFirstUser(t, client) - org, err := client.Organization(context.Background(), owner.OrganizationID) - require.NoError(t, err) - templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin()) inv, root := clitest.New(t, "templates", "list") @@ -110,8 +107,7 @@ func TestTemplateList(t *testing.T) { require.NoError(t, <-errC) - pty.ExpectMatch("No templates found in") - pty.ExpectMatch(org.Name) + pty.ExpectMatch("No templates found") pty.ExpectMatch("Create one:") }) From 60b28d143a386740e25e0dd67f5e1688a0ae13a4 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 9 Jul 2024 09:15:08 -0500 Subject: [PATCH 4/4] update golden files --- cli/testdata/coder_templates_list_--help.golden | 3 --- 1 file changed, 3 deletions(-) diff --git a/cli/testdata/coder_templates_list_--help.golden b/cli/testdata/coder_templates_list_--help.golden index a45c5062ddaae..d8bfc63665d10 100644 --- a/cli/testdata/coder_templates_list_--help.golden +++ b/cli/testdata/coder_templates_list_--help.golden @@ -8,9 +8,6 @@ USAGE: Aliases: ls OPTIONS: - -O, --org string, $CODER_ORGANIZATION - Select which organization (uuid or name) to use. - -c, --column string-array (default: name,organization name,last updated,used by) Columns to display in table output. Available columns: name, created at, last updated, organization id, organization name, provisioner,