-
Notifications
You must be signed in to change notification settings - Fork 0
/
l7rules.go
304 lines (280 loc) · 8.29 KB
/
l7rules.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package l7rules
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/Edge-Center/edgecentercloud-go/client/flags"
"github.com/Edge-Center/edgecentercloud-go/client/l7policies/v1/client"
"github.com/Edge-Center/edgecentercloud-go/client/utils"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/loadbalancer/v1/l7policies"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/task/v1/tasks"
)
var ruleIDText = "rule_id is mandatory argument"
var L7RuleCommands = cli.Command{
Name: "l7rule",
Usage: "EdgeCloud l7rule API",
Subcommands: []*cli.Command{
&l7RuleListSubCommand,
&l7RuleGetSubCommand,
&l7RuleReplaceSubCommand,
&l7RuleDeleteSubCommand,
&l7RuleCreateSubCommand,
},
}
var l7RuleListSubCommand = cli.Command{
Name: "list",
Usage: "List l7rules",
Category: "l7rule",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "policy-id",
Aliases: []string{"p"},
Usage: "Policy id",
Required: true,
},
},
Action: func(c *cli.Context) error {
policyID := c.String("policy-id")
client, err := client.NewL7RulesClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
results, err := l7policies.ListAllRule(client, policyID)
if err != nil {
return cli.Exit(err, 1)
}
if results == nil {
return cli.Exit(err, 1)
}
utils.ShowResults(results, c.String("format"))
return nil
},
}
var l7RuleGetSubCommand = cli.Command{
Name: "show",
Usage: "Show l7rules",
ArgsUsage: "<l7rule_id>",
Category: "l7rule",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "policy-id",
Aliases: []string{"p"},
Usage: "Policy id",
Required: true,
},
},
Action: func(c *cli.Context) error {
ruleID, err := flags.GetFirstStringArg(c, ruleIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "show")
return err
}
policyID := c.String("policy-id")
client, err := client.NewL7RulesClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
result, err := l7policies.GetRule(client, policyID, ruleID).Extract()
if err != nil {
return cli.Exit(err, 1)
}
if result != nil {
utils.ShowResults(result, c.String("format"))
}
return nil
},
}
var l7RuleDeleteSubCommand = cli.Command{
Name: "delete",
Usage: "Delete l7rules",
ArgsUsage: "<l7rule_id>",
Category: "l7rule",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "policy-id",
Aliases: []string{"p"},
Usage: "Policy id",
Required: true,
},
},
Action: func(c *cli.Context) error {
ruleID, err := flags.GetFirstStringArg(c, ruleIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "delete")
return err
}
policyID := c.String("policy-id")
client, err := client.NewL7RulesClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
result, err := l7policies.DeleteRule(client, policyID, ruleID).Extract()
if err != nil {
return cli.Exit(err, 1)
}
if result != nil {
utils.ShowResults(result, c.String("format"))
}
return nil
},
}
var l7RuleCreateSubCommand = cli.Command{
Name: "create",
Usage: "Create l7rules",
Category: "l7rule",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "policy-id",
Aliases: []string{"p"},
Usage: "Policy id",
Required: true,
},
&cli.StringFlag{
Name: "compare-type",
Usage: `The comparison type for the L7 rule. Available value is "CONTAINS" "ENDS_WITH" "EQUAL_TO" "REGEX" "STARTS_WITH"`,
Required: true,
},
&cli.BoolFlag{
Name: "invert",
Usage: `When true the logic of the rule is inverted. For example, with invert true, 'equal to' would become 'not equal to'. Default is false.`,
},
&cli.StringFlag{
Name: "key",
Usage: "The key to use for the comparison. For example, the name of the cookie to evaluate.",
},
&cli.StringFlag{
Name: "type",
Usage: `The L7 rule type. Available value is "COOKIE" "FILE_TYPE" "HEADER" "HOST_NAME" "PATH" "SSL_CONN_HAS_CERT" "SSL_VERIFY_RESULT" "SSL_DN_FIELD"`,
Required: true,
},
&cli.StringFlag{
Name: "value",
Usage: "The value to use for the comparison. For example, the file type to compare",
Required: true,
},
&cli.StringSliceFlag{
Name: "tags",
Usage: "A list of simple strings assigned to the resource.",
},
}, flags.WaitCommandFlags...),
Action: func(c *cli.Context) error {
policyID := c.String("policy-id")
client, err := client.NewL7RulesClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
opts := l7policies.CreateRuleOpts{
CompareType: l7policies.CompareType(c.String("compare-type")),
Invert: c.Bool("invert"),
Key: c.String("key"),
Type: l7policies.RuleType(c.String("type")),
Value: c.String("value"),
Tags: c.StringSlice("tags"),
}
results, err := l7policies.CreateRule(client, policyID, opts).Extract()
if err != nil {
return cli.Exit(err, 1)
}
return utils.WaitTaskAndShowResult(c, client, results, true, func(task tasks.TaskID) (interface{}, error) {
taskInfo, err := tasks.Get(client, string(task)).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get task with ID: %s. Error: %w", task, err)
}
ruleID, err := l7policies.ExtractRuleIDFromTask(taskInfo)
if err != nil {
return nil, fmt.Errorf("cannot retrieve rule ID from task info: %w", err)
}
rule, err := l7policies.GetRule(client, policyID, ruleID).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get rule with ID: %s. Error: %w", ruleID, err)
}
utils.ShowResults(rule, c.String("format"))
return nil, nil
})
},
}
var l7RuleReplaceSubCommand = cli.Command{
Name: "replace",
Usage: "Replace l7rules",
ArgsUsage: "<l7rule_id>",
Category: "l7rule",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "policy-id",
Aliases: []string{"p"},
Usage: "Policy id",
Required: true,
},
&cli.StringFlag{
Name: "compare-type",
Usage: `The comparison type for the L7 rule. Available value is "CONTAINS" "ENDS_WITH" "EQUAL_TO" "REGEX" "STARTS_WITH"`,
Required: true,
},
&cli.BoolFlag{
Name: "invert",
Usage: `When true the logic of the rule is inverted. For example, with invert true, 'equal to' would become 'not equal to'. Default is false.`,
},
&cli.StringFlag{
Name: "key",
Usage: "The key to use for the comparison. For example, the name of the cookie to evaluate.",
},
&cli.StringFlag{
Name: "type",
Usage: `The L7 rule type. Available value is "COOKIE" "FILE_TYPE" "HEADER" "HOST_NAME" "PATH" "SSL_CONN_HAS_CERT" "SSL_VERIFY_RESULT" "SSL_DN_FIELD"`,
Required: true,
},
&cli.StringFlag{
Name: "value",
Usage: "The value to use for the comparison. For example, the file type to compare",
Required: true,
},
&cli.StringSliceFlag{
Name: "tags",
Usage: "A list of simple strings assigned to the resource.",
},
}, flags.WaitCommandFlags...),
Action: func(c *cli.Context) error {
ruleID, err := flags.GetFirstStringArg(c, ruleIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "delete")
return err
}
policyID := c.String("policy-id")
client, err := client.NewL7RulesClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.Exit(err, 1)
}
opts := l7policies.CreateRuleOpts{
CompareType: l7policies.CompareType(c.String("compare-type")),
Invert: c.Bool("invert"),
Key: c.String("key"),
Type: l7policies.RuleType(c.String("type")),
Value: c.String("value"),
Tags: c.StringSlice("tags"),
}
results, err := l7policies.ReplaceRule(client, policyID, ruleID, opts).Extract()
if err != nil {
return cli.Exit(err, 1)
}
return utils.WaitTaskAndShowResult(c, client, results, true, func(task tasks.TaskID) (interface{}, error) {
taskInfo, err := tasks.Get(client, string(task)).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get task with ID: %s. Error: %w", task, err)
}
ruleID, err := l7policies.ExtractRuleIDFromTask(taskInfo)
if err != nil {
return nil, fmt.Errorf("cannot retrieve rule ID from task info: %w", err)
}
rule, err := l7policies.GetRule(client, policyID, ruleID).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get rule with ID: %s. Error: %w", ruleID, err)
}
utils.ShowResults(rule, c.String("format"))
return nil, nil
})
},
}