github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.put_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 newIndicesPutTemplateFunc(t Transport) IndicesPutTemplate { 39 return func(name string, body io.Reader, o ...func(*IndicesPutTemplateRequest)) (*Response, error) { 40 var r = IndicesPutTemplateRequest{Name: name, Body: body} 41 for _, f := range o { 42 f(&r) 43 } 44 return r.Do(r.ctx, t) 45 } 46 } 47 48 // ----- API Definition ------------------------------------------------------- 49 50 // IndicesPutTemplate creates or updates an index template. 51 // 52 // 53 type IndicesPutTemplate func(name string, body io.Reader, o ...func(*IndicesPutTemplateRequest)) (*Response, error) 54 55 // IndicesPutTemplateRequest configures the Indices Put Template API request. 56 // 57 type IndicesPutTemplateRequest struct { 58 Body io.Reader 59 60 Name string 61 62 Create *bool 63 MasterTimeout time.Duration 64 ClusterManagerTimeout time.Duration 65 Order *int 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 IndicesPutTemplateRequest) 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 = "PUT" 87 88 path.Grow(1 + len("_template") + 1 + len(r.Name)) 89 path.WriteString("/") 90 path.WriteString("_template") 91 path.WriteString("/") 92 path.WriteString(r.Name) 93 94 params = make(map[string]string) 95 96 if r.Create != nil { 97 params["create"] = strconv.FormatBool(*r.Create) 98 } 99 100 if r.MasterTimeout != 0 { 101 params["master_timeout"] = formatDuration(r.MasterTimeout) 102 } 103 104 if r.ClusterManagerTimeout != 0 { 105 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 106 } 107 108 if r.Order != nil { 109 params["order"] = strconv.FormatInt(int64(*r.Order), 10) 110 } 111 112 if r.Pretty { 113 params["pretty"] = "true" 114 } 115 116 if r.Human { 117 params["human"] = "true" 118 } 119 120 if r.ErrorTrace { 121 params["error_trace"] = "true" 122 } 123 124 if len(r.FilterPath) > 0 { 125 params["filter_path"] = strings.Join(r.FilterPath, ",") 126 } 127 128 req, err := newRequest(method, path.String(), r.Body) 129 if err != nil { 130 return nil, err 131 } 132 133 if len(params) > 0 { 134 q := req.URL.Query() 135 for k, v := range params { 136 q.Set(k, v) 137 } 138 req.URL.RawQuery = q.Encode() 139 } 140 141 if r.Body != nil { 142 req.Header[headerContentType] = headerContentTypeJSON 143 } 144 145 if len(r.Header) > 0 { 146 if len(req.Header) == 0 { 147 req.Header = r.Header 148 } else { 149 for k, vv := range r.Header { 150 for _, v := range vv { 151 req.Header.Add(k, v) 152 } 153 } 154 } 155 } 156 157 if ctx != nil { 158 req = req.WithContext(ctx) 159 } 160 161 res, err := transport.Perform(req) 162 if err != nil { 163 return nil, err 164 } 165 166 response := Response{ 167 StatusCode: res.StatusCode, 168 Body: res.Body, 169 Header: res.Header, 170 } 171 172 return &response, nil 173 } 174 175 // WithContext sets the request context. 176 // 177 func (f IndicesPutTemplate) WithContext(v context.Context) func(*IndicesPutTemplateRequest) { 178 return func(r *IndicesPutTemplateRequest) { 179 r.ctx = v 180 } 181 } 182 183 // WithCreate - whether the index template should only be added if new or can also replace an existing one. 184 // 185 func (f IndicesPutTemplate) WithCreate(v bool) func(*IndicesPutTemplateRequest) { 186 return func(r *IndicesPutTemplateRequest) { 187 r.Create = &v 188 } 189 } 190 191 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 192 // 193 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 194 // 195 func (f IndicesPutTemplate) WithMasterTimeout(v time.Duration) func(*IndicesPutTemplateRequest) { 196 return func(r *IndicesPutTemplateRequest) { 197 r.MasterTimeout = v 198 } 199 } 200 201 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 202 // 203 func (f IndicesPutTemplate) WithClusterManagerTimeout(v time.Duration) func(*IndicesPutTemplateRequest) { 204 return func(r *IndicesPutTemplateRequest) { 205 r.ClusterManagerTimeout = v 206 } 207 } 208 209 // WithOrder - the order for this template when merging multiple matching ones (higher numbers are merged later, overriding the lower numbers). 210 // 211 func (f IndicesPutTemplate) WithOrder(v int) func(*IndicesPutTemplateRequest) { 212 return func(r *IndicesPutTemplateRequest) { 213 r.Order = &v 214 } 215 } 216 217 // WithPretty makes the response body pretty-printed. 218 // 219 func (f IndicesPutTemplate) WithPretty() func(*IndicesPutTemplateRequest) { 220 return func(r *IndicesPutTemplateRequest) { 221 r.Pretty = true 222 } 223 } 224 225 // WithHuman makes statistical values human-readable. 226 // 227 func (f IndicesPutTemplate) WithHuman() func(*IndicesPutTemplateRequest) { 228 return func(r *IndicesPutTemplateRequest) { 229 r.Human = true 230 } 231 } 232 233 // WithErrorTrace includes the stack trace for errors in the response body. 234 // 235 func (f IndicesPutTemplate) WithErrorTrace() func(*IndicesPutTemplateRequest) { 236 return func(r *IndicesPutTemplateRequest) { 237 r.ErrorTrace = true 238 } 239 } 240 241 // WithFilterPath filters the properties of the response body. 242 // 243 func (f IndicesPutTemplate) WithFilterPath(v ...string) func(*IndicesPutTemplateRequest) { 244 return func(r *IndicesPutTemplateRequest) { 245 r.FilterPath = v 246 } 247 } 248 249 // WithHeader adds the headers to the HTTP request. 250 // 251 func (f IndicesPutTemplate) WithHeader(h map[string]string) func(*IndicesPutTemplateRequest) { 252 return func(r *IndicesPutTemplateRequest) { 253 if r.Header == nil { 254 r.Header = make(http.Header) 255 } 256 for k, v := range h { 257 r.Header.Add(k, v) 258 } 259 } 260 } 261 262 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 263 // 264 func (f IndicesPutTemplate) WithOpaqueID(s string) func(*IndicesPutTemplateRequest) { 265 return func(r *IndicesPutTemplateRequest) { 266 if r.Header == nil { 267 r.Header = make(http.Header) 268 } 269 r.Header.Set("X-Opaque-Id", s) 270 } 271 }