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