ingest_put_pipeline.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "fmt"
  9. "net/url"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // IngestPutPipelineService adds pipelines and updates existing pipelines in
  13. // the cluster.
  14. //
  15. // It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.2/put-pipeline-api.html.
  16. type IngestPutPipelineService struct {
  17. client *Client
  18. pretty bool
  19. id string
  20. masterTimeout string
  21. timeout string
  22. bodyJson interface{}
  23. bodyString string
  24. }
  25. // NewIngestPutPipelineService creates a new IngestPutPipelineService.
  26. func NewIngestPutPipelineService(client *Client) *IngestPutPipelineService {
  27. return &IngestPutPipelineService{
  28. client: client,
  29. }
  30. }
  31. // Id is the pipeline ID.
  32. func (s *IngestPutPipelineService) Id(id string) *IngestPutPipelineService {
  33. s.id = id
  34. return s
  35. }
  36. // MasterTimeout is an explicit operation timeout for connection to master node.
  37. func (s *IngestPutPipelineService) MasterTimeout(masterTimeout string) *IngestPutPipelineService {
  38. s.masterTimeout = masterTimeout
  39. return s
  40. }
  41. // Timeout specifies an explicit operation timeout.
  42. func (s *IngestPutPipelineService) Timeout(timeout string) *IngestPutPipelineService {
  43. s.timeout = timeout
  44. return s
  45. }
  46. // Pretty indicates that the JSON response be indented and human readable.
  47. func (s *IngestPutPipelineService) Pretty(pretty bool) *IngestPutPipelineService {
  48. s.pretty = pretty
  49. return s
  50. }
  51. // BodyJson is the ingest definition, defined as a JSON-serializable document.
  52. // Use e.g. a map[string]interface{} here.
  53. func (s *IngestPutPipelineService) BodyJson(body interface{}) *IngestPutPipelineService {
  54. s.bodyJson = body
  55. return s
  56. }
  57. // BodyString is the ingest definition, specified as a string.
  58. func (s *IngestPutPipelineService) BodyString(body string) *IngestPutPipelineService {
  59. s.bodyString = body
  60. return s
  61. }
  62. // buildURL builds the URL for the operation.
  63. func (s *IngestPutPipelineService) buildURL() (string, url.Values, error) {
  64. // Build URL
  65. path, err := uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{
  66. "id": s.id,
  67. })
  68. if err != nil {
  69. return "", url.Values{}, err
  70. }
  71. // Add query string parameters
  72. params := url.Values{}
  73. if s.pretty {
  74. params.Set("pretty", "1")
  75. }
  76. if s.masterTimeout != "" {
  77. params.Set("master_timeout", s.masterTimeout)
  78. }
  79. if s.timeout != "" {
  80. params.Set("timeout", s.timeout)
  81. }
  82. return path, params, nil
  83. }
  84. // Validate checks if the operation is valid.
  85. func (s *IngestPutPipelineService) Validate() error {
  86. var invalid []string
  87. if s.id == "" {
  88. invalid = append(invalid, "Id")
  89. }
  90. if s.bodyString == "" && s.bodyJson == nil {
  91. invalid = append(invalid, "BodyJson")
  92. }
  93. if len(invalid) > 0 {
  94. return fmt.Errorf("missing required fields: %v", invalid)
  95. }
  96. return nil
  97. }
  98. // Do executes the operation.
  99. func (s *IngestPutPipelineService) Do(ctx context.Context) (*IngestPutPipelineResponse, error) {
  100. // Check pre-conditions
  101. if err := s.Validate(); err != nil {
  102. return nil, err
  103. }
  104. // Get URL for request
  105. path, params, err := s.buildURL()
  106. if err != nil {
  107. return nil, err
  108. }
  109. // Setup HTTP request body
  110. var body interface{}
  111. if s.bodyJson != nil {
  112. body = s.bodyJson
  113. } else {
  114. body = s.bodyString
  115. }
  116. // Get HTTP response
  117. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. // Return operation response
  122. ret := new(IngestPutPipelineResponse)
  123. if err := json.Unmarshal(res.Body, ret); err != nil {
  124. return nil, err
  125. }
  126. return ret, nil
  127. }
  128. // IngestPutPipelineResponse is the response of IngestPutPipelineService.Do.
  129. type IngestPutPipelineResponse struct {
  130. Acknowledged bool `json:"acknowledged"`
  131. }