ingest_get_pipeline.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "net/url"
  9. "strings"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // IngestGetPipelineService returns pipelines based on ID.
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/get-pipeline-api.html
  14. // for documentation.
  15. type IngestGetPipelineService struct {
  16. client *Client
  17. pretty bool
  18. id []string
  19. masterTimeout string
  20. }
  21. // NewIngestGetPipelineService creates a new IngestGetPipelineService.
  22. func NewIngestGetPipelineService(client *Client) *IngestGetPipelineService {
  23. return &IngestGetPipelineService{
  24. client: client,
  25. }
  26. }
  27. // Id is a list of pipeline ids. Wildcards supported.
  28. func (s *IngestGetPipelineService) Id(id ...string) *IngestGetPipelineService {
  29. s.id = append(s.id, id...)
  30. return s
  31. }
  32. // MasterTimeout is an explicit operation timeout for connection to master node.
  33. func (s *IngestGetPipelineService) MasterTimeout(masterTimeout string) *IngestGetPipelineService {
  34. s.masterTimeout = masterTimeout
  35. return s
  36. }
  37. // Pretty indicates that the JSON response be indented and human readable.
  38. func (s *IngestGetPipelineService) Pretty(pretty bool) *IngestGetPipelineService {
  39. s.pretty = pretty
  40. return s
  41. }
  42. // buildURL builds the URL for the operation.
  43. func (s *IngestGetPipelineService) buildURL() (string, url.Values, error) {
  44. var err error
  45. var path string
  46. // Build URL
  47. if len(s.id) > 0 {
  48. path, err = uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{
  49. "id": strings.Join(s.id, ","),
  50. })
  51. } else {
  52. path = "/_ingest/pipeline"
  53. }
  54. if err != nil {
  55. return "", url.Values{}, err
  56. }
  57. // Add query string parameters
  58. params := url.Values{}
  59. if s.pretty {
  60. params.Set("pretty", "1")
  61. }
  62. if s.masterTimeout != "" {
  63. params.Set("master_timeout", s.masterTimeout)
  64. }
  65. return path, params, nil
  66. }
  67. // Validate checks if the operation is valid.
  68. func (s *IngestGetPipelineService) Validate() error {
  69. return nil
  70. }
  71. // Do executes the operation.
  72. func (s *IngestGetPipelineService) Do(ctx context.Context) (IngestGetPipelineResponse, error) {
  73. // Check pre-conditions
  74. if err := s.Validate(); err != nil {
  75. return nil, err
  76. }
  77. // Get URL for request
  78. path, params, err := s.buildURL()
  79. if err != nil {
  80. return nil, err
  81. }
  82. // Get HTTP response
  83. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // Return operation response
  88. var ret IngestGetPipelineResponse
  89. if err := json.Unmarshal(res.Body, &ret); err != nil {
  90. return nil, err
  91. }
  92. return ret, nil
  93. }
  94. // IngestGetPipelineResponse is the response of IngestGetPipelineService.Do.
  95. type IngestGetPipelineResponse map[string]*IngestGetPipeline
  96. type IngestGetPipeline struct {
  97. ID string `json:"id"`
  98. Config map[string]interface{} `json:"config"`
  99. }