github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.shrink.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  	"io"
    32  	"net/http"
    33  	"strconv"
    34  	"strings"
    35  	"time"
    36  )
    37  
    38  func newIndicesShrinkFunc(t Transport) IndicesShrink {
    39  	return func(index string, target string, o ...func(*IndicesShrinkRequest)) (*Response, error) {
    40  		var r = IndicesShrinkRequest{Index: index, Target: target}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // IndicesShrink allow to shrink an existing index into a new index with fewer primary shards.
    51  //
    52  //
    53  type IndicesShrink func(index string, target string, o ...func(*IndicesShrinkRequest)) (*Response, error)
    54  
    55  // IndicesShrinkRequest configures the Indices Shrink API request.
    56  //
    57  type IndicesShrinkRequest struct {
    58  	Index string
    59  
    60  	Body io.Reader
    61  
    62  	Target string
    63  
    64  	CopySettings          *bool
    65  	MasterTimeout         time.Duration
    66  	ClusterManagerTimeout time.Duration
    67  	Timeout               time.Duration
    68  	WaitForActiveShards   string
    69  
    70  	Pretty     bool
    71  	Human      bool
    72  	ErrorTrace bool
    73  	FilterPath []string
    74  
    75  	Header http.Header
    76  
    77  	ctx context.Context
    78  }
    79  
    80  // Do executes the request and returns response or error.
    81  //
    82  func (r IndicesShrinkRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    83  	var (
    84  		method string
    85  		path   strings.Builder
    86  		params map[string]string
    87  	)
    88  
    89  	method = "PUT"
    90  
    91  	path.Grow(1 + len(r.Index) + 1 + len("_shrink") + 1 + len(r.Target))
    92  	path.WriteString("/")
    93  	path.WriteString(r.Index)
    94  	path.WriteString("/")
    95  	path.WriteString("_shrink")
    96  	path.WriteString("/")
    97  	path.WriteString(r.Target)
    98  
    99  	params = make(map[string]string)
   100  
   101  	if r.CopySettings != nil {
   102  		params["copy_settings"] = strconv.FormatBool(*r.CopySettings)
   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.WaitForActiveShards != "" {
   118  		params["wait_for_active_shards"] = r.WaitForActiveShards
   119  	}
   120  
   121  	if r.Pretty {
   122  		params["pretty"] = "true"
   123  	}
   124  
   125  	if r.Human {
   126  		params["human"] = "true"
   127  	}
   128  
   129  	if r.ErrorTrace {
   130  		params["error_trace"] = "true"
   131  	}
   132  
   133  	if len(r.FilterPath) > 0 {
   134  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   135  	}
   136  
   137  	req, err := newRequest(method, path.String(), r.Body)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	if len(params) > 0 {
   143  		q := req.URL.Query()
   144  		for k, v := range params {
   145  			q.Set(k, v)
   146  		}
   147  		req.URL.RawQuery = q.Encode()
   148  	}
   149  
   150  	if r.Body != nil {
   151  		req.Header[headerContentType] = headerContentTypeJSON
   152  	}
   153  
   154  	if len(r.Header) > 0 {
   155  		if len(req.Header) == 0 {
   156  			req.Header = r.Header
   157  		} else {
   158  			for k, vv := range r.Header {
   159  				for _, v := range vv {
   160  					req.Header.Add(k, v)
   161  				}
   162  			}
   163  		}
   164  	}
   165  
   166  	if ctx != nil {
   167  		req = req.WithContext(ctx)
   168  	}
   169  
   170  	res, err := transport.Perform(req)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	response := Response{
   176  		StatusCode: res.StatusCode,
   177  		Body:       res.Body,
   178  		Header:     res.Header,
   179  	}
   180  
   181  	return &response, nil
   182  }
   183  
   184  // WithContext sets the request context.
   185  //
   186  func (f IndicesShrink) WithContext(v context.Context) func(*IndicesShrinkRequest) {
   187  	return func(r *IndicesShrinkRequest) {
   188  		r.ctx = v
   189  	}
   190  }
   191  
   192  // WithBody - The configuration for the target index (`settings` and `aliases`).
   193  //
   194  func (f IndicesShrink) WithBody(v io.Reader) func(*IndicesShrinkRequest) {
   195  	return func(r *IndicesShrinkRequest) {
   196  		r.Body = v
   197  	}
   198  }
   199  
   200  // WithCopySettings - whether or not to copy settings from the source index (defaults to false).
   201  //
   202  func (f IndicesShrink) WithCopySettings(v bool) func(*IndicesShrinkRequest) {
   203  	return func(r *IndicesShrinkRequest) {
   204  		r.CopySettings = &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 IndicesShrink) WithMasterTimeout(v time.Duration) func(*IndicesShrinkRequest) {
   213  	return func(r *IndicesShrinkRequest) {
   214  		r.MasterTimeout = v
   215  	}
   216  }
   217  
   218  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   219  //
   220  func (f IndicesShrink) WithClusterManagerTimeout(v time.Duration) func(*IndicesShrinkRequest) {
   221  	return func(r *IndicesShrinkRequest) {
   222  		r.ClusterManagerTimeout = v
   223  	}
   224  }
   225  
   226  // WithTimeout - explicit operation timeout.
   227  //
   228  func (f IndicesShrink) WithTimeout(v time.Duration) func(*IndicesShrinkRequest) {
   229  	return func(r *IndicesShrinkRequest) {
   230  		r.Timeout = v
   231  	}
   232  }
   233  
   234  // WithWaitForActiveShards - set the number of active shards to wait for on the shrunken index before the operation returns..
   235  //
   236  func (f IndicesShrink) WithWaitForActiveShards(v string) func(*IndicesShrinkRequest) {
   237  	return func(r *IndicesShrinkRequest) {
   238  		r.WaitForActiveShards = v
   239  	}
   240  }
   241  
   242  // WithPretty makes the response body pretty-printed.
   243  //
   244  func (f IndicesShrink) WithPretty() func(*IndicesShrinkRequest) {
   245  	return func(r *IndicesShrinkRequest) {
   246  		r.Pretty = true
   247  	}
   248  }
   249  
   250  // WithHuman makes statistical values human-readable.
   251  //
   252  func (f IndicesShrink) WithHuman() func(*IndicesShrinkRequest) {
   253  	return func(r *IndicesShrinkRequest) {
   254  		r.Human = true
   255  	}
   256  }
   257  
   258  // WithErrorTrace includes the stack trace for errors in the response body.
   259  //
   260  func (f IndicesShrink) WithErrorTrace() func(*IndicesShrinkRequest) {
   261  	return func(r *IndicesShrinkRequest) {
   262  		r.ErrorTrace = true
   263  	}
   264  }
   265  
   266  // WithFilterPath filters the properties of the response body.
   267  //
   268  func (f IndicesShrink) WithFilterPath(v ...string) func(*IndicesShrinkRequest) {
   269  	return func(r *IndicesShrinkRequest) {
   270  		r.FilterPath = v
   271  	}
   272  }
   273  
   274  // WithHeader adds the headers to the HTTP request.
   275  //
   276  func (f IndicesShrink) WithHeader(h map[string]string) func(*IndicesShrinkRequest) {
   277  	return func(r *IndicesShrinkRequest) {
   278  		if r.Header == nil {
   279  			r.Header = make(http.Header)
   280  		}
   281  		for k, v := range h {
   282  			r.Header.Add(k, v)
   283  		}
   284  	}
   285  }
   286  
   287  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   288  //
   289  func (f IndicesShrink) WithOpaqueID(s string) func(*IndicesShrinkRequest) {
   290  	return func(r *IndicesShrinkRequest) {
   291  		if r.Header == nil {
   292  			r.Header = make(http.Header)
   293  		}
   294  		r.Header.Set("X-Opaque-Id", s)
   295  	}
   296  }