-
Notifications
You must be signed in to change notification settings - Fork 10
/
clientdevice.go
73 lines (61 loc) · 1.82 KB
/
clientdevice.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
// Copyright (c) 2016 Intel Corporation. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package spec
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/intel-hpdd/go-lustre/lnet"
)
// ClientDevice represents a mgsspec:/fsname client mount device
type ClientDevice struct {
MgsSpec lnet.TargetSpec
FsName string
}
func (d *ClientDevice) String() string {
return fmt.Sprintf("%s:/%s", d.MgsSpec, d.FsName)
}
// MarshalJSON implements json.Marshaler
func (d *ClientDevice) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON implements json.Unmarshaler
func (d *ClientDevice) UnmarshalJSON(data []byte) error {
var devStr string
if err := json.Unmarshal(data, &devStr); err != nil {
return errors.Wrap(err, "unmarshal failed")
}
dev, err := ClientDeviceFromString(devStr)
if err != nil {
return errors.Wrap(err, "parsing client device failed")
}
*d = *dev
return nil
}
// ClientDeviceFromString attempts to parse a device out of a string
// and returns an instance of ClientDevice if successful
func ClientDeviceFromString(inString string) (*ClientDevice, error) {
devRe := regexp.MustCompile(`^(.+):/([^:/]+)$`)
matches := devRe.FindStringSubmatch(inString)
if len(matches) < 3 {
return nil, errors.Errorf("Cannot parse client mount device from %q", inString)
}
dev := &ClientDevice{
FsName: matches[2],
}
for _, nodeStr := range strings.Split(matches[1], ":") {
var nidList lnet.NidList
for _, nidStr := range strings.Split(nodeStr, ",") {
nid, err := lnet.NidFromString(nidStr)
if err != nil {
return nil, errors.Wrap(err, "parsing nid failed")
}
nidList = append(nidList, nid)
}
dev.MgsSpec = append(dev.MgsSpec, nidList)
}
return dev, nil
}