-
Notifications
You must be signed in to change notification settings - Fork 39
/
service.go
463 lines (419 loc) · 13.4 KB
/
service.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Neutron double testing service - internal direct API implementation
package neutronservice
import (
"net/url"
"strconv"
"strings"
"gopkg.in/goose.v3/neutron"
"gopkg.in/goose.v3/testservices"
"gopkg.in/goose.v3/testservices/identityservice"
"gopkg.in/goose.v3/testservices/neutronmodel"
)
var _ testservices.HttpService = (*Neutron)(nil)
var _ identityservice.ServiceProvider = (*Neutron)(nil)
// Neutron implements a OpenStack Neutron testing service and
// contains the service double's internal state.
type Neutron struct {
testservices.ServiceInstance
neutronModel *neutronmodel.NeutronModel
groups map[string]neutron.SecurityGroupV2
rules map[string]neutron.SecurityGroupRuleV2
floatingIPs map[string]neutron.FloatingIPV2
networks map[string]neutron.NetworkV2
subnets map[string]neutron.SubnetV2
nextGroupId int
nextRuleId int
nextPortId int
nextIPId int
}
func errorJSONEncode(err error) (int, string) {
serverError, ok := err.(*testservices.ServerError)
if !ok {
serverError = testservices.NewInternalServerError(err.Error())
}
return serverError.Code(), serverError.AsJSON()
}
// endpoint returns endpoint URL from the given path.
// openstack catalog list
// | neutron | network | RegionOne
// | | | publicURL: http://<keystone ip>:9696
// | | | internalURL: http://<keystone ip>:9696
// | | | adminURL: http://<keystone ip>:9696
func (n *Neutron) endpointURL(version bool, path string) string {
ep := n.Scheme + "://" + n.Hostname
if version {
ep += n.VersionPath + "/"
}
if path != "" {
ep += strings.TrimLeft(path, "/")
}
return ep
}
func (n *Neutron) Endpoints() []identityservice.Endpoint {
ep := identityservice.Endpoint{
AdminURL: n.endpointURL(false, ""),
InternalURL: n.endpointURL(false, ""),
PublicURL: n.endpointURL(false, ""),
Region: n.Region,
}
return []identityservice.Endpoint{ep}
}
func (n *Neutron) V3Endpoints() []identityservice.V3Endpoint {
url := n.endpointURL(false, "")
return identityservice.NewV3Endpoints(url, url, url, n.RegionID)
}
// New creates an instance of the Neutron object, given the parameters.
func New(hostURL, versionPath, tenantId, region string, identityService, fallbackIdentity identityservice.IdentityService) *Neutron {
URL, err := url.Parse(hostURL)
if err != nil {
panic(err)
}
hostname := URL.Host
if !strings.HasSuffix(hostname, "/") {
hostname += "/"
}
defaultSubnets := []neutron.SubnetV2{
{
Id: "999-01",
NetworkId: "999",
Name: "subnet-999",
Cidr: "10.9.0.0/24",
},
{
Id: "998-01",
NetworkId: "998",
Name: "subnet-998",
Cidr: "10.8.0.0/24",
},
{
Id: "997-01",
NetworkId: "997",
Name: "subnet-997",
Cidr: "2001:db8::/32",
},
}
neutronService := &Neutron{
subnets: make(map[string]neutron.SubnetV2),
ServiceInstance: testservices.ServiceInstance{
IdentityService: identityService,
FallbackIdentityService: fallbackIdentity,
Scheme: URL.Scheme,
Hostname: hostname,
VersionPath: versionPath,
TenantId: tenantId,
Region: region,
},
}
if identityService != nil {
identityService.RegisterServiceProvider("neutron", "network", neutronService)
}
for _, subnet := range defaultSubnets {
err := neutronService.addSubnet(subnet)
if err != nil {
panic(err)
}
}
return neutronService
}
func (n *Neutron) Stop() {
// noop
}
// AddNeutronModel setups up the test double for shared data between the nova
// and neutron test doubles. Required for the neutron test double.
func (n *Neutron) AddNeutronModel(neutronModel *neutronmodel.NeutronModel) {
n.neutronModel = neutronModel
}
// NeutronModel returns the current NeutronModel, which can then be used to
// update internal state for neutron test doubles.
func (n *Neutron) NeutronModel() *neutronmodel.NeutronModel {
return n.neutronModel
}
// updateSecurityGroup updates an existing security group.
func (n *Neutron) updateSecurityGroup(group neutron.SecurityGroupV2) error {
if err := n.ProcessFunctionHook(n, group); err != nil {
return err
}
return n.neutronModel.UpdateSecurityGroup(group)
}
// addPort creates a new port.
func (n *Neutron) addPort(port neutron.PortV2) error {
if err := n.ProcessFunctionHook(n, port); err != nil {
return err
}
return n.neutronModel.AddPort(port)
}
// port retrieves an existing port by ID.
func (n *Neutron) port(portId string) (*neutron.PortV2, error) {
if err := n.ProcessFunctionHook(n, portId); err != nil {
return nil, err
}
return n.neutronModel.Port(portId)
}
// allPorts returns a list of all existing ports.
func (n *Neutron) allPorts() []neutron.PortV2 {
return n.neutronModel.AllPorts()
}
// removePort deletes an existing port.
func (n *Neutron) removePort(portId string) error {
if err := n.ProcessFunctionHook(n, portId); err != nil {
return err
}
return n.neutronModel.RemovePort(portId)
}
// addSecurityGroup creates a new security group.
func (n *Neutron) addSecurityGroup(group neutron.SecurityGroupV2) error {
if err := n.ProcessFunctionHook(n, group); err != nil {
return err
}
return n.neutronModel.AddSecurityGroup(group)
}
// securityGroup retrieves an existing group by ID.
func (n *Neutron) securityGroup(groupId string) (*neutron.SecurityGroupV2, error) {
if err := n.ProcessFunctionHook(n, groupId); err != nil {
return nil, err
}
return n.neutronModel.SecurityGroup(groupId)
}
// securityGroupByName retrieves an existing named group.
func (n *Neutron) securityGroupByName(groupName string) ([]neutron.SecurityGroupV2, error) {
if err := n.ProcessFunctionHook(n, groupName); err != nil {
return nil, err
}
return n.neutronModel.SecurityGroupByName(groupName)
}
// allSecurityGroups returns a list of all existing groups.
func (n *Neutron) allSecurityGroups() []neutron.SecurityGroupV2 {
return n.neutronModel.AllSecurityGroups()
}
// removeSecurityGroup deletes an existing group.
func (n *Neutron) removeSecurityGroup(groupId string) error {
if err := n.ProcessFunctionHook(n, groupId); err != nil {
return err
}
return n.neutronModel.RemoveSecurityGroup(groupId)
}
// addSecurityGroupRule creates a new rule in an existing group.
// This can be either an ingress or an egress rule (see the notes
// about neutron.RuleInfoV2).
func (n *Neutron) addSecurityGroupRule(ruleId string, rule neutron.RuleInfoV2) error {
if err := n.ProcessFunctionHook(n, ruleId, rule); err != nil {
return err
}
return n.neutronModel.AddSecurityGroupRule(ruleId, rule)
}
// hasSecurityGroupRule returns whether the given group contains the given rule.
func (n *Neutron) hasSecurityGroupRule(groupId, ruleId string) bool {
return n.neutronModel.HasSecurityGroupRule(groupId, ruleId)
}
// securityGroupRule retrieves an existing rule by ID.
func (n *Neutron) securityGroupRule(ruleId string) (*neutron.SecurityGroupRuleV2, error) {
if err := n.ProcessFunctionHook(n, ruleId); err != nil {
return nil, err
}
return n.neutronModel.SecurityGroupRule(ruleId)
}
// removeSecurityGroupRule deletes an existing rule from its group.
func (n *Neutron) removeSecurityGroupRule(ruleId string) error {
if err := n.ProcessFunctionHook(n, ruleId); err != nil {
return err
}
return n.neutronModel.RemoveSecurityGroupRule(ruleId)
}
// addFloatingIP creates a new floating IP address in the pool.
func (n *Neutron) addFloatingIP(ip neutron.FloatingIPV2) error {
if err := n.ProcessFunctionHook(n, ip); err != nil {
return err
}
return n.neutronModel.AddFloatingIP(ip)
}
// hasFloatingIP returns whether the given floating IP address exists.
func (n *Neutron) hasFloatingIP(address string) bool {
return n.neutronModel.HasFloatingIP(address)
}
// floatingIP retrieves the floating IP by ID.
func (n *Neutron) floatingIP(ipId string) (*neutron.FloatingIPV2, error) {
if err := n.ProcessFunctionHook(n, ipId); err != nil {
return nil, err
}
return n.neutronModel.FloatingIP(ipId)
}
// floatingIPByAddr retrieves the floating IP by address.
func (n *Neutron) floatingIPByAddr(address string) (*neutron.FloatingIPV2, error) {
if err := n.ProcessFunctionHook(n, address); err != nil {
return nil, err
}
return n.neutronModel.FloatingIPByAddr(address)
}
// matchFloatingIPs returns a list of matching FloatingIPs, after applying the
// given filter. Each separate filter is combined with a logical AND.
// Each filter can have only one value. A nil filter matches all FloatingIPs.
//
// This is tested to match OpenStack behavior. Regular expression
// matching is supported for FilterProjectId only, and the supported
// syntax is limited to whatever DB backend is used (see SQL
// REGEXP/RLIKE).
//
// Example:
//
// f := filter{
// neutron.FilterProjectId: `foo.*`,
// }
//
// This will match all FloatingIPs with project_id starting
// with "foo".
func (n *Neutron) matchFloatingIPs(f filter) []neutron.FloatingIPV2 {
fips := n.neutronModel.AllFloatingIPs()
if len(f) == 0 {
return fips
}
if projectId := f[neutron.FilterProjectId]; projectId != "" {
matched := []neutron.FloatingIPV2{}
externalNetworks, err := n.matchNetworks(filter{neutron.FilterRouterExternal: "true"})
if err != nil {
return fips
}
for _, fip := range fips {
for _, network := range externalNetworks {
if fip.FloatingNetworkId == network.Id && projectId == network.TenantId {
matched = append(matched, fip)
}
}
}
fips = matched
}
return fips
}
// allFloatingIPs returns a list of all created floating IPs.
func (n *Neutron) allFloatingIPs(f filter) []neutron.FloatingIPV2 {
return n.matchFloatingIPs(f)
}
// removeFloatingIP deletes an existing floating IP by ID.
func (n *Neutron) removeFloatingIP(ipId string) error {
if err := n.ProcessFunctionHook(n, ipId); err != nil {
return err
}
return n.neutronModel.RemoveFloatingIP(ipId)
}
// filter is used internally by matchNetworks and matchFloatingIPs.
type filter map[string]string
// matchNetworks returns a list of matching networks, after applying the
// given filter. Each separate filter is combined with a logical AND.
// Each filter can have only one value. A nil filter matches all networks.
//
// This is tested to match OpenStack behavior. Regular expression
// matching is supported for FilterNetwork only, and the supported
// syntax is limited to whatever DB backend is used (see SQL
// REGEXP/RLIKE).
//
// Example:
//
// f := filter{
// neutron.FilterRouterExternal: true,
// neutron.FilterNetwork: `foo.*`,
// }
//
// This will match all external neworks with names starting
// with "foo".
func (n *Neutron) matchNetworks(f filter) ([]neutron.NetworkV2, error) {
networks := n.neutronModel.AllNetworks()
if len(f) == 0 {
return networks, nil
}
if external := f[neutron.FilterRouterExternal]; external != "" {
matched := []neutron.NetworkV2{}
externalBool, err := strconv.ParseBool(external)
if err != nil {
return nil, err
}
for _, network := range networks {
if network.External == externalBool {
matched = append(matched, network)
}
}
if len(matched) == 0 {
// no match, so no need to look further
return nil, nil
}
networks = matched
}
if name := f[neutron.FilterNetwork]; name != "" {
matched := []neutron.NetworkV2{}
for _, network := range networks {
if name == network.Name {
matched = append(matched, network)
}
}
if len(matched) == 0 {
// no match, so no need to look further
return nil, nil
}
networks = matched
}
return networks, nil
}
// allNetworks returns a list of all existing networks.
func (n *Neutron) allNetworks(f filter) ([]neutron.NetworkV2, error) {
return n.matchNetworks(f)
}
// network retrieves the network by ID.
func (n *Neutron) network(networkId string) (*neutron.NetworkV2, error) {
if err := n.ProcessFunctionHook(n, networkId); err != nil {
return nil, err
}
return n.neutronModel.Network(networkId)
}
// addNetwork creates a new network.
func (n *Neutron) addNetwork(network neutron.NetworkV2) error {
if err := n.ProcessFunctionHook(n, network); err != nil {
return err
}
return n.neutronModel.AddNetwork(network)
}
// removeNetwork deletes an existing group.
func (n *Neutron) removeNetwork(netId string) error {
if err := n.ProcessFunctionHook(n, netId); err != nil {
return err
}
return n.neutronModel.RemoveNetwork(netId)
}
// allSubnets returns a list of all existing subnets.
func (n *Neutron) allSubnets() (subnets []neutron.SubnetV2) {
for _, sub := range n.subnets {
subnets = append(subnets, sub)
}
return subnets
}
// subnet retrieves the subnet by ID.
func (n *Neutron) subnet(subnetId string) (*neutron.SubnetV2, error) {
if err := n.ProcessFunctionHook(n, subnetId); err != nil {
return nil, err
}
subnet, ok := n.subnets[subnetId]
if !ok {
return nil, testservices.NewSubnetNotFoundError(subnetId)
}
return &subnet, nil
}
// addSubnet creates a new subnet.
func (n *Neutron) addSubnet(subnet neutron.SubnetV2) error {
if err := n.ProcessFunctionHook(n, subnet); err != nil {
return err
}
if _, err := n.subnet(subnet.Id); err == nil {
return testservices.NewSubnetAlreadyExistsError(subnet.Id)
}
subnet.TenantId = n.TenantId
n.subnets[subnet.Id] = subnet
return nil
}
// removeSubnet deletes an existing subnet.
func (n *Neutron) removeSubnet(subnetId string) error {
if err := n.ProcessFunctionHook(n, subnetId); err != nil {
return err
}
if _, err := n.subnet(subnetId); err != nil {
return err
}
delete(n.subnets, subnetId)
return nil
}