github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.disk_usage.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  )
    35  
    36  func newIndicesDiskUsageFunc(t Transport) IndicesDiskUsage {
    37  	return func(index string, o ...func(*IndicesDiskUsageRequest)) (*Response, error) {
    38  		var r = IndicesDiskUsageRequest{Index: index}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // IndicesDiskUsage analyzes the disk usage of each field of an index or data stream
    49  //
    50  // This API is experimental.
    51  //
    52  //
    53  type IndicesDiskUsage func(index string, o ...func(*IndicesDiskUsageRequest)) (*Response, error)
    54  
    55  // IndicesDiskUsageRequest configures the Indices Disk Usage API request.
    56  //
    57  type IndicesDiskUsageRequest struct {
    58  	Index string
    59  
    60  	AllowNoIndices    *bool
    61  	ExpandWildcards   string
    62  	Flush             *bool
    63  	IgnoreUnavailable *bool
    64  	RunExpensiveTasks *bool
    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 IndicesDiskUsageRequest) 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 = "POST"
    86  
    87  	path.Grow(1 + len(r.Index) + 1 + len("_disk_usage"))
    88  	path.WriteString("/")
    89  	path.WriteString(r.Index)
    90  	path.WriteString("/")
    91  	path.WriteString("_disk_usage")
    92  
    93  	params = make(map[string]string)
    94  
    95  	if r.AllowNoIndices != nil {
    96  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
    97  	}
    98  
    99  	if r.ExpandWildcards != "" {
   100  		params["expand_wildcards"] = r.ExpandWildcards
   101  	}
   102  
   103  	if r.Flush != nil {
   104  		params["flush"] = strconv.FormatBool(*r.Flush)
   105  	}
   106  
   107  	if r.IgnoreUnavailable != nil {
   108  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   109  	}
   110  
   111  	if r.RunExpensiveTasks != nil {
   112  		params["run_expensive_tasks"] = strconv.FormatBool(*r.RunExpensiveTasks)
   113  	}
   114  
   115  	if r.Pretty {
   116  		params["pretty"] = "true"
   117  	}
   118  
   119  	if r.Human {
   120  		params["human"] = "true"
   121  	}
   122  
   123  	if r.ErrorTrace {
   124  		params["error_trace"] = "true"
   125  	}
   126  
   127  	if len(r.FilterPath) > 0 {
   128  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   129  	}
   130  
   131  	req, err := newRequest(method, path.String(), nil)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	if len(params) > 0 {
   137  		q := req.URL.Query()
   138  		for k, v := range params {
   139  			q.Set(k, v)
   140  		}
   141  		req.URL.RawQuery = q.Encode()
   142  	}
   143  
   144  	if len(r.Header) > 0 {
   145  		if len(req.Header) == 0 {
   146  			req.Header = r.Header
   147  		} else {
   148  			for k, vv := range r.Header {
   149  				for _, v := range vv {
   150  					req.Header.Add(k, v)
   151  				}
   152  			}
   153  		}
   154  	}
   155  
   156  	if ctx != nil {
   157  		req = req.WithContext(ctx)
   158  	}
   159  
   160  	res, err := transport.Perform(req)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	response := Response{
   166  		StatusCode: res.StatusCode,
   167  		Body:       res.Body,
   168  		Header:     res.Header,
   169  	}
   170  
   171  	return &response, nil
   172  }
   173  
   174  // WithContext sets the request context.
   175  //
   176  func (f IndicesDiskUsage) WithContext(v context.Context) func(*IndicesDiskUsageRequest) {
   177  	return func(r *IndicesDiskUsageRequest) {
   178  		r.ctx = v
   179  	}
   180  }
   181  
   182  // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
   183  //
   184  func (f IndicesDiskUsage) WithAllowNoIndices(v bool) func(*IndicesDiskUsageRequest) {
   185  	return func(r *IndicesDiskUsageRequest) {
   186  		r.AllowNoIndices = &v
   187  	}
   188  }
   189  
   190  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   191  //
   192  func (f IndicesDiskUsage) WithExpandWildcards(v string) func(*IndicesDiskUsageRequest) {
   193  	return func(r *IndicesDiskUsageRequest) {
   194  		r.ExpandWildcards = v
   195  	}
   196  }
   197  
   198  // WithFlush - whether flush or not before analyzing the index disk usage. defaults to true.
   199  //
   200  func (f IndicesDiskUsage) WithFlush(v bool) func(*IndicesDiskUsageRequest) {
   201  	return func(r *IndicesDiskUsageRequest) {
   202  		r.Flush = &v
   203  	}
   204  }
   205  
   206  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   207  //
   208  func (f IndicesDiskUsage) WithIgnoreUnavailable(v bool) func(*IndicesDiskUsageRequest) {
   209  	return func(r *IndicesDiskUsageRequest) {
   210  		r.IgnoreUnavailable = &v
   211  	}
   212  }
   213  
   214  // WithRunExpensiveTasks - must be set to [true] in order for the task to be performed. defaults to false..
   215  //
   216  func (f IndicesDiskUsage) WithRunExpensiveTasks(v bool) func(*IndicesDiskUsageRequest) {
   217  	return func(r *IndicesDiskUsageRequest) {
   218  		r.RunExpensiveTasks = &v
   219  	}
   220  }
   221  
   222  // WithPretty makes the response body pretty-printed.
   223  //
   224  func (f IndicesDiskUsage) WithPretty() func(*IndicesDiskUsageRequest) {
   225  	return func(r *IndicesDiskUsageRequest) {
   226  		r.Pretty = true
   227  	}
   228  }
   229  
   230  // WithHuman makes statistical values human-readable.
   231  //
   232  func (f IndicesDiskUsage) WithHuman() func(*IndicesDiskUsageRequest) {
   233  	return func(r *IndicesDiskUsageRequest) {
   234  		r.Human = true
   235  	}
   236  }
   237  
   238  // WithErrorTrace includes the stack trace for errors in the response body.
   239  //
   240  func (f IndicesDiskUsage) WithErrorTrace() func(*IndicesDiskUsageRequest) {
   241  	return func(r *IndicesDiskUsageRequest) {
   242  		r.ErrorTrace = true
   243  	}
   244  }
   245  
   246  // WithFilterPath filters the properties of the response body.
   247  //
   248  func (f IndicesDiskUsage) WithFilterPath(v ...string) func(*IndicesDiskUsageRequest) {
   249  	return func(r *IndicesDiskUsageRequest) {
   250  		r.FilterPath = v
   251  	}
   252  }
   253  
   254  // WithHeader adds the headers to the HTTP request.
   255  //
   256  func (f IndicesDiskUsage) WithHeader(h map[string]string) func(*IndicesDiskUsageRequest) {
   257  	return func(r *IndicesDiskUsageRequest) {
   258  		if r.Header == nil {
   259  			r.Header = make(http.Header)
   260  		}
   261  		for k, v := range h {
   262  			r.Header.Add(k, v)
   263  		}
   264  	}
   265  }
   266  
   267  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   268  //
   269  func (f IndicesDiskUsage) WithOpaqueID(s string) func(*IndicesDiskUsageRequest) {
   270  	return func(r *IndicesDiskUsageRequest) {
   271  		if r.Header == nil {
   272  			r.Header = make(http.Header)
   273  		}
   274  		r.Header.Set("X-Opaque-Id", s)
   275  	}
   276  }