github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.simulate_index_template.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  	"strconv"
    34  	"strings"
    35  	"time"
    36  )
    37  
    38  func newIndicesSimulateIndexTemplateFunc(t Transport) IndicesSimulateIndexTemplate {
    39  	return func(name string, o ...func(*IndicesSimulateIndexTemplateRequest)) (*Response, error) {
    40  		var r = IndicesSimulateIndexTemplateRequest{Name: name}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // IndicesSimulateIndexTemplate simulate matching the given index name against the index templates in the system
    51  //
    52  //
    53  type IndicesSimulateIndexTemplate func(name string, o ...func(*IndicesSimulateIndexTemplateRequest)) (*Response, error)
    54  
    55  // IndicesSimulateIndexTemplateRequest configures the Indices Simulate Index Template API request.
    56  //
    57  type IndicesSimulateIndexTemplateRequest struct {
    58  	Body io.Reader
    59  
    60  	Name string
    61  
    62  	Cause                 string
    63  	Create                *bool
    64  	MasterTimeout         time.Duration
    65  	ClusterManagerTimeout time.Duration
    66  
    67  	Pretty     bool
    68  	Human      bool
    69  	ErrorTrace bool
    70  	FilterPath []string
    71  
    72  	Header http.Header
    73  
    74  	ctx context.Context
    75  }
    76  
    77  // Do executes the request and returns response or error.
    78  //
    79  func (r IndicesSimulateIndexTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    80  	var (
    81  		method string
    82  		path   strings.Builder
    83  		params map[string]string
    84  	)
    85  
    86  	method = "POST"
    87  
    88  	path.Grow(1 + len("_index_template") + 1 + len("_simulate_index") + 1 + len(r.Name))
    89  	path.WriteString("/")
    90  	path.WriteString("_index_template")
    91  	path.WriteString("/")
    92  	path.WriteString("_simulate_index")
    93  	path.WriteString("/")
    94  	path.WriteString(r.Name)
    95  
    96  	params = make(map[string]string)
    97  
    98  	if r.Cause != "" {
    99  		params["cause"] = r.Cause
   100  	}
   101  
   102  	if r.Create != nil {
   103  		params["create"] = strconv.FormatBool(*r.Create)
   104  	}
   105  
   106  	if r.MasterTimeout != 0 {
   107  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   108  	}
   109  
   110  	if r.ClusterManagerTimeout != 0 {
   111  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   112  	}
   113  
   114  	if r.Pretty {
   115  		params["pretty"] = "true"
   116  	}
   117  
   118  	if r.Human {
   119  		params["human"] = "true"
   120  	}
   121  
   122  	if r.ErrorTrace {
   123  		params["error_trace"] = "true"
   124  	}
   125  
   126  	if len(r.FilterPath) > 0 {
   127  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   128  	}
   129  
   130  	req, err := newRequest(method, path.String(), r.Body)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	if len(params) > 0 {
   136  		q := req.URL.Query()
   137  		for k, v := range params {
   138  			q.Set(k, v)
   139  		}
   140  		req.URL.RawQuery = q.Encode()
   141  	}
   142  
   143  	if r.Body != nil {
   144  		req.Header[headerContentType] = headerContentTypeJSON
   145  	}
   146  
   147  	if len(r.Header) > 0 {
   148  		if len(req.Header) == 0 {
   149  			req.Header = r.Header
   150  		} else {
   151  			for k, vv := range r.Header {
   152  				for _, v := range vv {
   153  					req.Header.Add(k, v)
   154  				}
   155  			}
   156  		}
   157  	}
   158  
   159  	if ctx != nil {
   160  		req = req.WithContext(ctx)
   161  	}
   162  
   163  	res, err := transport.Perform(req)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	response := Response{
   169  		StatusCode: res.StatusCode,
   170  		Body:       res.Body,
   171  		Header:     res.Header,
   172  	}
   173  
   174  	return &response, nil
   175  }
   176  
   177  // WithContext sets the request context.
   178  //
   179  func (f IndicesSimulateIndexTemplate) WithContext(v context.Context) func(*IndicesSimulateIndexTemplateRequest) {
   180  	return func(r *IndicesSimulateIndexTemplateRequest) {
   181  		r.ctx = v
   182  	}
   183  }
   184  
   185  // WithBody - New index template definition, which will be included in the simulation, as if it already exists in the system.
   186  //
   187  func (f IndicesSimulateIndexTemplate) WithBody(v io.Reader) func(*IndicesSimulateIndexTemplateRequest) {
   188  	return func(r *IndicesSimulateIndexTemplateRequest) {
   189  		r.Body = v
   190  	}
   191  }
   192  
   193  // WithCause - user defined reason for dry-run creating the new template for simulation purposes.
   194  //
   195  func (f IndicesSimulateIndexTemplate) WithCause(v string) func(*IndicesSimulateIndexTemplateRequest) {
   196  	return func(r *IndicesSimulateIndexTemplateRequest) {
   197  		r.Cause = v
   198  	}
   199  }
   200  
   201  // WithCreate - whether the index template we optionally defined in the body should only be dry-run added if new or can also replace an existing one.
   202  //
   203  func (f IndicesSimulateIndexTemplate) WithCreate(v bool) func(*IndicesSimulateIndexTemplateRequest) {
   204  	return func(r *IndicesSimulateIndexTemplateRequest) {
   205  		r.Create = &v
   206  	}
   207  }
   208  
   209  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   210  //
   211  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   212  //
   213  func (f IndicesSimulateIndexTemplate) WithMasterTimeout(v time.Duration) func(*IndicesSimulateIndexTemplateRequest) {
   214  	return func(r *IndicesSimulateIndexTemplateRequest) {
   215  		r.MasterTimeout = v
   216  	}
   217  }
   218  
   219  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   220  //
   221  func (f IndicesSimulateIndexTemplate) WithClusterManagerTimeout(v time.Duration) func(*IndicesSimulateIndexTemplateRequest) {
   222  	return func(r *IndicesSimulateIndexTemplateRequest) {
   223  		r.ClusterManagerTimeout = v
   224  	}
   225  }
   226  
   227  // WithPretty makes the response body pretty-printed.
   228  //
   229  func (f IndicesSimulateIndexTemplate) WithPretty() func(*IndicesSimulateIndexTemplateRequest) {
   230  	return func(r *IndicesSimulateIndexTemplateRequest) {
   231  		r.Pretty = true
   232  	}
   233  }
   234  
   235  // WithHuman makes statistical values human-readable.
   236  //
   237  func (f IndicesSimulateIndexTemplate) WithHuman() func(*IndicesSimulateIndexTemplateRequest) {
   238  	return func(r *IndicesSimulateIndexTemplateRequest) {
   239  		r.Human = true
   240  	}
   241  }
   242  
   243  // WithErrorTrace includes the stack trace for errors in the response body.
   244  //
   245  func (f IndicesSimulateIndexTemplate) WithErrorTrace() func(*IndicesSimulateIndexTemplateRequest) {
   246  	return func(r *IndicesSimulateIndexTemplateRequest) {
   247  		r.ErrorTrace = true
   248  	}
   249  }
   250  
   251  // WithFilterPath filters the properties of the response body.
   252  //
   253  func (f IndicesSimulateIndexTemplate) WithFilterPath(v ...string) func(*IndicesSimulateIndexTemplateRequest) {
   254  	return func(r *IndicesSimulateIndexTemplateRequest) {
   255  		r.FilterPath = v
   256  	}
   257  }
   258  
   259  // WithHeader adds the headers to the HTTP request.
   260  //
   261  func (f IndicesSimulateIndexTemplate) WithHeader(h map[string]string) func(*IndicesSimulateIndexTemplateRequest) {
   262  	return func(r *IndicesSimulateIndexTemplateRequest) {
   263  		if r.Header == nil {
   264  			r.Header = make(http.Header)
   265  		}
   266  		for k, v := range h {
   267  			r.Header.Add(k, v)
   268  		}
   269  	}
   270  }
   271  
   272  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   273  //
   274  func (f IndicesSimulateIndexTemplate) WithOpaqueID(s string) func(*IndicesSimulateIndexTemplateRequest) {
   275  	return func(r *IndicesSimulateIndexTemplateRequest) {
   276  		if r.Header == nil {
   277  			r.Header = make(http.Header)
   278  		}
   279  		r.Header.Set("X-Opaque-Id", s)
   280  	}
   281  }