-
Notifications
You must be signed in to change notification settings - Fork 15
/
share.go
421 lines (383 loc) · 11.7 KB
/
share.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
/*
* Copyright 2018-2019, CS Systemes d'Information, http://www.c-s.fr
*
* 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 metadata
import (
"github.com/CS-SI/SafeScale/lib/server/iaas"
"github.com/CS-SI/SafeScale/lib/utils/concurrency"
"github.com/CS-SI/SafeScale/lib/utils/metadata"
"github.com/CS-SI/SafeScale/lib/utils/retry"
"github.com/CS-SI/SafeScale/lib/utils/scerr"
"github.com/CS-SI/SafeScale/lib/utils/serialize"
"github.com/CS-SI/SafeScale/lib/utils/temporal"
)
const (
// nasFolderName is the technical name of the container used to store nas info
shareFolderName = "shares"
)
// Share contains information to maintain in Object Storage a list of shared folders
type Share struct {
item *metadata.Item
name *string
id *string
}
// NewShare creates an instance of metadata.Nas
func NewShare(svc iaas.Service) (*Share, error) {
aShare, err := metadata.NewItem(svc, shareFolderName)
if err != nil {
return nil, err
}
return &Share{
item: aShare,
}, nil
}
type shareItem struct {
HostID string `json:"host_id"` // contains the ID of the host serving the share
HostName string `json:"host_name"` // contains the Name of the host serving the share
ShareID string `json:"share_id"` // contains the ID of the share
ShareName string `json:"share_name"` // contains the name of the share
}
// Serialize ...
func (n *shareItem) Serialize() ([]byte, error) {
return serialize.ToJSON(n)
}
// Deserialize ...
func (n *shareItem) Deserialize(buf []byte) error {
return serialize.FromJSON(buf, n)
}
// Carry links an export instance to the Metadata instance
func (ms *Share) Carry(hostID, hostName, shareID, shareName string) (*Share, error) {
if ms == nil {
return nil, scerr.InvalidInstanceError()
}
if ms.item == nil {
return nil, scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
if hostID == "" {
return nil, scerr.InvalidParameterError("hostID", "cannot be empty string")
}
if hostName == "" {
return nil, scerr.InvalidParameterError("hostName", "cannot be empty string")
}
if shareID == "" {
return nil, scerr.InvalidParameterError("shareID", "cannot be empty string")
}
if shareName == "" {
return nil, scerr.InvalidParameterError("shareName", "cannot be empty string")
}
ni := shareItem{
HostID: hostID,
HostName: hostName,
ShareID: shareID,
ShareName: shareName,
}
ms.item.Carry(&ni)
ms.name = &ni.ShareName
ms.id = &ni.ShareID
return ms, nil
}
// Get returns the ID of the host owning the share
func (ms *Share) Get() (string, error) {
if ms == nil {
return "", scerr.InvalidInstanceError()
}
if ms.item == nil {
return "", scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
if ei, ok := ms.item.Get().(*shareItem); ok {
return ei.HostName, nil
}
return "", scerr.InconsistentError("share metadata content must be a *shareItem")
}
// Write updates the metadata corresponding to the share in the Object Storage
func (ms *Share) Write() error {
if ms == nil {
return scerr.InvalidInstanceError()
}
if ms.item == nil {
return scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
err := ms.item.WriteInto(ByIDFolderName, *ms.id)
if err != nil {
return err
}
return ms.item.WriteInto(ByNameFolderName, *ms.name)
}
// ReadByReference tries to read 'ref' as an ID, and if not found as a name
func (ms *Share) ReadByReference(id string) (err error) {
if ms == nil {
return scerr.InvalidInstanceError()
}
errID := ms.ReadByID(id)
if errID != nil {
errName := ms.ReadByName(id)
if errName != nil {
return errName
}
}
return nil
}
// ReadByID reads the metadata of an export identified by ID from Object Storage
func (ms *Share) ReadByID(id string) error {
if ms == nil {
return scerr.InvalidInstanceError()
}
if ms.item == nil {
return scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
var si shareItem
err := ms.item.ReadFrom(ByIDFolderName, id, func(buf []byte) (serialize.Serializable, error) {
err := (&si).Deserialize(buf)
if err != nil {
return nil, err
}
return &si, nil
})
if err != nil {
return err
}
if _, err := ms.Carry(si.HostID, si.HostName, si.ShareID, si.ShareName); err != nil {
return err
}
return nil
}
// ReadByName reads the metadata of a nas identified by name
func (ms *Share) ReadByName(name string) error {
if ms == nil {
return scerr.InvalidInstanceError()
}
if ms.item == nil {
return scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
var si shareItem
err := ms.item.ReadFrom(ByNameFolderName, name, func(buf []byte) (serialize.Serializable, error) {
err := (&si).Deserialize(buf)
if err != nil {
return nil, err
}
return &si, nil
})
if err != nil {
return err
}
if _, err := ms.Carry(si.HostID, si.HostName, si.ShareID, si.ShareName); err != nil {
return err
}
return nil
}
// Delete updates the metadata corresponding to the share
func (ms *Share) Delete() error {
if ms == nil {
return scerr.InvalidInstanceError()
}
if ms.item == nil {
return scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
err := ms.item.DeleteFrom(ByIDFolderName, *ms.id)
if err != nil {
return err
}
return ms.item.DeleteFrom(ByNameFolderName, *ms.name)
}
// Browse walks through shares folder and executes a callback for each entry
func (ms *Share) Browse(callback func(string, string) error) error {
if ms == nil {
return scerr.InvalidInstanceError()
}
if ms.item == nil {
return scerr.InvalidInstanceContentError("ms.item", "cannot be nil")
}
return ms.item.BrowseInto(ByNameFolderName, func(buf []byte) error {
si := shareItem{}
err := (&si).Deserialize(buf)
if err != nil {
return err
}
return callback(si.HostName, si.ShareID)
})
}
// // AddClient adds a client to the Nas definition in Object Storage
// func (m *Nas) AddClient(nas *resources.Nas) error {
// return NewNas(m.item.GetService()).Carry(nas).item.WriteInto(*m.id, nas.ID)
// // return m.item.WriteInto(m.id, nas.ID)
// }
// // RemoveClient removes a client to the Nas definition in Object Storage
// func (m *Nas) RemoveClient(nas *resources.Nas) error {
// return m.item.DeleteFrom(*m.id, nas.ID)
// }
// // Listclients returns the list of ID of hosts clients of the NAS server
// func (m *Nas) Listclients() ([]*resources.Nas, error) {
// var list []*resources.Nas
// err := m.item.BrowseInto(*m.id, func(buf []byte) error {
// nas := resources.Nas{}
// err := (&nas).Deserialize(buf)
// if err != nil {
// return err
// }
// list = append(list, &nas)
// return nil
// })
// return list, err
// }
// // FindClient returns the client hosted by the Host whose name is given
// func (m *Nas) FindClient(hostName string) (*resources.Nas, error) {
// var client *resources.Nas
// err := m.item.BrowseInto(*m.id, func(buf []byte) error {
// nas := resources.Nas{}
// err := (&nas).Deserialize(buf)
// if err != nil {
// return err
// }
// if nas.Host == hostName {
// client = &nas
// return nil
// }
// return nil
// })
// if err != nil {
// return nil, err
// }
// if client == nil {
// return nil, fmt.Errorf("no client found for nas '%s' on host '%s'", *m.name, hostName)
// }
// return client, nil
// }
// Acquire waits until the write lock is available, then locks the metadata.
//
// May panic (see scerr.OnPanic() usage to intercept and translate it to an error)
func (ms *Share) Acquire() {
if ms == nil {
panic("invalid instance")
}
if ms.item == nil {
panic("invalid instance content: ms.item cannot be nil")
}
ms.item.Acquire()
}
// Release unlocks the metadata
//
// May panic (see scerr.OnPanic() usage to intercept and translate it to an error)
func (ms *Share) Release() {
if ms == nil {
panic("invalid instance")
}
if ms.item == nil {
panic("invalid instance content: ms.item cannot be nil")
}
ms.item.Release()
}
// SaveShare saves the Nas definition in Object Storage
func SaveShare(svc iaas.Service, hostID, hostName, shareID, shareName string) (*Share, error) {
if svc == nil {
return nil, scerr.InvalidParameterError("svc", "cannot be nil")
}
if hostID == "" {
return nil, scerr.InvalidParameterError("hostID", "cannot be empty string")
}
if hostName == "" {
return nil, scerr.InvalidParameterError("hostName", "cannot be empty string")
}
if shareID == "" {
return nil, scerr.InvalidParameterError("shareID", "cannot be empty string")
}
if shareName == "" {
return nil, scerr.InvalidParameterError("shareName", "cannot be empty string")
}
aShare, err := NewShare(svc)
if err != nil {
return nil, err
}
ms, err := aShare.Carry(hostID, hostName, shareID, shareName)
if err != nil {
return nil, err
}
return ms, ms.Write()
}
// RemoveShare removes the share definition from Object Storage
func RemoveShare(svc iaas.Service, hostID, hostName, shareID, shareName string) error {
if svc == nil {
return scerr.InvalidParameterError("svc", "cannot be nil")
}
if hostID == "" {
return scerr.InvalidParameterError("hostID", "cannot be empty string")
}
if hostName == "" {
return scerr.InvalidParameterError("hostName", "cannot be empty string")
}
if shareID == "" {
return scerr.InvalidParameterError("shareID", "cannot be empty string")
}
if shareName == "" {
return scerr.InvalidParameterError("shareName", "cannot be empty string")
}
aShare, err := NewShare(svc)
if err != nil {
return err
}
aShare, err = aShare.Carry(hostID, hostName, shareID, shareName)
if err != nil {
return err
}
return aShare.Delete()
}
// LoadShare returns the name of the host owing the share 'ref', read from Object Storage
// logic: Read by ID; if error is ErrNotFound then read by name; if error is ErrNotFound return this error
// In case of any other error, abort the retry to propagate the error
// If retry times out, return errNotFound
func LoadShare(svc iaas.Service, ref string) (share string, err error) {
if svc == nil {
return "", scerr.InvalidParameterError("svc", "cannot be nil")
}
if ref == "" {
return "", scerr.InvalidParameterError("ref", "cannot be empty string")
}
tracer := concurrency.NewTracer(nil, "(<svc>, '"+ref+"')", true).GoingIn()
defer tracer.OnExitTrace()()
defer scerr.OnExitLogError(tracer.TraceMessage(""), &err)()
ms, err := NewShare(svc)
if err != nil {
return "", err
}
retryErr := retry.WhileUnsuccessfulDelay1Second(
func() error {
innerErr := ms.ReadByReference(ref)
if innerErr != nil {
if _, ok := innerErr.(scerr.ErrNotFound); ok {
return retry.StopRetryError("no metadata found", innerErr)
}
return innerErr
}
return nil
},
2*temporal.GetDefaultDelay(),
)
// If retry timed out, log it and return error ErrNotFound
if retryErr != nil {
// If it's not a timeout is something we don't know how to handle yet
if _, ok := retryErr.(scerr.ErrTimeout); !ok {
return "", scerr.Cause(retryErr)
}
return "", retryErr
}
return ms.Get()
}
// // MountNas add the client nas to the Nas definition from Object Storage
// func MountNas(svc *providers.Service, client *resources.Nas, server *resources.Nas) error {
// return NewNas(svc).Carry(server).AddClient(client)
// }
// // UmountNas remove the client nas to the Nas definition from Object Storage
// func UmountNas(svc *providers.Service, client *resources.Nas, server *resources.Nas) error {
// return NewNas(svc).Carry(server).RemoveClient(client)
// }