-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathibm-pi-instance.go
299 lines (275 loc) · 13.2 KB
/
ibm-pi-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
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
package instance
import (
"context"
"fmt"
"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_p_vm_instances"
"github.com/IBM-Cloud/power-go-client/power/models"
)
// IBMPIInstanceClient
type IBMPIInstanceClient struct {
IBMPIClient
}
// NewIBMPIInstanceClient
func NewIBMPIInstanceClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIInstanceClient {
return &IBMPIInstanceClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}
// Get an Instance
func (f *IBMPIInstanceClient) Get(id string) (*models.PVMInstance, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Get PVM Instance %s :%w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get PVM Instance %s", id)
}
return resp.Payload, nil
}
// Get All Instances
func (f *IBMPIInstanceClient) GetAll() (*models.PVMInstances, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Get all PVM Instances of Power Instance %s :%w", f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get all PVM Instances of Power Instance %s", f.cloudInstanceID)
}
return resp.Payload, nil
}
// Create an Instance
func (f *IBMPIInstanceClient) Create(body *models.PVMInstanceCreate) (*models.PVMInstanceList, error) {
// Check for satellite differences in this endpoint
if f.session.IsOnPrem() && (body.SoftwareLicenses != nil || body.SharedProcessorPool != "") {
return nil, fmt.Errorf("software licenses and shared processor pool parameters are not supported in satellite location, check documentation")
}
params := p_cloud_p_vm_instances.NewPcloudPvminstancesPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithBody(body)
postok, postcreated, postAccepted, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Create PVM Instance :%w", err)
}
if postok != nil && len(postok.Payload) > 0 {
return &postok.Payload, nil
}
if postcreated != nil && len(postcreated.Payload) > 0 {
return &postcreated.Payload, nil
}
if postAccepted != nil && len(postAccepted.Payload) > 0 {
return &postAccepted.Payload, nil
}
return nil, fmt.Errorf("failed to Create PVM Instance")
}
// Delete an Instance
func (f *IBMPIInstanceClient) Delete(id string) error {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesDeleteParams().
WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
_, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to Delete PVM Instance %s :%w", id, err)
}
return nil
}
// Update an Instance
func (f *IBMPIInstanceClient) Update(id string, body *models.PVMInstanceUpdate) (*models.PVMInstanceUpdateResponse, error) {
// Check for satellite differences in this endpoint
if f.session.IsOnPrem() && body.SapProfileID != "" {
return nil, fmt.Errorf("sap profile id parameter is not supported in satellite location, check documentation")
}
params := p_cloud_p_vm_instances.NewPcloudPvminstancesPutParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).WithBody(body)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Update PVM Instance %s :%w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Update PVM Instance %s", id)
}
return resp.Payload, nil
}
// Action Operation for an Instance
func (f *IBMPIInstanceClient) Action(id string, body *models.PVMInstanceAction) error {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesActionPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
_, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesActionPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to perform Action on PVM Instance %s :%w", id, err)
}
return nil
}
// Generate the Console URL for an Instance
func (f *IBMPIInstanceClient) PostConsoleURL(id string) (*models.PVMInstanceConsole, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesConsolePostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
postok, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesConsolePost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Generate the Console URL PVM Instance %s :%w", id, err)
}
if postok == nil || postok.Payload == nil {
return nil, fmt.Errorf("failed to Generate the Console URL PVM Instance %s", id)
}
return postok.Payload, nil
}
// List the available Console Languages for an Instance
func (f *IBMPIInstanceClient) GetConsoleLanguages(id string) (*models.ConsoleLanguages, error) {
if f.session.IsOnPrem() {
return nil, fmt.Errorf("operation not supported in satellite location, check documentation")
}
params := p_cloud_p_vm_instances.NewPcloudPvminstancesConsoleGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesConsoleGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Get Console Languages for PVM Instance %s :%w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get Console Languages for PVM Instance %s", id)
}
return resp.Payload, nil
}
// Update the available Console Languages for an Instance
func (f *IBMPIInstanceClient) UpdateConsoleLanguage(id string, body *models.ConsoleLanguage) (*models.ConsoleLanguage, error) {
if f.session.IsOnPrem() {
return nil, fmt.Errorf("operation not supported in satellite location, check documentation")
}
params := p_cloud_p_vm_instances.NewPcloudPvminstancesConsolePutParams().
WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesConsolePut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Update Console Language for PVM Instance %s :%w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Update Console Language for PVM Instance %s", id)
}
return resp.Payload, nil
}
// Capture an Instance
func (f *IBMPIInstanceClient) CaptureInstanceToImageCatalog(id string, body *models.PVMInstanceCapture) error {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesCapturePostParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
_, _, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesCapturePost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to Capture the PVM Instance %s: %w", id, err)
}
return nil
}
// Capture an Instance (V2)
func (f *IBMPIInstanceClient) CaptureInstanceToImageCatalogV2(id string, body *models.PVMInstanceCapture) (*models.JobReference, error) {
params := p_cloud_p_vm_instances.NewPcloudV2PvminstancesCapturePostParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
resp, err := f.session.Power.PCloudpVMInstances.PcloudV2PvminstancesCapturePost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Capture the PVM Instance %s: %w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Capture the PVM Instance %s", id)
}
return resp.Payload, nil
}
// Create a snapshot of an Instance
func (f *IBMPIInstanceClient) CreatePvmSnapShot(id string, body *models.SnapshotCreate) (*models.SnapshotCreateResponse, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesSnapshotsPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
snapshotpostaccepted, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesSnapshotsPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Create the snapshot for the pvminstance %s: %w", id, err)
}
if snapshotpostaccepted == nil || snapshotpostaccepted.Payload == nil {
return nil, fmt.Errorf("failed to Create the snapshot for the pvminstance %s", id)
}
return snapshotpostaccepted.Payload, nil
}
// Create a Clone of an Instance
func (f *IBMPIInstanceClient) CreateClone(id string, body *models.PVMInstanceClone) (*models.PVMInstance, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesClonePostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
clonePost, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesClonePost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to create the clone of the pvm instance %s: %w", id, err)
}
if clonePost == nil || clonePost.Payload == nil {
return nil, fmt.Errorf("failed to create the clone of the pvm instance %s", id)
}
return clonePost.Payload, nil
}
// Get an Instance's Snapshots
func (f *IBMPIInstanceClient) GetSnapShotVM(id string) (*models.Snapshots, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesSnapshotsGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesSnapshotsGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to Get the snapshot for the pvminstance %s: %w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get the snapshot for the pvminstance %s", id)
}
return resp.Payload, nil
}
// Restore a Snapshot of an Instance
func (f *IBMPIInstanceClient) RestoreSnapShotVM(id, snapshotid, restoreAction string, body *models.SnapshotRestore) (*models.Snapshot, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesSnapshotsRestorePostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithSnapshotID(snapshotid).WithRestoreFailAction(&restoreAction).
WithBody(body)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesSnapshotsRestorePost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to restrore the snapshot for the pvminstance %s: %w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to restrore the snapshot for the pvminstance %s", id)
}
return resp.Payload, nil
}
// Add a Network to an Instance
func (f *IBMPIInstanceClient) AddNetwork(id string, body *models.PVMInstanceAddNetwork) (*models.PVMInstanceNetwork, error) {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesNetworksPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
resp, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesNetworksPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf("failed to attach the network to the pvminstanceid %s: %w", id, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to attach the network to the pvminstanceid %s", id)
}
return resp.Payload, nil
}
// Delete a Network from an Instance
func (f *IBMPIInstanceClient) DeleteNetwork(id string, body *models.PVMInstanceRemoveNetwork) error {
params := p_cloud_p_vm_instances.NewPcloudPvminstancesNetworksDeleteParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(id).
WithBody(body)
_, err := f.session.Power.PCloudpVMInstances.PcloudPvminstancesNetworksDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to delete the network to the pvminstanceid %s: %w", id, err)
}
return nil
}