Catch bad state in common and compute operations (#2931)

<!-- This change is generated by MagicModules. -->
/cc @chrisst
This commit is contained in:
The Magician 2019-01-25 09:29:24 -08:00 committed by Chris Stephens
parent 6779f4dbf4
commit b2bcc2f255
2 changed files with 31 additions and 5 deletions

View File

@ -42,6 +42,10 @@ type CommonOperationWaiter struct {
} }
func (w *CommonOperationWaiter) State() string { func (w *CommonOperationWaiter) State() string {
if w == nil {
return fmt.Sprintf("Operation is nil!")
}
return fmt.Sprintf("done: %v", w.Op.Done) return fmt.Sprintf("done: %v", w.Op.Done)
} }
@ -60,6 +64,10 @@ func (w *CommonOperationWaiter) SetOp(op interface{}) error {
} }
func (w *CommonOperationWaiter) OpName() string { func (w *CommonOperationWaiter) OpName() string {
if w == nil {
return "<nil>"
}
return w.Op.Name return w.Op.Name
} }
@ -86,14 +94,19 @@ func CommonRefreshFunc(w Waiter) resource.StateRefreshFunc {
op, err := w.QueryOp() op, err := w.QueryOp()
// If we got a non-retryable error, return it. // If we got a non-retryable error, return it.
if err != nil && !isRetryableError(err) { if err != nil {
return nil, "", err if !isRetryableError(err) {
return nil, "", fmt.Errorf("Not retriable error: %s", err)
}
if op == nil {
return nil, "", fmt.Errorf("Cannot continue, Operation is nil. %s", err)
}
} }
// Try to set the operation (so we can check it's Error/State), // Try to set the operation (so we can check it's Error/State),
// and fail if we can't. // and fail if we can't.
if err = w.SetOp(op); err != nil { if err = w.SetOp(op); err != nil {
return nil, "", err return nil, "", fmt.Errorf("Cannot continue %s", err)
} }
// Fail if the operation object contains an error. // Fail if the operation object contains an error.

View File

@ -2,6 +2,7 @@ package google
import ( import (
"bytes" "bytes"
"fmt"
computeBeta "google.golang.org/api/compute/v0.beta" computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1" "google.golang.org/api/compute/v1"
@ -14,18 +15,26 @@ type ComputeOperationWaiter struct {
} }
func (w *ComputeOperationWaiter) State() string { func (w *ComputeOperationWaiter) State() string {
if w == nil || w.Op == nil {
return fmt.Sprintf("Operation is nil!")
}
return w.Op.Status return w.Op.Status
} }
func (w *ComputeOperationWaiter) Error() error { func (w *ComputeOperationWaiter) Error() error {
if w.Op.Error != nil { if w != nil && w.Op != nil && w.Op.Error != nil {
return ComputeOperationError(*w.Op.Error) return ComputeOperationError(*w.Op.Error)
} }
return nil return nil
} }
func (w *ComputeOperationWaiter) SetOp(op interface{}) error { func (w *ComputeOperationWaiter) SetOp(op interface{}) error {
w.Op = op.(*compute.Operation) var ok bool
w.Op, ok = op.(*compute.Operation)
if !ok {
return fmt.Errorf("Unable to set operation. Bad type!")
}
return nil return nil
} }
@ -41,6 +50,10 @@ func (w *ComputeOperationWaiter) QueryOp() (interface{}, error) {
} }
func (w *ComputeOperationWaiter) OpName() string { func (w *ComputeOperationWaiter) OpName() string {
if w == nil || w.Op == nil {
return "<nil> Compute Op"
}
return w.Op.Name return w.Op.Name
} }