forked from enix/dothill-api-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
snapshots.go
89 lines (69 loc) · 3.59 KB
/
snapshots.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
// Copyright (c) 2023 Seagate Technology LLC and/or its Affiliates
package mcapi
import (
"net/http"
"time"
openapiclient "github.com/Seagate/seagate-exos-x-api-go/v2/pkg/client"
"github.com/Seagate/seagate-exos-x-api-go/v2/pkg/common"
"k8s.io/klog/v2"
)
// CreateSnapshot : create a snapshot in a snap pool and the snap pool if it doesn't already exist
func (client *Client) CreateSnapshot(volumeName string, snapshotName string) (*common.ResponseStatus, error) {
logger := klog.FromContext(client.Ctx)
response, httpRes, err := client.apiClient.DefaultApi.CreateSnapshotsVolumesNamesGet(client.Ctx, volumeName, snapshotName).Execute()
logger.V(2).Info("create snapshot", "volume", volumeName, "snapshot", snapshotName, "http", httpRes.Status)
status := CreateCommonStatus(logger, &response.Status)
return status, err
}
// ShowSnapshots : Show one snaphot, or all snapshots, or all snapshots for a volume
func (client *Client) ShowSnapshots(snapshotId string, sourceVolumeId string) ([]common.SnapshotObject, *common.ResponseStatus, error) {
logger := klog.FromContext(client.Ctx)
var err error
var httpRes *http.Response
var response *openapiclient.SnapshotsObject
// Call the correct OpenAPI Client function
if sourceVolumeId != "" {
response, httpRes, err = client.apiClient.DefaultApi.ShowSnapshotsVolumeGet(client.Ctx, sourceVolumeId).Execute()
logger.V(2).Info("show snapshots volume", "volume", sourceVolumeId, "snapshot", snapshotId, "http", httpRes.Status)
} else if snapshotId != "" {
response, httpRes, err = client.apiClient.DefaultApi.ShowSnapshotsPatternGet(client.Ctx, snapshotId).Execute()
logger.V(2).Info("show snapshots pattern", "volume", sourceVolumeId, "snapshot", snapshotId, "http", httpRes.Status)
} else {
response, httpRes, err = client.apiClient.DefaultApi.ShowSnapshotsGet(client.Ctx).Execute()
logger.V(2).Info("show snapshots", "volume", sourceVolumeId, "snapshot", snapshotId, "http", httpRes.Status)
}
returnSnapshots := []common.SnapshotObject{}
status := common.ResponseStatus{}
if err == nil && response != nil {
status.ResponseType = response.Status[0].GetResponseType()
status.ResponseTypeNumeric = int(response.Status[0].GetResponseTypeNumeric())
status.Response = response.Status[0].GetResponse()
status.ReturnCode = int(response.Status[0].GetReturnCode())
status.Time = time.Unix(int64(response.Status[0].GetTimeStampNumeric()), 0)
}
// Fill in Snapshot properties for all data objects returned
if err == nil && status.ResponseTypeNumeric == 0 {
for _, s := range response.Snapshots {
snapshot := common.SnapshotObject{}
snapshot.ObjectName = s.GetObjectName()
snapshot.CreationTime, _ = common.CreationTimeFromString(s.GetCreationDateTime())
snapshot.CreationDateTime = s.GetCreationDateTime()
snapshot.CreationDateTimeNumeric = s.GetCreationDateTimeNumeric()
snapshot.MasterVolumeName = s.GetMasterVolumeName()
snapshot.Name = s.GetName()
snapshot.StoragePoolName = s.GetStoragePoolName()
snapshot.TotalSize = s.GetTotalSize()
snapshot.TotalSizeNumeric = s.GetTotalSizeNumeric()
snapshot.VolumeParent = s.GetVolumeParent()
returnSnapshots = append(returnSnapshots, snapshot)
}
}
return returnSnapshots, &status, err
}
// DeleteSnapshot : delete a snapshot
func (client *Client) DeleteSnapshot(name string) (*common.ResponseStatus, error) {
logger := klog.FromContext(client.Ctx)
response, httpRes, err := client.apiClient.DefaultApi.DeleteSnapshotNamesGet(client.Ctx, name).Execute()
logger.V(2).Info("delete snapshot", "name", name, "http", httpRes.Status)
return CreateCommonStatus(logger, &response.Status), err
}