-
Notifications
You must be signed in to change notification settings - Fork 18
/
iam_apikey_create.go
79 lines (64 loc) · 1.82 KB
/
iam_apikey_create.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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type apiKeyCreateItemOutput struct {
Name string `json:"name"`
Key string `json:"key"`
Secret string `json:"secret,omitempty"`
Operations []string `json:"operations,omitempty"`
Type string `json:"type"`
}
func (o *apiKeyCreateItemOutput) toJSON() { outputJSON(o) }
func (o *apiKeyCreateItemOutput) toText() { outputText(o) }
func (o *apiKeyCreateItemOutput) toTable() { outputTable(o) }
// apiKeyCreateCmd represents an API key creation command
var apiKeyCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create an API key",
Long: fmt.Sprintf(`This command create an API key.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&apiKeyCreateItemOutput{}), ", ")),
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
ops, err := cmd.Flags().GetStringSlice("operation")
if err != nil {
return err
}
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateAPIKey{
Name: args[0],
Operations: strings.Join(ops, ","),
})
if err != nil {
return err
}
apiKey := resp.(*egoscale.APIKey)
if !gQuiet {
o := apiKeyCreateItemOutput{
Name: apiKey.Name,
Key: apiKey.Key,
Secret: apiKey.Secret,
Operations: apiKey.Operations,
Type: string(apiKey.Type),
}
if err := output(&o, err); err != nil {
return err
}
}
fmt.Print(`
/!\ Ensure to save your API Secret somewhere, /!\
/!\ as there is no way to recover it afterwards. /!\
`)
return nil
},
}
func init() {
apiKeyCreateCmd.Flags().StringSliceP("operation", "o", []string{}, "API key allowed operation")
apiKeyCmd.AddCommand(apiKeyCreateCmd)
}