Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis resource #1485

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion google/provider_redis_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ package google

import "github.com/hashicorp/terraform/helper/schema"

var GeneratedRedisResourcesMap = map[string]*schema.Resource{}
var GeneratedRedisResourcesMap = map[string]*schema.Resource{
"google_redis_instance": resourceRedisInstance(),
}
64 changes: 64 additions & 0 deletions google/redis_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package google

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/redis/v1beta1"
)

type RedisOperationWaiter struct {
Service *redis.ProjectsLocationsService
Op *redis.Operation
}

func (w *RedisOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
op, err := w.Service.Operations.Get(w.Op.Name).Do()

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

log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name)

return op, fmt.Sprint(op.Done), nil
}
}

func (w *RedisOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: w.RefreshFunc(),
}
}

func redisOperationWait(service *redis.Service, op *redis.Operation, project, activity string) error {
return redisOperationWaitTime(service, op, project, activity, 4)
}

func redisOperationWaitTime(service *redis.Service, op *redis.Operation, project, activity string, timeoutMin int) error {
w := &RedisOperationWaiter{
Service: service.Projects.Locations,
Op: op,
}

state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = time.Duration(timeoutMin) * time.Minute
state.MinTimeout = 2 * time.Second
opRaw, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}

op = opRaw.(*redis.Operation)
if op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
}

return nil
}
Loading