Skip to content

Commit

Permalink
Add support for regional GKE clusters in google_container_cluster:
Browse files Browse the repository at this point in the history
* implement operation wait for v1beta1 api
* implement container clusters get for regional clusters
* implement container clusters delete for regional cluster
* implement container clusters update for regional cluster
* TestAccContainerCluster_regional test is now successful
* 'TestAccContainerCluster' tests however crash.
  • Loading branch information
Ashish Amarnath authored and ashish-amarnath committed Mar 23, 2018
1 parent f3fdb2b commit 6f748f7
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 147 deletions.
60 changes: 50 additions & 10 deletions google/container_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ type ContainerOperationWaiter struct {
Zone string
}

type ContainerBetaOperationWaiter struct {
Service *containerBeta.Service
Op *containerBeta.Operation
Project string
Location string
}

func (w *ContainerBetaOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"PENDING", "RUNNING"},
Target: []string{"DONE"},
Refresh: w.RefreshFunc(),
}
}

func (w *ContainerBetaOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
name := fmt.Sprintf("projects/%s/locations/%s/operations/%s",
w.Project, w.Location, w.Op.Name)
resp, err := w.Service.Projects.Locations.Operations.Get(name).Do()

if err != nil {
return nil, "", err
}

if resp.StatusMessage != "" {
return resp, resp.Status, fmt.Errorf(resp.StatusMessage)
}

log.Printf("[DEBUG] Progress of operation %q: %q", w.Op.Name, resp.Status)

return resp, resp.Status, err
}
}

func (w *ContainerOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"PENDING", "RUNNING"},
Expand Down Expand Up @@ -53,36 +88,41 @@ func containerOperationWait(config *Config, op *container.Operation, project, zo
}

state := w.Conf()
return waitForState(state, activity, timeoutMinutes, minTimeoutSeconds)
}

func waitForState(state *resource.StateChangeConf, activity string, timeoutMinutes, minTimeoutSeconds int) error {
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
state.MinTimeout = time.Duration(minTimeoutSeconds) * time.Second
_, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}

return nil
}

func containerBetaOperationWait(config *Config, op *containerBeta.Operation, project, zone, activity string, timeoutMinutes, minTimeoutSeconds int) error {
opV1 := &container.Operation{}
err := Convert(op, opV1)
if err != nil {
return err
func containerBetaOperationWait(config *Config, op *containerBeta.Operation, project, location, activity string, timeoutMinutes, minTimeoutSeconds int) error {
w := &ContainerBetaOperationWaiter{
Service: config.clientContainerBeta,
Op: op,
Project: project,
Location: location,
}

return containerOperationWait(config, opV1, project, zone, activity, timeoutMinutes, minTimeoutSeconds)
state := w.Conf()
return waitForState(state, activity, timeoutMinutes, minTimeoutSeconds)
}

func containerSharedOperationWait(config *Config, op interface{}, project, zone, activity string, timeoutMinutes, minTimeoutSeconds int) error {
func containerSharedOperationWait(config *Config, op interface{}, project, location, activity string, timeoutMinutes, minTimeoutSeconds int) error {
if op == nil {
panic("Attempted to wait on an Operation that was nil.")
}

switch op.(type) {
case *container.Operation:
return containerOperationWait(config, op.(*container.Operation), project, zone, activity, timeoutMinutes, minTimeoutSeconds)
return containerOperationWait(config, op.(*container.Operation), project, location, activity, timeoutMinutes, minTimeoutSeconds)
case *containerBeta.Operation:
return containerBetaOperationWait(config, op.(*containerBeta.Operation), project, zone, activity, timeoutMinutes, minTimeoutSeconds)
return containerBetaOperationWait(config, op.(*containerBeta.Operation), project, location, activity, timeoutMinutes, minTimeoutSeconds)
default:
panic("Attempted to wait on an Operation of unknown type.")
}
Expand Down
Loading

0 comments on commit 6f748f7

Please sign in to comment.