-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_access_key_listoperations.go
112 lines (86 loc) · 2.96 KB
/
iam_access_key_listoperations.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
package cmd
import (
"fmt"
"os"
"sort"
"strings"
"github.com/exoscale/cli/table"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type iamAccessKeyListOperationsItemOutput struct {
Operation string `json:"operation"`
Tags []string `json:"tags"`
}
type iamAccessKeyListOperationsOutput []iamAccessKeyListOperationsItemOutput
func (o *iamAccessKeyListOperationsOutput) toJSON() { outputJSON(o) }
func (o *iamAccessKeyListOperationsOutput) toText() { outputText(o) }
func (o *iamAccessKeyListOperationsOutput) toTable() {
operationByTag := make(map[string][]string)
for _, op := range *o {
for _, tag := range op.Tags {
if _, ok := operationByTag[tag]; !ok {
operationByTag[tag] = make([]string, 0)
}
operationByTag[tag] = append(operationByTag[tag], op.Operation)
}
}
t := table.NewTable(os.Stdout)
defer t.Render()
t.SetHeader([]string{"Tag", "Operations"})
sortedTags := make([]string, 0)
for tag := range operationByTag {
sortedTags = append(sortedTags, tag)
}
sort.Strings(sortedTags)
for _, tag := range sortedTags {
operations := operationByTag[tag]
sort.Strings(operations)
t.Append([]string{tag, strings.Join(operations, "\n")})
}
}
type iamAccessKeyListOperationsCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list-operations"`
Mine bool `cli-usage:"only report operations available to the IAM access key used to perform the API request"`
}
func (c *iamAccessKeyListOperationsCmd) cmdAliases() []string { return gListAlias }
func (c *iamAccessKeyListOperationsCmd) cmdShort() string { return "List IAM access keys operations" }
func (c *iamAccessKeyListOperationsCmd) cmdLong() string {
return fmt.Sprintf(`This command lists operations available to IAM access keys.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&iamAccessKeyListOperationsOutput{}), ", "))
}
func (c *iamAccessKeyListOperationsCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *iamAccessKeyListOperationsCmd) cmdRun(_ *cobra.Command, _ []string) error {
var (
iamAccessKeyOperations []*egoscale.IAMAccessKeyOperation
err error
)
zone := gCurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
if c.Mine {
iamAccessKeyOperations, err = cs.ListMyIAMAccessKeyOperations(ctx, zone)
} else {
iamAccessKeyOperations, err = cs.ListIAMAccessKeyOperations(ctx, zone)
}
if err != nil {
return err
}
out := make(iamAccessKeyListOperationsOutput, 0)
for _, o := range iamAccessKeyOperations {
out = append(out, iamAccessKeyListOperationsItemOutput{
Operation: o.Name,
Tags: o.Tags,
})
}
return c.outputFunc(&out, err)
}
func init() {
cobra.CheckErr(registerCLICommand(iamAccessKeyCmd, &iamAccessKeyListOperationsCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}