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