-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_types.go
351 lines (314 loc) · 12 KB
/
common_types.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2022 API7.ai, Inc
//
// 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 cloud
import (
"encoding/json"
"errors"
"strconv"
"github.com/sony/sonyflake"
)
// ID is the type of the id field used for any entities
type ID uint64
// String indicates how to convert ID to a string.
func (id ID) String() string {
return strconv.FormatUint(uint64(id), 10)
}
// MarshalJSON is the way to encode ID to JSON string.
func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(strconv.FormatUint(uint64(id), 10))
}
// UnmarshalJSON is the way to decode ID from JSON string.
func (id *ID) UnmarshalJSON(data []byte) error {
var value interface{}
if err := json.Unmarshal(data, &value); err != nil {
return err
}
switch v := value.(type) {
case string:
u, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return err
}
*id = ID(u)
default:
panic("unknown type")
}
return nil
}
// IDGenerator is an interface for generating IDs.
type IDGenerator interface {
// NextID generates an ID.
NextID() ID
}
type snowflake sonyflake.Sonyflake
func (s *snowflake) NextID() ID {
uid, err := (*sonyflake.Sonyflake)(s).NextID()
if err != nil {
panic("get sony flake uid failed:" + err.Error())
}
return ID(uid)
}
// NewIDGenerator returns an IDGenerator object.
func NewIDGenerator() (IDGenerator, error) {
ips, err := getLocalIPs()
if err != nil {
panic(err)
}
sf := (*snowflake)(sonyflake.NewSonyflake(sonyflake.Settings{
MachineID: func() (u uint16, e error) {
return sumIPs(ips), nil
},
}))
if sf == nil {
return nil, errors.New("failed to new snoyflake object")
}
return sf, nil
}
const (
// Any means any status
Any = EntityStatus(-1)
// Uninitialized represents the entity has been saved to the db, but the associated resource has not yet been ready
Uninitialized = EntityStatus(0)
// Normal indicates that the entity and associated resources are ready
Normal = EntityStatus(50)
// Deleted indicates the entity has been deleted
Deleted = EntityStatus(100)
)
const (
// APITypeRest indicates this is a Rest API.
APITypeRest = "Rest"
// APITypeWebSocket indicates this is a Websocket API.
APITypeWebSocket = "WebSocket"
// CanaryReleaseTypePercent means using percent to do canary release
CanaryReleaseTypePercent = "percent"
// CanaryReleaseTypeRule means using rule match to do canary release
CanaryReleaseTypeRule = "rule"
)
const (
// PathPrefixMatch means the requests' URL path leads with the API path will match this API;
PathPrefixMatch = "Prefix"
// PathExactMatch means the requests' URL path has to be same to the API path.
PathExactMatch = "Exact"
)
const (
// ProtocolHTTP indicates the HTTP protocol.
ProtocolHTTP = "HTTP"
// ProtocolHTTPS indicates the HTTPS protocol.
ProtocolHTTPS = "HTTPS"
)
const (
// ActiveStatus indicates an object is active, and this object
// will be seen by gateway instances.
ActiveStatus = iota
// InactiveStatus indicates an object is inactive, and this object
// won't be seen by gateway instances.
InactiveStatus
)
const (
// CanaryReleaseStatePaused indicates the pause state of CanaryRelease.
CanaryReleaseStatePaused = "paused"
// CanaryReleaseStateInProgress indicates the in_progress state of CanaryRelease.
CanaryReleaseStateInProgress = "in_progress"
// CanaryReleaseStateFinished indicates the finish state of CanaryRelease.
CanaryReleaseStateFinished = "finished"
)
// EntityStatus is common status definition for any kind of entity:
// * Uninitialized represents the entity has been saved to the db, but the associated resource has not yet been ready.
// * Normal indicates that the entity and associated resources are ready.
// * Deleted indicates the entity has been deleted.
type EntityStatus int
type ResourceCommonOpts interface {
GetCluster() *Cluster
}
// ResourceCreateOptions contains some options for creating an API7 Cloud resource.
type ResourceCreateOptions struct {
// Organization indicates where the resources are.
// This field should be specified when users want to create resources.
// in the organization. e.g., when inviting a member, the
// Organization.ID should be specified.
Organization *Organization
// Cluster indicates where the resource belongs.
// This field should be specified when users want to create resources
// in the cluster. e.g., when creating Application, the
// Cluster.ID should be specified.
Cluster *Cluster
// Application indicates which Application should this resource belong.
// This field should be specified when users want to update sub-resources
// in the Application. e.g., when creating API, CanaryRelease, the
// Application.ID should be specified.
Application *Application
}
func (r *ResourceCreateOptions) GetCluster() *Cluster {
if r == nil {
return nil
}
return r.Cluster
}
// ResourceUpdateOptions contains some options for updating an API7 Cloud resource.
type ResourceUpdateOptions struct {
// Organization indicates where the resources are.
// This field should be specified when users want to update resources.
// in the organization. e.g., when re-inviting a member, the
// Organization.ID should be specified.
Organization *Organization
// Cluster indicates where the resource belongs.
// This field should be specified when users want to update resources
// in the cluster. e.g., when updating Application, the
// Cluster.ID should be specified.
Cluster *Cluster
// Application indicates which Application should this resource belong.
// This field should be specified when users want to update sub-resources
// in the Application. e.g., when updating API, the
// Application.ID should be specified.
Application *Application
}
func (r *ResourceUpdateOptions) GetCluster() *Cluster {
if r == nil {
return nil
}
return r.Cluster
}
// ResourceDeleteOptions contains some options for deleting an API7 Cloud resource.
type ResourceDeleteOptions struct {
// Organization indicates where the resources are.
// This field should be specified when users want to update resources.
// in the organization. e.g., when deleting a member, the
// Organization.ID should be specified.
Organization *Organization
// Cluster indicates where the resource is.
// This field should be specified when users want to delete resources
// in the cluster. e.g., when deleting Application, the
// Cluster.ID should be specified.
Cluster *Cluster
// Application indicates which Application should this resource belong.
// This field should be specified when users want to delete sub-resources
// in the Application. e.g., when deleting API, the
// Application.ID should be specified.
Application *Application
}
func (r *ResourceDeleteOptions) GetCluster() *Cluster {
if r == nil {
return nil
}
return r.Cluster
}
// ResourceGetOptions contains some options for getting an API7 Cloud resource.
type ResourceGetOptions struct {
// Organization indicates where the resources are.
// This field should be specified when users want to list resources.
// in the organization. e.g., when getting a member, the
// Organization.ID should be specified.
Organization *Organization
// Cluster indicates where the resource is.
// This field should be specified when users want to get a resource.
// in the cluster. e.g., when getting Application, the
// Cluster.ID should be specified.
Cluster *Cluster
// Application indicates which Application should this resource belong.
// This field should be specified when users want to fetch sub-resources
// in the Application. e.g., when fetching API, the
// Application.ID should be specified.
Application *Application
}
func (r *ResourceGetOptions) GetCluster() *Cluster {
if r == nil {
return nil
}
return r.Cluster
}
// ResourceListOptions contains some options for listing the same kind of API7 Cloud resources.
type ResourceListOptions struct {
// Organization indicates where the resources are.
// This field should be specified when users want to list resources.
// in the organization. e.g., when iterating Cluster, the
// Organization.ID should be specified.
Organization *Organization
// Cluster indicates where the resources are.
// This field should be specified when users want to list resources.
// in the cluster. e.g., when iterating Application, the
// Cluster.ID should be specified.
Cluster *Cluster
// Application indicates which Application should this resource belong.
// This field should be specified when users want to list sub-resources
// in the Application. e.g., when listing API, the
// Application.ID should be specified.
Application *Application
// Pagination indicates the start page and the page size for listing resources.
Pagination *Pagination
// Filter indicates conditions to filter out resources.
Filter *Filter
}
func (r *ResourceListOptions) GetCluster() *Cluster {
if r == nil {
return nil
}
return r.Cluster
}
// ExpressionLogicalRelationship is the logical relationship between expressions.
type ExpressionLogicalRelationship string
const (
// MatchAll meaning all the expressions should be matched.
MatchAll ExpressionLogicalRelationship = "All"
// MatchAny meaning any of the expressions should be matched.
MatchAny ExpressionLogicalRelationship = "Any"
)
// ExpressionSubject is the subject category of the expression.
type ExpressionSubject string
const (
// HeaderSubject indicates the expression subject is from a HTTP request header.
HeaderSubject ExpressionSubject = "header"
// QuerySubject indicates the expression subject is from the HTTP query string.
QuerySubject ExpressionSubject = "query"
// CookieSubject indicates the expression subject is from Cookie header.
CookieSubject ExpressionSubject = "cookie"
// PathSubject indicates the expression subject is from the URI path.
PathSubject ExpressionSubject = "path"
// VariableSubject indicates the expression subject is a Nginx or APISIX variable.
VariableSubject ExpressionSubject = "variable"
)
// ExpressionOperator is the operator of the expression.
type ExpressionOperator string
const (
// EqualOperator indicates the expression operator is "equal"
EqualOperator ExpressionOperator = "equal"
// NotEqualOperator indicates the expression operator is "not_equal"
NotEqualOperator ExpressionOperator = "not_equal"
// RegexMatchOperator indicates the expression operator is "regex_match"
RegexMatchOperator ExpressionOperator = "regex_match"
// RegexNotMatchOperator indicates the expression operator is "regex_not_match"
RegexNotMatchOperator ExpressionOperator = "regex_not_match"
// PresentOperator indicates the expression operator is "present"
PresentOperator ExpressionOperator = "present"
// NotPresentOperator indicates the expression operator is "not_present"
NotPresentOperator ExpressionOperator = "not_present"
// LargerEqualOperator indicates the expression operator is "larger_equal"
LargerEqualOperator ExpressionOperator = "larger_equal"
// LargerThanOperator indicates the expression operator is "larger_than"
LargerThanOperator ExpressionOperator = "larger_than"
// LessEqualOperator indicates the expression operator is "less_equal"
LessEqualOperator ExpressionOperator = "less_equal"
// LessThanOperator indicates the expression operator is "less_than"
LessThanOperator ExpressionOperator = "less_than"
)
// Expression is the route match expressions.
type Expression struct {
// Subject is the subject category of the expression.
Subject ExpressionSubject `json:"subject,omitempty"`
// Name is the subject name of the expression.
Name string `json:"name,omitempty"`
// Operator is the operator of the expression.
Operator ExpressionOperator `json:"operator,omitempty"`
// Value is the value that the expression should be matched.
Value string `json:"value,omitempty"`
}