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