Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cli/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ func AddListCmd(rootCmd *cobra.Command, opts GlobalOptions) {
}
rootCmd.AddCommand(list.cmd)
AddListPackagesCmd(list.cmd, opts)
AddListAPIKeysCmd(list.cmd, opts)
}
56 changes: 56 additions & 0 deletions cli/cmd/list_api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"fmt"

"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/util"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

type ListAPIKeysCmd struct {
cmd *cobra.Command
TableWriter util.TableWriter
}

func (c *ListAPIKeysCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
keys, err := p.ListAPIKeys()
if err != nil {
return fmt.Errorf("failed to list api keys: %w", err)
}

c.PrintKeysTable(keys)
return nil
}

func AddListAPIKeysCmd(list *cobra.Command, opts GlobalOptions) {
c := ListAPIKeysCmd{
cmd: &cobra.Command{
Use: "api-keys",
Short: "List API keys",
Long: io.Long(`List API keys registered in the OMS portal.`),
},
TableWriter: util.GetTableWriter(),
}

c.cmd.RunE = c.RunE

list.AddCommand(c.cmd)
}

func (c *ListAPIKeysCmd) PrintKeysTable(keys []portal.ApiKey) {
c.TableWriter.AppendHeader(table.Row{"ID", "Owner", "Organization", "Role", "Created", "Expires"})

for _, k := range keys {
c.TableWriter.AppendRow(table.Row{k.KeyID, k.Owner, k.Organization, k.Role, k.CreatedAt, k.ExpiresAt})
}

c.TableWriter.Render()
}
5 changes: 3 additions & 2 deletions internal/portal/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package portal
import "time"

type ApiKey struct {
RID string `json:"rid"`
ApiKey string `json:"api_key"`
KeyID string `json:"key_id"`
Owner string `json:"owner"`
Organization string `json:"organization"`
Role string `json:"role"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
// Temp
ApiKey string `json:"api_key"`
}
15 changes: 15 additions & 0 deletions internal/portal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Portal interface {
DownloadBuildArtifact(product Product, build Build, file io.Writer) error
RegisterAPIKey(owner string, organization string, role string, expiresAt time.Time) error
RevokeAPIKey(key string) error
ListAPIKeys() ([]ApiKey, error)
}

type PortalClient struct {
Expand Down Expand Up @@ -256,3 +257,17 @@ func (c *PortalClient) RevokeAPIKey(keyId string) error {

return nil
}

func (c *PortalClient) ListAPIKeys() ([]ApiKey, error) {
res, _, err := c.GetBody("/keys")
if err != nil {
return nil, fmt.Errorf("failed to list api keys: %w", err)
}

var keys []ApiKey
if err := json.Unmarshal(res, &keys); err != nil {
return nil, fmt.Errorf("failed to parse api keys response: %w", err)
}

return keys, nil
}
56 changes: 55 additions & 1 deletion internal/portal/mocks.go

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