forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataproc_job_operation.go
110 lines (91 loc) · 2.87 KB
/
dataproc_job_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package google
import (
"fmt"
"time"
"net/http"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/dataproc/v1"
"google.golang.org/api/googleapi"
)
type DataprocJobOperationWaiter struct {
Service *dataproc.Service
Region string
ProjectId string
JobId string
}
func (w *DataprocJobOperationWaiter) ConfForDelete() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"EXISTS"},
Target: []string{"DELETED"},
Refresh: w.RefreshFuncForDelete(),
}
}
func (w *DataprocJobOperationWaiter) Conf() *resource.StateChangeConf {
// For more info on each of the states please see
// https://cloud.google.com/dataproc/docs/reference/rest/v1/projects.regions.jobs#JobStatus
return &resource.StateChangeConf{
Pending: []string{"PENDING", "CANCEL_PENDING", "CANCEL_STARTED", "SETUP_DONE", "RUNNING"},
Target: []string{"CANCELLED", "DONE", "ATTEMPT_FAILURE", "ERROR"},
Refresh: w.RefreshFunc(),
}
}
func isNotFound(err error) bool {
if err == nil {
return false
}
ae, ok := err.(*googleapi.Error)
return ok && ae.Code == http.StatusNotFound
}
func (w *DataprocJobOperationWaiter) RefreshFuncForDelete() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
_, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
if err != nil {
if isNotFound(err) {
return "NA", "DELETED", nil
}
return nil, "", err
}
return "JOB", "EXISTS", err
}
}
func (w *DataprocJobOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
if err != nil {
return nil, "", err
}
return job, job.Status.State, err
}
}
func dataprocDeleteOperationWait(config *Config, region, projectId, jobId string, activity string, timeoutMinutes, minTimeoutSeconds int) error {
w := &DataprocJobOperationWaiter{
Service: config.clientDataproc,
Region: region,
ProjectId: projectId,
JobId: jobId,
}
state := w.ConfForDelete()
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
state.MinTimeout = time.Duration(minTimeoutSeconds) * time.Second
_, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}
return nil
}
func dataprocJobOperationWait(config *Config, region, projectId, jobId string, activity string, timeoutMinutes, minTimeoutSeconds int) error {
w := &DataprocJobOperationWaiter{
Service: config.clientDataproc,
Region: region,
ProjectId: projectId,
JobId: jobId,
}
state := w.Conf()
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
state.MinTimeout = time.Duration(minTimeoutSeconds) * time.Second
_, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for operation %s: %s", activity, err)
}
return nil
}