2017-11-21 17:32:43 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"google.golang.org/api/dataproc/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DataprocJobOperationWaiter struct {
|
|
|
|
Service *dataproc.Service
|
|
|
|
Region string
|
|
|
|
ProjectId string
|
|
|
|
JobId string
|
2018-12-27 01:42:37 +00:00
|
|
|
Status string
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) State() string {
|
|
|
|
return w.Status
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) Error() error {
|
|
|
|
// The "operation" is just the job, which has no special error field that we
|
|
|
|
// want to expose.
|
|
|
|
return nil
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) SetOp(job interface{}) error {
|
|
|
|
// The "operation" is just the job. Instead of holding onto the whole job
|
|
|
|
// object, we only care about the state, which gets set in QueryOp, so this
|
|
|
|
// doesn't have to do anything.
|
|
|
|
return nil
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) {
|
|
|
|
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
|
|
|
|
if job != nil {
|
|
|
|
w.Status = job.Status.State
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return job, err
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) OpName() string {
|
|
|
|
return w.JobId
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) PendingStates() []string {
|
|
|
|
return []string{"PENDING", "CANCEL_PENDING", "CANCEL_STARTED", "SETUP_DONE", "RUNNING"}
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocJobOperationWaiter) TargetStates() []string {
|
|
|
|
return []string{"CANCELLED", "DONE", "ATTEMPT_FAILURE", "ERROR"}
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func dataprocJobOperationWait(config *Config, region, projectId, jobId string, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
2017-11-21 17:32:43 +00:00
|
|
|
w := &DataprocJobOperationWaiter{
|
|
|
|
Service: config.clientDataproc,
|
|
|
|
Region: region,
|
|
|
|
ProjectId: projectId,
|
|
|
|
JobId: jobId,
|
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return OperationWait(w, activity, timeoutMinutes)
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
type DataprocDeleteJobOperationWaiter struct {
|
|
|
|
DataprocJobOperationWaiter
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocDeleteJobOperationWaiter) PendingStates() []string {
|
|
|
|
return []string{"EXISTS", "ERROR"}
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocDeleteJobOperationWaiter) TargetStates() []string {
|
|
|
|
return []string{"DELETED"}
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *DataprocDeleteJobOperationWaiter) QueryOp() (interface{}, error) {
|
|
|
|
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
|
2017-11-21 17:32:43 +00:00
|
|
|
if err != nil {
|
2018-12-27 01:42:37 +00:00
|
|
|
if isGoogleApiErrorWithCode(err, http.StatusNotFound) {
|
|
|
|
w.Status = "DELETED"
|
|
|
|
return job, nil
|
|
|
|
}
|
|
|
|
w.Status = "ERROR"
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
w.Status = "EXISTS"
|
|
|
|
return job, err
|
|
|
|
}
|
2017-11-21 17:32:43 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func dataprocDeleteOperationWait(config *Config, region, projectId, jobId string, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
|
|
|
w := &DataprocDeleteJobOperationWaiter{
|
|
|
|
DataprocJobOperationWaiter{
|
|
|
|
Service: config.clientDataproc,
|
|
|
|
Region: region,
|
|
|
|
ProjectId: projectId,
|
|
|
|
JobId: jobId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return OperationWait(w, activity, timeoutMinutes)
|
2017-11-21 17:32:43 +00:00
|
|
|
}
|