-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.go
106 lines (95 loc) · 2.75 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
package sdk
import (
"encoding/json"
"fmt"
"time"
)
type Snapshots struct {
Snapshot Snapshot `json:"snapshots"`
}
type Snapshot struct {
Pagination struct {
Size int `json:"size"`
TotalElements int `json:"totalElements"`
TotalPages int `json:"totalPages"`
Page int `json:"page"`
} `json:"_pagination"`
Data []struct {
TenantId string `json:"tenantId"`
CustomerId string `json:"customerId"`
SnapshotId string `json:"snapshotId"`
Name string `json:"name"`
Description string `json:"description"`
InstanceId int `json:"instanceId"`
CreatedDate time.Time `json:"createdDate"`
AutoDeleteDate time.Time `json:"autoDeleteDate"`
ImageId string `json:"imageId"`
ImageName string `json:"imageName"`
} `json:"data"`
Links struct {
First string `json:"first"`
Next string `json:"next"`
Self string `json:"self"`
Previous string `json:"previous"`
Last string `json:"last"`
} `json:"_links"`
}
type CreateSnapShopRes struct {
Data []struct {
TenantId string `json:"tenantId"`
CustomerId string `json:"customerId"`
SnapshotId string `json:"snapshotId"`
ImageId string `json:"imageId"`
ImageName string `json:"imageName"`
} `json:"data"`
Links struct {
Self string `json:"self"`
} `json:"_links"`
}
func (s *Snapshots) GetInstanceSnapshots(instanceId int) (*Snapshot, error) {
url := fmt.Sprintf("%s/%d/snapshots", ComputeInstancesUrl, instanceId)
res, _ := Do(GET, URL(url), nil)
var snapshots Snapshot
err := json.Unmarshal(res, &snapshots)
if err != nil {
return nil, err
}
return &snapshots, nil
}
func (s *Snapshots) GetSnapshot(instanceId int, snapshotId string) (*Snapshot, error) {
url := fmt.Sprintf("%s/%d/snapshots/%s", ComputeInstancesUrl, instanceId, snapshotId)
res, _ := Do(GET, URL(url), nil)
var snapshots Snapshot
err := json.Unmarshal(res, &snapshots)
if err != nil {
return nil, err
}
return &snapshots, nil
}
func (s *Snapshots) CreateSnapshot(instanceId int, name, description string) (*CreateSnapShopRes, error) {
type Payload struct {
Name string `json:"name"`
Description string `json:"description"`
}
data := Payload{
Name: name,
Description: description,
}
url := fmt.Sprintf("%s/%d/snapshots", ComputeInstancesUrl, instanceId)
fmt.Println(url)
res, _ := Do(POST, URL(url), data)
var snapshots CreateSnapShopRes
err := json.Unmarshal(res, &snapshots)
if err != nil {
return nil, err
}
return &snapshots, nil
}
func (s *Snapshots) DeleteSnapshot(instanceId int, snapshotId string) error {
url := fmt.Sprintf("%s/%d/snapshots/%s", ComputeInstancesUrl, instanceId, snapshotId)
_, err := Do(DELETE, URL(url), nil)
if err != nil {
return err
}
return nil
}