2015-10-23 16:10:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"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) {
|
|
|
|
var op *sqladmin.Operation
|
|
|
|
var err error
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] self_link: %s", w.Op.SelfLink)
|
|
|
|
op, err = w.Service.Operations.Get(w.Project, w.Op.Name).Do()
|
|
|
|
|
|
|
|
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 *SqlAdminOperationWaiter) Conf() *resource.StateChangeConf {
|
|
|
|
return &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING", "RUNNING"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"DONE"},
|
2015-10-23 16:10:41 +02: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqladminOperationWait(config *Config, op *sqladmin.Operation, activity string) error {
|
|
|
|
w := &SqlAdminOperationWaiter{
|
|
|
|
Service: config.clientSqlAdmin,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
2016-10-28 14:41:03 +02:00
|
|
|
state.Timeout = 10 * time.Minute
|
2015-10-23 16:10:41 +02:00
|
|
|
state.MinTimeout = 2 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
2017-03-04 01:45:25 +01:00
|
|
|
return fmt.Errorf("Error waiting for %s (op %s): %s", activity, op.Name, err)
|
2015-10-23 16:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
op = opRaw.(*sqladmin.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
return SqlAdminOperationError(*op.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|