Skip to content

feat: add provisioner key cli commands #13875

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 9 commits into from
Jul 18, 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
add tests
  • Loading branch information
f0ssel committed Jul 16, 2024
commit c67ed19310dfd18a0077712e850c9a8bddf6aef3
23 changes: 23 additions & 0 deletions docs/cli/provisionerd_keys.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions docs/cli/provisionerd_keys_create.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions docs/cli/provisionerd_keys_delete.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions docs/cli/provisionerd_keys_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions enterprise/cli/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (r *RootCmd) provisionerKeysCreate() *serpent.Command {
return xerrors.Errorf("create provisioner key: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Successfully created provisioner key %s!\n\n%s", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0]), pretty.Sprint(cliui.DefaultStyles.Keyword, res.Key))
_, _ = fmt.Fprintf(inv.Stdout, "Successfully created provisioner key %s!\n\n%s\n", pretty.Sprint(cliui.DefaultStyles.Keyword, strings.ToLower(inv.Args[0])), pretty.Sprint(cliui.DefaultStyles.Keyword, res.Key))

return nil
},
Expand Down Expand Up @@ -104,8 +105,9 @@ func (r *RootCmd) provisionerKeysList() *serpent.Command {

client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "list",
Short: "List provisioner keys",
Use: "list",
Short: "List provisioner keys",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
Expand Down Expand Up @@ -165,18 +167,28 @@ func (r *RootCmd) provisionerKeysDelete() *serpent.Command {
return xerrors.Errorf("current organization: %w", err)
}

_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Are you sure you want to delete provisioner key %s?", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0])),
IsConfirm: true,
})
if err != nil {
return err
}

err = client.DeleteProvisionerKey(ctx, org.ID, inv.Args[0])
if err != nil {
return xerrors.Errorf("delete provisioner key: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted provisioner key %s!", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0]))
_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted provisioner key %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, strings.ToLower(inv.Args[0])))

return nil
},
}

cmd.Options = serpent.OptionSet{}
cmd.Options = serpent.OptionSet{
cliui.SkipPromptOption(),
}
orgContext.AttachOptions(cmd)

return cmd
Expand Down
111 changes: 111 additions & 0 deletions enterprise/cli/provisionerkeys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cli_test

import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)

func TestProvisionerKeys(t *testing.T) {
t.Parallel()

t.Run("CRUD", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
client, owner := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureMultipleOrganizations: 1,
},
},
})
orgAdminClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.ScopedRoleOrgAdmin(owner.OrganizationID))

name := "dont-TEST-me"
ctx := testutil.Context(t, testutil.WaitMedium)
inv, conf := newCLI(
t,
"provisioner", "keys", "create", name,
)

pty := ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, orgAdminClient, conf)

err := inv.WithContext(ctx).Run()
require.NoError(t, err)

line := pty.ReadLine(ctx)
require.Contains(t, line, "Successfully created provisioner key")
require.Contains(t, line, strings.ToLower(name))
// empty line
_ = pty.ReadLine(ctx)
key := pty.ReadLine(ctx)
require.NotEmpty(t, key)
parts := strings.Split(key, ":")
require.Len(t, parts, 2, "expected 2 parts")
_, err = uuid.Parse(parts[0])
require.NoError(t, err, "expected token to be a uuid")

inv, conf = newCLI(
t,
"provisioner", "keys", "ls",
)
pty = ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, orgAdminClient, conf)

err = inv.WithContext(ctx).Run()
require.NoError(t, err)
line = pty.ReadLine(ctx)
require.Contains(t, line, "NAME")
require.Contains(t, line, "CREATED AT")
require.Contains(t, line, "ORGANIZATION ID")
line = pty.ReadLine(ctx)
require.Contains(t, line, strings.ToLower(name))

inv, conf = newCLI(
t,
"provisioner", "keys", "delete", "-y", name,
)

pty = ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, orgAdminClient, conf)

err = inv.WithContext(ctx).Run()
require.NoError(t, err)
line = pty.ReadLine(ctx)
require.Contains(t, line, "Successfully deleted provisioner key")
require.Contains(t, line, strings.ToLower(name))

inv, conf = newCLI(
t,
"provisioner", "keys", "ls",
)
pty = ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, orgAdminClient, conf)

err = inv.WithContext(ctx).Run()
require.NoError(t, err)
line = pty.ReadLine(ctx)
require.Contains(t, line, "No provisioner keys found")
})
}