-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_apikey_operations.go
80 lines (66 loc) · 2.18 KB
/
iam_apikey_operations.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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type apiKeyOperationsItemOutput struct {
Account []string `json:"account,omitempty"`
Compute []string `json:"compute,omitempty"`
DNS []string `json:"dns,omitempty"`
IAM []string `json:"iam,omitempty"`
SOS []string `json:"sos,omitempty"`
}
func (o *apiKeyOperationsItemOutput) ToJSON() { output.JSON(o) }
func (o *apiKeyOperationsItemOutput) ToText() { output.Text(o) }
func (o *apiKeyOperationsItemOutput) ToTable() { output.Table(o) }
var apiKeyOperationsCmd = &cobra.Command{
Use: "operations [FILTER]...",
Short: "List supported API key operations",
Long: fmt.Sprintf(`This command lists all supported operations for an API key.
Optional patterns can be provided to filter results by compute, DNS, IAM or SOS operations.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&apiKeyOperationsItemOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
return printOutput(listAPIKeyOperations(args))
},
}
func listAPIKeyOperations(filters []string) (output.Outputter, error) {
resp, err := globalstate.EgoscaleClient.RequestWithContext(gContext, &egoscale.ListAPIKeyOperations{})
if err != nil {
return nil, err
}
opes := resp.(*egoscale.ListAPIKeyOperationsResponse)
out := apiKeyOperationsItemOutput{}
for _, o := range opes.Operations {
operation := strings.ToLower(o)
result := operation
for _, f := range filters {
result = ""
filter := strings.ToLower(f)
if strings.Contains(operation, filter) {
result = operation
break
}
}
switch {
case strings.HasPrefix(result, "account/"):
out.Account = append(out.Account, o)
case strings.HasPrefix(result, "compute/"):
out.Compute = append(out.Compute, o)
case strings.HasPrefix(result, "dns/"):
out.DNS = append(out.DNS, o)
case strings.HasPrefix(result, "iam/"):
out.IAM = append(out.IAM, o)
case strings.HasPrefix(result, "sos/"):
out.SOS = append(out.SOS, o)
}
}
return &out, nil
}
func init() {
apiKeyCmd.AddCommand(apiKeyOperationsCmd)
}