Adding nil guards to the wait operations (#2936)

<!-- This change is generated by MagicModules. -->
/cc @chrisst
This commit is contained in:
The Magician 2019-01-25 13:03:43 -08:00 committed by Chris Stephens
parent d1f1222c01
commit cf489d4909
14 changed files with 101 additions and 10 deletions

View File

@ -18,6 +18,9 @@ type AppEngineOperationWaiter struct {
} }
func (w *AppEngineOperationWaiter) QueryOp() (interface{}, error) { func (w *AppEngineOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name) matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name)
if len(matches) != 2 { if len(matches) != 2 {
return nil, fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name) return nil, fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name)

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
"google.golang.org/api/cloudfunctions/v1" "google.golang.org/api/cloudfunctions/v1"
) )
@ -10,6 +12,9 @@ type CloudFunctionsOperationWaiter struct {
} }
func (w *CloudFunctionsOperationWaiter) QueryOp() (interface{}, error) { func (w *CloudFunctionsOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -50,7 +50,7 @@ func (w *CommonOperationWaiter) State() string {
} }
func (w *CommonOperationWaiter) Error() error { func (w *CommonOperationWaiter) Error() error {
if w.Op.Error != nil { if w != nil && w.Op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", w.Op.Error.Code, w.Op.Error.Message) return fmt.Errorf("Error code %v, message: %s", w.Op.Error.Code, w.Op.Error.Message)
} }
return nil return nil

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
composer "google.golang.org/api/composer/v1beta1" composer "google.golang.org/api/composer/v1beta1"
) )
@ -10,6 +12,9 @@ type ComposerOperationWaiter struct {
} }
func (w *ComposerOperationWaiter) QueryOp() (interface{}, error) { func (w *ComposerOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -16,7 +16,7 @@ type ComputeOperationWaiter struct {
func (w *ComputeOperationWaiter) State() string { func (w *ComputeOperationWaiter) State() string {
if w == nil || w.Op == nil { if w == nil || w.Op == nil {
return fmt.Sprintf("Operation is nil!") return "<nil>"
} }
return w.Op.Status return w.Op.Status
@ -39,6 +39,9 @@ func (w *ComputeOperationWaiter) SetOp(op interface{}) error {
} }
func (w *ComputeOperationWaiter) QueryOp() (interface{}, error) { func (w *ComputeOperationWaiter) QueryOp() (interface{}, error) {
if w == nil || w.Op == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
if w.Op.Zone != "" { if w.Op.Zone != "" {
zone := GetResourceNameFromSelfLink(w.Op.Zone) zone := GetResourceNameFromSelfLink(w.Op.Zone)
return w.Service.ZoneOperations.Get(w.Project, zone, w.Op.Name).Do() return w.Service.ZoneOperations.Get(w.Project, zone, w.Op.Name).Do()

View File

@ -3,7 +3,7 @@ package google
import ( import (
"fmt" "fmt"
"google.golang.org/api/container/v1beta1" container "google.golang.org/api/container/v1beta1"
) )
type ContainerOperationWaiter struct { type ContainerOperationWaiter struct {
@ -14,28 +14,41 @@ type ContainerOperationWaiter struct {
} }
func (w *ContainerOperationWaiter) State() string { func (w *ContainerOperationWaiter) State() string {
if w == nil || w.Op == nil {
return "<nil>"
}
return w.Op.Status return w.Op.Status
} }
func (w *ContainerOperationWaiter) Error() error { func (w *ContainerOperationWaiter) Error() error {
if w.Op.StatusMessage != "" { if w != nil && w.Op != nil {
return fmt.Errorf(w.Op.StatusMessage) return fmt.Errorf(w.Op.StatusMessage)
} }
return nil return nil
} }
func (w *ContainerOperationWaiter) SetOp(op interface{}) error { func (w *ContainerOperationWaiter) SetOp(op interface{}) error {
w.Op = op.(*container.Operation) var ok bool
w.Op, ok = op.(*container.Operation)
if !ok {
return fmt.Errorf("Unable to set operation. Bad type!")
}
return nil return nil
} }
func (w *ContainerOperationWaiter) QueryOp() (interface{}, error) { func (w *ContainerOperationWaiter) QueryOp() (interface{}, error) {
if w == nil || w.Op == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
name := fmt.Sprintf("projects/%s/locations/%s/operations/%s", name := fmt.Sprintf("projects/%s/locations/%s/operations/%s",
w.Project, w.Location, w.Op.Name) w.Project, w.Location, w.Op.Name)
return w.Service.Projects.Locations.Operations.Get(name).Do() return w.Service.Projects.Locations.Operations.Get(name).Do()
} }
func (w *ContainerOperationWaiter) OpName() string { func (w *ContainerOperationWaiter) OpName() string {
if w == nil || w.Op == nil {
return "<nil>"
}
return w.Op.Name return w.Op.Name
} }

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
"google.golang.org/api/dataproc/v1" "google.golang.org/api/dataproc/v1"
) )
@ -10,6 +12,9 @@ type DataprocClusterOperationWaiter struct {
} }
func (w *DataprocClusterOperationWaiter) QueryOp() (interface{}, error) { func (w *DataprocClusterOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Projects.Regions.Operations.Get(w.Op.Name).Do() return w.Service.Projects.Regions.Operations.Get(w.Op.Name).Do()
} }

View File

@ -1,6 +1,7 @@
package google package google
import ( import (
"fmt"
"net/http" "net/http"
"google.golang.org/api/dataproc/v1" "google.golang.org/api/dataproc/v1"
@ -15,6 +16,9 @@ type DataprocJobOperationWaiter struct {
} }
func (w *DataprocJobOperationWaiter) State() string { func (w *DataprocJobOperationWaiter) State() string {
if w == nil {
return "<nil>"
}
return w.Status return w.Status
} }
@ -32,6 +36,9 @@ func (w *DataprocJobOperationWaiter) SetOp(job interface{}) error {
} }
func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) { func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do() job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
if job != nil { if job != nil {
w.Status = job.Status.State w.Status = job.Status.State
@ -40,6 +47,9 @@ func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) {
} }
func (w *DataprocJobOperationWaiter) OpName() string { func (w *DataprocJobOperationWaiter) OpName() string {
if w == nil {
return "<nil>"
}
return w.JobId return w.JobId
} }
@ -74,6 +84,9 @@ func (w *DataprocDeleteJobOperationWaiter) TargetStates() []string {
} }
func (w *DataprocDeleteJobOperationWaiter) QueryOp() (interface{}, error) { func (w *DataprocDeleteJobOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do() job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
if err != nil { if err != nil {
if isGoogleApiErrorWithCode(err, http.StatusNotFound) { if isGoogleApiErrorWithCode(err, http.StatusNotFound) {

View File

@ -1,7 +1,9 @@
package google package google
import ( import (
"google.golang.org/api/redis/v1beta1" "fmt"
redis "google.golang.org/api/redis/v1beta1"
) )
type RedisOperationWaiter struct { type RedisOperationWaiter struct {
@ -10,6 +12,9 @@ type RedisOperationWaiter struct {
} }
func (w *RedisOperationWaiter) QueryOp() (interface{}, error) { func (w *RedisOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
"google.golang.org/api/cloudresourcemanager/v1" "google.golang.org/api/cloudresourcemanager/v1"
resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1" resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
) )
@ -11,6 +13,9 @@ type ResourceManagerOperationWaiter struct {
} }
func (w *ResourceManagerOperationWaiter) QueryOp() (interface{}, error) { func (w *ResourceManagerOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
"google.golang.org/api/googleapi" "google.golang.org/api/googleapi"
"google.golang.org/api/servicemanagement/v1" "google.golang.org/api/servicemanagement/v1"
) )
@ -11,6 +13,9 @@ type ServiceManagementOperationWaiter struct {
} }
func (w *ServiceManagementOperationWaiter) QueryOp() (interface{}, error) { func (w *ServiceManagementOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -1,7 +1,9 @@
package google package google
import ( import (
"google.golang.org/api/serviceusage/v1beta1" "fmt"
serviceusage "google.golang.org/api/serviceusage/v1beta1"
) )
type ServiceUsageOperationWaiter struct { type ServiceUsageOperationWaiter struct {
@ -10,6 +12,9 @@ type ServiceUsageOperationWaiter struct {
} }
func (w *ServiceUsageOperationWaiter) QueryOp() (interface{}, error) { func (w *ServiceUsageOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Operations.Get(w.Op.Name).Do() return w.Service.Operations.Get(w.Op.Name).Do()
} }

View File

@ -1,6 +1,8 @@
package google package google
import ( import (
"fmt"
"google.golang.org/api/spanner/v1" "google.golang.org/api/spanner/v1"
) )
@ -10,6 +12,9 @@ type SpannerInstanceOperationWaiter struct {
} }
func (w *SpannerInstanceOperationWaiter) QueryOp() (interface{}, error) { func (w *SpannerInstanceOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
return w.Service.Projects.Instances.Operations.Get(w.Op.Name).Do() return w.Service.Projects.Instances.Operations.Get(w.Op.Name).Do()
} }

View File

@ -2,8 +2,9 @@ package google
import ( import (
"bytes" "bytes"
"fmt"
"google.golang.org/api/sqladmin/v1beta4" sqladmin "google.golang.org/api/sqladmin/v1beta4"
) )
type SqlAdminOperationWaiter struct { type SqlAdminOperationWaiter struct {
@ -13,26 +14,44 @@ type SqlAdminOperationWaiter struct {
} }
func (w *SqlAdminOperationWaiter) State() string { func (w *SqlAdminOperationWaiter) State() string {
if w == nil || w.Op == nil {
return "Operation is nil!"
}
return w.Op.Status return w.Op.Status
} }
func (w *SqlAdminOperationWaiter) Error() error { func (w *SqlAdminOperationWaiter) Error() error {
if w.Op.Error != nil { if w != nil && w.Op != nil && w.Op.Error != nil {
return SqlAdminOperationError(*w.Op.Error) return SqlAdminOperationError(*w.Op.Error)
} }
return nil return nil
} }
func (w *SqlAdminOperationWaiter) SetOp(op interface{}) error { func (w *SqlAdminOperationWaiter) SetOp(op interface{}) error {
w.Op = op.(*sqladmin.Operation) var ok bool
w.Op, ok = op.(*sqladmin.Operation)
if !ok {
return fmt.Errorf("Unable to set operation. Bad type!")
}
return nil return nil
} }
func (w *SqlAdminOperationWaiter) QueryOp() (interface{}, error) { func (w *SqlAdminOperationWaiter) QueryOp() (interface{}, error) {
if w == nil || w.Op == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
if w.Service == nil {
return nil, fmt.Errorf("Cannot query operation, service is nil.")
}
return w.Service.Operations.Get(w.Project, w.Op.Name).Do() return w.Service.Operations.Get(w.Project, w.Op.Name).Do()
} }
func (w *SqlAdminOperationWaiter) OpName() string { func (w *SqlAdminOperationWaiter) OpName() string {
if w == nil || w.Op == nil {
return "<nil>"
}
return w.Op.Name return w.Op.Name
} }