2015-10-23 14:10:41 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2018-03-23 18:19:39 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
2015-10-23 14:10:41 +00:00
|
|
|
"google.golang.org/api/sqladmin/v1beta4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SqlAdminOperationWaiter struct {
|
|
|
|
Service *sqladmin.Service
|
|
|
|
Op *sqladmin.Operation
|
|
|
|
Project string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *SqlAdminOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] self_link: %s", w.Op.SelfLink)
|
2018-03-23 18:19:39 +00:00
|
|
|
op, err := w.Service.Operations.Get(w.Project, w.Op.Name).Do()
|
2015-10-23 14:10:41 +00:00
|
|
|
|
2018-03-23 18:19:39 +00:00
|
|
|
if e, ok := err.(*googleapi.Error); ok && (e.Code == 429 || e.Code == 503) {
|
|
|
|
return w.Op, "PENDING", nil
|
|
|
|
} else if err != nil {
|
2015-10-23 14:10:41 +00:00
|
|
|
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 *SqlAdminOperationWaiter) Conf() *resource.StateChangeConf {
|
|
|
|
return &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING", "RUNNING"},
|
2016-01-21 01:20:41 +00:00
|
|
|
Target: []string{"DONE"},
|
2015-10-23 14:10:41 +00:00
|
|
|
Refresh: w.RefreshFunc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SqlAdminOperationError wraps sqladmin.OperationError and implements the
|
|
|
|
// error interface so it can be returned.
|
|
|
|
type SqlAdminOperationError sqladmin.OperationErrors
|
|
|
|
|
|
|
|
func (e SqlAdminOperationError) Error() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
for _, err := range e.Errors {
|
|
|
|
buf.WriteString(err.Message + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-09-27 00:01:52 +00:00
|
|
|
func sqladminOperationWait(config *Config, op *sqladmin.Operation, project, activity string) error {
|
2018-04-03 21:44:18 +00:00
|
|
|
return sqladminOperationWaitTime(config, op, project, activity, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqladminOperationWaitTime(config *Config, op *sqladmin.Operation, project, activity string, timeoutMinutes int) error {
|
2018-06-08 23:04:55 +00:00
|
|
|
if op.Status == "DONE" {
|
|
|
|
if op.Error != nil {
|
|
|
|
return SqlAdminOperationError(*op.Error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-23 14:10:41 +00:00
|
|
|
w := &SqlAdminOperationWaiter{
|
|
|
|
Service: config.clientSqlAdmin,
|
|
|
|
Op: op,
|
2017-09-27 00:01:52 +00:00
|
|
|
Project: project,
|
2015-10-23 14:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
2018-04-03 21:44:18 +00:00
|
|
|
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
|
2015-10-23 14:10:41 +00:00
|
|
|
state.MinTimeout = 2 * time.Second
|
2017-06-09 10:33:29 +00:00
|
|
|
state.Delay = 5 * time.Second
|
2015-10-23 14:10:41 +00:00
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
2017-03-04 00:45:25 +00:00
|
|
|
return fmt.Errorf("Error waiting for %s (op %s): %s", activity, op.Name, err)
|
2015-10-23 14:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
op = opRaw.(*sqladmin.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
return SqlAdminOperationError(*op.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|