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