|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + |
| 11 | + computeBeta "google.golang.org/api/compute/v0.beta" |
| 12 | +) |
| 13 | + |
| 14 | +// OperationBetaWaitType is an enum specifying what type of operation |
| 15 | +// we're waiting on from the beta API. |
| 16 | +type ComputeBetaOperationWaitType byte |
| 17 | + |
| 18 | +const ( |
| 19 | + ComputeBetaOperationWaitInvalid ComputeBetaOperationWaitType = iota |
| 20 | + ComputeBetaOperationWaitGlobal |
| 21 | + ComputeBetaOperationWaitRegion |
| 22 | + ComputeBetaOperationWaitZone |
| 23 | +) |
| 24 | + |
| 25 | +type ComputeBetaOperationWaiter struct { |
| 26 | + Service *computeBeta.Service |
| 27 | + Op *computeBeta.Operation |
| 28 | + Project string |
| 29 | + Region string |
| 30 | + Type ComputeBetaOperationWaitType |
| 31 | + Zone string |
| 32 | +} |
| 33 | + |
| 34 | +func (w *ComputeBetaOperationWaiter) RefreshFunc() resource.StateRefreshFunc { |
| 35 | + return func() (interface{}, string, error) { |
| 36 | + var op *computeBeta.Operation |
| 37 | + var err error |
| 38 | + |
| 39 | + switch w.Type { |
| 40 | + case ComputeBetaOperationWaitGlobal: |
| 41 | + op, err = w.Service.GlobalOperations.Get( |
| 42 | + w.Project, w.Op.Name).Do() |
| 43 | + case ComputeBetaOperationWaitRegion: |
| 44 | + op, err = w.Service.RegionOperations.Get( |
| 45 | + w.Project, w.Region, w.Op.Name).Do() |
| 46 | + case ComputeBetaOperationWaitZone: |
| 47 | + op, err = w.Service.ZoneOperations.Get( |
| 48 | + w.Project, w.Zone, w.Op.Name).Do() |
| 49 | + default: |
| 50 | + return nil, "bad-type", fmt.Errorf( |
| 51 | + "Invalid wait type: %#v", w.Type) |
| 52 | + } |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return nil, "", err |
| 56 | + } |
| 57 | + |
| 58 | + log.Printf("[DEBUG] Got %q when asking for operation %q", op.Status, w.Op.Name) |
| 59 | + |
| 60 | + return op, op.Status, nil |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (w *ComputeBetaOperationWaiter) Conf() *resource.StateChangeConf { |
| 65 | + return &resource.StateChangeConf{ |
| 66 | + Pending: []string{"PENDING", "RUNNING"}, |
| 67 | + Target: []string{"DONE"}, |
| 68 | + Refresh: w.RefreshFunc(), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// ComputeBetaOperationError wraps computeBeta.OperationError and implements the |
| 73 | +// error interface so it can be returned. |
| 74 | +type ComputeBetaOperationError computeBeta.OperationError |
| 75 | + |
| 76 | +func (e ComputeBetaOperationError) Error() string { |
| 77 | + var buf bytes.Buffer |
| 78 | + |
| 79 | + for _, err := range e.Errors { |
| 80 | + buf.WriteString(err.Message + "\n") |
| 81 | + } |
| 82 | + |
| 83 | + return buf.String() |
| 84 | +} |
| 85 | + |
| 86 | +func computeBetaOperationWaitGlobal(config *Config, op *computeBeta.Operation, project string, activity string) error { |
| 87 | + return computeBetaOperationWaitGlobalTime(config, op, project, activity, 4) |
| 88 | +} |
| 89 | + |
| 90 | +func computeBetaOperationWaitGlobalTime(config *Config, op *computeBeta.Operation, project string, activity string, timeoutMin int) error { |
| 91 | + w := &ComputeBetaOperationWaiter{ |
| 92 | + Service: config.clientComputeBeta, |
| 93 | + Op: op, |
| 94 | + Project: project, |
| 95 | + Type: ComputeBetaOperationWaitGlobal, |
| 96 | + } |
| 97 | + |
| 98 | + state := w.Conf() |
| 99 | + state.Delay = 10 * time.Second |
| 100 | + state.Timeout = time.Duration(timeoutMin) * time.Minute |
| 101 | + state.MinTimeout = 2 * time.Second |
| 102 | + opRaw, err := state.WaitForState() |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("Error waiting for %s: %s", activity, err) |
| 105 | + } |
| 106 | + |
| 107 | + op = opRaw.(*computeBeta.Operation) |
| 108 | + if op.Error != nil { |
| 109 | + return ComputeBetaOperationError(*op.Error) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func computeBetaOperationWaitRegion(config *Config, op *computeBeta.Operation, project string, region, activity string) error { |
| 116 | + w := &ComputeBetaOperationWaiter{ |
| 117 | + Service: config.clientComputeBeta, |
| 118 | + Op: op, |
| 119 | + Project: project, |
| 120 | + Type: ComputeBetaOperationWaitRegion, |
| 121 | + Region: region, |
| 122 | + } |
| 123 | + |
| 124 | + state := w.Conf() |
| 125 | + state.Delay = 10 * time.Second |
| 126 | + state.Timeout = 4 * time.Minute |
| 127 | + state.MinTimeout = 2 * time.Second |
| 128 | + opRaw, err := state.WaitForState() |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("Error waiting for %s: %s", activity, err) |
| 131 | + } |
| 132 | + |
| 133 | + op = opRaw.(*computeBeta.Operation) |
| 134 | + if op.Error != nil { |
| 135 | + return ComputeBetaOperationError(*op.Error) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func computeBetaOperationWaitZone(config *Config, op *computeBeta.Operation, project string, zone, activity string) error { |
| 142 | + return computeBetaOperationWaitZoneTime(config, op, project, zone, 4, activity) |
| 143 | +} |
| 144 | + |
| 145 | +func computeBetaOperationWaitZoneTime(config *Config, op *computeBeta.Operation, project string, zone string, minutes int, activity string) error { |
| 146 | + w := &ComputeBetaOperationWaiter{ |
| 147 | + Service: config.clientComputeBeta, |
| 148 | + Op: op, |
| 149 | + Project: project, |
| 150 | + Zone: zone, |
| 151 | + Type: ComputeBetaOperationWaitZone, |
| 152 | + } |
| 153 | + state := w.Conf() |
| 154 | + state.Delay = 10 * time.Second |
| 155 | + state.Timeout = time.Duration(minutes) * time.Minute |
| 156 | + state.MinTimeout = 2 * time.Second |
| 157 | + opRaw, err := state.WaitForState() |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("Error waiting for %s: %s", activity, err) |
| 160 | + } |
| 161 | + op = opRaw.(*computeBeta.Operation) |
| 162 | + if op.Error != nil { |
| 163 | + // Return the error |
| 164 | + return ComputeBetaOperationError(*op.Error) |
| 165 | + } |
| 166 | + return nil |
| 167 | +} |
0 commit comments