2018-05-10 22:01:22 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-05-14 05:49:26 +00:00
|
|
|
"regexp"
|
2018-05-10 22:01:22 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
|
|
|
|
"google.golang.org/api/appengine/v1"
|
|
|
|
)
|
|
|
|
|
2018-05-14 05:49:26 +00:00
|
|
|
var (
|
2018-05-19 00:01:40 +00:00
|
|
|
appEngineOperationIdRegexp = regexp.MustCompile(fmt.Sprintf("apps/%s/operations/(.*)", ProjectRegex))
|
2018-05-14 05:49:26 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 22:01:22 +00:00
|
|
|
type AppEngineOperationWaiter struct {
|
|
|
|
Service *appengine.APIService
|
|
|
|
Op *appengine.Operation
|
|
|
|
AppId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *AppEngineOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
2018-05-19 00:01:40 +00:00
|
|
|
matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name)
|
2018-05-14 05:49:26 +00:00
|
|
|
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()
|
2018-05-10 22:01:22 +00:00
|
|
|
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 {
|
2018-06-08 23:04:55 +00:00
|
|
|
if op.Done {
|
|
|
|
if op.Error != nil {
|
|
|
|
return AppEngineOperationError(*op.Error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:01:22 +00:00
|
|
|
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
|
|
|
|
}
|