forked from thecodeteam/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoragepool.go
More file actions
148 lines (119 loc) · 4.61 KB
/
storagepool.go
File metadata and controls
148 lines (119 loc) · 4.61 KB
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
package goscaleio
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
types "github.com/codedellemc/goscaleio/types/v1"
)
type StoragePool struct {
StoragePool *types.StoragePool
client *Client
}
func NewStoragePool(client *Client) *StoragePool {
return &StoragePool{
StoragePool: new(types.StoragePool),
client: client,
}
}
func NewStoragePoolEx(client *Client, pool *types.StoragePool) *StoragePool {
return &StoragePool{
StoragePool: pool,
client: client,
}
}
func (protectionDomain *ProtectionDomain) CreateStoragePool(name string) (string, error) {
endpoint := protectionDomain.client.SIOEndpoint
storagePoolParam := &types.StoragePoolParam{}
storagePoolParam.Name = name
storagePoolParam.ProtectionDomainID = protectionDomain.ProtectionDomain.ID
jsonOutput, err := json.Marshal(&storagePoolParam)
if err != nil {
return "", fmt.Errorf("error marshaling: %s", err)
}
endpoint.Path = fmt.Sprintf("/api/types/StoragePool/instances")
req := protectionDomain.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", protectionDomain.client.Token)
req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+protectionDomain.client.configConnect.Version)
resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("error reading body")
}
var sp types.StoragePoolResp
err = json.Unmarshal(bs, &sp)
if err != nil {
return "", err
}
return sp.ID, nil
}
func (protectionDomain *ProtectionDomain) GetStoragePool(storagepoolhref string) (storagePools []*types.StoragePool, err error) {
endpoint := protectionDomain.client.SIOEndpoint
if storagepoolhref == "" {
link, err := GetLink(protectionDomain.ProtectionDomain.Links, "/api/ProtectionDomain/relationship/StoragePool")
if err != nil {
return []*types.StoragePool{}, errors.New("Error: problem finding link")
}
endpoint.Path = link.HREF
} else {
endpoint.Path = storagepoolhref
}
req := protectionDomain.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", protectionDomain.client.Token)
req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
if err != nil {
return []*types.StoragePool{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if storagepoolhref == "" {
if err = protectionDomain.client.decodeBody(resp, &storagePools); err != nil {
return []*types.StoragePool{}, fmt.Errorf("error decoding storage pool response: %s", err)
}
} else {
storagePool := &types.StoragePool{}
if err = protectionDomain.client.decodeBody(resp, &storagePool); err != nil {
return []*types.StoragePool{}, fmt.Errorf("error decoding instances response: %s", err)
}
storagePools = append(storagePools, storagePool)
}
return storagePools, nil
}
func (protectionDomain *ProtectionDomain) FindStoragePool(id, name, href string) (storagePool *types.StoragePool, err error) {
storagePools, err := protectionDomain.GetStoragePool(href)
if err != nil {
return &types.StoragePool{}, fmt.Errorf("Error getting protection domains %s", err)
}
for _, storagePool = range storagePools {
if storagePool.ID == id || storagePool.Name == name || href != "" {
return storagePool, nil
}
}
return &types.StoragePool{}, errors.New("Couldn't find protection domain")
}
func (storagePool *StoragePool) GetStatistics() (statistics *types.Statistics, err error) {
link, err := GetLink(storagePool.StoragePool.Links, "/api/StoragePool/relationship/Statistics")
if err != nil {
return &types.Statistics{}, errors.New("Error: problem finding link")
}
endpoint := storagePool.client.SIOEndpoint
endpoint.Path = link.HREF
req := storagePool.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", storagePool.client.Token)
req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
if err != nil {
return &types.Statistics{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if err = storagePool.client.decodeBody(resp, &statistics); err != nil {
return &types.Statistics{}, fmt.Errorf("error decoding instances response: %s", err)
}
return statistics, nil
}