/
ops_disk.go
342 lines (297 loc) · 9.49 KB
/
ops_disk.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
// 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 *DiskOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.DiskFindResult, error) {
results, _ := find(o.key, zone, conditions)
var values []*iaas.Disk
for _, res := range results {
dest := &iaas.Disk{}
copySameNameField(res, dest)
values = append(values, dest)
}
return &iaas.DiskFindResult{
Total: len(results),
Count: len(results),
From: 0,
Disks: values,
}, nil
}
// Create is fake implementation
func (o *DiskOp) Create(ctx context.Context, zone string, param *iaas.DiskCreateRequest, distantFrom []types.ID) (*iaas.Disk, error) {
result := &iaas.Disk{}
copySameNameField(param, result)
fill(result, fillID, fillCreatedAt, fillDiskPlan)
result.Availability = types.Availabilities.Migrating
result.Storage = &iaas.Storage{
ID: types.ID(123456789012),
Name: "dummy",
}
if result.Connection == types.EDiskConnection("") {
result.Connection = types.DiskConnections.VirtIO
}
if !param.SourceArchiveID.IsEmpty() {
archiveOp := NewArchiveOp()
source, err := archiveOp.Read(ctx, zone, param.SourceArchiveID)
if err != nil {
return nil, newErrorBadRequest(o.key, types.ID(0), "SourceArchive is not found")
}
result.SourceArchiveAvailability = source.Availability
}
if !param.SourceDiskID.IsEmpty() {
source, err := o.Read(ctx, zone, param.SourceDiskID)
if err != nil {
return nil, newErrorBadRequest(o.key, types.ID(0), "SourceDisk is not found")
}
result.SourceDiskAvailability = source.Availability
}
if !param.ServerID.IsEmpty() {
server, err := NewServerOp().Read(ctx, zone, param.ServerID)
if err != nil {
return nil, newErrorConflict(o.key, types.ID(0), "Server is not found")
}
server.Disks = append(server.Disks, &iaas.ServerConnectedDisk{
ID: result.ID,
Name: result.Name,
Availability: result.Availability,
Connection: result.Connection,
ConnectionOrder: result.ConnectionOrder,
ReinstallCount: result.ReinstallCount,
SizeMB: result.SizeMB,
DiskPlanID: result.DiskPlanID,
Storage: result.Storage,
})
putServer(zone, server)
}
putDisk(zone, result)
id := result.ID
startDiskCopy(o.key, zone, func() (interface{}, error) {
disk, err := o.Read(context.Background(), zone, id)
if err != nil {
return nil, err
}
return disk, nil
})
return result, nil
}
// Config is fake implementation
func (o *DiskOp) Config(ctx context.Context, zone string, id types.ID, edit *iaas.DiskEditRequest) error {
disk, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if disk.ServerID.IsEmpty() {
return nil
}
serverOp := NewServerOp()
server, err := serverOp.Read(ctx, zone, disk.ServerID)
if err != nil {
return err
}
if edit.HostName != "" {
server.HostName = edit.HostName
putServer(zone, server)
}
if len(server.Interfaces) > 0 {
nic := server.Interfaces[0]
if nic.SwitchScope == types.Scopes.Shared {
nic.IPAddress = pool().nextSharedIP().String()
} else {
nic.UserIPAddress = edit.UserIPAddress
}
swOp := NewSwitchOp()
sw, err := swOp.Read(ctx, zone, nic.SwitchID)
if err != nil {
return err
}
if len(sw.Subnets) == 0 {
nic.UserSubnetDefaultRoute = edit.UserSubnet.DefaultRoute
nic.UserSubnetNetworkMaskLen = edit.UserSubnet.NetworkMaskLen
} else {
nic.UserSubnetDefaultRoute = sw.Subnets[0].DefaultRoute
nic.UserSubnetNetworkMaskLen = sw.Subnets[0].NetworkMaskLen
nic.SubnetDefaultRoute = sw.Subnets[0].DefaultRoute
nic.SubnetNetworkAddress = sw.Subnets[0].NetworkAddress
}
putServer(zone, server)
}
return nil
}
// CreateWithConfig is fake implementation
func (o *DiskOp) CreateWithConfig(ctx context.Context, zone string, createParam *iaas.DiskCreateRequest, editParam *iaas.DiskEditRequest, bootAtAvailable bool, distantFrom []types.ID) (*iaas.Disk, error) {
// check
if !createParam.ServerID.IsEmpty() {
serverOp := NewServerOp()
_, err := serverOp.Read(ctx, zone, createParam.ServerID)
if err != nil {
return nil, newErrorBadRequest(o.key, types.ID(0), fmt.Sprintf("Server %s is not found", createParam.ServerID))
}
}
result, err := o.Create(ctx, zone, createParam, distantFrom)
if err != nil {
return nil, err
}
if err := o.Config(ctx, zone, result.ID, editParam); err != nil {
return nil, err
}
if !createParam.ServerID.IsEmpty() && bootAtAvailable {
waiter := iaas.WaiterForReady(func() (interface{}, error) {
disk, err := o.Read(ctx, zone, result.ID)
if err != nil {
return nil, err
}
return disk, nil
})
res, err := waiter.WaitForState(ctx)
if err != nil {
return nil, err
}
result = res.(*iaas.Disk)
// boot server
serverOp := NewServerOp()
if err := serverOp.Boot(ctx, zone, createParam.ServerID); err != nil {
return nil, err
}
}
return result, nil
}
// ResizePartition is fake implementation
func (o *DiskOp) ResizePartition(ctx context.Context, zone string, id types.ID, param *iaas.DiskResizePartitionRequest) error {
_, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
return nil
}
// ConnectToServer is fake implementation
func (o *DiskOp) ConnectToServer(ctx context.Context, zone string, id types.ID, serverID types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
serverOp := NewServerOp()
server, err := serverOp.Read(ctx, zone, serverID)
if err != nil {
return newErrorBadRequest(o.key, id, fmt.Sprintf("Server[%d] is not exists", serverID))
}
for _, connected := range server.Disks {
if connected.ID == value.ID {
return newErrorBadRequest(o.key, id, fmt.Sprintf("Disk[%d] is already connected to Server[%d]", id, serverID))
}
}
// TODO とりあえず同時実行制御は考慮しない。更新対象リソースが増えるようであれば実装方法を考える
connectedDisk := &iaas.ServerConnectedDisk{}
copySameNameField(value, connectedDisk)
server.Disks = append(server.Disks, connectedDisk)
putServer(zone, server)
value.ServerID = serverID
value.ServerName = server.Name
putDisk(zone, value)
return nil
}
// DisconnectFromServer is fake implementation
func (o *DiskOp) DisconnectFromServer(ctx context.Context, zone string, id types.ID) error {
value, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
if value.ServerID.IsEmpty() {
return newErrorBadRequest(o.key, id, fmt.Sprintf("Disk[%d] is not connected to Server", id))
}
serverOp := NewServerOp()
server, err := serverOp.Read(ctx, zone, value.ServerID)
if err != nil {
return newErrorBadRequest(o.key, id, fmt.Sprintf("Server[%d] is not exists", value.ServerID))
}
var disks []*iaas.ServerConnectedDisk
for _, connected := range server.Disks {
if connected.ID != value.ID {
connectedDisk := &iaas.ServerConnectedDisk{}
copySameNameField(value, connectedDisk)
server.Disks = append(server.Disks, connectedDisk)
disks = append(disks, connected)
}
}
if len(disks) == len(server.Disks) {
return newInternalServerError(o.key, id, fmt.Sprintf("Disk[%d] is not found on server's connected disks", id))
}
server.Disks = disks
putServer(zone, server)
value.ServerID = types.ID(0)
value.ServerName = ""
putDisk(zone, value)
return nil
}
// Read is fake implementation
func (o *DiskOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.Disk, error) {
value := getDiskByID(zone, id)
if value == nil {
return nil, newErrorNotFound(o.key, id)
}
dest := &iaas.Disk{}
copySameNameField(value, dest)
return dest, nil
}
// Update is fake implementation
func (o *DiskOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.DiskUpdateRequest) (*iaas.Disk, error) {
value, err := o.Read(ctx, zone, id)
if err != nil {
return nil, err
}
copySameNameField(param, value)
fill(value, fillModifiedAt)
putDisk(zone, value)
return value, nil
}
// Delete is fake implementation
func (o *DiskOp) Delete(ctx context.Context, zone string, id types.ID) error {
_, err := o.Read(ctx, zone, id)
if err != nil {
return err
}
ds().Delete(o.key, zone, id)
return nil
}
// Monitor is fake implementation
func (o *DiskOp) Monitor(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.DiskActivity, 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.DiskActivity{}
for i := 0; i < 5; i++ {
res.Values = append(res.Values, &iaas.MonitorDiskValue{
Time: now.Add(time.Duration(i*-5) * time.Minute),
Read: float64(random(1000)),
Write: float64(random(1000)),
})
}
return res, nil
}
// MonitorDisk is fake implementation
func (o *DiskOp) MonitorDisk(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.DiskActivity, error) {
return o.Monitor(ctx, zone, id, condition)
}