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