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