-
Notifications
You must be signed in to change notification settings - Fork 6
/
ops_interface.go
323 lines (282 loc) · 8.13 KB
/
ops_interface.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
// Copyright 2022-2023 The sacloud/iaas-api-go Authors
//
// 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 fake
import (
"context"
"fmt"
"time"
"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/types"
)
// Find is fake implementation
func (o *InterfaceOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.InterfaceFindResult, error) {
results, _ := find(o.key, zone, conditions)
var values []*iaas.Interface
for _, res := range results {
dest := &iaas.Interface{}
copySameNameField(res, dest)
values = append(values, dest)
}
return &iaas.InterfaceFindResult{
Total: len(results),
Count: len(results),
From: 0,
Interfaces: values,
}, nil
}
// Create is fake implementation
func (o *InterfaceOp) Create(ctx context.Context, zone string, param *iaas.InterfaceCreateRequest) (*iaas.Interface, error) {
result := &iaas.Interface{}
copySameNameField(param, result)
fill(result, fillID, fillCreatedAt)
result.MACAddress = pool().nextMACAddress().String()
// connect to server
if param != nil && !param.ServerID.IsEmpty() {
serverOp := NewServerOp()
server, err := serverOp.Read(ctx, zone, param.ServerID)
if err == nil {
ifaceView := &iaas.InterfaceView{}
copySameNameField(result, ifaceView)
server.Interfaces = append(server.Interfaces, ifaceView)
putServer(zone, server)
}
}
putInterface(zone, result)
return result, nil
}
// Read is fake implementation
func (o *InterfaceOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.Interface, error) {
value := getInterfaceByID(zone, id)
if value == nil {
return nil, newErrorNotFound(o.key, id)
}
dest := &iaas.Interface{}
copySameNameField(value, dest)
return dest, nil
}
// Update is fake implementation
func (o *InterfaceOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.InterfaceUpdateRequest) (*iaas.Interface, error) {
value, err := o.Read(ctx, zone, id)
if err != nil {
return nil, err
}
copySameNameField(param, value)
fill(value, fillModifiedAt)
serverOp := NewServerOp()
searched, err := serverOp.Find(ctx, zone, nil)
if err == nil {
for _, server := range searched.Servers {
for _, iface := range server.Interfaces {
if iface.ID == id {
iface.UserIPAddress = param.UserIPAddress
putServer(zone, server)
}
}
}
}
putInterface(zone, value)
return value, nil
}
// Delete is fake implementation
func (o *InterfaceOp) Delete(ctx context.Context, zone string, id types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
ds().Delete(o.key, zone, id)
if !value.ServerID.IsEmpty() {
server, err := NewServerOp().Read(ctx, zone, value.ServerID)
if err == nil {
var deleted []*iaas.InterfaceView
for _, iface := range server.Interfaces {
if iface.ID != id {
deleted = append(deleted, iface)
}
}
server.Interfaces = deleted
putServer(zone, server)
}
}
return nil
}
// Monitor is fake implementation
func (o *InterfaceOp) Monitor(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.InterfaceActivity, error) {
_, err := o.Read(ctx, zone, id)
if err != nil {
return nil, err
}
now := time.Now().Truncate(time.Second)
m := now.Minute() % 5
if m != 0 {
now.Add(time.Duration(m) * time.Minute)
}
res := &iaas.InterfaceActivity{}
for i := 0; i < 5; i++ {
res.Values = append(res.Values, &iaas.MonitorInterfaceValue{
Time: now.Add(time.Duration(i*-5) * time.Minute),
Send: float64(random(1000)),
Receive: float64(random(1000)),
})
}
return res, nil
}
// ConnectToSharedSegment is fake implementation
func (o *InterfaceOp) ConnectToSharedSegment(ctx context.Context, zone string, id types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if !value.SwitchID.IsEmpty() {
return newErrorConflict(o.key, id,
fmt.Sprintf("Interface[%d] is already connected to switch[%d]", value.ID, value.SwitchID))
}
value.SwitchID = sharedSegmentSwitch.ID
putInterface(zone, value)
if !value.ServerID.IsEmpty() {
server, err := NewServerOp().Read(ctx, zone, value.ServerID)
if err == nil {
for _, iface := range server.Interfaces {
if iface.ID == id {
iface.SwitchScope = types.Scopes.Shared
iface.SwitchID = sharedSegmentSwitch.ID
iface.SwitchName = sharedSegmentSwitch.Name
}
}
putServer(zone, server)
}
}
return nil
}
// ConnectToSwitch is fake implementation
func (o *InterfaceOp) ConnectToSwitch(ctx context.Context, zone string, id types.ID, switchID types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if !value.SwitchID.IsEmpty() {
return newErrorConflict(o.key, id,
fmt.Sprintf("Interface[%d] is already connected to switch[%d]", value.ID, switchID))
}
sw, err := NewSwitchOp().Read(ctx, zone, switchID)
if err != nil {
return err
}
sw.ServerCount++
putSwitch(zone, sw)
value.SwitchID = switchID
putInterface(zone, value)
if !value.ServerID.IsEmpty() {
server, err := NewServerOp().Read(ctx, zone, value.ServerID)
if err == nil {
for _, iface := range server.Interfaces {
if iface.ID == id {
iface.SwitchScope = types.Scopes.User
iface.SwitchID = sw.ID
iface.SwitchName = sw.Name
}
}
putServer(zone, server)
}
}
return nil
}
// DisconnectFromSwitch is fake implementation
func (o *InterfaceOp) DisconnectFromSwitch(ctx context.Context, zone string, id types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if value.SwitchID.IsEmpty() {
return newErrorConflict(o.key, id,
fmt.Sprintf("Interface[%d] is already disconnected", value.ID))
}
value.SwitchID = types.ID(0)
putInterface(zone, value)
if !value.ServerID.IsEmpty() {
server, err := NewServerOp().Read(ctx, zone, value.ServerID)
if err == nil {
for _, iface := range server.Interfaces {
if iface.ID == id {
iface.SwitchScope = types.EScope("")
iface.SwitchID = types.ID(0)
iface.SwitchName = ""
}
}
putServer(zone, server)
}
}
return nil
}
// ConnectToPacketFilter is fake implementation
func (o *InterfaceOp) ConnectToPacketFilter(ctx context.Context, zone string, id types.ID, packetFilterID types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if !value.PacketFilterID.IsEmpty() {
return newErrorConflict(o.key, id,
fmt.Sprintf("Interface[%d] is already connected to packetfilter[%s]", value.ID, value.PacketFilterID))
}
value.PacketFilterID = packetFilterID
putInterface(zone, value)
// server配下のInterfaceの修正
searched, err := NewServerOp().Find(ctx, zone, nil)
if err != nil {
return err
}
for _, server := range searched.Servers {
upd := false
for _, nic := range server.Interfaces {
if nic.ID == id {
nic.PacketFilterID = packetFilterID
upd = true
}
}
if upd {
putServer(zone, server)
}
}
return nil
}
// DisconnectFromPacketFilter is fake implementation
func (o *InterfaceOp) DisconnectFromPacketFilter(ctx context.Context, zone string, id types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if value.PacketFilterID.IsEmpty() {
return newErrorConflict(o.key, id,
fmt.Sprintf("Interface[%d] is already disconnected", value.ID))
}
value.PacketFilterID = types.ID(0)
putInterface(zone, value)
// server配下のInterfaceの修正
searched, err := NewServerOp().Find(ctx, zone, nil)
if err != nil {
return err
}
for _, server := range searched.Servers {
upd := false
for _, nic := range server.Interfaces {
if nic.ID == id {
nic.PacketFilterID = types.ID(0)
upd = true
}
}
if upd {
putServer(zone, server)
}
}
return nil
}