-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83e3868
add api routes
f0ssel e4cf813
feat: add provisioner key cli commands
f0ssel 9fd47c8
fix imports
f0ssel 3d1b075
fmt
f0ssel c67ed19
add tests
f0ssel adffefd
make golden
f0ssel da95666
hide commands
f0ssel 90412c7
pr comments
f0ssel 86c0d02
remove rows type
f0ssel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add provisioner key cli commands
- Loading branch information
commit e4cf8131b7e88908d922fe9b72f73e1f6516f30d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
|
||
agpl "github.com/coder/coder/v2/cli" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/pretty" | ||
"github.com/coder/serpent" | ||
) | ||
|
||
func (r *RootCmd) provisionerKeys() *serpent.Command { | ||
cmd := &serpent.Command{ | ||
Use: "keys", | ||
Short: "Manage provisioner keys", | ||
Handler: func(inv *serpent.Invocation) error { | ||
return inv.Command.HelpHandler(inv) | ||
}, | ||
Aliases: []string{"key"}, | ||
Children: []*serpent.Command{ | ||
r.provisionerKeysCreate(), | ||
r.provisionerKeysList(), | ||
r.provisionerKeysDelete(), | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (r *RootCmd) provisionerKeysCreate() *serpent.Command { | ||
var ( | ||
orgContext = agpl.NewOrganizationContext() | ||
) | ||
|
||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
Use: "create <name>", | ||
Short: "Create a new provisioner key", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(1), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
ctx := inv.Context() | ||
|
||
org, err := orgContext.Selected(inv, client) | ||
if err != nil { | ||
return xerrors.Errorf("current organization: %w", err) | ||
} | ||
|
||
res, err := client.CreateProvisionerKey(ctx, org.ID, codersdk.CreateProvisionerKeyRequest{ | ||
Name: inv.Args[0], | ||
}) | ||
if err != nil { | ||
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], res.Key)) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Options = serpent.OptionSet{} | ||
|
||
return cmd | ||
} | ||
|
||
type provisionerKeysTableRow struct { | ||
// For json output: | ||
Key codersdk.ProvisionerKey `table:"-"` | ||
|
||
// For table output: | ||
Name string `json:"-" table:"name,default_sort"` | ||
CreatedAt time.Time `json:"-" table:"created_at"` | ||
OrganizationID uuid.UUID `json:"-" table:"organization_id"` | ||
} | ||
|
||
func provisionerKeysToRows(keys ...codersdk.ProvisionerKey) []provisionerKeysTableRow { | ||
rows := make([]provisionerKeysTableRow, 0, len(keys)) | ||
for _, key := range keys { | ||
rows = append(rows, provisionerKeysTableRow{ | ||
Name: key.Name, | ||
CreatedAt: key.CreatedAt, | ||
OrganizationID: key.OrganizationID, | ||
}) | ||
} | ||
|
||
return rows | ||
} | ||
|
||
func (r *RootCmd) provisionerKeysList() *serpent.Command { | ||
var ( | ||
orgContext = agpl.NewOrganizationContext() | ||
formatter = cliui.NewOutputFormatter( | ||
cliui.TableFormat([]provisionerKeysTableRow{}, nil), | ||
cliui.JSONFormat(), | ||
) | ||
) | ||
|
||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
Use: "list", | ||
Short: "List provisioner keys", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(0), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
ctx := inv.Context() | ||
|
||
org, err := orgContext.Selected(inv, client) | ||
if err != nil { | ||
return xerrors.Errorf("current organization: %w", err) | ||
} | ||
|
||
keys, err := client.ListProvisionerKeys(ctx, org.ID) | ||
if err != nil { | ||
return xerrors.Errorf("list provisioner keys: %w", err) | ||
} | ||
|
||
if len(keys) == 0 { | ||
_, _ = fmt.Fprintln(inv.Stdout, "No provisioner keys found") | ||
return nil | ||
} | ||
|
||
rows := provisionerKeysToRows(keys...) | ||
out, err := formatter.Format(inv.Context(), rows) | ||
if err != nil { | ||
return xerrors.Errorf("display provisioner keys: %w", err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(inv.Stdout, out) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Options = serpent.OptionSet{} | ||
|
||
return cmd | ||
} | ||
|
||
func (r *RootCmd) provisionerKeysDelete() *serpent.Command { | ||
var ( | ||
orgContext = agpl.NewOrganizationContext() | ||
) | ||
|
||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
Use: "delete <name>", | ||
Short: "Delete a provisioner key", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(1), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
ctx := inv.Context() | ||
|
||
org, err := orgContext.Selected(inv, client) | ||
if err != nil { | ||
return xerrors.Errorf("current organization: %w", 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])) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Options = serpent.OptionSet{} | ||
|
||
return cmd | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.