-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot.go
157 lines (133 loc) · 4.41 KB
/
snapshot.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
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 (
vmSnapshotv2Path = "/snapshots"
vmSnapshotv2VMSinglePath = vmSnapshotv2Path + "?vm_uuid=%s"
vmSnapshotv2SinglePath = vmSnapshotv2Path + "/%s"
vmSnapshotv2RestorePath = vmSinglePath + "/restore"
)
// SnapshotClient ... is a client for the vm API.
type SnapshotClient struct {
client *Client
}
// ListByVM ...
func (c *SnapshotClient) ListByVM(ctx context.Context, vm *schema.VMIntent) (*v2.SnapshotList, error) {
filter := &v2.Metadata{FilterCriteria: fmt.Sprintf("vm_uuid==%s", vm.Metadata.UUID)}
return c.List(ctx, filter, vm)
}
// Get ...
func (c *SnapshotClient) Get(ctx context.Context, idOrName string, vm *schema.VMIntent) (*v2.SnapshotSpec, error) {
if utils.IsValidUUID(idOrName) {
return c.GetByUUID(ctx, idOrName, vm)
}
return c.GetByName(ctx, idOrName, vm)
}
// GetByUUID retrieves an vm by its UUID. If the vm does not exist, nil is returned.
func (c *SnapshotClient) GetByUUID(ctx context.Context, uuid string, vm *schema.VMIntent) (*v2.SnapshotSpec, error) {
req, err := c.client.NewV2PERequest(ctx, "GET", vm.Spec.ClusterReference.UUID, fmt.Sprintf(vmSnapshotv2SinglePath, uuid), nil)
if err != nil {
return nil, err
}
snapshot := new(v2.SnapshotSpec)
err = c.client.Do(req, &snapshot)
if err != nil {
return nil, err
}
return snapshot, nil
}
// GetByName retrieves an vm by its name. If the vm does not exist, nil is returned.
func (c *SnapshotClient) GetByName(ctx context.Context, name string, vm *schema.VMIntent) (*v2.SnapshotSpec, error) {
_ = fmt.Sprintf(vmSnapshotv2SinglePath, vm.Metadata.UUID)
// filter := &v2.Metadata{FilterCriteria: utils.StringPtr(fmt.Sprintf("name==%s", name))}
filter := &v2.Metadata{}
list, err := c.List(ctx, filter, vm)
if len(list.Entities) == 0 {
return nil, fmt.Errorf("VM Snapshot not found: %s", name)
}
return list.Entities[0], err
}
// List ...
func (c *SnapshotClient) List(ctx context.Context, opts *v2.Metadata, vm *schema.VMIntent) (*v2.SnapshotList, error) {
path := fmt.Sprintf(vmSnapshotv2VMSinglePath, vm.Metadata.UUID)
reqBodyData, err := json.Marshal(opts)
if err != nil {
return nil, err
}
req, err := c.client.NewV2PERequest(ctx, http.MethodGet, vm.Spec.ClusterReference.UUID, path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, err
}
list := new(v2.SnapshotList)
err = c.client.Do(req, &list)
if err != nil {
return nil, err
}
return list, nil
}
func (c *SnapshotClient) Restore(ctx context.Context, snapshot *v2.SnapshotSpec, vm *schema.VMIntent) (*v2.Task, error) {
snapshotRestore := &v2.SnapshotRestore{
UUID: vm.Metadata.UUID,
SnapshotUUID: snapshot.UUID,
RestoreNetworkConfiguration: true,
}
reqBodyData, err := json.Marshal(snapshotRestore)
if err != nil {
return nil, err
}
req, err := c.client.NewV2PERequest(ctx, http.MethodPost, vm.Spec.ClusterReference.UUID, fmt.Sprintf(vmSnapshotv2RestorePath, vm.Metadata.UUID), 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
}
// Create ...
func (c *SnapshotClient) Create(ctx context.Context, name string, vm *schema.VMIntent) (*v2.Task, error) {
snapspec := &v2.SnapshotSpec{
VMUUID: vm.Metadata.UUID,
Name: name,
}
snapshot := new(v2.SnapshotCreate)
snapshot.Entities = append(snapshot.Entities, snapspec)
reqBodyData, err := json.Marshal(snapshot)
if err != nil {
return nil, err
}
req, err := c.client.NewV2PERequest(ctx, http.MethodPost, vm.Spec.ClusterReference.UUID, vmSnapshotv2Path, 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
}
// Delete ...
func (c *SnapshotClient) Delete(ctx context.Context, vm *schema.VMIntent, snapshot *v2.SnapshotSpec) (*v2.Task, error) {
response := new(v2.Task)
path := fmt.Sprintf(vmSnapshotv2SinglePath, snapshot.UUID)
req, err := c.client.NewV2PERequest(ctx, http.MethodDelete, vm.Spec.ClusterReference.UUID, path, nil)
if err != nil {
return response, err
}
err = c.client.Do(req, &response)
if err != nil {
return response, err
}
return response, nil
}