Add additional fingerprint error to check for when updating metadata (#221)

This commit is contained in:
Joe Selman 2017-07-19 15:33:07 -07:00 committed by GitHub
parent adaae73e8f
commit 4c8f62edf6

View File

@ -3,12 +3,14 @@ package google
import (
"fmt"
"log"
"strings"
"google.golang.org/api/compute/v1"
)
const FINGERPRINT_RETRIES = 10
const FINGERPRINT_FAIL = "Invalid fingerprint."
var FINGERPRINT_FAIL_ERRORS = []string{"Invalid fingerprint.", "Supplied fingerprint does not match current metadata fingerprint."}
// Since the google compute API uses optimistic locking, there is a chance
// we need to resubmit our updated metadata. To do this, you need to provide
@ -17,11 +19,25 @@ func MetadataRetryWrapper(update func() error) error {
attempt := 0
for attempt < FINGERPRINT_RETRIES {
err := update()
if err != nil && err.Error() == FINGERPRINT_FAIL {
attempt++
} else {
if err == nil {
return nil
}
// Check to see if the error matches any of our fingerprint-related failure messages
var fingerprintError bool
for _, msg := range FINGERPRINT_FAIL_ERRORS {
if strings.Contains(err.Error(), msg) {
fingerprintError = true
break
}
}
if !fingerprintError {
// Something else went wrong, don't retry
return err
}
attempt++
}
return fmt.Errorf("Failed to update metadata after %d retries", attempt)