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