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
/
instance.go
175 lines (142 loc) · 3.65 KB
/
instance.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
package goscaleio
import (
"errors"
"fmt"
"net/http"
types "github.com/thecodeteam/goscaleio/types/v1"
)
func (c *Client) GetInstance(systemhref string) ([]*types.System, error) {
var (
err error
system = &types.System{}
systems []*types.System
)
if systemhref == "" {
err = c.getJSONWithRetry(
http.MethodGet, "api/types/System/instances", nil, &systems)
} else {
err = c.getJSONWithRetry(
http.MethodGet, systemhref, nil, system)
}
if err != nil {
return nil, err
}
if systemhref != "" {
systems = append(systems, system)
}
return systems, nil
}
func (c *Client) GetVolume(
volumehref, volumeid, ancestorvolumeid, volumename string,
getSnapshots bool) ([]*types.Volume, error) {
var (
err error
path string
volume = &types.Volume{}
volumes []*types.Volume
)
if volumename != "" {
volumeid, err = c.FindVolumeID(volumename)
if err != nil && err.Error() == "Not found" {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error: problem finding volume: %s", err)
}
}
if volumeid != "" {
path = fmt.Sprintf("/api/instances/Volume::%s", volumeid)
} else if volumehref == "" {
path = "/api/types/Volume/instances"
} else {
path = volumehref
}
if volumehref == "" && volumeid == "" {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, &volumes)
} else {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, volume)
}
if err != nil {
return nil, err
}
if volumehref == "" && volumeid == "" {
var volumesNew []*types.Volume
for _, volume := range volumes {
if (!getSnapshots && volume.AncestorVolumeID == ancestorvolumeid) || (getSnapshots && volume.AncestorVolumeID != "") {
volumesNew = append(volumesNew, volume)
}
}
volumes = volumesNew
} else {
volumes = append(volumes, volume)
}
return volumes, nil
}
func (c *Client) FindVolumeID(volumename string) (string, error) {
volumeQeryIdByKeyParam := &types.VolumeQeryIdByKeyParam{
Name: volumename,
}
path := fmt.Sprintf("/api/types/Volume/instances/action/queryIdByKey")
volumeID, err := c.getStringWithRetry(http.MethodPost, path,
volumeQeryIdByKeyParam)
if err != nil {
return "", err
}
return volumeID, nil
}
func (c *Client) CreateVolume(
volume *types.VolumeParam,
storagePoolName string) (*types.VolumeResp, error) {
path := "/api/types/Volume/instances"
storagePool, err := c.FindStoragePool("", storagePoolName, "")
if err != nil {
return nil, err
}
volume.StoragePoolID = storagePool.ID
volume.ProtectionDomainID = storagePool.ProtectionDomainID
vol := &types.VolumeResp{}
err = c.getJSONWithRetry(
http.MethodPost, path, volume, vol)
if err != nil {
return nil, err
}
return vol, nil
}
func (c *Client) GetStoragePool(
storagepoolhref string) ([]*types.StoragePool, error) {
var (
err error
storagePool = &types.StoragePool{}
storagePools []*types.StoragePool
)
if storagepoolhref == "" {
err = c.getJSONWithRetry(
http.MethodGet, "/api/types/StoragePool/instances",
nil, &storagePools)
} else {
err = c.getJSONWithRetry(
http.MethodGet, storagepoolhref, nil, storagePool)
}
if err != nil {
return nil, err
}
if storagepoolhref != "" {
storagePools = append(storagePools, storagePool)
}
return storagePools, nil
}
func (c *Client) FindStoragePool(
id, name, href string) (*types.StoragePool, error) {
storagePools, err := c.GetStoragePool(href)
if err != nil {
return nil, fmt.Errorf("Error getting storage pool %s", err)
}
for _, storagePool := range storagePools {
if storagePool.ID == id || storagePool.Name == name || href != "" {
return storagePool, nil
}
}
return nil, errors.New("Couldn't find storage pool")
}