-
Notifications
You must be signed in to change notification settings - Fork 20
/
virtual.go
303 lines (277 loc) · 9.79 KB
/
virtual.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
// Copyright 2016 e-Xpert Solutions SA. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ltm
import (
"encoding/json"
"net/http"
"net/url"
"github.com/e-XpertSolutions/f5-rest-client/f5"
)
type Persistence struct {
Name string `json:"name,omitempty"`
Partition string `json:"partition,omitempty"`
TMDefault string `json:"tmDefault,omitempty"`
}
// VirtualServerList holds a list of virtual server uration.
type VirtualServerList struct {
Items []VirtualServer `json:"items,omitempty"`
Kind string `json:"kind,omitempty" pretty:",expanded"`
SelfLink string `json:"selfLink,omitempty" pretty:",expanded"`
}
// VirtualServer holds the uration of a single virtual server.
type VirtualServer struct {
AddressStatus string `json:"addressStatus,omitempty"`
AutoLasthop string `json:"autoLasthop,omitempty"`
CmpEnabled string `json:"cmpEnabled,omitempty"`
ConnectionLimit int64 `json:"connectionLimit,omitempty"`
Description string `json:"description,omitempty"`
Destination string `json:"destination,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Enabled bool `json:"enabled,omitempty"`
FallbackPersistence string `json:"fallbackPersistence,omitempty"`
FullPath string `json:"fullPath,omitempty" pretty:",expanded"`
FwEnforcedPolicy string `json:"fwEnforcedPolicy,omitempty"`
Generation int64 `json:"generation,omitempty" pretty:",expanded"`
GtmScore int64 `json:"gtmScore,omitempty" pretty:",expanded"`
IPProtocol string `json:"ipProtocol,omitempty"`
Kind string `json:"kind,omitempty" pretty:",expanded"`
Mask string `json:"mask,omitempty"`
Mirror string `json:"mirror,omitempty"`
MobileAppTunnel string `json:"mobileAppTunnel,omitempty" pretty:",expanded"`
Name string `json:"name,omitempty"`
Nat64 string `json:"nat64,omitempty" pretty:",expanded"`
Partition string `json:"partition,omitempty"`
Persistences []Persistence `json:"persist,omitempty"`
PoliciesReference struct {
IsSubcollection bool `json:"isSubcollection,omitempty"`
Link string `json:"link,omitempty"`
} `json:"policiesReference,omitempty"`
Pool string `json:"pool,omitempty"`
Profiles []string `json:"profiles,omitempty"` // only used to link existing profiles a creation or update
ProfilesReference struct {
IsSubcollection bool `json:"isSubcollection,omitempty"`
Link string `json:"link,omitempty"`
Profiles []Profile `json:"items,omitempty"`
} `json:"profilesReference,omitempty"`
RateLimit string `json:"rateLimit,omitempty" pretty:",expanded"`
RateLimitDstMask int64 `json:"rateLimitDstMask,omitempty" pretty:",expanded"`
RateLimitMode string `json:"rateLimitMode,omitempty" pretty:",expanded"`
RateLimitSrcMask int64 `json:"rateLimitSrcMask,omitempty" pretty:",expanded"`
Rules []string `json:"rules,omitempty"`
SelfLink string `json:"selfLink,omitempty" pretty:",expanded"`
SecurityLogProfiles []string `json:"securityLogProfiles,omitempty" pretty:",expanded"`
Source string `json:"source,omitempty"`
SourceAddressTranslation SourceAddressTranslation `json:"sourceAddressTranslation,omitempty"`
SourcePort string `json:"sourcePort,omitempty"`
SynCookieStatus string `json:"synCookieStatus,omitempty"`
TranslateAddress string `json:"translateAddress,omitempty"`
TranslatePort string `json:"translatePort,omitempty"`
Vlans []string `json:"vlans,omitempty"`
VlansDisabled bool `json:"vlansDisabled,omitempty"`
VlansEnabled bool `json:"vlansEnabled,omitempty"`
VsIndex int64 `json:"vsIndex,omitempty" pretty:",expanded"`
}
type SourceAddressTranslation struct {
Type string `json:"type,omitempty"`
Pool string `json:"pool,omitempty"`
}
type Profile struct {
Name string `json:"name,omitempty"`
Context string `json:"context,omitempty"`
}
// VirtualEndpoint represents the REST resource for managing virtual server.
const VirtualEndpoint = "/virtual"
// VirtualResponse provide a simple mechanism to read paginated results.
//
// TODO(gilliek): use VirtualResponse object where pagination is needed.
type VirtualResponse struct {
}
// VirtualResource provides an API to manage virtual server urations.
type VirtualResource struct {
c *f5.Client
}
// ListAll lists all the virtual server urations.
func (vr *VirtualResource) ListAll() (*VirtualServerList, error) {
resp, err := vr.doRequest("GET", "", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return nil, err
}
var vsc VirtualServerList
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&vsc); err != nil {
return nil, err
}
return &vsc, nil
}
// ListAllWithParams lists all the virtual server urations.
func (vr *VirtualResource) ListAllWithParams(v url.Values) (*VirtualServerList, error) {
params := v.Encode()
resp, err := vr.doRequest("GET", "?"+params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return nil, err
}
var vsc VirtualServerList
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&vsc); err != nil {
return nil, err
}
return &vsc, nil
}
// Get a single virtual server uration identified by id.
func (vr *VirtualResource) Get(id string) (*VirtualServer, error) {
resp, err := vr.doRequest("GET", id, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return nil, err
}
var vsci VirtualServer
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&vsci); err != nil {
return nil, err
}
return &vsci, nil
}
// Create a new virtual server uration.
func (vr *VirtualResource) Create(item VirtualServer) error {
if err := vr.c.ModQuery("POST", BasePath+VirtualEndpoint, item); err != nil {
return err
}
return nil
}
// Edit a virtual server uration identified by id.
func (vr *VirtualResource) Edit(id string, item VirtualServer) error {
resp, err := vr.doRequest("PUT", id, item)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// Enabling a virtual server item identified by id.
func (vr *VirtualResource) Enable(id string) error {
item := VirtualServer{Enabled: true}
resp, err := vr.doRequest("PATCH", id, item)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// Disabling a virtual server item identified by id.
func (vr *VirtualResource) Disable(id string) error {
item := VirtualServer{Disabled: true}
resp, err := vr.doRequest("PATCH", id, item)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// Delete a single server uration identified by id.
func (vr *VirtualResource) Delete(id string) error {
resp, err := vr.doRequest("DELETE", id, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// Rules gets the iRules uration for a virtual server identified by id.
func (vr *VirtualResource) Rules(id string) ([]Rule, error) {
resp, err := vr.doRequest("GET", id+"/rule", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return nil, err
}
var rules []Rule
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&rules); err != nil {
return nil, err
}
return rules, nil
}
// AddRule adds an iRule to the virtual server identified by id.
func (vr *VirtualResource) AddRule(id string, rule Rule) error {
resp, err := vr.doRequest("POST", id+"/rule", rule)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// RemoveRule removes a single iRule from the virtual server identified by id.
func (vr *VirtualResource) RemoveRule(vsID, ruleID string) error {
resp, err := vr.doRequest("DELETE", vsID+"/rule/"+ruleID, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if err := vr.readError(resp); err != nil {
return err
}
return nil
}
// doRequest creates and send HTTP request using the F5 client.
//
// TODO(gilliek): decorate errors
func (vr *VirtualResource) doRequest(method, id string, data interface{}) (*http.Response, error) {
req, err := vr.c.MakeRequest(method, BasePath+VirtualEndpoint+"/"+id, data)
if err != nil {
return nil, err
}
resp, err := vr.c.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
// readError reads request error object from a HTTP response.
//
// TODO(gilliek): move this function into F5 package.
func (vr *VirtualResource) readError(resp *http.Response) error {
if resp.StatusCode >= 400 {
errResp, err := f5.NewRequestError(resp.Body)
if err != nil {
return err
}
return errResp
}
return nil
}
func (vr *VirtualResource) ShowStats(id string) (*VirtualStatsList, error) {
var item VirtualStatsList
if err := vr.c.ReadQuery(BasePath+VirtualEndpoint+"/"+id+"/stats", &item); err != nil {
return nil, err
}
return &item, nil
}