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