tasks_get_task.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package elastic
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "gopkg.in/olivere/elastic.v5/uritemplates"
  7. )
  8. // TasksGetTaskService retrieves the state of a task in the cluster. It is part of the Task Management API
  9. // documented at http://www.elastic.co/guide/en/elasticsearch/reference/5.2/tasks-list.html.
  10. //
  11. // It is supported as of Elasticsearch 2.3.0.
  12. type TasksGetTaskService struct {
  13. client *Client
  14. pretty bool
  15. taskId string
  16. waitForCompletion *bool
  17. }
  18. // NewTasksGetTaskService creates a new TasksGetTaskService.
  19. func NewTasksGetTaskService(client *Client) *TasksGetTaskService {
  20. return &TasksGetTaskService{
  21. client: client,
  22. }
  23. }
  24. // TaskId indicates to return the task with specified id.
  25. func (s *TasksGetTaskService) TaskId(taskId string) *TasksGetTaskService {
  26. s.taskId = taskId
  27. return s
  28. }
  29. // WaitForCompletion indicates whether to wait for the matching tasks
  30. // to complete (default: false).
  31. func (s *TasksGetTaskService) WaitForCompletion(waitForCompletion bool) *TasksGetTaskService {
  32. s.waitForCompletion = &waitForCompletion
  33. return s
  34. }
  35. // Pretty indicates that the JSON response be indented and human readable.
  36. func (s *TasksGetTaskService) Pretty(pretty bool) *TasksGetTaskService {
  37. s.pretty = pretty
  38. return s
  39. }
  40. // buildURL builds the URL for the operation.
  41. func (s *TasksGetTaskService) buildURL() (string, url.Values, error) {
  42. // Build URL
  43. path, err := uritemplates.Expand("/_tasks/{task_id}", map[string]string{
  44. "task_id": s.taskId,
  45. })
  46. if err != nil {
  47. return "", url.Values{}, err
  48. }
  49. // Add query string parameters
  50. params := url.Values{}
  51. if s.pretty {
  52. params.Set("pretty", "1")
  53. }
  54. if s.waitForCompletion != nil {
  55. params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion))
  56. }
  57. return path, params, nil
  58. }
  59. // Validate checks if the operation is valid.
  60. func (s *TasksGetTaskService) Validate() error {
  61. return nil
  62. }
  63. // Do executes the operation.
  64. func (s *TasksGetTaskService) Do(ctx context.Context) (*TasksGetTaskResponse, error) {
  65. // Check pre-conditions
  66. if err := s.Validate(); err != nil {
  67. return nil, err
  68. }
  69. // Get URL for request
  70. path, params, err := s.buildURL()
  71. if err != nil {
  72. return nil, err
  73. }
  74. // Get HTTP response
  75. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // Return operation response
  80. ret := new(TasksGetTaskResponse)
  81. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  82. return nil, err
  83. }
  84. return ret, nil
  85. }
  86. type TasksGetTaskResponse struct {
  87. Completed bool `json:"completed"`
  88. Task *TaskInfo `json:"task,omitempty"`
  89. }