-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.go
238 lines (205 loc) · 6.82 KB
/
vm.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
package nutanix
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/tecbiz-ch/nutanix-go-sdk/pkg/utils"
"github.com/tecbiz-ch/nutanix-go-sdk/schema"
v2 "github.com/tecbiz-ch/nutanix-go-sdk/schema/v2"
)
const (
vmBasePath = "/vms"
vmListPath = vmBasePath + "/list"
vmSinglePath = vmBasePath + "/%s"
vmClonePath = vmSinglePath + "/clone"
vmRevertPath = vmSinglePath + "/revert"
vmPowerStatePath = vmSinglePath + "/set_power_state"
vmSnapshotPath = vmSinglePath + "/snapshot"
)
// VMClient is a client for the VM API.
type VMClient struct {
client *Client
}
// Get retrieves an vm by its UUID if the input can be parsed as an uuid, otherwise it
// retrieves an vm by its name
func (c *VMClient) Get(ctx context.Context, idOrName string) (*schema.VMIntent, error) {
if utils.IsValidUUID(idOrName) {
return c.GetByUUID(ctx, idOrName)
}
return c.GetByName(ctx, idOrName)
}
// GetByUUID retrieves an vm by its UUID
func (c *VMClient) GetByUUID(ctx context.Context, uuid string) (*schema.VMIntent, error) {
response := new(schema.VMIntent)
err := c.client.requestHelper(ctx, fmt.Sprintf(vmSinglePath, uuid), http.MethodGet, nil, response)
return response, err
}
// GetByName retrieves an vm by its name
func (c *VMClient) GetByName(ctx context.Context, name string) (*schema.VMIntent, error) {
vms, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("vm_name==%s", name)})
if err != nil {
return nil, err
}
if len(vms.Entities) == 0 {
return nil, fmt.Errorf("VM not found: %s", name)
}
return vms.Entities[0], err
}
// List returns a list of vms for a specific page.
func (c *VMClient) List(ctx context.Context, opts *schema.DSMetadata) (*schema.VMListIntent, error) {
response := new(schema.VMListIntent)
err := c.client.requestHelper(ctx, vmListPath, http.MethodPost, opts, response)
return response, err
/*
if response.Metadata.Length < response.Metadata.TotalMatches {
var wg sync.WaitGroup
responsechannel := make(chan *schema.VMListIntent, response.Metadata.TotalMatches/response.Metadata.Length+1)
errorchannel := make(chan error, response.Metadata.TotalMatches/response.Metadata.Length+1)
var i int64
for i = *opts.Length; i < response.Metadata.TotalMatches; i = i + *opts.Length {
wg.Add(1)
//pagedresponse := new(schema.VMListIntent)
go func(i int64) {
defer wg.Done()
pagedopts := schema.DSMetadata(*opts)
pagedopts.Offset = utils.Int64Ptr(i)
pagedresponse := new(schema.VMListIntent)
err := c.client.requestHelper(ctx, vmListPath, http.MethodPost, &pagedopts, pagedresponse)
if err != nil {
errorchannel <- err
}
responsechannel <- pagedresponse
}(i)
}
go func() {
wg.Wait()
close(responsechannel)
close(errorchannel)
}()
for item := range responsechannel {
response.Entities = append(response.Entities, item.Entities...)
response.Metadata.Length += item.Metadata.Length
}
for err := range errorchannel {
if err != nil {
return response, err
}
}
}
return response, err
*/
}
// All returns all vms
func (c *VMClient) All(ctx context.Context) (*schema.VMListIntent, error) {
return c.List(ctx, &schema.DSMetadata{Length: utils.Int64Ptr(itemsPerPage), Offset: utils.Int64Ptr(0)})
}
// Create creates a vm
func (c *VMClient) Create(ctx context.Context, createRequest *schema.VMIntent) (*schema.VMIntent, error) {
response := new(schema.VMIntent)
err := c.client.requestHelper(ctx, vmBasePath, http.MethodPost, createRequest, response)
return response, err
}
// Update a vm
func (c *VMClient) Update(ctx context.Context, updateRequest *schema.VMIntent) (*schema.VMIntent, error) {
updateRequest.Status = nil
response := new(schema.VMIntent)
err := c.client.requestHelper(ctx, fmt.Sprintf(vmSinglePath, updateRequest.Metadata.UUID), http.MethodPut, updateRequest, response)
return response, err
}
// Delete deletes a vm
func (c *VMClient) Delete(ctx context.Context, uuid string) error {
return c.client.requestHelper(ctx, fmt.Sprintf(vmSinglePath, uuid), http.MethodDelete, nil, nil)
}
// RevertToRecoveryPoint ...
func (c *VMClient) RevertToRecoveryPoint(ctx context.Context, vm *schema.VMIntent, vmRevertRequest *schema.VMRevertRequest) (*v2.Task, error) {
reqBodyData, err := json.Marshal(&vmRevertRequest)
if err != nil {
return nil, err
}
req, err := c.client.NewV3PERequest(ctx, http.MethodPost, vm.Spec.ClusterReference.UUID, fmt.Sprintf(vmRevertPath, vm.Metadata.UUID), bytes.NewReader(reqBodyData))
if err != nil {
return nil, err
}
task := new(v2.Task)
err = c.client.Do(req, &task)
if err != nil {
return nil, err
}
return task, nil
}
// CreateRecoveryPoint ...
func (c *VMClient) CreateRecoveryPoint(ctx context.Context, vm *schema.VMIntent) (*schema.ExecutionContext, error) {
// Returns a Task UUID
response := new(schema.ExecutionContext)
err := c.client.requestHelper(ctx, fmt.Sprintf(vmSnapshotPath, vm.Metadata.UUID), http.MethodPost, bytes.NewReader([]byte("{}")), response)
return response, err
}
// CreateV3Snapshot ...
func (c *VMClient) CreateV3Snapshot(ctx context.Context) (*schema.ExecutionContext, error) {
p := &schema.VMIntent{
Metadata: &schema.Metadata{
Kind: "vm_snapshot",
},
}
reqBodyData, err := json.Marshal(&p)
if err != nil {
return nil, err
}
req, err := c.client.NewV3PCRequest(
ctx,
http.MethodPost,
"/vm_snapshots",
bytes.NewReader(reqBodyData),
)
if err != nil {
return nil, err
}
task := new(schema.ExecutionContext)
err = c.client.Do(req, &task)
if err != nil {
return nil, err
}
return task, nil
}
// SetPowerState ...
func (c *VMClient) SetPowerState(ctx context.Context, powerState v2.PowerState, vm *schema.VMIntent) (*v2.Task, error) {
path := fmt.Sprintf(vmPowerStatePath, vm.Metadata.UUID)
powerStateSpec := &v2.VMPowerStateCreate{
Transition: &powerState,
}
reqBodyData, err := json.Marshal(powerStateSpec)
if err != nil {
return nil, err
}
req, err := c.client.NewV2PERequest(ctx, http.MethodPost, vm.Spec.ClusterReference.UUID, path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, err
}
response := new(v2.Task)
err = c.client.Do(req, &response)
if err != nil {
return nil, err
}
return response, nil
}
// Clone ...
func (c *VMClient) Clone(ctx context.Context, sourcevm *schema.VMIntent) (*v2.Task, error) {
// TODO:
// POST to https://<prism>:9440/api/nutanix/v3/idempotence_identifiers to get a NEW UUID
// POST Body IdempotenceIdentifiersInput
// Response is a IdempotenceIdentifiersResponse
// get first UUID in Array UUIDList
// POST NEW VMCLoneInput with UUID retrieved
req, err := c.client.NewV3PCRequest(ctx, http.MethodPost, fmt.Sprintf(vmClonePath, sourcevm.Metadata.UUID), bytes.NewReader([]byte("{}")))
if err != nil {
return nil, err
}
task := new(v2.Task)
err = c.client.Do(req, &task)
if err != nil {
return nil, err
}
return task, nil
}