forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
appengine_operation.go
83 lines (68 loc) · 2.17 KB
/
appengine_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
package google
import (
"fmt"
"log"
"regexp"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/appengine/v1"
)
var (
appEngineOperationIdRegexp = regexp.MustCompile(fmt.Sprintf("apps/%s/operations/(.*)", ProjectRegex))
)
type AppEngineOperationWaiter struct {
Service *appengine.APIService
Op *appengine.Operation
AppId string
}
func (w *AppEngineOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name)
if len(matches) != 2 {
return nil, "", fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name)
}
op, err := w.Service.Apps.Operations.Get(w.AppId, matches[1]).Do()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] Got %v when asking for operation %q", op.Done, w.Op.Name)
return op, strconv.FormatBool(op.Done), nil
}
}
func (w *AppEngineOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: w.RefreshFunc(),
}
}
// AppEngineOperationError wraps appengine.Status and implements the
// error interface so it can be returned.
type AppEngineOperationError appengine.Status
func (e AppEngineOperationError) Error() string {
return e.Message
}
func appEngineOperationWait(client *appengine.APIService, op *appengine.Operation, appId, activity string) error {
return appEngineOperationWaitTime(client, op, appId, activity, 4)
}
func appEngineOperationWaitTime(client *appengine.APIService, op *appengine.Operation, appId, activity string, timeoutMin int) error {
w := &AppEngineOperationWaiter{
Service: client,
Op: op,
AppId: appId,
}
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)
}
resultOp := opRaw.(*appengine.Operation)
if resultOp.Error != nil {
return AppEngineOperationError(*resultOp.Error)
}
return nil
}