Debian Codegraph @

ingest_put_pipeline.go

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