This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
device.go
86 lines (69 loc) · 1.58 KB
/
device.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
package goscaleio
import (
"errors"
"fmt"
"net/http"
"reflect"
types "github.com/thecodeteam/goscaleio/types/v1"
)
type Device struct {
Device *types.Device
client *Client
}
func NewDevice(client *Client) *Device {
return &Device{
Device: &types.Device{},
client: client,
}
}
func NewDeviceEx(client *Client, device *types.Device) *Device {
return &Device{
Device: device,
client: client,
}
}
func (sp *StoragePool) AttachDevice(
path string,
sdsID string) (string, error) {
deviceParam := &types.DeviceParam{
Name: path,
DeviceCurrentPathname: path,
StoragePoolID: sp.StoragePool.ID,
SdsID: sdsID,
TestMode: "testAndActivate"}
dev := types.DeviceResp{}
err := sp.client.getJSONWithRetry(
http.MethodPost, "/api/types/Device/instances",
deviceParam, &dev)
if err != nil {
return "", err
}
return dev.ID, nil
}
func (sp *StoragePool) GetDevice() ([]types.Device, error) {
path := fmt.Sprintf(
"/api/instances/StoragePool::%v/relationships/Device",
sp.StoragePool.ID)
var devices []types.Device
err := sp.client.getJSONWithRetry(
http.MethodGet, path, nil, &devices)
if err != nil {
return nil, err
}
return devices, nil
}
func (sp *StoragePool) FindDevice(
field, value string) (*types.Device, error) {
devices, err := sp.GetDevice()
if err != nil {
return nil, err
}
for _, device := range devices {
valueOf := reflect.ValueOf(device)
switch {
case reflect.Indirect(valueOf).FieldByName(field).String() == value:
return &device, nil
}
}
return nil, errors.New("Couldn't find DEV")
}