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