github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.ingest.put_pipeline.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  package opensearchapi
    28  
    29  import (
    30  	"context"
    31  	"io"
    32  	"net/http"
    33  	"strings"
    34  	"time"
    35  )
    36  
    37  func newIngestPutPipelineFunc(t Transport) IngestPutPipeline {
    38  	return func(id string, body io.Reader, o ...func(*IngestPutPipelineRequest)) (*Response, error) {
    39  		var r = IngestPutPipelineRequest{PipelineID: id, Body: body}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // IngestPutPipeline creates or updates a pipeline.
    50  //
    51  //
    52  type IngestPutPipeline func(id string, body io.Reader, o ...func(*IngestPutPipelineRequest)) (*Response, error)
    53  
    54  // IngestPutPipelineRequest configures the Ingest Put Pipeline API request.
    55  //
    56  type IngestPutPipelineRequest struct {
    57  	PipelineID string
    58  
    59  	Body io.Reader
    60  
    61  	MasterTimeout         time.Duration
    62  	ClusterManagerTimeout time.Duration
    63  	Timeout               time.Duration
    64  
    65  	Pretty     bool
    66  	Human      bool
    67  	ErrorTrace bool
    68  	FilterPath []string
    69  
    70  	Header http.Header
    71  
    72  	ctx context.Context
    73  }
    74  
    75  // Do executes the request and returns response or error.
    76  //
    77  func (r IngestPutPipelineRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    78  	var (
    79  		method string
    80  		path   strings.Builder
    81  		params map[string]string
    82  	)
    83  
    84  	method = "PUT"
    85  
    86  	path.Grow(1 + len("_ingest") + 1 + len("pipeline") + 1 + len(r.PipelineID))
    87  	path.WriteString("/")
    88  	path.WriteString("_ingest")
    89  	path.WriteString("/")
    90  	path.WriteString("pipeline")
    91  	path.WriteString("/")
    92  	path.WriteString(r.PipelineID)
    93  
    94  	params = make(map[string]string)
    95  
    96  	if r.MasterTimeout != 0 {
    97  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    98  	}
    99  
   100  	if r.ClusterManagerTimeout != 0 {
   101  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   102  	}
   103  
   104  	if r.Timeout != 0 {
   105  		params["timeout"] = formatDuration(r.Timeout)
   106  	}
   107  
   108  	if r.Pretty {
   109  		params["pretty"] = "true"
   110  	}
   111  
   112  	if r.Human {
   113  		params["human"] = "true"
   114  	}
   115  
   116  	if r.ErrorTrace {
   117  		params["error_trace"] = "true"
   118  	}
   119  
   120  	if len(r.FilterPath) > 0 {
   121  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   122  	}
   123  
   124  	req, err := newRequest(method, path.String(), r.Body)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	if len(params) > 0 {
   130  		q := req.URL.Query()
   131  		for k, v := range params {
   132  			q.Set(k, v)
   133  		}
   134  		req.URL.RawQuery = q.Encode()
   135  	}
   136  
   137  	if r.Body != nil {
   138  		req.Header[headerContentType] = headerContentTypeJSON
   139  	}
   140  
   141  	if len(r.Header) > 0 {
   142  		if len(req.Header) == 0 {
   143  			req.Header = r.Header
   144  		} else {
   145  			for k, vv := range r.Header {
   146  				for _, v := range vv {
   147  					req.Header.Add(k, v)
   148  				}
   149  			}
   150  		}
   151  	}
   152  
   153  	if ctx != nil {
   154  		req = req.WithContext(ctx)
   155  	}
   156  
   157  	res, err := transport.Perform(req)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	response := Response{
   163  		StatusCode: res.StatusCode,
   164  		Body:       res.Body,
   165  		Header:     res.Header,
   166  	}
   167  
   168  	return &response, nil
   169  }
   170  
   171  // WithContext sets the request context.
   172  //
   173  func (f IngestPutPipeline) WithContext(v context.Context) func(*IngestPutPipelineRequest) {
   174  	return func(r *IngestPutPipelineRequest) {
   175  		r.ctx = v
   176  	}
   177  }
   178  
   179  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   180  //
   181  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   182  //
   183  func (f IngestPutPipeline) WithMasterTimeout(v time.Duration) func(*IngestPutPipelineRequest) {
   184  	return func(r *IngestPutPipelineRequest) {
   185  		r.MasterTimeout = v
   186  	}
   187  }
   188  
   189  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   190  //
   191  func (f IngestPutPipeline) WithClusterManagerTimeout(v time.Duration) func(*IngestPutPipelineRequest) {
   192  	return func(r *IngestPutPipelineRequest) {
   193  		r.ClusterManagerTimeout = v
   194  	}
   195  }
   196  
   197  // WithTimeout - explicit operation timeout.
   198  //
   199  func (f IngestPutPipeline) WithTimeout(v time.Duration) func(*IngestPutPipelineRequest) {
   200  	return func(r *IngestPutPipelineRequest) {
   201  		r.Timeout = v
   202  	}
   203  }
   204  
   205  // WithPretty makes the response body pretty-printed.
   206  //
   207  func (f IngestPutPipeline) WithPretty() func(*IngestPutPipelineRequest) {
   208  	return func(r *IngestPutPipelineRequest) {
   209  		r.Pretty = true
   210  	}
   211  }
   212  
   213  // WithHuman makes statistical values human-readable.
   214  //
   215  func (f IngestPutPipeline) WithHuman() func(*IngestPutPipelineRequest) {
   216  	return func(r *IngestPutPipelineRequest) {
   217  		r.Human = true
   218  	}
   219  }
   220  
   221  // WithErrorTrace includes the stack trace for errors in the response body.
   222  //
   223  func (f IngestPutPipeline) WithErrorTrace() func(*IngestPutPipelineRequest) {
   224  	return func(r *IngestPutPipelineRequest) {
   225  		r.ErrorTrace = true
   226  	}
   227  }
   228  
   229  // WithFilterPath filters the properties of the response body.
   230  //
   231  func (f IngestPutPipeline) WithFilterPath(v ...string) func(*IngestPutPipelineRequest) {
   232  	return func(r *IngestPutPipelineRequest) {
   233  		r.FilterPath = v
   234  	}
   235  }
   236  
   237  // WithHeader adds the headers to the HTTP request.
   238  //
   239  func (f IngestPutPipeline) WithHeader(h map[string]string) func(*IngestPutPipelineRequest) {
   240  	return func(r *IngestPutPipelineRequest) {
   241  		if r.Header == nil {
   242  			r.Header = make(http.Header)
   243  		}
   244  		for k, v := range h {
   245  			r.Header.Add(k, v)
   246  		}
   247  	}
   248  }
   249  
   250  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   251  //
   252  func (f IngestPutPipeline) WithOpaqueID(s string) func(*IngestPutPipelineRequest) {
   253  	return func(r *IngestPutPipelineRequest) {
   254  		if r.Header == nil {
   255  			r.Header = make(http.Header)
   256  		}
   257  		r.Header.Set("X-Opaque-Id", s)
   258  	}
   259  }