-
Notifications
You must be signed in to change notification settings - Fork 6
/
snapshot.go
130 lines (115 loc) · 4.4 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
package resources
import (
"context"
"github.com/ionos-cloud/ionosctl/pkg/config"
"github.com/fatih/structs"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
)
type Snapshot struct {
ionoscloud.Snapshot
}
type Snapshots struct {
ionoscloud.Snapshots
}
type SnapshotProperties struct {
ionoscloud.SnapshotProperties
}
// SnapshotsService is a wrapper around ionoscloud.Snapshot
type SnapshotsService interface {
List(params ListQueryParams) (Snapshots, *Response, error)
Get(snapshotId string, params QueryParams) (*Snapshot, *Response, error)
Create(datacenterId, volumeId, name, description, licenceType string, secAuthProtection bool, params QueryParams) (*Snapshot, *Response, error)
Update(snapshotId string, snapshotProp SnapshotProperties, params QueryParams) (*Snapshot, *Response, error)
Restore(datacenterId, volumeId, snapshotId string, params QueryParams) (*Response, error)
Delete(snapshotId string, params QueryParams) (*Response, error)
}
type snapshotsService struct {
client *ionoscloud.APIClient
context context.Context
}
var _ SnapshotsService = &snapshotsService{}
func NewSnapshotService(client *config.Client, ctx context.Context) SnapshotsService {
return &snapshotsService{
client: client.CloudClient,
context: ctx,
}
}
func (s *snapshotsService) List(params ListQueryParams) (Snapshots, *Response, error) {
req := s.client.SnapshotsApi.SnapshotsGet(s.context)
if !structs.IsZero(params) {
if params.Filters != nil {
for k, v := range *params.Filters {
req = req.Filter(k, v)
}
}
if params.OrderBy != nil {
req = req.OrderBy(*params.OrderBy)
}
if params.MaxResults != nil {
req = req.MaxResults(*params.MaxResults)
}
if !structs.IsZero(params.QueryParams) {
if params.QueryParams.Depth != nil {
req = req.Depth(*params.QueryParams.Depth)
}
if params.QueryParams.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.QueryParams.Pretty)
}
}
}
snapshots, resp, err := s.client.SnapshotsApi.SnapshotsGetExecute(req)
return Snapshots{snapshots}, &Response{*resp}, err
}
func (s *snapshotsService) Get(snapshotId string, params QueryParams) (*Snapshot, *Response, error) {
req := s.client.SnapshotsApi.SnapshotsFindById(s.context, snapshotId)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
snapshot, resp, err := s.client.SnapshotsApi.SnapshotsFindByIdExecute(req)
return &Snapshot{snapshot}, &Response{*resp}, err
}
func (s *snapshotsService) Create(datacenterId, volumeId, name, description, licenceType string, secAuthProtection bool, params QueryParams) (*Snapshot, *Response, error) {
req := s.client.VolumesApi.DatacentersVolumesCreateSnapshotPost(s.context, datacenterId, volumeId).Name(name).Description(description).LicenceType(licenceType).SecAuthProtection(secAuthProtection)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
snapshot, resp, err := s.client.VolumesApi.DatacentersVolumesCreateSnapshotPostExecute(req)
return &Snapshot{snapshot}, &Response{*resp}, err
}
func (s *snapshotsService) Update(snapshotId string, snapshotProp SnapshotProperties, params QueryParams) (*Snapshot, *Response, error) {
req := s.client.SnapshotsApi.SnapshotsPatch(s.context, snapshotId).Snapshot(snapshotProp.SnapshotProperties)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
snapshot, resp, err := s.client.SnapshotsApi.SnapshotsPatchExecute(req)
return &Snapshot{snapshot}, &Response{*resp}, err
}
func (s *snapshotsService) Restore(datacenterId, volumeId, snapshotId string, params QueryParams) (*Response, error) {
req := s.client.VolumesApi.DatacentersVolumesRestoreSnapshotPost(s.context, datacenterId, volumeId).SnapshotId(snapshotId)
resp, err := s.client.VolumesApi.DatacentersVolumesRestoreSnapshotPostExecute(req)
return &Response{*resp}, err
}
func (s *snapshotsService) Delete(snapshotId string, params QueryParams) (*Response, error) {
req := s.client.SnapshotsApi.SnapshotsDelete(s.context, snapshotId)
resp, err := s.client.SnapshotsApi.SnapshotsDeleteExecute(req)
return &Response{*resp}, err
}