-
Notifications
You must be signed in to change notification settings - Fork 19
/
iam_apikey_create.go
161 lines (131 loc) · 3.68 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
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
}
}
return addAPIKeyInConfigFile(apiKey)
},
}
func addAPIKeyInConfigFile(apiKey *egoscale.APIKey) error {
reader := bufio.NewReader(os.Stdin)
config := &config{}
var client *egoscale.Client
newAccount := &account{
Endpoint: defaultEndpoint,
Key: apiKey.Key,
Secret: apiKey.Secret,
}
if askQuestion("do you wish to add this account in your config file?") {
for i := 0; ; i++ {
if i > 0 {
endpoint, err := readInput(reader, "API Endpoint", newAccount.Endpoint)
if err != nil {
return err
}
if endpoint != newAccount.Endpoint {
newAccount.Endpoint = endpoint
}
}
client = egoscale.NewClient(newAccount.Endpoint, newAccount.Key, newAccount.APISecret())
fmt.Printf("Checking the credentials of %q...", newAccount.Key)
resp, err := client.GetWithContext(gContext, egoscale.Account{})
if err != nil {
fmt.Print(` failure.
Let's start over.
`)
} else {
fmt.Print(" success!\n\n")
acc := resp.(*egoscale.Account)
newAccount.Name = apiKey.Name
newAccount.Account = acc.Name
break
}
}
name, err := readInput(reader, "Name", newAccount.Name)
if err != nil {
return err
}
if name != "" {
newAccount.Name = name
}
for {
if a := getAccountByName(newAccount.Name); a == nil {
break
}
fmt.Printf("Name [%s] already exist\n", name)
name, err = readInput(reader, "Name", newAccount.Name)
if err != nil {
return err
}
newAccount.Name = name
}
defaultZone, err := chooseZone(newAccount.Name, client)
if err != nil {
return err
}
newAccount.DefaultZone = defaultZone
newAccount.DNSEndpoint = strings.Replace(newAccount.Endpoint, "/compute", "/dns", 1)
config.Accounts = append(config.Accounts, *newAccount)
if askQuestion("Make [" + newAccount.Name + "] your default profile?") {
config.DefaultAccount = newAccount.Name
viper.Set("defaultAccount", newAccount.Name)
}
return addAccount(viper.ConfigFileUsed(), config)
}
return nil
}
func init() {
apiKeyCreateCmd.Flags().StringSliceP("operation", "o", []string{}, "API key allowed operation")
apiKeyCmd.AddCommand(apiKeyCreateCmd)
}