Skip to content

Commit

Permalink
Adding nil guards to the wait operations (#383)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @chrisst
  • Loading branch information
modular-magician authored and chrisst committed Jan 25, 2019
1 parent 33d8970 commit 664824b
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 10 deletions.
3 changes: 3 additions & 0 deletions google-beta/appengine_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions google-beta/cloudfunctions_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

"google.golang.org/api/cloudfunctions/v1"
)

Expand All @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion google-beta/common_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions google-beta/composer_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

composer "google.golang.org/api/composer/v1beta1"
)

Expand All @@ -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()
}

Expand Down
5 changes: 4 additions & 1 deletion google-beta/compute_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<nil>"
}

return w.Op.Status
Expand All @@ -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()
Expand Down
19 changes: 16 additions & 3 deletions google-beta/container_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package google
import (
"fmt"

"google.golang.org/api/container/v1beta1"
container "google.golang.org/api/container/v1beta1"
)

type ContainerOperationWaiter struct {
Expand All @@ -14,28 +14,41 @@ type ContainerOperationWaiter struct {
}

func (w *ContainerOperationWaiter) State() string {
if w == nil || w.Op == nil {
return "<nil>"
}
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 "<nil>"
}
return w.Op.Name
}

Expand Down
5 changes: 5 additions & 0 deletions google-beta/dataproc_cluster_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

"google.golang.org/api/dataproc/v1"
)

Expand All @@ -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()
}

Expand Down
13 changes: 13 additions & 0 deletions google-beta/dataproc_job_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"fmt"
"net/http"

"google.golang.org/api/dataproc/v1"
Expand All @@ -15,6 +16,9 @@ type DataprocJobOperationWaiter struct {
}

func (w *DataprocJobOperationWaiter) State() string {
if w == nil {
return "<nil>"
}
return w.Status
}

Expand All @@ -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
Expand All @@ -40,6 +47,9 @@ func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) {
}

func (w *DataprocJobOperationWaiter) OpName() string {
if w == nil {
return "<nil>"
}
return w.JobId
}

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion google-beta/redis_operation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package google

import (
"google.golang.org/api/redis/v1beta1"
"fmt"

redis "google.golang.org/api/redis/v1beta1"
)

type RedisOperationWaiter struct {
Expand All @@ -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()
}

Expand Down
5 changes: 5 additions & 0 deletions google-beta/resourcemanager_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

"google.golang.org/api/cloudresourcemanager/v1"
resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
)
Expand All @@ -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()
}

Expand Down
5 changes: 5 additions & 0 deletions google-beta/serviceman_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

"google.golang.org/api/googleapi"
"google.golang.org/api/servicemanagement/v1"
)
Expand All @@ -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()
}

Expand Down
7 changes: 6 additions & 1 deletion google-beta/serviceusage_operation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package google

import (
"google.golang.org/api/serviceusage/v1beta1"
"fmt"

serviceusage "google.golang.org/api/serviceusage/v1beta1"
)

type ServiceUsageOperationWaiter struct {
Expand All @@ -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()
}

Expand Down
5 changes: 5 additions & 0 deletions google-beta/spanner_instance_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package google

import (
"fmt"

"google.golang.org/api/spanner/v1"
)

Expand All @@ -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()
}

Expand Down
25 changes: 22 additions & 3 deletions google-beta/sqladmin_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 "<nil>"
}
return w.Op.Name
}

Expand Down

0 comments on commit 664824b

Please sign in to comment.