Skip to content

Commit

Permalink
Redis resource (#1485)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @danawillow
  • Loading branch information
modular-magician authored and danawillow committed May 23, 2018
1 parent cac3502 commit 488b9e8
Show file tree
Hide file tree
Showing 6 changed files with 838 additions and 8 deletions.
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

0 comments on commit 488b9e8

Please sign in to comment.