github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.allocation_explain.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 ) 36 37 func newClusterAllocationExplainFunc(t Transport) ClusterAllocationExplain { 38 return func(o ...func(*ClusterAllocationExplainRequest)) (*Response, error) { 39 var r = ClusterAllocationExplainRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // ClusterAllocationExplain provides explanations for shard allocations in the cluster. 50 // 51 // 52 type ClusterAllocationExplain func(o ...func(*ClusterAllocationExplainRequest)) (*Response, error) 53 54 // ClusterAllocationExplainRequest configures the Cluster Allocation Explain API request. 55 // 56 type ClusterAllocationExplainRequest struct { 57 Body io.Reader 58 59 IncludeDiskInfo *bool 60 IncludeYesDecisions *bool 61 62 Pretty bool 63 Human bool 64 ErrorTrace bool 65 FilterPath []string 66 67 Header http.Header 68 69 ctx context.Context 70 } 71 72 // Do executes the request and returns response or error. 73 // 74 func (r ClusterAllocationExplainRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 75 var ( 76 method string 77 path strings.Builder 78 params map[string]string 79 ) 80 81 method = "POST" 82 83 path.Grow(len("/_cluster/allocation/explain")) 84 path.WriteString("/_cluster/allocation/explain") 85 86 params = make(map[string]string) 87 88 if r.IncludeDiskInfo != nil { 89 params["include_disk_info"] = strconv.FormatBool(*r.IncludeDiskInfo) 90 } 91 92 if r.IncludeYesDecisions != nil { 93 params["include_yes_decisions"] = strconv.FormatBool(*r.IncludeYesDecisions) 94 } 95 96 if r.Pretty { 97 params["pretty"] = "true" 98 } 99 100 if r.Human { 101 params["human"] = "true" 102 } 103 104 if r.ErrorTrace { 105 params["error_trace"] = "true" 106 } 107 108 if len(r.FilterPath) > 0 { 109 params["filter_path"] = strings.Join(r.FilterPath, ",") 110 } 111 112 req, err := newRequest(method, path.String(), r.Body) 113 if err != nil { 114 return nil, err 115 } 116 117 if len(params) > 0 { 118 q := req.URL.Query() 119 for k, v := range params { 120 q.Set(k, v) 121 } 122 req.URL.RawQuery = q.Encode() 123 } 124 125 if r.Body != nil { 126 req.Header[headerContentType] = headerContentTypeJSON 127 } 128 129 if len(r.Header) > 0 { 130 if len(req.Header) == 0 { 131 req.Header = r.Header 132 } else { 133 for k, vv := range r.Header { 134 for _, v := range vv { 135 req.Header.Add(k, v) 136 } 137 } 138 } 139 } 140 141 if ctx != nil { 142 req = req.WithContext(ctx) 143 } 144 145 res, err := transport.Perform(req) 146 if err != nil { 147 return nil, err 148 } 149 150 response := Response{ 151 StatusCode: res.StatusCode, 152 Body: res.Body, 153 Header: res.Header, 154 } 155 156 return &response, nil 157 } 158 159 // WithContext sets the request context. 160 // 161 func (f ClusterAllocationExplain) WithContext(v context.Context) func(*ClusterAllocationExplainRequest) { 162 return func(r *ClusterAllocationExplainRequest) { 163 r.ctx = v 164 } 165 } 166 167 // WithBody - The index, shard, and primary flag to explain. Empty means 'explain the first unassigned shard'. 168 // 169 func (f ClusterAllocationExplain) WithBody(v io.Reader) func(*ClusterAllocationExplainRequest) { 170 return func(r *ClusterAllocationExplainRequest) { 171 r.Body = v 172 } 173 } 174 175 // WithIncludeDiskInfo - return information about disk usage and shard sizes (default: false). 176 // 177 func (f ClusterAllocationExplain) WithIncludeDiskInfo(v bool) func(*ClusterAllocationExplainRequest) { 178 return func(r *ClusterAllocationExplainRequest) { 179 r.IncludeDiskInfo = &v 180 } 181 } 182 183 // WithIncludeYesDecisions - return 'yes' decisions in explanation (default: false). 184 // 185 func (f ClusterAllocationExplain) WithIncludeYesDecisions(v bool) func(*ClusterAllocationExplainRequest) { 186 return func(r *ClusterAllocationExplainRequest) { 187 r.IncludeYesDecisions = &v 188 } 189 } 190 191 // WithPretty makes the response body pretty-printed. 192 // 193 func (f ClusterAllocationExplain) WithPretty() func(*ClusterAllocationExplainRequest) { 194 return func(r *ClusterAllocationExplainRequest) { 195 r.Pretty = true 196 } 197 } 198 199 // WithHuman makes statistical values human-readable. 200 // 201 func (f ClusterAllocationExplain) WithHuman() func(*ClusterAllocationExplainRequest) { 202 return func(r *ClusterAllocationExplainRequest) { 203 r.Human = true 204 } 205 } 206 207 // WithErrorTrace includes the stack trace for errors in the response body. 208 // 209 func (f ClusterAllocationExplain) WithErrorTrace() func(*ClusterAllocationExplainRequest) { 210 return func(r *ClusterAllocationExplainRequest) { 211 r.ErrorTrace = true 212 } 213 } 214 215 // WithFilterPath filters the properties of the response body. 216 // 217 func (f ClusterAllocationExplain) WithFilterPath(v ...string) func(*ClusterAllocationExplainRequest) { 218 return func(r *ClusterAllocationExplainRequest) { 219 r.FilterPath = v 220 } 221 } 222 223 // WithHeader adds the headers to the HTTP request. 224 // 225 func (f ClusterAllocationExplain) WithHeader(h map[string]string) func(*ClusterAllocationExplainRequest) { 226 return func(r *ClusterAllocationExplainRequest) { 227 if r.Header == nil { 228 r.Header = make(http.Header) 229 } 230 for k, v := range h { 231 r.Header.Add(k, v) 232 } 233 } 234 } 235 236 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 237 // 238 func (f ClusterAllocationExplain) WithOpaqueID(s string) func(*ClusterAllocationExplainRequest) { 239 return func(r *ClusterAllocationExplainRequest) { 240 if r.Header == nil { 241 r.Header = make(http.Header) 242 } 243 r.Header.Set("X-Opaque-Id", s) 244 } 245 }