-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_apikey.go
61 lines (48 loc) · 1.14 KB
/
iam_apikey.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
package cmd
import (
"fmt"
"sort"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// apiKeycmd represent the API key command
var apiKeyCmd = &cobra.Command{
Use: "apikey",
Short: "API Keys management",
}
func getAPIKeyByKey(key string) (*egoscale.APIKey, error) {
resp, err := cs.RequestWithContext(gContext, egoscale.GetAPIKey{
Key: key,
})
if err != nil {
return nil, err
}
return resp.(*egoscale.APIKey), nil
}
func getAPIKeyByName(name string) (*egoscale.APIKey, error) {
apiKeys := []egoscale.APIKey{}
if apiKey, err := getAPIKeyByKey(name); err == nil {
return apiKey, err
}
resp, err := cs.RequestWithContext(gContext, egoscale.ListAPIKeys{})
if err != nil {
return nil, err
}
r := resp.(*egoscale.ListAPIKeysResponse)
for _, i := range r.APIKeys {
if i.Name == name {
apiKeys = append(apiKeys, i)
}
}
switch count := len(apiKeys); {
case count == 0:
return nil, fmt.Errorf("not found: %q", name)
case count > 1:
return nil, fmt.Errorf(`more than one element found: %d`, count)
}
sort.Strings(apiKeys[0].Operations)
return &apiKeys[0], nil
}
func init() {
iamCmd.AddCommand(apiKeyCmd)
}