Skip to content

Commit 4fff6d7

Browse files
author
Foivos Filippopoulos
committed
Remove code duplication on function to parse operations
1 parent 69eed39 commit 4fff6d7

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

snapshot/client.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func CreateGCPSnapClient(project, snapPrefix string, zones []string, labels mode
6363
}
6464
}
6565

66-
// Helper function
6766
// In case of a gcp link it returns the target (final part after /)
6867
func formatLinkString(in string) string {
6968

@@ -187,6 +186,21 @@ func (gsc *GCPSnapClient) DeleteSnapshot(snapName string) (string, error) {
187186
return resp.SelfLink, nil
188187
}
189188

189+
func parseOperationOut(operation *compute.Operation) (string, error) {
190+
191+
// Get status (Possible values: "DONE", "PENDING", "RUNNING") and errors
192+
status := operation.Status
193+
if operation.Error != nil {
194+
var err_msgs []string
195+
for _, err := range operation.Error.Errors {
196+
err_msgs = append(err_msgs, err.Message)
197+
}
198+
return status, errors.New(strings.Join(err_msgs, ","))
199+
}
200+
return status, nil
201+
202+
}
203+
190204
func (gsc *GCPSnapClient) GetZonalOperationStatus(operation, zone string) (string, error) {
191205

192206
// Format in case of link
@@ -198,16 +212,7 @@ func (gsc *GCPSnapClient) GetZonalOperationStatus(operation, zone string) (strin
198212
return "", errors.Wrap(err, "error getting zonal operation:")
199213
}
200214

201-
// Get status (Possible values: "DONE", "PENDING", "RUNNING") and errors
202-
status := op.Status
203-
if op.Error != nil {
204-
var err_msgs []string
205-
for _, err := range op.Error.Errors {
206-
err_msgs = append(err_msgs, err.Message)
207-
}
208-
return status, errors.New(strings.Join(err_msgs, ","))
209-
}
210-
return status, nil
215+
return parseOperationOut(op)
211216
}
212217

213218
func (gsc *GCPSnapClient) GetGlobalOperationStatus(operation string) (string, error) {
@@ -220,14 +225,5 @@ func (gsc *GCPSnapClient) GetGlobalOperationStatus(operation string) (string, er
220225
return "", errors.Wrap(err, "error getting global operation:")
221226
}
222227

223-
// Get status (Possible values: "DONE", "PENDING", "RUNNING") and errors
224-
status := op.Status
225-
if op.Error != nil {
226-
var err_msgs []string
227-
for _, err := range op.Error.Errors {
228-
err_msgs = append(err_msgs, err.Message)
229-
}
230-
return status, errors.New(strings.Join(err_msgs, ","))
231-
}
232-
return status, nil
228+
return parseOperationOut(op)
233229
}

0 commit comments

Comments
 (0)