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