-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_apikey_create.go
91 lines (75 loc) · 2.18 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
80
81
82
83
84
85
86
87
88
89
90
91
package cmd
import (
"fmt"
"os"
"sort"
"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"`
Resources []string `json:"resources,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
}
res, err := cmd.Flags().GetStringSlice("resource")
if err != nil {
return err
}
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateAPIKey{
Name: args[0],
Operations: strings.Join(ops, ","),
Resources: strings.Join(res, ","),
})
if err != nil {
return err
}
apiKey := resp.(*egoscale.APIKey)
sort.Strings(apiKey.Operations)
if !gQuiet {
o := apiKeyCreateItemOutput{
Name: apiKey.Name,
Key: apiKey.Key,
Secret: apiKey.Secret,
Operations: apiKey.Operations,
Resources: apiKey.Resources,
Type: string(apiKey.Type),
}
if err := output(&o, err); err != nil {
return err
}
}
fmt.Fprint(os.Stderr, `
/!\ 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")
apiKeyCreateCmd.Flags().StringSliceP("resource", "r", []string{}, "API key allowed resource")
apiKeyCmd.AddCommand(apiKeyCreateCmd)
}