terraform-provider-google/google/dataproc_job_operation.go

113 lines
2.9 KiB
Go
Raw Normal View History

package google
import (
"fmt"
"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
}
2018-12-27 01:42:37 +00:00
func (w *DataprocJobOperationWaiter) State() string {
if w == nil {
return "<nil>"
}
2018-12-27 01:42:37 +00:00
return w.Status
}
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
}
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
}
2018-12-27 01:42:37 +00:00
func (w *DataprocJobOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
2018-12-27 01:42:37 +00:00
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
if job != nil {
w.Status = job.Status.State
}
2018-12-27 01:42:37 +00:00
return job, err
}
2018-12-27 01:42:37 +00:00
func (w *DataprocJobOperationWaiter) OpName() string {
if w == nil {
return "<nil>"
}
2018-12-27 01:42:37 +00:00
return w.JobId
}
2018-12-27 01:42:37 +00:00
func (w *DataprocJobOperationWaiter) PendingStates() []string {
return []string{"PENDING", "CANCEL_PENDING", "CANCEL_STARTED", "SETUP_DONE", "RUNNING"}
}
2018-12-27 01:42:37 +00:00
func (w *DataprocJobOperationWaiter) TargetStates() []string {
return []string{"CANCELLED", "DONE", "ATTEMPT_FAILURE", "ERROR"}
}
2018-12-27 01:42:37 +00:00
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,
}
2018-12-27 01:42:37 +00:00
return OperationWait(w, activity, timeoutMinutes)
}
2018-12-27 01:42:37 +00:00
type DataprocDeleteJobOperationWaiter struct {
DataprocJobOperationWaiter
}
2018-12-27 01:42:37 +00:00
func (w *DataprocDeleteJobOperationWaiter) PendingStates() []string {
return []string{"EXISTS", "ERROR"}
}
2018-12-27 01:42:37 +00:00
func (w *DataprocDeleteJobOperationWaiter) TargetStates() []string {
return []string{"DELETED"}
}
2018-12-27 01:42:37 +00:00
func (w *DataprocDeleteJobOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
2018-12-27 01:42:37 +00:00
job, err := w.Service.Projects.Regions.Jobs.Get(w.ProjectId, w.Region, w.JobId).Do()
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"
}
2018-12-27 01:42:37 +00:00
w.Status = "EXISTS"
return job, err
}
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)
}