-
Notifications
You must be signed in to change notification settings - Fork 18
/
iam_apikey.go
72 lines (59 loc) · 1.56 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
62
63
64
65
66
67
68
69
70
71
72
package cmd
import (
"fmt"
"os"
"sort"
"time"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// apiKeycmd represent the API key command
var apiKeyCmd = &cobra.Command{
Use: "apikey",
Short: "API Keys management",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
fmt.Fprintln(os.Stderr,
`**********************************************************************
The "exo iam apikey" commands are deprecated and will be removed in a future
version, please use "exo iam access-key" replacement commands.
**********************************************************************`)
time.Sleep(3 * time.Second)
},
Hidden: true,
}
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)
}