-
Notifications
You must be signed in to change notification settings - Fork 0
/
apitokens.go
227 lines (203 loc) · 5.36 KB
/
apitokens.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package apitokens
import (
"fmt"
"strings"
"time"
"github.com/urfave/cli/v2"
edgecloud "github.com/Edge-Center/edgecentercloud-go"
"github.com/Edge-Center/edgecentercloud-go/client/apitokens/v1/client"
"github.com/Edge-Center/edgecentercloud-go/client/flags"
"github.com/Edge-Center/edgecentercloud-go/client/utils"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/apitoken/v1/apitokens"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/apitoken/v1/types"
)
var apiTokenIDText = "apitoken_id is mandatory argument"
var Commands = cli.Command{
Name: "apitokens",
Usage: "EdgeCloud api token API. Could be used only with platform client type",
Subcommands: []*cli.Command{
&apiTokenListCommand,
&apiTokenGetCommand,
&apiTokenDeleteCommand,
&apiTokenCreateCommand,
},
}
var apiTokenListCommand = cli.Command{
Name: "list",
Usage: "List api tokens",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "client-id",
Aliases: []string{"c"},
Usage: "Client id",
Required: true,
},
&cli.IntFlag{
Name: "role-id",
Usage: fmt.Sprintf("Available value: %s", strings.Join(types.RoleIDType(0).StringList(), ", ")),
},
&cli.IntFlag{
Name: "issued-by",
Usage: "User's ID. Use to get API tokens issued by a particular user.",
},
&cli.IntFlag{
Name: "not-issued-by",
Usage: "User's ID. Use to get API tokens issued by anyone except a particular user.",
},
// todo: figure out
// &cli.BoolFlag{
// Name: "deleted",
// Usage: "If set, in the response wil be included only deleted tokens",
// },
},
Action: func(c *cli.Context) error {
client, err := client.NewAPITokenClient(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
opts := apitokens.ListOpts{}
if c.Int("role-id") != 0 {
opts.RoleID = types.RoleIDType(c.Int("role-id"))
}
if c.Int("issued-by") != 0 {
opts.IssuedBy = c.Int("issued-by")
}
if c.Int("not-issued-by") != 0 {
opts.NotIssuedBy = c.Int("not-issued-by")
}
results, err := apitokens.List(client, c.Int("client-id"), opts).Extract()
if err != nil {
return cli.Exit(err, 1)
}
utils.ShowResults(results, c.String("format"))
return nil
},
}
var apiTokenGetCommand = cli.Command{
Name: "show",
Usage: "Show api token",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "client-id",
Aliases: []string{"c"},
Usage: "Client id",
Required: true,
},
},
ArgsUsage: "<apitoken_id>",
Action: func(c *cli.Context) error {
apiTokenID, err := flags.GetFirstIntArg(c, apiTokenIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "show")
return err
}
client, err := client.NewAPITokenClient(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
result, err := apitokens.Get(client, c.Int("client-id"), apiTokenID).Extract()
if err != nil {
return cli.Exit(err, 1)
}
utils.ShowResults(result, c.String("format"))
return nil
},
}
var apiTokenDeleteCommand = cli.Command{
Name: "delete",
Usage: "Delete api token",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "client-id",
Aliases: []string{"c"},
Usage: "Client id",
Required: true,
},
},
ArgsUsage: "<apitoken_id>",
Action: func(c *cli.Context) error {
apiTokenID, err := flags.GetFirstIntArg(c, apiTokenIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "show")
return err
}
client, err := client.NewAPITokenClient(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
err = apitokens.Delete(client, c.Int("client-id"), apiTokenID).ExtractErr()
if err != nil {
return cli.Exit(err, 1)
}
return nil
},
}
var apiTokenCreateCommand = cli.Command{
Name: "create",
Usage: "Create api token",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "client-id",
Aliases: []string{"c"},
Usage: "Client id",
Required: true,
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Required: true,
},
&cli.StringFlag{
Name: "description",
},
&cli.IntFlag{
Name: "role-id",
Usage: fmt.Sprintf("Available value: %s", strings.Join(types.RoleIDType(0).StringList(), ", ")),
Required: true,
},
&cli.StringFlag{
Name: "role-name",
Usage: fmt.Sprintf("Available value: %s", strings.Join(types.RoleNameType("").StringList(), ", ")),
Required: true,
},
&cli.StringFlag{
Name: "expiration-time",
Usage: "Date when the API token becomes expired (ISO 8086/RFC 3339 format), UTC. If null, then the API token will never expire.",
},
},
Action: func(c *cli.Context) error {
client, err := client.NewAPITokenClient(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
opts := apitokens.CreateOpts{
Name: c.String("name"),
Description: c.String("description"),
ClientUser: apitokens.CreateClientUser{
Role: apitokens.ClientRole{
ID: types.RoleIDType(c.Int("role-id")),
Name: types.RoleNameType(c.String("role-name")),
},
},
}
expDateRaw := c.String("expiration-date")
if expDateRaw != "" {
expDate, err := time.Parse(edgecloud.RFC3339ZZ, expDateRaw)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
opts.ExpDate = &edgecloud.JSONRFC3339Z{Time: expDate}
}
result, err := apitokens.Create(client, c.Int("client-id"), opts).Extract()
if err != nil {
return cli.Exit(err, 1)
}
utils.ShowResults(result, c.String("format"))
return nil
},
}