-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl7rules.go
234 lines (187 loc) · 6.98 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
package edgecloud
import (
"context"
"fmt"
"net/http"
)
const (
l7rulesPath = "rules"
)
// L7RulesService is an interface for creating and managing L7Rules with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/l7rules
type L7RulesService interface {
List(context.Context, string) ([]L7Rule, *Response, error)
Create(context.Context, string, *L7RuleCreateRequest) (*TaskResponse, *Response, error)
Delete(context.Context, string, string) (*TaskResponse, *Response, error)
Get(context.Context, string, string) (*L7Rule, *Response, error)
Update(context.Context, string, string, *L7RuleUpdateRequest) (*TaskResponse, *Response, error)
}
// L7RulesServiceOp handles communication with L7Rules methods of the EdgecenterCloud API.
type L7RulesServiceOp struct {
client *Client
}
var _ L7RulesService = &L7RulesServiceOp{}
type L7Rule struct {
RegionID int `json:"region_id"`
ProjectID int `json:"project_id"`
TaskID string `json:"task_id"`
ID string `json:"id"`
Region string `json:"region"`
ProvisioningStatus string `json:"provisioning_status"`
OperatingStatus string `json:"operating_status"`
Tags []string `json:"tags"`
Value string `json:"value"`
Key string `json:"key"`
Invert bool `json:"invert"`
Type L7RuleType `json:"type"`
CompareType L7RuleCompareType `json:"compare_type"`
}
type L7RuleType string
const (
L7RuleTypeCookie L7RuleType = "COOKIE"
L7RuleTypeFyleType L7RuleType = "FILE_TYPE"
L7RuleTypeHeader L7RuleType = "HEADER"
L7RuleTypeHostName L7RuleType = "HOST_NAME"
L7RuleTypePath L7RuleType = "PATH"
L7RuleTypeSSLConnHasCert L7RuleType = "SSL_CONN_HAS_CERT"
L7RuleTypeSSLVerifyResult L7RuleType = "SSL_VERIFY_RESULT"
L7RuleTypeSSLDNField L7RuleType = "SSL_DN_FIELD"
)
type L7RuleCompareType string
const (
L7RuleCompareTypeContains L7RuleCompareType = "CONTAINS"
L7RuleCompareTypeEndsWith L7RuleCompareType = "ENDS_WITH"
L7RuleCompareTypeEqualTo L7RuleCompareType = "EQUAL_TO"
L7RuleCompareTypeRegex L7RuleCompareType = "REGEX"
L7RuleCompareTypeStartsWith L7RuleCompareType = "STARTS_WITH"
)
type L7RuleUpdateRequest struct {
Tags *[]string `json:"tags,omitempty"`
CompareType *L7RuleCompareType `json:"compare_type,omitempty"`
Value *string `json:"value,omitempty"`
Key *string `json:"key,omitempty"`
Type *L7RuleType `json:"type,omitempty"`
Invert *bool `json:"invert,omitempty"`
}
type L7RuleCreateRequest struct {
Tags []string `json:"tags,omitempty"`
CompareType L7RuleCompareType `json:"compare_type" required:"true" validate:"required"`
Value string `json:"value" required:"true" validate:"required"`
Key string `json:"key,omitempty"`
Type L7RuleType `json:"type" required:"true" validate:"required"`
Invert bool `json:"invert,omitempty"`
}
// l7rulesRoot represents a L7Rule root.
type l7rulesRoot struct {
Count int
L7Rules []L7Rule `json:"results"`
}
// List get L7Rules.
func (s *L7RulesServiceOp) List(ctx context.Context, l7PolicyID string) ([]L7Rule, *Response, error) {
if resp, err := isValidUUID(l7PolicyID, "l7PolicyID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s", s.client.addProjectRegionPath(l7policiesBasePathV1), l7PolicyID, l7rulesPath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(l7rulesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.L7Rules, resp, err
}
// Create a L7Rule.
func (s *L7RulesServiceOp) Create(ctx context.Context, l7PolicyID string, reqBody *L7RuleCreateRequest) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(l7PolicyID, "l7PolicyID"); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s", s.client.addProjectRegionPath(l7policiesBasePathV1), l7PolicyID, l7rulesPath)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
// Delete a L7Rule.
func (s *L7RulesServiceOp) Delete(ctx context.Context, l7PolicyID string, l7RuleID string) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(l7PolicyID, "l7PolicyID"); err != nil {
return nil, resp, err
}
if resp, err := isValidUUID(l7RuleID, "l7RuleID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s/%s", s.client.addProjectRegionPath(l7policiesBasePathV1), l7PolicyID, l7rulesPath, l7RuleID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
// Get a L7Rule.
func (s *L7RulesServiceOp) Get(ctx context.Context, l7PolicyID string, l7RuleID string) (*L7Rule, *Response, error) {
if resp, err := isValidUUID(l7PolicyID, "l7PolicyID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s/%s", s.client.addProjectRegionPath(l7policiesBasePathV1), l7PolicyID, l7rulesPath, l7RuleID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
l7Rule := new(L7Rule)
resp, err := s.client.Do(ctx, req, l7Rule)
if err != nil {
return nil, resp, err
}
return l7Rule, resp, err
}
// Update replace L7Rule properties.
func (s *L7RulesServiceOp) Update(ctx context.Context, l7PolicyID string, l7RuleID string, reqBody *L7RuleUpdateRequest) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(l7PolicyID, "l7PolicyID"); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s/%s", s.client.addProjectRegionPath(l7policiesBasePathV1), l7PolicyID, l7rulesPath, l7RuleID)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}