github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.rollover.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 newIndicesRolloverFunc(t Transport) IndicesRollover {
    39  	return func(alias string, o ...func(*IndicesRolloverRequest)) (*Response, error) {
    40  		var r = IndicesRolloverRequest{Alias: alias}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // IndicesRollover updates an alias to point to a new index when the existing index
    51  // is considered to be too large or too old.
    52  //
    53  //
    54  type IndicesRollover func(alias string, o ...func(*IndicesRolloverRequest)) (*Response, error)
    55  
    56  // IndicesRolloverRequest configures the Indices Rollover API request.
    57  //
    58  type IndicesRolloverRequest struct {
    59  	Body io.Reader
    60  
    61  	Alias    string
    62  	NewIndex string
    63  
    64  	DryRun                *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 IndicesRolloverRequest) 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 = "POST"
    90  
    91  	path.Grow(1 + len(r.Alias) + 1 + len("_rollover") + 1 + len(r.NewIndex))
    92  	path.WriteString("/")
    93  	path.WriteString(r.Alias)
    94  	path.WriteString("/")
    95  	path.WriteString("_rollover")
    96  	if r.NewIndex != "" {
    97  		path.WriteString("/")
    98  		path.WriteString(r.NewIndex)
    99  	}
   100  
   101  	params = make(map[string]string)
   102  
   103  	if r.DryRun != nil {
   104  		params["dry_run"] = strconv.FormatBool(*r.DryRun)
   105  	}
   106  
   107  	if r.MasterTimeout != 0 {
   108  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   109  	}
   110  
   111  	if r.ClusterManagerTimeout != 0 {
   112  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   113  	}
   114  
   115  	if r.Timeout != 0 {
   116  		params["timeout"] = formatDuration(r.Timeout)
   117  	}
   118  
   119  	if r.WaitForActiveShards != "" {
   120  		params["wait_for_active_shards"] = r.WaitForActiveShards
   121  	}
   122  
   123  	if r.Pretty {
   124  		params["pretty"] = "true"
   125  	}
   126  
   127  	if r.Human {
   128  		params["human"] = "true"
   129  	}
   130  
   131  	if r.ErrorTrace {
   132  		params["error_trace"] = "true"
   133  	}
   134  
   135  	if len(r.FilterPath) > 0 {
   136  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   137  	}
   138  
   139  	req, err := newRequest(method, path.String(), r.Body)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	if len(params) > 0 {
   145  		q := req.URL.Query()
   146  		for k, v := range params {
   147  			q.Set(k, v)
   148  		}
   149  		req.URL.RawQuery = q.Encode()
   150  	}
   151  
   152  	if r.Body != nil {
   153  		req.Header[headerContentType] = headerContentTypeJSON
   154  	}
   155  
   156  	if len(r.Header) > 0 {
   157  		if len(req.Header) == 0 {
   158  			req.Header = r.Header
   159  		} else {
   160  			for k, vv := range r.Header {
   161  				for _, v := range vv {
   162  					req.Header.Add(k, v)
   163  				}
   164  			}
   165  		}
   166  	}
   167  
   168  	if ctx != nil {
   169  		req = req.WithContext(ctx)
   170  	}
   171  
   172  	res, err := transport.Perform(req)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	response := Response{
   178  		StatusCode: res.StatusCode,
   179  		Body:       res.Body,
   180  		Header:     res.Header,
   181  	}
   182  
   183  	return &response, nil
   184  }
   185  
   186  // WithContext sets the request context.
   187  //
   188  func (f IndicesRollover) WithContext(v context.Context) func(*IndicesRolloverRequest) {
   189  	return func(r *IndicesRolloverRequest) {
   190  		r.ctx = v
   191  	}
   192  }
   193  
   194  // WithBody - The conditions that needs to be met for executing rollover.
   195  //
   196  func (f IndicesRollover) WithBody(v io.Reader) func(*IndicesRolloverRequest) {
   197  	return func(r *IndicesRolloverRequest) {
   198  		r.Body = v
   199  	}
   200  }
   201  
   202  // WithNewIndex - the name of the rollover index.
   203  //
   204  func (f IndicesRollover) WithNewIndex(v string) func(*IndicesRolloverRequest) {
   205  	return func(r *IndicesRolloverRequest) {
   206  		r.NewIndex = v
   207  	}
   208  }
   209  
   210  // WithDryRun - if set to true the rollover action will only be validated but not actually performed even if a condition matches. the default is false.
   211  //
   212  func (f IndicesRollover) WithDryRun(v bool) func(*IndicesRolloverRequest) {
   213  	return func(r *IndicesRolloverRequest) {
   214  		r.DryRun = &v
   215  	}
   216  }
   217  
   218  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   219  //
   220  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   221  //
   222  func (f IndicesRollover) WithMasterTimeout(v time.Duration) func(*IndicesRolloverRequest) {
   223  	return func(r *IndicesRolloverRequest) {
   224  		r.MasterTimeout = v
   225  	}
   226  }
   227  
   228  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   229  //
   230  func (f IndicesRollover) WithClusterManagerTimeout(v time.Duration) func(*IndicesRolloverRequest) {
   231  	return func(r *IndicesRolloverRequest) {
   232  		r.ClusterManagerTimeout = v
   233  	}
   234  }
   235  
   236  // WithTimeout - explicit operation timeout.
   237  //
   238  func (f IndicesRollover) WithTimeout(v time.Duration) func(*IndicesRolloverRequest) {
   239  	return func(r *IndicesRolloverRequest) {
   240  		r.Timeout = v
   241  	}
   242  }
   243  
   244  // WithWaitForActiveShards - set the number of active shards to wait for on the newly created rollover index before the operation returns..
   245  //
   246  func (f IndicesRollover) WithWaitForActiveShards(v string) func(*IndicesRolloverRequest) {
   247  	return func(r *IndicesRolloverRequest) {
   248  		r.WaitForActiveShards = v
   249  	}
   250  }
   251  
   252  // WithPretty makes the response body pretty-printed.
   253  //
   254  func (f IndicesRollover) WithPretty() func(*IndicesRolloverRequest) {
   255  	return func(r *IndicesRolloverRequest) {
   256  		r.Pretty = true
   257  	}
   258  }
   259  
   260  // WithHuman makes statistical values human-readable.
   261  //
   262  func (f IndicesRollover) WithHuman() func(*IndicesRolloverRequest) {
   263  	return func(r *IndicesRolloverRequest) {
   264  		r.Human = true
   265  	}
   266  }
   267  
   268  // WithErrorTrace includes the stack trace for errors in the response body.
   269  //
   270  func (f IndicesRollover) WithErrorTrace() func(*IndicesRolloverRequest) {
   271  	return func(r *IndicesRolloverRequest) {
   272  		r.ErrorTrace = true
   273  	}
   274  }
   275  
   276  // WithFilterPath filters the properties of the response body.
   277  //
   278  func (f IndicesRollover) WithFilterPath(v ...string) func(*IndicesRolloverRequest) {
   279  	return func(r *IndicesRolloverRequest) {
   280  		r.FilterPath = v
   281  	}
   282  }
   283  
   284  // WithHeader adds the headers to the HTTP request.
   285  //
   286  func (f IndicesRollover) WithHeader(h map[string]string) func(*IndicesRolloverRequest) {
   287  	return func(r *IndicesRolloverRequest) {
   288  		if r.Header == nil {
   289  			r.Header = make(http.Header)
   290  		}
   291  		for k, v := range h {
   292  			r.Header.Add(k, v)
   293  		}
   294  	}
   295  }
   296  
   297  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   298  //
   299  func (f IndicesRollover) WithOpaqueID(s string) func(*IndicesRolloverRequest) {
   300  	return func(r *IndicesRolloverRequest) {
   301  		if r.Header == nil {
   302  			r.Header = make(http.Header)
   303  		}
   304  		r.Header.Set("X-Opaque-Id", s)
   305  	}
   306  }