forked from dell/goscaleio
/
instance.go
203 lines (169 loc) · 4.96 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
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
// Copyright © 2019 - 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// 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 goscaleio
import (
"errors"
"fmt"
"net/http"
"time"
types "github.com/AnshumanPradipPatil1506/goscaleio/types/v1"
)
// GetInstance returns an instance
func (c *Client) GetInstance(systemhref string) ([]*types.System, error) {
defer TimeSpent("GetInstance", time.Now())
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
}
// GetVolume returns a volume
func (c *Client) GetVolume(
volumehref, volumeid, ancestorvolumeid, volumename string,
getSnapshots bool) ([]*types.Volume, error) {
defer TimeSpent("GetVolume", time.Now())
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
}
// FindVolumeID returns a VolumeID
func (c *Client) FindVolumeID(volumename string) (string, error) {
defer TimeSpent("FindVolumeID", time.Now())
volumeQeryIDByKeyParam := &types.VolumeQeryIDByKeyParam{
Name: volumename,
}
path := fmt.Sprintf("/api/types/Volume/instances/action/queryIdByKey")
volumeID, err := c.getStringWithRetry(http.MethodPost, path,
volumeQeryIDByKeyParam)
fmt.Printf("[FindVolumeID] volumeID: %+v\n", volumeID)
if err != nil {
return "", err
}
return volumeID, nil
}
// CreateVolume creates a volume
func (c *Client) CreateVolume(
volume *types.VolumeParam,
storagePoolName, protectionDomain string) (*types.VolumeResp, error) {
defer TimeSpent("CreateVolume", time.Now())
path := "/api/types/Volume/instances"
storagePool, err := c.FindStoragePool("", storagePoolName, "", protectionDomain)
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
}
// GetStoragePool returns a storagepool
func (c *Client) GetStoragePool(
storagepoolhref string) ([]*types.StoragePool, error) {
defer TimeSpent("GetStoragePool", time.Now())
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
}
// FindStoragePool returns a StoragePool
func (c *Client) FindStoragePool(
id, name, href, protectionDomain string) (*types.StoragePool, error) {
defer TimeSpent("FindStoragePool", time.Now())
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 != "" {
if storagePool.ProtectionDomainID == protectionDomain || protectionDomain == "" {
return storagePool, nil
}
}
}
return nil, errors.New("Couldn't find storage pool")
}