github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.clone.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 "strings" 34 "time" 35 ) 36 37 func newIndicesCloneFunc(t Transport) IndicesClone { 38 return func(index string, target string, o ...func(*IndicesCloneRequest)) (*Response, error) { 39 var r = IndicesCloneRequest{Index: index, Target: target} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // IndicesClone clones an index 50 // 51 // 52 type IndicesClone func(index string, target string, o ...func(*IndicesCloneRequest)) (*Response, error) 53 54 // IndicesCloneRequest configures the Indices Clone API request. 55 // 56 type IndicesCloneRequest struct { 57 Index string 58 59 Body io.Reader 60 61 Target string 62 63 MasterTimeout time.Duration 64 ClusterManagerTimeout time.Duration 65 Timeout time.Duration 66 WaitForActiveShards string 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 IndicesCloneRequest) 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(r.Index) + 1 + len("_clone") + 1 + len(r.Target)) 90 path.WriteString("/") 91 path.WriteString(r.Index) 92 path.WriteString("/") 93 path.WriteString("_clone") 94 path.WriteString("/") 95 path.WriteString(r.Target) 96 97 params = make(map[string]string) 98 99 if r.MasterTimeout != 0 { 100 params["master_timeout"] = formatDuration(r.MasterTimeout) 101 } 102 103 if r.ClusterManagerTimeout != 0 { 104 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 105 } 106 107 if r.Timeout != 0 { 108 params["timeout"] = formatDuration(r.Timeout) 109 } 110 111 if r.WaitForActiveShards != "" { 112 params["wait_for_active_shards"] = r.WaitForActiveShards 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(), r.Body) 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 r.Body != nil { 145 req.Header[headerContentType] = headerContentTypeJSON 146 } 147 148 if len(r.Header) > 0 { 149 if len(req.Header) == 0 { 150 req.Header = r.Header 151 } else { 152 for k, vv := range r.Header { 153 for _, v := range vv { 154 req.Header.Add(k, v) 155 } 156 } 157 } 158 } 159 160 if ctx != nil { 161 req = req.WithContext(ctx) 162 } 163 164 res, err := transport.Perform(req) 165 if err != nil { 166 return nil, err 167 } 168 169 response := Response{ 170 StatusCode: res.StatusCode, 171 Body: res.Body, 172 Header: res.Header, 173 } 174 175 return &response, nil 176 } 177 178 // WithContext sets the request context. 179 // 180 func (f IndicesClone) WithContext(v context.Context) func(*IndicesCloneRequest) { 181 return func(r *IndicesCloneRequest) { 182 r.ctx = v 183 } 184 } 185 186 // WithBody - The configuration for the target index (`settings` and `aliases`). 187 // 188 func (f IndicesClone) WithBody(v io.Reader) func(*IndicesCloneRequest) { 189 return func(r *IndicesCloneRequest) { 190 r.Body = v 191 } 192 } 193 194 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 195 // 196 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 197 // 198 func (f IndicesClone) WithMasterTimeout(v time.Duration) func(*IndicesCloneRequest) { 199 return func(r *IndicesCloneRequest) { 200 r.MasterTimeout = v 201 } 202 } 203 204 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 205 // 206 func (f IndicesClone) WithClusterManagerTimeout(v time.Duration) func(*IndicesCloneRequest) { 207 return func(r *IndicesCloneRequest) { 208 r.ClusterManagerTimeout = v 209 } 210 } 211 212 // WithTimeout - explicit operation timeout. 213 // 214 func (f IndicesClone) WithTimeout(v time.Duration) func(*IndicesCloneRequest) { 215 return func(r *IndicesCloneRequest) { 216 r.Timeout = v 217 } 218 } 219 220 // WithWaitForActiveShards - set the number of active shards to wait for on the cloned index before the operation returns.. 221 // 222 func (f IndicesClone) WithWaitForActiveShards(v string) func(*IndicesCloneRequest) { 223 return func(r *IndicesCloneRequest) { 224 r.WaitForActiveShards = v 225 } 226 } 227 228 // WithPretty makes the response body pretty-printed. 229 // 230 func (f IndicesClone) WithPretty() func(*IndicesCloneRequest) { 231 return func(r *IndicesCloneRequest) { 232 r.Pretty = true 233 } 234 } 235 236 // WithHuman makes statistical values human-readable. 237 // 238 func (f IndicesClone) WithHuman() func(*IndicesCloneRequest) { 239 return func(r *IndicesCloneRequest) { 240 r.Human = true 241 } 242 } 243 244 // WithErrorTrace includes the stack trace for errors in the response body. 245 // 246 func (f IndicesClone) WithErrorTrace() func(*IndicesCloneRequest) { 247 return func(r *IndicesCloneRequest) { 248 r.ErrorTrace = true 249 } 250 } 251 252 // WithFilterPath filters the properties of the response body. 253 // 254 func (f IndicesClone) WithFilterPath(v ...string) func(*IndicesCloneRequest) { 255 return func(r *IndicesCloneRequest) { 256 r.FilterPath = v 257 } 258 } 259 260 // WithHeader adds the headers to the HTTP request. 261 // 262 func (f IndicesClone) WithHeader(h map[string]string) func(*IndicesCloneRequest) { 263 return func(r *IndicesCloneRequest) { 264 if r.Header == nil { 265 r.Header = make(http.Header) 266 } 267 for k, v := range h { 268 r.Header.Add(k, v) 269 } 270 } 271 } 272 273 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 274 // 275 func (f IndicesClone) WithOpaqueID(s string) func(*IndicesCloneRequest) { 276 return func(r *IndicesCloneRequest) { 277 if r.Header == nil { 278 r.Header = make(http.Header) 279 } 280 r.Header.Set("X-Opaque-Id", s) 281 } 282 }