forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
79 lines (65 loc) · 1.69 KB
/
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
package google
import (
"bytes"
"fmt"
"code.google.com/p/google-api-go-client/compute/v1"
"github.com/hashicorp/terraform/helper/resource"
)
// OperationWaitType is an enum specifying what type of operation
// we're waiting on.
type OperationWaitType byte
const (
OperationWaitInvalid OperationWaitType = iota
OperationWaitGlobal
OperationWaitRegion
OperationWaitZone
)
type OperationWaiter struct {
Service *compute.Service
Op *compute.Operation
Project string
Region string
Zone string
Type OperationWaitType
}
func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
var op *compute.Operation
var err error
switch w.Type {
case OperationWaitGlobal:
op, err = w.Service.GlobalOperations.Get(
w.Project, w.Op.Name).Do()
case OperationWaitRegion:
op, err = w.Service.RegionOperations.Get(
w.Project, w.Region, w.Op.Name).Do()
case OperationWaitZone:
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
}
return op, op.Status, nil
}
}
func (w *OperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"PENDING", "RUNNING"},
Target: "DONE",
Refresh: w.RefreshFunc(),
}
}
// OperationError wraps compute.OperationError and implements the
// error interface so it can be returned.
type OperationError compute.OperationError
func (e OperationError) Error() string {
var buf bytes.Buffer
for _, err := range e.Errors {
buf.WriteString(err.Message + "\n")
}
return buf.String()
}