@@ -4,11 +4,12 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- log "github.com/sirupsen/logrus"
8
7
"net/http"
9
8
"strings"
10
9
"time"
11
10
11
+ log "github.com/sirupsen/logrus"
12
+
12
13
"github.com/pkg/errors"
13
14
"golang.org/x/oauth2/google"
14
15
compute "google.golang.org/api/compute/v1"
@@ -33,8 +34,8 @@ type GCPSnapClient struct {
33
34
type GCPSnapClientInterface interface {
34
35
GetDisksFromLabel (label * models.Label ) ([]compute.Disk , error )
35
36
GetDisksFromDescription (label * models.Description ) ([]compute.Disk , error )
36
- ListSnapshots (diskSelfLink string ) ([]compute.Snapshot , error )
37
- ListClientCreatedSnapshots (diskSelfLink string ) ([]compute.Snapshot , error )
37
+ ListSnapshots (diskSelfLink string ) ([]* compute.Snapshot , error )
38
+ ListClientCreatedSnapshots (diskSelfLink string ) ([]* compute.Snapshot , error )
38
39
CreateSnapshot (diskName , zone string ) (string , error )
39
40
DeleteSnapshot (snapName string ) (string , error )
40
41
GetZonalOperationStatus (operation , zone string ) (string , error )
@@ -43,7 +44,6 @@ type GCPSnapClientInterface interface {
43
44
44
45
// Basic Init function for the snapshotter
45
46
func CreateGCPSnapClient (project , snapPrefix string , zones []string ) * GCPSnapClient {
46
-
47
47
ctx := context .Background ()
48
48
googleClient , err := google .DefaultClient (ctx , compute .ComputeScope )
49
49
if err != nil {
@@ -65,7 +65,6 @@ func CreateGCPSnapClient(project, snapPrefix string, zones []string) *GCPSnapCli
65
65
66
66
// In case of a gcp link it returns the target (final part after /)
67
67
func formatLinkString (in string ) string {
68
-
69
68
if strings .ContainsAny (in , "/" ) {
70
69
elems := strings .Split (in , "/" )
71
70
return elems [len (elems )- 1 ]
@@ -75,7 +74,6 @@ func formatLinkString(in string) string {
75
74
76
75
// GetDiskList: Returns a list of disks that contain one of the given labels
77
76
func (gsc * GCPSnapClient ) GetDisksFromLabel (label * models.Label ) ([]compute.Disk , error ) {
78
-
79
77
disks := []compute.Disk {}
80
78
81
79
for _ , zone := range gsc .Zones {
@@ -97,7 +95,6 @@ func (gsc *GCPSnapClient) GetDisksFromLabel(label *models.Label) ([]compute.Disk
97
95
}
98
96
99
97
func (gsc * GCPSnapClient ) GetDisksFromDescription (desc * models.Description ) ([]compute.Disk , error ) {
100
-
101
98
disks := []compute.Disk {}
102
99
103
100
for _ , zone := range gsc .Zones {
@@ -124,42 +121,47 @@ func (gsc *GCPSnapClient) GetDisksFromDescription(desc *models.Description) ([]c
124
121
}
125
122
126
123
// ListSnapshots: Lists Snapshots for a given disk
127
- func (gsc * GCPSnapClient ) ListSnapshots (diskSelfLink string ) ([]compute.Snapshot , error ) {
124
+ func (gsc * GCPSnapClient ) ListSnapshots (diskSelfLink string ) ([]* compute.Snapshot , error ) {
125
+ var snapshots []* compute.Snapshot
128
126
129
- snapshots := []compute. Snapshot {}
127
+ req := gsc . ComputeService . Snapshots . List ( gsc . Project )
130
128
131
- resp , err := gsc . ComputeService . Snapshots . List ( gsc . Project ). Do ()
132
- if err != nil {
133
- return snapshots , errors . Wrap ( err , "error requesting snapshots list:" )
134
- }
135
- for _ , snap := range resp . Items {
136
- // If not created by the snapshotter just ignore
137
- if val , ok := snap . Labels [ SnapshotterLabel ]; ok {
138
- if val != SnapshotterLabelValue {
129
+ err := req . Pages ( context . Background (), func ( page * compute. SnapshotList ) error {
130
+ for _ , snap := range page . Items {
131
+ // If not created by the snapshotter just ignore
132
+ if val , ok := snap . Labels [ SnapshotterLabel ]; ok {
133
+ if val != SnapshotterLabelValue {
134
+ continue
135
+ }
136
+ } else {
139
137
continue
140
138
}
141
- } else {
142
- continue
143
- }
144
139
145
- // If it's a snapshot of the input disk add to the list
146
- if snap .SourceDisk == diskSelfLink {
147
- snapshots = append (snapshots , * snap )
140
+ // Skip if snapshot is not from the requested disk
141
+ if snap .SourceDisk != diskSelfLink {
142
+ continue
143
+ }
144
+
145
+ snapshots = append (snapshots , snap )
148
146
}
147
+ return nil
148
+ })
149
+ if err != nil {
150
+ return nil , errors .Wrap (err , "error requesting snapshots list:" )
149
151
}
152
+
150
153
return snapshots , nil
151
154
}
152
155
153
- // ListClientCreatedSnapshots: Lists snapshots for a given disk that were create by the client,
156
+ // ListClientCreatedSnapshots: Lists snapshots for a given disk that were created by the client,
154
157
// meaning that they have the SnapshotterLabel
155
- func (gsc * GCPSnapClient ) ListClientCreatedSnapshots (diskSelfLink string ) ([]compute.Snapshot , error ) {
156
-
158
+ func (gsc * GCPSnapClient ) ListClientCreatedSnapshots (diskSelfLink string ) ([]* compute.Snapshot , error ) {
157
159
snaps , err := gsc .ListSnapshots (diskSelfLink )
158
160
if err != nil {
159
- return []compute. Snapshot {} , err
161
+ return nil , err
160
162
}
161
163
162
- res := []compute.Snapshot {}
164
+ res := []* compute.Snapshot {}
163
165
for _ , snap := range snaps {
164
166
if val , ok := snap .Labels [SnapshotterLabel ]; ok {
165
167
if SnapshotterLabelValue == val {
@@ -175,7 +177,6 @@ func (gsc *GCPSnapClient) ListClientCreatedSnapshots(diskSelfLink string) ([]com
175
177
// CreateSnapshot: Gets a disk name and a zone, issues a create snapshot command to api
176
178
// and returns a link to the create snapshot operation
177
179
func (gsc * GCPSnapClient ) CreateSnapshot (diskName , zone string ) (string , error ) {
178
-
179
180
// format zone if link
180
181
zn := formatLinkString (zone )
181
182
@@ -205,7 +206,6 @@ func (gsc *GCPSnapClient) CreateSnapshot(diskName, zone string) (string, error)
205
206
206
207
// DeleteSnapshot: Gets a snapshot name and issues a delete. Returns a link to the delete operation
207
208
func (gsc * GCPSnapClient ) DeleteSnapshot (snapName string ) (string , error ) {
208
-
209
209
resp , err := gsc .ComputeService .Snapshots .Delete (gsc .Project , snapName ).Do ()
210
210
if err != nil {
211
211
return "" , errors .Wrap (err , "error deleting snapshot:" )
@@ -215,7 +215,6 @@ func (gsc *GCPSnapClient) DeleteSnapshot(snapName string) (string, error) {
215
215
}
216
216
217
217
func parseOperationOut (operation * compute.Operation ) (string , error ) {
218
-
219
218
// Get status (Possible values: "DONE", "PENDING", "RUNNING") and errors
220
219
status := operation .Status
221
220
if operation .Error != nil {
@@ -226,11 +225,9 @@ func parseOperationOut(operation *compute.Operation) (string, error) {
226
225
return status , errors .New (strings .Join (err_msgs , "," ))
227
226
}
228
227
return status , nil
229
-
230
228
}
231
229
232
230
func (gsc * GCPSnapClient ) GetZonalOperationStatus (operation , zone string ) (string , error ) {
233
-
234
231
// Format in case of link
235
232
operation = formatLinkString (operation )
236
233
zone = formatLinkString (zone )
@@ -244,7 +241,6 @@ func (gsc *GCPSnapClient) GetZonalOperationStatus(operation, zone string) (strin
244
241
}
245
242
246
243
func (gsc * GCPSnapClient ) GetGlobalOperationStatus (operation string ) (string , error ) {
247
-
248
244
// Format in case of link
249
245
operation = formatLinkString (operation )
250
246
0 commit comments