github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.add_block.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 "time" 35 ) 36 37 func newIndicesAddBlockFunc(t Transport) IndicesAddBlock { 38 return func(index []string, block string, o ...func(*IndicesAddBlockRequest)) (*Response, error) { 39 var r = IndicesAddBlockRequest{Index: index, Block: block} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // IndicesAddBlock adds a block to an index. 50 // 51 // 52 type IndicesAddBlock func(index []string, block string, o ...func(*IndicesAddBlockRequest)) (*Response, error) 53 54 // IndicesAddBlockRequest configures the Indices Add Block API request. 55 // 56 type IndicesAddBlockRequest struct { 57 Index []string 58 59 Block string 60 61 AllowNoIndices *bool 62 ExpandWildcards string 63 IgnoreUnavailable *bool 64 MasterTimeout time.Duration 65 ClusterManagerTimeout time.Duration 66 Timeout time.Duration 67 68 Pretty bool 69 Human bool 70 ErrorTrace bool 71 FilterPath []string 72 73 Header http.Header 74 75 ctx context.Context 76 } 77 78 // Do executes the request and returns response or error. 79 // 80 func (r IndicesAddBlockRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 81 var ( 82 method string 83 path strings.Builder 84 params map[string]string 85 ) 86 87 method = "PUT" 88 89 path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_block") + 1 + len(r.Block)) 90 path.WriteString("/") 91 path.WriteString(strings.Join(r.Index, ",")) 92 path.WriteString("/") 93 path.WriteString("_block") 94 path.WriteString("/") 95 path.WriteString(r.Block) 96 97 params = make(map[string]string) 98 99 if r.AllowNoIndices != nil { 100 params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) 101 } 102 103 if r.ExpandWildcards != "" { 104 params["expand_wildcards"] = r.ExpandWildcards 105 } 106 107 if r.IgnoreUnavailable != nil { 108 params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) 109 } 110 111 if r.MasterTimeout != 0 { 112 params["master_timeout"] = formatDuration(r.MasterTimeout) 113 } 114 115 if r.ClusterManagerTimeout != 0 { 116 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 117 } 118 119 if r.Timeout != 0 { 120 params["timeout"] = formatDuration(r.Timeout) 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(), nil) 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 len(r.Header) > 0 { 153 if len(req.Header) == 0 { 154 req.Header = r.Header 155 } else { 156 for k, vv := range r.Header { 157 for _, v := range vv { 158 req.Header.Add(k, v) 159 } 160 } 161 } 162 } 163 164 if ctx != nil { 165 req = req.WithContext(ctx) 166 } 167 168 res, err := transport.Perform(req) 169 if err != nil { 170 return nil, err 171 } 172 173 response := Response{ 174 StatusCode: res.StatusCode, 175 Body: res.Body, 176 Header: res.Header, 177 } 178 179 return &response, nil 180 } 181 182 // WithContext sets the request context. 183 // 184 func (f IndicesAddBlock) WithContext(v context.Context) func(*IndicesAddBlockRequest) { 185 return func(r *IndicesAddBlockRequest) { 186 r.ctx = v 187 } 188 } 189 190 // 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). 191 // 192 func (f IndicesAddBlock) WithAllowNoIndices(v bool) func(*IndicesAddBlockRequest) { 193 return func(r *IndicesAddBlockRequest) { 194 r.AllowNoIndices = &v 195 } 196 } 197 198 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 199 // 200 func (f IndicesAddBlock) WithExpandWildcards(v string) func(*IndicesAddBlockRequest) { 201 return func(r *IndicesAddBlockRequest) { 202 r.ExpandWildcards = v 203 } 204 } 205 206 // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed). 207 // 208 func (f IndicesAddBlock) WithIgnoreUnavailable(v bool) func(*IndicesAddBlockRequest) { 209 return func(r *IndicesAddBlockRequest) { 210 r.IgnoreUnavailable = &v 211 } 212 } 213 214 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 215 // 216 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 217 // 218 func (f IndicesAddBlock) WithMasterTimeout(v time.Duration) func(*IndicesAddBlockRequest) { 219 return func(r *IndicesAddBlockRequest) { 220 r.MasterTimeout = v 221 } 222 } 223 224 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 225 // 226 func (f IndicesAddBlock) WithClusterManagerTimeout(v time.Duration) func(*IndicesAddBlockRequest) { 227 return func(r *IndicesAddBlockRequest) { 228 r.ClusterManagerTimeout = v 229 } 230 } 231 232 // WithTimeout - explicit operation timeout. 233 // 234 func (f IndicesAddBlock) WithTimeout(v time.Duration) func(*IndicesAddBlockRequest) { 235 return func(r *IndicesAddBlockRequest) { 236 r.Timeout = v 237 } 238 } 239 240 // WithPretty makes the response body pretty-printed. 241 // 242 func (f IndicesAddBlock) WithPretty() func(*IndicesAddBlockRequest) { 243 return func(r *IndicesAddBlockRequest) { 244 r.Pretty = true 245 } 246 } 247 248 // WithHuman makes statistical values human-readable. 249 // 250 func (f IndicesAddBlock) WithHuman() func(*IndicesAddBlockRequest) { 251 return func(r *IndicesAddBlockRequest) { 252 r.Human = true 253 } 254 } 255 256 // WithErrorTrace includes the stack trace for errors in the response body. 257 // 258 func (f IndicesAddBlock) WithErrorTrace() func(*IndicesAddBlockRequest) { 259 return func(r *IndicesAddBlockRequest) { 260 r.ErrorTrace = true 261 } 262 } 263 264 // WithFilterPath filters the properties of the response body. 265 // 266 func (f IndicesAddBlock) WithFilterPath(v ...string) func(*IndicesAddBlockRequest) { 267 return func(r *IndicesAddBlockRequest) { 268 r.FilterPath = v 269 } 270 } 271 272 // WithHeader adds the headers to the HTTP request. 273 // 274 func (f IndicesAddBlock) WithHeader(h map[string]string) func(*IndicesAddBlockRequest) { 275 return func(r *IndicesAddBlockRequest) { 276 if r.Header == nil { 277 r.Header = make(http.Header) 278 } 279 for k, v := range h { 280 r.Header.Add(k, v) 281 } 282 } 283 } 284 285 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 286 // 287 func (f IndicesAddBlock) WithOpaqueID(s string) func(*IndicesAddBlockRequest) { 288 return func(r *IndicesAddBlockRequest) { 289 if r.Header == nil { 290 r.Header = make(http.Header) 291 } 292 r.Header.Set("X-Opaque-Id", s) 293 } 294 }