|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + "google.golang.org/api/redis/v1beta1" |
| 10 | +) |
| 11 | + |
| 12 | +type RedisOperationWaiter struct { |
| 13 | + Service *redis.ProjectsLocationsService |
| 14 | + Op *redis.Operation |
| 15 | +} |
| 16 | + |
| 17 | +func (w *RedisOperationWaiter) RefreshFunc() resource.StateRefreshFunc { |
| 18 | + return func() (interface{}, string, error) { |
| 19 | + op, err := w.Service.Operations.Get(w.Op.Name).Do() |
| 20 | + |
| 21 | + if err != nil { |
| 22 | + return nil, "", err |
| 23 | + } |
| 24 | + |
| 25 | + log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name) |
| 26 | + |
| 27 | + return op, fmt.Sprint(op.Done), nil |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (w *RedisOperationWaiter) Conf() *resource.StateChangeConf { |
| 32 | + return &resource.StateChangeConf{ |
| 33 | + Pending: []string{"false"}, |
| 34 | + Target: []string{"true"}, |
| 35 | + Refresh: w.RefreshFunc(), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func redisOperationWait(service *redis.Service, op *redis.Operation, project, activity string) error { |
| 40 | + return redisOperationWaitTime(service, op, project, activity, 4) |
| 41 | +} |
| 42 | + |
| 43 | +func redisOperationWaitTime(service *redis.Service, op *redis.Operation, project, activity string, timeoutMin int) error { |
| 44 | + w := &RedisOperationWaiter{ |
| 45 | + Service: service.Projects.Locations, |
| 46 | + Op: op, |
| 47 | + } |
| 48 | + |
| 49 | + state := w.Conf() |
| 50 | + state.Delay = 10 * time.Second |
| 51 | + state.Timeout = time.Duration(timeoutMin) * time.Minute |
| 52 | + state.MinTimeout = 2 * time.Second |
| 53 | + opRaw, err := state.WaitForState() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("Error waiting for %s: %s", activity, err) |
| 56 | + } |
| 57 | + |
| 58 | + op = opRaw.(*redis.Operation) |
| 59 | + if op.Error != nil { |
| 60 | + return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message) |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
0 commit comments