github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.simulate_template.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 "time" 36 ) 37 38 func newIndicesSimulateTemplateFunc(t Transport) IndicesSimulateTemplate { 39 return func(o ...func(*IndicesSimulateTemplateRequest)) (*Response, error) { 40 var r = IndicesSimulateTemplateRequest{} 41 for _, f := range o { 42 f(&r) 43 } 44 return r.Do(r.ctx, t) 45 } 46 } 47 48 // ----- API Definition ------------------------------------------------------- 49 50 // IndicesSimulateTemplate simulate resolving the given template name or body 51 // 52 // 53 type IndicesSimulateTemplate func(o ...func(*IndicesSimulateTemplateRequest)) (*Response, error) 54 55 // IndicesSimulateTemplateRequest configures the Indices Simulate Template API request. 56 // 57 type IndicesSimulateTemplateRequest struct { 58 Body io.Reader 59 60 Name string 61 62 Cause string 63 Create *bool 64 MasterTimeout time.Duration 65 ClusterManagerTimeout time.Duration 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 IndicesSimulateTemplateRequest) 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("_index_template") + 1 + len("_simulate") + 1 + len(r.Name)) 89 path.WriteString("/") 90 path.WriteString("_index_template") 91 path.WriteString("/") 92 path.WriteString("_simulate") 93 if r.Name != "" { 94 path.WriteString("/") 95 path.WriteString(r.Name) 96 } 97 98 params = make(map[string]string) 99 100 if r.Cause != "" { 101 params["cause"] = r.Cause 102 } 103 104 if r.Create != nil { 105 params["create"] = strconv.FormatBool(*r.Create) 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.Pretty { 117 params["pretty"] = "true" 118 } 119 120 if r.Human { 121 params["human"] = "true" 122 } 123 124 if r.ErrorTrace { 125 params["error_trace"] = "true" 126 } 127 128 if len(r.FilterPath) > 0 { 129 params["filter_path"] = strings.Join(r.FilterPath, ",") 130 } 131 132 req, err := newRequest(method, path.String(), r.Body) 133 if err != nil { 134 return nil, err 135 } 136 137 if len(params) > 0 { 138 q := req.URL.Query() 139 for k, v := range params { 140 q.Set(k, v) 141 } 142 req.URL.RawQuery = q.Encode() 143 } 144 145 if r.Body != nil { 146 req.Header[headerContentType] = headerContentTypeJSON 147 } 148 149 if len(r.Header) > 0 { 150 if len(req.Header) == 0 { 151 req.Header = r.Header 152 } else { 153 for k, vv := range r.Header { 154 for _, v := range vv { 155 req.Header.Add(k, v) 156 } 157 } 158 } 159 } 160 161 if ctx != nil { 162 req = req.WithContext(ctx) 163 } 164 165 res, err := transport.Perform(req) 166 if err != nil { 167 return nil, err 168 } 169 170 response := Response{ 171 StatusCode: res.StatusCode, 172 Body: res.Body, 173 Header: res.Header, 174 } 175 176 return &response, nil 177 } 178 179 // WithContext sets the request context. 180 // 181 func (f IndicesSimulateTemplate) WithContext(v context.Context) func(*IndicesSimulateTemplateRequest) { 182 return func(r *IndicesSimulateTemplateRequest) { 183 r.ctx = v 184 } 185 } 186 187 // WithBody - New index template definition to be simulated, if no index template name is specified. 188 // 189 func (f IndicesSimulateTemplate) WithBody(v io.Reader) func(*IndicesSimulateTemplateRequest) { 190 return func(r *IndicesSimulateTemplateRequest) { 191 r.Body = v 192 } 193 } 194 195 // WithName - the name of the index template. 196 // 197 func (f IndicesSimulateTemplate) WithName(v string) func(*IndicesSimulateTemplateRequest) { 198 return func(r *IndicesSimulateTemplateRequest) { 199 r.Name = v 200 } 201 } 202 203 // WithCause - user defined reason for dry-run creating the new template for simulation purposes. 204 // 205 func (f IndicesSimulateTemplate) WithCause(v string) func(*IndicesSimulateTemplateRequest) { 206 return func(r *IndicesSimulateTemplateRequest) { 207 r.Cause = v 208 } 209 } 210 211 // WithCreate - whether the index template we optionally defined in the body should only be dry-run added if new or can also replace an existing one. 212 // 213 func (f IndicesSimulateTemplate) WithCreate(v bool) func(*IndicesSimulateTemplateRequest) { 214 return func(r *IndicesSimulateTemplateRequest) { 215 r.Create = &v 216 } 217 } 218 219 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 220 // 221 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 222 // 223 func (f IndicesSimulateTemplate) WithMasterTimeout(v time.Duration) func(*IndicesSimulateTemplateRequest) { 224 return func(r *IndicesSimulateTemplateRequest) { 225 r.MasterTimeout = v 226 } 227 } 228 229 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 230 // 231 func (f IndicesSimulateTemplate) WithClusterManagerTimeout(v time.Duration) func(*IndicesSimulateTemplateRequest) { 232 return func(r *IndicesSimulateTemplateRequest) { 233 r.ClusterManagerTimeout = v 234 } 235 } 236 237 // WithPretty makes the response body pretty-printed. 238 // 239 func (f IndicesSimulateTemplate) WithPretty() func(*IndicesSimulateTemplateRequest) { 240 return func(r *IndicesSimulateTemplateRequest) { 241 r.Pretty = true 242 } 243 } 244 245 // WithHuman makes statistical values human-readable. 246 // 247 func (f IndicesSimulateTemplate) WithHuman() func(*IndicesSimulateTemplateRequest) { 248 return func(r *IndicesSimulateTemplateRequest) { 249 r.Human = true 250 } 251 } 252 253 // WithErrorTrace includes the stack trace for errors in the response body. 254 // 255 func (f IndicesSimulateTemplate) WithErrorTrace() func(*IndicesSimulateTemplateRequest) { 256 return func(r *IndicesSimulateTemplateRequest) { 257 r.ErrorTrace = true 258 } 259 } 260 261 // WithFilterPath filters the properties of the response body. 262 // 263 func (f IndicesSimulateTemplate) WithFilterPath(v ...string) func(*IndicesSimulateTemplateRequest) { 264 return func(r *IndicesSimulateTemplateRequest) { 265 r.FilterPath = v 266 } 267 } 268 269 // WithHeader adds the headers to the HTTP request. 270 // 271 func (f IndicesSimulateTemplate) WithHeader(h map[string]string) func(*IndicesSimulateTemplateRequest) { 272 return func(r *IndicesSimulateTemplateRequest) { 273 if r.Header == nil { 274 r.Header = make(http.Header) 275 } 276 for k, v := range h { 277 r.Header.Add(k, v) 278 } 279 } 280 } 281 282 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 283 // 284 func (f IndicesSimulateTemplate) WithOpaqueID(s string) func(*IndicesSimulateTemplateRequest) { 285 return func(r *IndicesSimulateTemplateRequest) { 286 if r.Header == nil { 287 r.Header = make(http.Header) 288 } 289 r.Header.Set("X-Opaque-Id", s) 290 } 291 }