Skip to content

Commit ce23639

Browse files
modular-magiciandanawillow
authored andcommitted
clean up operation code (hashicorp#267)
1 parent 70cb1d4 commit ce23639

25 files changed

+467
-1037
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,24 @@
11
package google
22

33
import (
4-
"fmt"
5-
"log"
6-
"time"
7-
8-
"github.com/hashicorp/terraform/helper/resource"
94
"google.golang.org/api/accesscontextmanager/v1beta"
105
)
116

127
type AccessContextManagerOperationWaiter struct {
138
Service *accesscontextmanager.OperationsService
14-
Op *accesscontextmanager.Operation
15-
}
16-
17-
func (w *AccessContextManagerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
18-
return func() (interface{}, string, error) {
19-
op, err := w.Service.Get(w.Op.Name).Do()
20-
21-
if err != nil {
22-
return nil, "", err
23-
}
24-
25-
log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name)
26-
27-
return op, fmt.Sprint(op.Done), nil
28-
}
9+
CommonOperationWaiter
2910
}
3011

31-
func (w *AccessContextManagerOperationWaiter) Conf() *resource.StateChangeConf {
32-
return &resource.StateChangeConf{
33-
Pending: []string{"false"},
34-
Target: []string{"true"},
35-
Refresh: w.RefreshFunc(),
36-
}
12+
func (w *AccessContextManagerOperationWaiter) QueryOp() (interface{}, error) {
13+
return w.Service.Get(w.Op.Name).Do()
3714
}
3815

39-
func accessContextManagerOperationWaitTime(service *accesscontextmanager.Service, op *accesscontextmanager.Operation, activity string, timeoutMin int) error {
40-
if op.Done {
41-
if op.Error != nil {
42-
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
43-
}
44-
return nil
45-
}
46-
16+
func accessContextManagerOperationWaitTime(service *accesscontextmanager.Service, op *accesscontextmanager.Operation, activity string, timeoutMinutes int) error {
4717
w := &AccessContextManagerOperationWaiter{
4818
Service: service.Operations,
49-
Op: op,
5019
}
51-
52-
state := w.Conf()
53-
state.Delay = 10 * time.Second
54-
state.Timeout = time.Duration(timeoutMin) * time.Minute
55-
state.MinTimeout = 2 * time.Second
56-
opRaw, err := state.WaitForState()
57-
if err != nil {
58-
return fmt.Errorf("Error waiting for %s: %s", activity, err)
20+
if err := w.SetOp(op); err != nil {
21+
return err
5922
}
60-
61-
op = opRaw.(*accesscontextmanager.Operation)
62-
if op.Error != nil {
63-
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
64-
}
65-
66-
return nil
23+
return OperationWait(w, activity, timeoutMinutes)
6724
}

google-beta/appengine_operation.go

+10-58
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ package google
22

33
import (
44
"fmt"
5-
"log"
65
"regexp"
7-
"strconv"
8-
"time"
9-
10-
"github.com/hashicorp/terraform/helper/resource"
116

127
"google.golang.org/api/appengine/v1"
138
)
@@ -18,73 +13,30 @@ var (
1813

1914
type AppEngineOperationWaiter struct {
2015
Service *appengine.APIService
21-
Op *appengine.Operation
2216
AppId string
17+
CommonOperationWaiter
2318
}
2419

25-
func (w *AppEngineOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
26-
return func() (interface{}, string, error) {
27-
matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name)
28-
if len(matches) != 2 {
29-
return nil, "", fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name)
30-
}
31-
op, err := w.Service.Apps.Operations.Get(w.AppId, matches[1]).Do()
32-
if err != nil {
33-
return nil, "", err
34-
}
35-
36-
log.Printf("[DEBUG] Got %v when asking for operation %q", op.Done, w.Op.Name)
37-
return op, strconv.FormatBool(op.Done), nil
20+
func (w *AppEngineOperationWaiter) QueryOp() (interface{}, error) {
21+
matches := appEngineOperationIdRegexp.FindStringSubmatch(w.Op.Name)
22+
if len(matches) != 2 {
23+
return nil, fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name)
3824
}
39-
}
40-
41-
func (w *AppEngineOperationWaiter) Conf() *resource.StateChangeConf {
42-
return &resource.StateChangeConf{
43-
Pending: []string{"false"},
44-
Target: []string{"true"},
45-
Refresh: w.RefreshFunc(),
46-
}
47-
}
48-
49-
// AppEngineOperationError wraps appengine.Status and implements the
50-
// error interface so it can be returned.
51-
type AppEngineOperationError appengine.Status
52-
53-
func (e AppEngineOperationError) Error() string {
54-
return e.Message
25+
return w.Service.Apps.Operations.Get(w.AppId, matches[1]).Do()
5526
}
5627

5728
func appEngineOperationWait(client *appengine.APIService, op *appengine.Operation, appId, activity string) error {
5829
return appEngineOperationWaitTime(client, op, appId, activity, 4)
5930
}
6031

61-
func appEngineOperationWaitTime(client *appengine.APIService, op *appengine.Operation, appId, activity string, timeoutMin int) error {
62-
if op.Done {
63-
if op.Error != nil {
64-
return AppEngineOperationError(*op.Error)
65-
}
66-
return nil
67-
}
68-
32+
func appEngineOperationWaitTime(client *appengine.APIService, op *appengine.Operation, appId, activity string, timeoutMinutes int) error {
6933
w := &AppEngineOperationWaiter{
7034
Service: client,
71-
Op: op,
7235
AppId: appId,
7336
}
7437

75-
state := w.Conf()
76-
state.Delay = 10 * time.Second
77-
state.Timeout = time.Duration(timeoutMin) * time.Minute
78-
state.MinTimeout = 2 * time.Second
79-
opRaw, err := state.WaitForState()
80-
if err != nil {
81-
return fmt.Errorf("Error waiting for %s: %s", activity, err)
38+
if err := w.SetOp(op); err != nil {
39+
return err
8240
}
83-
84-
resultOp := opRaw.(*appengine.Operation)
85-
if resultOp.Error != nil {
86-
return AppEngineOperationError(*resultOp.Error)
87-
}
88-
89-
return nil
41+
return OperationWait(w, activity, timeoutMinutes)
9042
}
+8-61
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,24 @@
11
package google
22

33
import (
4-
"fmt"
5-
"log"
6-
"time"
7-
8-
"github.com/hashicorp/terraform/helper/resource"
94
"google.golang.org/api/cloudfunctions/v1"
105
)
116

127
type CloudFunctionsOperationWaiter struct {
138
Service *cloudfunctions.Service
14-
Op *cloudfunctions.Operation
15-
}
16-
17-
func (w *CloudFunctionsOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
18-
return func() (interface{}, string, error) {
19-
op, err := w.Service.Operations.Get(w.Op.Name).Do()
20-
21-
if err != nil {
22-
return nil, "", err
23-
}
24-
25-
status := "PENDING"
26-
if op.Done == true {
27-
status = "DONE"
28-
}
29-
30-
log.Printf("[DEBUG] Got %q when asking for operation %q", status, w.Op.Name)
31-
return op, status, nil
32-
}
33-
}
34-
35-
func (w *CloudFunctionsOperationWaiter) Conf() *resource.StateChangeConf {
36-
return &resource.StateChangeConf{
37-
Pending: []string{"PENDING"},
38-
Target: []string{"DONE"},
39-
Refresh: w.RefreshFunc(),
40-
}
9+
CommonOperationWaiter
4110
}
4211

43-
func cloudFunctionsOperationWait(client *cloudfunctions.Service,
44-
op *cloudfunctions.Operation, activity string) error {
45-
return cloudFunctionsOperationWaitTime(client, op, activity, 4)
12+
func (w *CloudFunctionsOperationWaiter) QueryOp() (interface{}, error) {
13+
return w.Service.Operations.Get(w.Op.Name).Do()
4614
}
4715

48-
func cloudFunctionsOperationWaitTime(client *cloudfunctions.Service, op *cloudfunctions.Operation,
49-
activity string, timeoutMin int) error {
50-
if op.Done {
51-
if op.Error != nil {
52-
return fmt.Errorf(op.Error.Message)
53-
}
54-
return nil
55-
}
56-
16+
func cloudFunctionsOperationWait(service *cloudfunctions.Service, op *cloudfunctions.Operation, activity string) error {
5717
w := &CloudFunctionsOperationWaiter{
58-
Service: client,
59-
Op: op,
18+
Service: service,
6019
}
61-
62-
state := w.Conf()
63-
state.Delay = 10 * time.Second
64-
state.Timeout = time.Duration(timeoutMin) * time.Minute
65-
state.MinTimeout = 2 * time.Second
66-
opRaw, err := state.WaitForState()
67-
if err != nil {
68-
return fmt.Errorf("Error waiting for %s: %s", activity, err)
69-
}
70-
71-
resultOp := opRaw.(*cloudfunctions.Operation)
72-
if resultOp.Error != nil {
73-
return fmt.Errorf(resultOp.Error.Message)
20+
if err := w.SetOp(op); err != nil {
21+
return err
7422
}
75-
76-
return nil
23+
return OperationWait(w, activity, 4)
7724
}

0 commit comments

Comments
 (0)