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