github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.exists_index_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 "net/http" 32 "strconv" 33 "strings" 34 "time" 35 ) 36 37 func newIndicesExistsIndexTemplateFunc(t Transport) IndicesExistsIndexTemplate { 38 return func(name string, o ...func(*IndicesExistsIndexTemplateRequest)) (*Response, error) { 39 var r = IndicesExistsIndexTemplateRequest{Name: name} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // IndicesExistsIndexTemplate returns information about whether a particular index template exists. 50 // 51 // 52 type IndicesExistsIndexTemplate func(name string, o ...func(*IndicesExistsIndexTemplateRequest)) (*Response, error) 53 54 // IndicesExistsIndexTemplateRequest configures the Indices Exists Index Template API request. 55 // 56 type IndicesExistsIndexTemplateRequest struct { 57 Name string 58 59 FlatSettings *bool 60 Local *bool 61 MasterTimeout time.Duration 62 ClusterManagerTimeout time.Duration 63 64 Pretty bool 65 Human bool 66 ErrorTrace bool 67 FilterPath []string 68 69 Header http.Header 70 71 ctx context.Context 72 } 73 74 // Do executes the request and returns response or error. 75 // 76 func (r IndicesExistsIndexTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 77 var ( 78 method string 79 path strings.Builder 80 params map[string]string 81 ) 82 83 method = "HEAD" 84 85 path.Grow(1 + len("_index_template") + 1 + len(r.Name)) 86 path.WriteString("/") 87 path.WriteString("_index_template") 88 path.WriteString("/") 89 path.WriteString(r.Name) 90 91 params = make(map[string]string) 92 93 if r.FlatSettings != nil { 94 params["flat_settings"] = strconv.FormatBool(*r.FlatSettings) 95 } 96 97 if r.Local != nil { 98 params["local"] = strconv.FormatBool(*r.Local) 99 } 100 101 if r.MasterTimeout != 0 { 102 params["master_timeout"] = formatDuration(r.MasterTimeout) 103 } 104 105 if r.ClusterManagerTimeout != 0 { 106 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 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(), nil) 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 len(r.Header) > 0 { 139 if len(req.Header) == 0 { 140 req.Header = r.Header 141 } else { 142 for k, vv := range r.Header { 143 for _, v := range vv { 144 req.Header.Add(k, v) 145 } 146 } 147 } 148 } 149 150 if ctx != nil { 151 req = req.WithContext(ctx) 152 } 153 154 res, err := transport.Perform(req) 155 if err != nil { 156 return nil, err 157 } 158 159 response := Response{ 160 StatusCode: res.StatusCode, 161 Body: res.Body, 162 Header: res.Header, 163 } 164 165 return &response, nil 166 } 167 168 // WithContext sets the request context. 169 // 170 func (f IndicesExistsIndexTemplate) WithContext(v context.Context) func(*IndicesExistsIndexTemplateRequest) { 171 return func(r *IndicesExistsIndexTemplateRequest) { 172 r.ctx = v 173 } 174 } 175 176 // WithFlatSettings - return settings in flat format (default: false). 177 // 178 func (f IndicesExistsIndexTemplate) WithFlatSettings(v bool) func(*IndicesExistsIndexTemplateRequest) { 179 return func(r *IndicesExistsIndexTemplateRequest) { 180 r.FlatSettings = &v 181 } 182 } 183 184 // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false). 185 // 186 func (f IndicesExistsIndexTemplate) WithLocal(v bool) func(*IndicesExistsIndexTemplateRequest) { 187 return func(r *IndicesExistsIndexTemplateRequest) { 188 r.Local = &v 189 } 190 } 191 192 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 193 // 194 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 195 // 196 func (f IndicesExistsIndexTemplate) WithMasterTimeout(v time.Duration) func(*IndicesExistsIndexTemplateRequest) { 197 return func(r *IndicesExistsIndexTemplateRequest) { 198 r.MasterTimeout = v 199 } 200 } 201 202 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 203 // 204 func (f IndicesExistsIndexTemplate) WithClusterManagerTimeout(v time.Duration) func(*IndicesExistsIndexTemplateRequest) { 205 return func(r *IndicesExistsIndexTemplateRequest) { 206 r.ClusterManagerTimeout = v 207 } 208 } 209 210 // WithPretty makes the response body pretty-printed. 211 // 212 func (f IndicesExistsIndexTemplate) WithPretty() func(*IndicesExistsIndexTemplateRequest) { 213 return func(r *IndicesExistsIndexTemplateRequest) { 214 r.Pretty = true 215 } 216 } 217 218 // WithHuman makes statistical values human-readable. 219 // 220 func (f IndicesExistsIndexTemplate) WithHuman() func(*IndicesExistsIndexTemplateRequest) { 221 return func(r *IndicesExistsIndexTemplateRequest) { 222 r.Human = true 223 } 224 } 225 226 // WithErrorTrace includes the stack trace for errors in the response body. 227 // 228 func (f IndicesExistsIndexTemplate) WithErrorTrace() func(*IndicesExistsIndexTemplateRequest) { 229 return func(r *IndicesExistsIndexTemplateRequest) { 230 r.ErrorTrace = true 231 } 232 } 233 234 // WithFilterPath filters the properties of the response body. 235 // 236 func (f IndicesExistsIndexTemplate) WithFilterPath(v ...string) func(*IndicesExistsIndexTemplateRequest) { 237 return func(r *IndicesExistsIndexTemplateRequest) { 238 r.FilterPath = v 239 } 240 } 241 242 // WithHeader adds the headers to the HTTP request. 243 // 244 func (f IndicesExistsIndexTemplate) WithHeader(h map[string]string) func(*IndicesExistsIndexTemplateRequest) { 245 return func(r *IndicesExistsIndexTemplateRequest) { 246 if r.Header == nil { 247 r.Header = make(http.Header) 248 } 249 for k, v := range h { 250 r.Header.Add(k, v) 251 } 252 } 253 } 254 255 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 256 // 257 func (f IndicesExistsIndexTemplate) WithOpaqueID(s string) func(*IndicesExistsIndexTemplateRequest) { 258 return func(r *IndicesExistsIndexTemplateRequest) { 259 if r.Header == nil { 260 r.Header = make(http.Header) 261 } 262 r.Header.Set("X-Opaque-Id", s) 263 } 264 }