forked from IBM-Cloud/power-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibm-pi-volume.go
302 lines (278 loc) · 13.7 KB
/
ibm-pi-volume.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package instance
import (
"context"
"fmt"
"github.com/IBM-Cloud/power-go-client/errors"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_volumes"
"github.com/IBM-Cloud/power-go-client/power/models"
)
// IBMPIVolumeClient
type IBMPIVolumeClient struct {
IBMPIClient
}
// NewIBMPIVolumeClient
func NewIBMPIVolumeClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIVolumeClient {
return &IBMPIVolumeClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}
// Get a Volume
func (f *IBMPIVolumeClient) Get(id string) (*models.Volume, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.GetVolumeOperationFailed, id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get Volume %s", id)
}
return resp.Payload, nil
}
// Get All Volumes
func (f *IBMPIVolumeClient) GetAll() (*models.Volumes, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to Get all Volumes for Cloud Instance %s: %w", f.cloudInstanceID, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get all Volumes for Cloud Instance %s", f.cloudInstanceID)
}
return resp.Payload, nil
}
// Get All Affinity Volumes
func (f *IBMPIVolumeClient) GetAllAffinityVolumes(affinity string) (*models.Volumes, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithAffinity(&affinity)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to Get all Volumes with affinity %s for Cloud Instance %s: %w", affinity, f.cloudInstanceID, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get all Volumes with affinity %s for Cloud Instance %s", affinity, f.cloudInstanceID)
}
return resp.Payload, nil
}
// Create a VolumeV2
func (f *IBMPIVolumeClient) CreateVolumeV2(body *models.MultiVolumesCreate) (*models.Volumes, error) {
params := p_cloud_volumes.NewPcloudV2VolumesPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudV2VolumesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.CreateVolumeV2OperationFailed, *body.Name, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Create Volume v2")
}
return resp.Payload, nil
}
// Create a Volume
func (f *IBMPIVolumeClient) CreateVolume(body *models.CreateDataVolume) (*models.Volume, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.CreateVolumeOperationFailed, *body.Name, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Create Volume")
}
return resp.Payload, nil
}
// Update a Volume
func (f *IBMPIVolumeClient) UpdateVolume(id string, body *models.UpdateVolume) (*models.Volume, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesPutParams().
WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id).
WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.UpdateVolumeOperationFailed, id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Update Volume %s", id)
}
return resp.Payload, nil
}
// Delete a Volume
func (f *IBMPIVolumeClient) DeleteVolume(id string) error {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesDeleteParams().
WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
_, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf(errors.DeleteVolumeOperationFailed, id, err)
}
return nil
}
// Attach a Volume to an Instance
func (f *IBMPIVolumeClient) Attach(id, volumename string) error {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithVolumeID(volumename)
_, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf(errors.AttachVolumeOperationFailed, volumename, err)
}
return nil
}
// Detach a Volume from an Instance
func (f *IBMPIVolumeClient) Detach(id, volumename string) error {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesDeleteParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithVolumeID(volumename)
_, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf(errors.DetachVolumeOperationFailed, volumename, err)
}
return nil
}
// Get All Volumes attached to an Instance
func (f *IBMPIVolumeClient) GetAllInstanceVolumes(id string) (*models.Volumes, error) {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to Get all Volumes for PI Instance %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get all Volumes for PI Instance %s", id)
}
return resp.Payload, nil
}
// Set a Volume as the Boot Volume for an Instance
func (f *IBMPIVolumeClient) SetBootVolume(id, volumename string) error {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesSetbootPutParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithVolumeID(volumename)
_, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesSetbootPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to set the boot volume %s for instance %s", volumename, id)
}
return nil
}
// Check if a Volume is attached to an Instance
func (f *IBMPIVolumeClient) CheckVolumeAttach(id, volumeID string) (*models.Volume, error) {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithVolumeID(volumeID)
resp, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to validate that the volume %s is attached to the pvminstance %s: %w", volumeID, id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to validate that the volume %s is attached to the pvminstance %s", volumeID, id)
}
return resp.Payload, nil
}
// Update a Volume attached to an Instance
func (f *IBMPIVolumeClient) UpdateVolumeAttach(id, volumeID string, body *models.PVMInstanceVolumeUpdate) error {
params := p_cloud_volumes.NewPcloudPvminstancesVolumesPutParams().
WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithVolumeID(volumeID).WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudPvminstancesVolumesPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to validate that the volume %s is attached to the pvminstance %s: %w", volumeID, id, err)
}
if resp == nil || resp.Payload == nil {
return fmt.Errorf("failed to validate that the volume %s is attached to the pvminstance %s", volumeID, id)
}
return nil
}
// Performs action on volume
func (f *IBMPIVolumeClient) VolumeAction(id string, body *models.VolumeAction) error {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesActionPostParams().
WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id).WithBody(body)
_, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesActionPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to perform action on volume %s with error %w", id, err)
}
return nil
}
// Get remote copy relationship of a volume
func (f *IBMPIVolumeClient) GetVolumeRemoteCopyRelationships(id string) (*models.VolumeRemoteCopyRelationship, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesRemoteCopyRelationshipGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesRemoteCopyRelationshipGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.GetVolumeRemoteCopyRelationshipsOperationFailed, id, f.cloudInstanceID, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get remote copy relationships of a volume %s", id)
}
return resp.Payload, nil
}
// Get a list of flashcopy mappings of a given volume
func (f *IBMPIVolumeClient) GetVolumeFlashCopyMappings(id string) (models.FlashCopyMappings, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesFlashCopyMappingsGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesFlashCopyMappingsGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.GetVolumeFlashCopyMappingOperationFailed, id, f.cloudInstanceID, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get flash copy mapping of a volume %s", id)
}
return resp.Payload, nil
}
// Bulk volume detach
func (f *IBMPIVolumeClient) BulkVolumeDetach(pvmID string, body *models.VolumesDetach) (*models.VolumesDetachmentResponse, error) {
params := p_cloud_volumes.NewPcloudV2PvminstancesVolumesDeleteParams().
WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(pvmID).
WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudV2PvminstancesVolumesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.DetachVolumesOperationFailed, pvmID, f.cloudInstanceID, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed detaching volumes for %s", pvmID)
}
return resp.Payload, nil
}
// Bulk volume delete
func (f *IBMPIVolumeClient) BulkVolumeDelete(body *models.VolumesDelete) (*models.VolumesDeleteResponse, error) {
params := p_cloud_volumes.NewPcloudV2VolumesDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithBody(body)
respOk, respPartial, err := f.session.Power.PCloudVolumes.PcloudV2VolumesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.DeleteVolumeOperationFailed, body.VolumeIDs, err))
}
if respOk != nil && respOk.Payload != nil {
return respOk.Payload, nil
}
if respPartial != nil && respPartial.Payload != nil {
return respPartial.Payload, nil
}
return nil, fmt.Errorf("failed Deleting volumes : %s", body.VolumeIDs)
}
// Bulk volutme attach
func (f *IBMPIVolumeClient) BulkVolumeAttach(pvmID string, body *models.VolumesAttach) (*models.VolumesAttachmentResponse, error) {
params := p_cloud_volumes.NewPcloudV2PvminstancesVolumesPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(pvmID).WithBody(body)
resp, err := f.session.Power.PCloudVolumes.PcloudV2PvminstancesVolumesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.AttachVolumesOperationFailed, body.VolumeIDs, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Attach volumes %s to server %s", body.VolumeIDs, pvmID)
}
return resp.Payload, nil
}