diff --git a/google/appengine_operation.go b/google/appengine_operation.go index 48288545..ca3eb26d 100644 --- a/google/appengine_operation.go +++ b/google/appengine_operation.go @@ -18,6 +18,9 @@ type AppEngineOperationWaiter struct { } 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) 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) diff --git a/google/cloudfunctions_operation.go b/google/cloudfunctions_operation.go index 71d3d8a8..ba39814c 100644 --- a/google/cloudfunctions_operation.go +++ b/google/cloudfunctions_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "google.golang.org/api/cloudfunctions/v1" ) @@ -10,6 +12,9 @@ type CloudFunctionsOperationWaiter struct { } 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() } diff --git a/google/common_operation.go b/google/common_operation.go index e595839e..31ad8a16 100644 --- a/google/common_operation.go +++ b/google/common_operation.go @@ -50,7 +50,7 @@ func (w *CommonOperationWaiter) State() string { } 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 nil diff --git a/google/composer_operation.go b/google/composer_operation.go index 44429809..ce222858 100644 --- a/google/composer_operation.go +++ b/google/composer_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + composer "google.golang.org/api/composer/v1beta1" ) @@ -10,6 +12,9 @@ type ComposerOperationWaiter struct { } 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() } diff --git a/google/compute_operation.go b/google/compute_operation.go index b8540381..992d5961 100644 --- a/google/compute_operation.go +++ b/google/compute_operation.go @@ -16,7 +16,7 @@ type ComputeOperationWaiter struct { func (w *ComputeOperationWaiter) State() string { if w == nil || w.Op == nil { - return fmt.Sprintf("Operation is nil!") + return "" } return w.Op.Status @@ -39,6 +39,9 @@ func (w *ComputeOperationWaiter) SetOp(op 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 != "" { zone := GetResourceNameFromSelfLink(w.Op.Zone) return w.Service.ZoneOperations.Get(w.Project, zone, w.Op.Name).Do() diff --git a/google/container_operation.go b/google/container_operation.go index 82c2933f..188cf884 100644 --- a/google/container_operation.go +++ b/google/container_operation.go @@ -3,7 +3,7 @@ package google import ( "fmt" - "google.golang.org/api/container/v1beta1" + container "google.golang.org/api/container/v1beta1" ) type ContainerOperationWaiter struct { @@ -14,28 +14,41 @@ type ContainerOperationWaiter struct { } func (w *ContainerOperationWaiter) State() string { + if w == nil || w.Op == nil { + return "" + } return w.Op.Status } func (w *ContainerOperationWaiter) Error() error { - if w.Op.StatusMessage != "" { + if w != nil && w.Op != nil { return fmt.Errorf(w.Op.StatusMessage) } return nil } 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 } 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", w.Project, w.Location, w.Op.Name) return w.Service.Projects.Locations.Operations.Get(name).Do() } func (w *ContainerOperationWaiter) OpName() string { + if w == nil || w.Op == nil { + return "" + } return w.Op.Name } diff --git a/google/dataproc_cluster_operation.go b/google/dataproc_cluster_operation.go index 5172a347..0cbd32c9 100644 --- a/google/dataproc_cluster_operation.go +++ b/google/dataproc_cluster_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "google.golang.org/api/dataproc/v1" ) @@ -10,6 +12,9 @@ type DataprocClusterOperationWaiter struct { } 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() } diff --git a/google/dataproc_job_operation.go b/google/dataproc_job_operation.go index 6e1c56e1..ce3cab01 100644 --- a/google/dataproc_job_operation.go +++ b/google/dataproc_job_operation.go @@ -1,6 +1,7 @@ package google import ( + "fmt" "net/http" "google.golang.org/api/dataproc/v1" @@ -15,6 +16,9 @@ type DataprocJobOperationWaiter struct { } func (w *DataprocJobOperationWaiter) State() string { + if w == nil { + return "" + } return w.Status } @@ -32,6 +36,9 @@ func (w *DataprocJobOperationWaiter) SetOp(job 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() if job != nil { w.Status = job.Status.State @@ -40,6 +47,9 @@ func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) { } func (w *DataprocJobOperationWaiter) OpName() string { + if w == nil { + return "" + } return w.JobId } @@ -74,6 +84,9 @@ func (w *DataprocDeleteJobOperationWaiter) TargetStates() []string { } 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() if err != nil { if isGoogleApiErrorWithCode(err, http.StatusNotFound) { diff --git a/google/redis_operation.go b/google/redis_operation.go index b907cf5c..14ee7cd2 100644 --- a/google/redis_operation.go +++ b/google/redis_operation.go @@ -1,7 +1,9 @@ package google import ( - "google.golang.org/api/redis/v1beta1" + "fmt" + + redis "google.golang.org/api/redis/v1beta1" ) type RedisOperationWaiter struct { @@ -10,6 +12,9 @@ type RedisOperationWaiter struct { } 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() } diff --git a/google/resourcemanager_operation.go b/google/resourcemanager_operation.go index 5df24935..55444041 100644 --- a/google/resourcemanager_operation.go +++ b/google/resourcemanager_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "google.golang.org/api/cloudresourcemanager/v1" resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1" ) @@ -11,6 +13,9 @@ type ResourceManagerOperationWaiter struct { } 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() } diff --git a/google/serviceman_operation.go b/google/serviceman_operation.go index 3f87ae31..078a8913 100644 --- a/google/serviceman_operation.go +++ b/google/serviceman_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "google.golang.org/api/googleapi" "google.golang.org/api/servicemanagement/v1" ) @@ -11,6 +13,9 @@ type ServiceManagementOperationWaiter struct { } 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() } diff --git a/google/serviceusage_operation.go b/google/serviceusage_operation.go index bb8e90ab..e595fd4c 100644 --- a/google/serviceusage_operation.go +++ b/google/serviceusage_operation.go @@ -1,7 +1,9 @@ package google import ( - "google.golang.org/api/serviceusage/v1beta1" + "fmt" + + serviceusage "google.golang.org/api/serviceusage/v1beta1" ) type ServiceUsageOperationWaiter struct { @@ -10,6 +12,9 @@ type ServiceUsageOperationWaiter struct { } 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() } diff --git a/google/spanner_instance_operation.go b/google/spanner_instance_operation.go index 2831cc48..d90dd50a 100644 --- a/google/spanner_instance_operation.go +++ b/google/spanner_instance_operation.go @@ -1,6 +1,8 @@ package google import ( + "fmt" + "google.golang.org/api/spanner/v1" ) @@ -10,6 +12,9 @@ type SpannerInstanceOperationWaiter struct { } 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() } diff --git a/google/sqladmin_operation.go b/google/sqladmin_operation.go index 635a6eb2..fe9c2bd4 100644 --- a/google/sqladmin_operation.go +++ b/google/sqladmin_operation.go @@ -2,8 +2,9 @@ package google import ( "bytes" + "fmt" - "google.golang.org/api/sqladmin/v1beta4" + sqladmin "google.golang.org/api/sqladmin/v1beta4" ) type SqlAdminOperationWaiter struct { @@ -13,26 +14,44 @@ type SqlAdminOperationWaiter struct { } func (w *SqlAdminOperationWaiter) State() string { + if w == nil || w.Op == nil { + return "Operation is nil!" + } + return w.Op.Status } 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 nil } 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 } 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() } func (w *SqlAdminOperationWaiter) OpName() string { + if w == nil || w.Op == nil { + return "" + } return w.Op.Name }