Skip to content

fix: improve provisioner key cli usability #14834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 27, 2024
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
Prev Previous commit
Next Next commit
tests:
  • Loading branch information
f0ssel committed Sep 27, 2024
commit 9f5653a0035c910e664adcc4ae2a949d4d2df15d
21 changes: 16 additions & 5 deletions codersdk/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/http/cookiejar"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -274,12 +275,22 @@ func (c *Client) ServeProvisionerDaemon(ctx context.Context, req ServeProvisione
return proto.NewDRPCProvisionerDaemonClient(drpc.MultiplexedConn(session)), nil
}

type ProvisionerKeyTags map[string]string

func (p ProvisionerKeyTags) String() string {
tags := []string{}
for key, value := range p {
tags = append(tags, fmt.Sprintf("%s=%s", key, value))
}
return strings.Join(tags, " ")
}

type ProvisionerKey struct {
ID uuid.UUID `json:"id" format:"uuid"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
OrganizationID uuid.UUID `json:"organization" format:"uuid"`
Name string `json:"name"`
Tags map[string]string `json:"tags"`
ID uuid.UUID `json:"id" table:"-" format:"uuid"`
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
OrganizationID uuid.UUID `json:"organization" table:"-" format:"uuid"`
Name string `json:"name" table:"name,default_sort"`
Tags ProvisionerKeyTags `json:"tags" table:"tags"`
// HashedSecret - never include the access token in the API response
}

Expand Down
28 changes: 2 additions & 26 deletions enterprise/cli/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"fmt"
"strings"
"time"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -96,7 +95,7 @@ func (r *RootCmd) provisionerKeysList() *serpent.Command {
var (
orgContext = agpl.NewOrganizationContext()
formatter = cliui.NewOutputFormatter(
cliui.TableFormat([]provisionerKeyListItem{}, nil),
cliui.TableFormat([]codersdk.ProvisionerKey{}, []string{"created at", "name", "tags"}),
cliui.JSONFormat(),
)
)
Expand Down Expand Up @@ -128,7 +127,7 @@ func (r *RootCmd) provisionerKeysList() *serpent.Command {
return nil
}

out, err := formatter.Format(inv.Context(), provisionerKeyList(keys))
out, err := formatter.Format(inv.Context(), keys)
if err != nil {
return xerrors.Errorf("display provisioner keys: %w", err)
}
Expand All @@ -145,29 +144,6 @@ func (r *RootCmd) provisionerKeysList() *serpent.Command {
return cmd
}

type provisionerKeyListItem struct {
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
Name string `json:"name" table:"name,default_sort"`
Tags string `json:"tags" table:"tags"`
}

func provisionerKeyList(items []codersdk.ProvisionerKey) []provisionerKeyListItem {
var out []provisionerKeyListItem
for _, item := range items {
tags := []string{}
for k, v := range item.Tags {
tags = append(tags, fmt.Sprintf("%s=%s", k, v))
}
out = append(out, provisionerKeyListItem{
Name: item.Name,
CreatedAt: item.CreatedAt,
Tags: strings.Join(tags, ", "),
})
}

return out
}

func (r *RootCmd) provisionerKeysDelete() *serpent.Command {
orgContext := agpl.NewOrganizationContext()

Expand Down
5 changes: 2 additions & 3 deletions enterprise/cli/provisionerkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestProvisionerKeys(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)
inv, conf := newCLI(
t,
"provisioner", "keys", "create", name, "--tag", "foo=bar",
"provisioner", "keys", "create", name, "--tag", "foo=bar", "--tag", "my=way",
)

pty := ptytest.New(t)
Expand Down Expand Up @@ -73,11 +73,10 @@ func TestProvisionerKeys(t *testing.T) {
line = pty.ReadLine(ctx)
require.Contains(t, line, "NAME")
require.Contains(t, line, "CREATED AT")
require.Contains(t, line, "ORGANIZATION ID")
require.Contains(t, line, "TAGS")
line = pty.ReadLine(ctx)
require.Contains(t, line, strings.ToLower(name))
require.Contains(t, line, "map[foo:bar]")
require.Contains(t, line, "foo=bar my=way")

inv, conf = newCLI(
t,
Expand Down
2 changes: 1 addition & 1 deletion enterprise/coderd/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func convertProvisionerKeys(dbKeys []database.ProvisionerKey) []codersdk.Provisi
CreatedAt: dbKey.CreatedAt,
OrganizationID: dbKey.OrganizationID,
Name: dbKey.Name,
Tags: dbKey.Tags,
Tags: codersdk.ProvisionerKeyTags(dbKey.Tags),
// HashedSecret - never include the access token in the API response
})
}
Expand Down
Loading