forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqladmin_operation.go
85 lines (68 loc) · 2.13 KB
/
sqladmin_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
84
85
package google
import (
"bytes"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/googleapi"
"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)
op, err := w.Service.Operations.Get(w.Project, w.Op.Name).Do()
if e, ok := err.(*googleapi.Error); ok && (e.Code == 429 || e.Code == 503) {
return w.Op, "PENDING", nil
} else 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"},
Target: []string{"DONE"},
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, project, activity string) error {
return sqladminOperationWaitTime(config, op, project, activity, 10)
}
func sqladminOperationWaitTime(config *Config, op *sqladmin.Operation, project, activity string, timeoutMinutes int) error {
w := &SqlAdminOperationWaiter{
Service: config.clientSqlAdmin,
Op: op,
Project: project,
}
state := w.Conf()
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
state.MinTimeout = 2 * time.Second
state.Delay = 5 * time.Second
opRaw, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s (op %s): %s", activity, op.Name, err)
}
op = opRaw.(*sqladmin.Operation)
if op.Error != nil {
return SqlAdminOperationError(*op.Error)
}
return nil
}