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