/
apiKey.go
182 lines (145 loc) · 4.59 KB
/
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright © 2020 Axway, Inc. <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"fmt"
"github.com/Axway-API-Management-Plus/go-apim-cli/apimgr"
"github.com/antihax/optional"
"github.com/skckadiyala/kubecrt-vms/utils"
"github.com/spf13/cobra"
)
// apiKeyCmd represents the apiKey command
var (
keyCmd = &cobra.Command{
Use: "key",
Aliases: []string{"apikeys"},
Short: "Create an APIKey",
Long: `Create an APIKey for an application.
For example:
# Create an apiKey in an application
apimanager create apiKey -o <appname>
apimanager create key -a <appname>
`,
Run: createAPIKey,
}
keyListCmd = &cobra.Command{
Use: "keys",
Aliases: []string{"apikeys"},
Short: "List all apiKeys",
Long: `List all apiKeys from an application.
For example:
# list all the applications
apimanager list keys -a <appName>
`,
Run: listAPIKeys,
}
keyDelCmd = &cobra.Command{
Use: "key",
Aliases: []string{"apikeys"},
Short: "Delete an apiKey",
Long: `Delete an apiKey from an application.
For example:
# Delete an apikey from the application
apimanager delete key -a <appName> -k <keyID>
`,
Run: deleteAPIKey,
}
)
func init() {
createCmd.AddCommand(keyCmd)
listCmd.AddCommand(keyListCmd)
deleteCmd.AddCommand(keyDelCmd)
keyCmd.Flags().StringVarP(&appName, "appName", "a", "", "The name to store application name")
keyCmd.MarkFlagRequired("appName")
keyCmd.Flags().StringVarP(&keyID, "apikey", "k", "", "The name to store unique apiKey")
keyCmd.Flags().StringVarP(&secret, "secret", "s", "", "The name to store unique Secret")
keyListCmd.Flags().StringVarP(&appName, "appName", "a", "", "The name to store application name")
keyListCmd.MarkFlagRequired("appName")
keyDelCmd.Flags().StringVarP(&appName, "appName", "a", "", "The name to store application name")
keyDelCmd.MarkFlagRequired("appName")
keyDelCmd.Flags().StringVarP(&keyID, "keyID", "k", "", "The keyID to delete")
keyDelCmd.MarkFlagRequired("keyID")
}
func createAPIKey(cmd *cobra.Command, args []string) {
cfg := getConfig()
name = appName
appID, err := getApplicationByName(args)
if err != nil {
utils.PrettyPrintErr("Application %v not found ", appName)
return
}
apikey := apimgr.ApiKey{}
apikey.ApplicationId = appID
if keyID != "" {
apikey.Id = keyID
}
if secret != "" {
apikey.Secret = secret
}
apikeyPost := &apimgr.ApplicationsIdApikeysPostOpts{}
apikeyPost.ApiKey = optional.NewInterface(apikey)
client := &apimgr.APIClient{}
client = apimgr.NewAPIClient(cfg)
apikey, _, err = client.ApplicationsApi.ApplicationsIdApikeysPost(context.Background(), appID, apikeyPost)
if err != nil {
utils.PrettyPrintErr("Error creating apikey: %v ", err)
return
}
utils.PrettyPrintInfo("APIKey %v and Secret %v", apikey.Id, apikey.Secret)
return
}
func listAPIKeys(cmd *cobra.Command, args []string) {
cfg := getConfig()
client := &apimgr.APIClient{}
client = apimgr.NewAPIClient(cfg)
stdout := fmtDisplay()
appID, err := getApplicationByName(args)
if err != nil {
utils.PrettyPrintErr("application %v not found ", appName)
return
}
keys, _, err := client.ApplicationsApi.ApplicationsIdApikeysGet(context.Background(), appID)
if err != nil {
utils.PrettyPrintErr("Error listing the apiKeys: %v", err)
return
}
if len(keys) != 0 {
fmt.Fprintf(stdout, "APIKEY\tSECRET\n")
for _, key := range keys {
fmt.Fprintf(stdout, "%v\t%v\n", key.Id, key.Secret)
}
fmt.Fprint(stdout)
stdout.Flush()
} else {
utils.PrettyPrintInfo("No apikeys found in the application %v", appName)
return
}
}
func deleteAPIKey(cmd *cobra.Command, args []string) {
cfg := getConfig()
appID, err := getApplicationByName(args)
if err != nil {
utils.PrettyPrintErr("application %v not found ", appName)
return
}
client := &apimgr.APIClient{}
client = apimgr.NewAPIClient(cfg)
_, err = client.ApplicationsApi.ApplicationsIdApikeysKeyIdDelete(context.Background(), appID, keyID)
if err != nil {
utils.PrettyPrintErr("Unable to delete the apikey: %v", err)
return
}
utils.PrettyPrintInfo("APIkey %v deleted from the applocation %v ....", keyID, appName)
return
}