This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots.go
201 lines (151 loc) · 4.22 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
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
package director
import (
"fmt"
"strconv"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Snapshot struct {
Job string
Index *int
CID string
CreatedAt time.Time
Clean bool
}
type SnapshotResp struct {
Job string
Index *int
SnapshotCID string `json:"snapshot_cid"`
CreatedAt string `json:"created_at"`
Clean bool
}
func (s Snapshot) InstanceDesc() string {
jobDesc := "?"
indexDesc := "?"
if len(s.Job) > 0 {
jobDesc = s.Job
}
if s.Index != nil {
indexDesc = strconv.Itoa(*s.Index)
}
return fmt.Sprintf("%s/%s", jobDesc, indexDesc)
}
func (d DeploymentImpl) Snapshots() ([]Snapshot, error) {
var snaps []Snapshot
resps, err := d.client.Snapshots(d.name)
if err != nil {
return snaps, err
}
for _, r := range resps {
createdAt, err := TimeParser{}.Parse(r.CreatedAt)
if err != nil {
return snaps, bosherr.WrapErrorf(err, "Converting created at '%s' to time", r.CreatedAt)
}
snap := Snapshot{
Job: r.Job,
Index: r.Index,
CID: r.SnapshotCID,
CreatedAt: createdAt.UTC(),
Clean: r.Clean,
}
snaps = append(snaps, snap)
}
return snaps, nil
}
func (d DeploymentImpl) TakeSnapshot(slug InstanceSlug) error {
return d.client.TakeSnapshot(d.name, slug.Name(), slug.IndexOrID())
}
func (d DeploymentImpl) DeleteSnapshot(cid string) error {
err := d.client.DeleteSnapshot(d.name, cid)
if err != nil {
resps, listErr := d.client.Snapshots(d.name)
if listErr != nil {
return err
}
for _, resp := range resps {
if resp.SnapshotCID == cid {
return err
}
}
}
return nil
}
func (d DeploymentImpl) TakeSnapshots() error {
return d.client.TakeSnapshots(d.name)
}
func (d DeploymentImpl) DeleteSnapshots() error {
return d.client.DeleteSnapshots(d.name)
}
func (c Client) Snapshots(deploymentName string) ([]SnapshotResp, error) {
var snaps []SnapshotResp
if len(deploymentName) == 0 {
return snaps, bosherr.Error("Expected non-empty deployment name")
}
path := fmt.Sprintf("/deployments/%s/snapshots", deploymentName)
// [{"job":"first","index":0,"snapshot_cid":"snap-7b1f0b00",
// "created_at":"2015-10-03 18:02:09 +0000","clean":false}]
err := c.clientRequest.Get(path, &snaps)
if err != nil {
return snaps, bosherr.WrapErrorf(err, "Finding snapshots")
}
return snaps, nil
}
func (c Client) TakeSnapshot(deploymentName, job, indexOrID string) error {
if len(deploymentName) == 0 {
return bosherr.Error("Expected non-empty deployment name")
}
if len(job) == 0 {
return bosherr.Error("Expected non-empty job name")
}
if len(indexOrID) == 0 {
return bosherr.Error("Expected non-empty index or ID")
}
path := fmt.Sprintf("/deployments/%s/jobs/%s/%s/snapshots",
deploymentName, job, indexOrID)
_, err := c.taskClientRequest.PostResult(path, nil, nil)
if err != nil {
return bosherr.WrapErrorf(
err, "Taking snapshot for instance '%s/%s' in deployment '%s'",
job, indexOrID, deploymentName)
}
return nil
}
func (c Client) DeleteSnapshot(deploymentName, cid string) error {
if len(deploymentName) == 0 {
return bosherr.Error("Expected non-empty deployment name")
}
if len(cid) == 0 {
return bosherr.Error("Expected non-empty snapshot CID")
}
path := fmt.Sprintf("/deployments/%s/snapshots/%s", deploymentName, cid)
_, err := c.taskClientRequest.DeleteResult(path)
if err != nil {
return bosherr.WrapErrorf(
err, "Deleting snapshot '%s' from deployment '%s'", cid, deploymentName)
}
return nil
}
func (c Client) TakeSnapshots(deploymentName string) error {
if len(deploymentName) == 0 {
return bosherr.Error("Expected non-empty deployment name")
}
path := fmt.Sprintf("/deployments/%s/snapshots", deploymentName)
_, err := c.taskClientRequest.PostResult(path, nil, nil)
if err != nil {
return bosherr.WrapErrorf(
err, "Taking snapshots for deployment '%s'", deploymentName)
}
return nil
}
func (c Client) DeleteSnapshots(deploymentName string) error {
if len(deploymentName) == 0 {
return bosherr.Error("Expected non-empty deployment name")
}
path := fmt.Sprintf("/deployments/%s/snapshots", deploymentName)
_, err := c.taskClientRequest.DeleteResult(path)
if err != nil {
return bosherr.WrapErrorf(
err, "Deleting snapshots for deployment '%s'", deploymentName)
}
return nil
}