api.indices.exists_template.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "errors"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. func newIndicesExistsTemplateFunc(t Transport) IndicesExistsTemplate {
  29. return func(name []string, o ...func(*IndicesExistsTemplateRequest)) (*Response, error) {
  30. var r = IndicesExistsTemplateRequest{Name: name}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesExistsTemplate returns information about whether a particular index template exists.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html.
  41. type IndicesExistsTemplate func(name []string, o ...func(*IndicesExistsTemplateRequest)) (*Response, error)
  42. // IndicesExistsTemplateRequest configures the Indices Exists Template API request.
  43. type IndicesExistsTemplateRequest struct {
  44. Name []string
  45. FlatSettings *bool
  46. Local *bool
  47. MasterTimeout time.Duration
  48. Pretty bool
  49. Human bool
  50. ErrorTrace bool
  51. FilterPath []string
  52. Header http.Header
  53. ctx context.Context
  54. }
  55. // Do executes the request and returns response or error.
  56. func (r IndicesExistsTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  57. var (
  58. method string
  59. path strings.Builder
  60. params map[string]string
  61. )
  62. method = "HEAD"
  63. if len(r.Name) == 0 {
  64. return nil, errors.New("name is required and cannot be nil or empty")
  65. }
  66. path.Grow(1 + len("_template") + 1 + len(strings.Join(r.Name, ",")))
  67. path.WriteString("/")
  68. path.WriteString("_template")
  69. path.WriteString("/")
  70. path.WriteString(strings.Join(r.Name, ","))
  71. params = make(map[string]string)
  72. if r.FlatSettings != nil {
  73. params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
  74. }
  75. if r.Local != nil {
  76. params["local"] = strconv.FormatBool(*r.Local)
  77. }
  78. if r.MasterTimeout != 0 {
  79. params["master_timeout"] = formatDuration(r.MasterTimeout)
  80. }
  81. if r.Pretty {
  82. params["pretty"] = "true"
  83. }
  84. if r.Human {
  85. params["human"] = "true"
  86. }
  87. if r.ErrorTrace {
  88. params["error_trace"] = "true"
  89. }
  90. if len(r.FilterPath) > 0 {
  91. params["filter_path"] = strings.Join(r.FilterPath, ",")
  92. }
  93. req, err := newRequest(method, path.String(), nil)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if len(params) > 0 {
  98. q := req.URL.Query()
  99. for k, v := range params {
  100. q.Set(k, v)
  101. }
  102. req.URL.RawQuery = q.Encode()
  103. }
  104. if len(r.Header) > 0 {
  105. if len(req.Header) == 0 {
  106. req.Header = r.Header
  107. } else {
  108. for k, vv := range r.Header {
  109. for _, v := range vv {
  110. req.Header.Add(k, v)
  111. }
  112. }
  113. }
  114. }
  115. if ctx != nil {
  116. req = req.WithContext(ctx)
  117. }
  118. res, err := transport.Perform(req)
  119. if err != nil {
  120. return nil, err
  121. }
  122. response := Response{
  123. StatusCode: res.StatusCode,
  124. Body: res.Body,
  125. Header: res.Header,
  126. }
  127. return &response, nil
  128. }
  129. // WithContext sets the request context.
  130. func (f IndicesExistsTemplate) WithContext(v context.Context) func(*IndicesExistsTemplateRequest) {
  131. return func(r *IndicesExistsTemplateRequest) {
  132. r.ctx = v
  133. }
  134. }
  135. // WithFlatSettings - return settings in flat format (default: false).
  136. func (f IndicesExistsTemplate) WithFlatSettings(v bool) func(*IndicesExistsTemplateRequest) {
  137. return func(r *IndicesExistsTemplateRequest) {
  138. r.FlatSettings = &v
  139. }
  140. }
  141. // WithLocal - return local information, do not retrieve the state from master node (default: false).
  142. func (f IndicesExistsTemplate) WithLocal(v bool) func(*IndicesExistsTemplateRequest) {
  143. return func(r *IndicesExistsTemplateRequest) {
  144. r.Local = &v
  145. }
  146. }
  147. // WithMasterTimeout - explicit operation timeout for connection to master node.
  148. func (f IndicesExistsTemplate) WithMasterTimeout(v time.Duration) func(*IndicesExistsTemplateRequest) {
  149. return func(r *IndicesExistsTemplateRequest) {
  150. r.MasterTimeout = v
  151. }
  152. }
  153. // WithPretty makes the response body pretty-printed.
  154. func (f IndicesExistsTemplate) WithPretty() func(*IndicesExistsTemplateRequest) {
  155. return func(r *IndicesExistsTemplateRequest) {
  156. r.Pretty = true
  157. }
  158. }
  159. // WithHuman makes statistical values human-readable.
  160. func (f IndicesExistsTemplate) WithHuman() func(*IndicesExistsTemplateRequest) {
  161. return func(r *IndicesExistsTemplateRequest) {
  162. r.Human = true
  163. }
  164. }
  165. // WithErrorTrace includes the stack trace for errors in the response body.
  166. func (f IndicesExistsTemplate) WithErrorTrace() func(*IndicesExistsTemplateRequest) {
  167. return func(r *IndicesExistsTemplateRequest) {
  168. r.ErrorTrace = true
  169. }
  170. }
  171. // WithFilterPath filters the properties of the response body.
  172. func (f IndicesExistsTemplate) WithFilterPath(v ...string) func(*IndicesExistsTemplateRequest) {
  173. return func(r *IndicesExistsTemplateRequest) {
  174. r.FilterPath = v
  175. }
  176. }
  177. // WithHeader adds the headers to the HTTP request.
  178. func (f IndicesExistsTemplate) WithHeader(h map[string]string) func(*IndicesExistsTemplateRequest) {
  179. return func(r *IndicesExistsTemplateRequest) {
  180. if r.Header == nil {
  181. r.Header = make(http.Header)
  182. }
  183. for k, v := range h {
  184. r.Header.Add(k, v)
  185. }
  186. }
  187. }
  188. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  189. func (f IndicesExistsTemplate) WithOpaqueID(s string) func(*IndicesExistsTemplateRequest) {
  190. return func(r *IndicesExistsTemplateRequest) {
  191. if r.Header == nil {
  192. r.Header = make(http.Header)
  193. }
  194. r.Header.Set("X-Opaque-Id", s)
  195. }
  196. }