-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_api_key_list.go
115 lines (90 loc) · 2.8 KB
/
iam_api_key_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type iamAPIKeyListItemOutput struct {
Name string `json:"name"`
Key string `json:"key"`
Role string `json:"role-id"`
}
type iamAPIKeyListOutput []iamAPIKeyListItemOutput
func (o *iamAPIKeyListOutput) ToJSON() { output.JSON(o) }
func (o *iamAPIKeyListOutput) ToText() { output.Text(o) }
func (o *iamAPIKeyListOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{
"NAME",
"KEY",
"ROLE",
})
defer t.Render()
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
// For better UX we will print both role name and ID
rolesMap := map[string]string{}
iamRoles, err := globalstate.EgoscaleClient.ListIAMRoles(ctx, zone)
// If API returns error, can continue (print name only) as this is non-essential feature
if err == nil {
for _, role := range iamRoles {
if role.ID != nil && role.Name != nil {
rolesMap[*role.ID] = *role.Name
}
}
}
for _, apikey := range *o {
role := apikey.Role
if name, ok := rolesMap[apikey.Role]; ok {
role = fmt.Sprintf("%s (%s)", name, apikey.Role)
}
t.Append([]string{
apikey.Name,
apikey.Key,
role,
})
}
}
type iamAPIKeyListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
}
func (c *iamAPIKeyListCmd) cmdAliases() []string { return gListAlias }
func (c *iamAPIKeyListCmd) cmdShort() string { return "List API Keys" }
func (c *iamAPIKeyListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists all API Keys.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&iamAPIKeyListOutput{}), ", "))
}
func (c *iamAPIKeyListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *iamAPIKeyListCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
apikeys, err := globalstate.EgoscaleClient.ListAPIKeys(ctx, zone)
if err != nil {
return err
}
out := make(iamAPIKeyListOutput, 0)
for _, apikey := range apikeys {
out = append(out, iamAPIKeyListItemOutput{
Name: utils.DefaultString(apikey.Name, ""),
Key: utils.DefaultString(apikey.Key, ""),
Role: utils.DefaultString(apikey.RoleID, ""),
})
}
return c.outputFunc(&out, err)
}
func init() {
cobra.CheckErr(registerCLICommand(iamAPIKeyCmd, &iamAPIKeyListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}