api.indices.put_template.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "io"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. func newIndicesPutTemplateFunc(t Transport) IndicesPutTemplate {
  29. return func(name string, body io.Reader, o ...func(*IndicesPutTemplateRequest)) (*Response, error) {
  30. var r = IndicesPutTemplateRequest{Name: name, Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesPutTemplate creates or updates an index template.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html.
  41. type IndicesPutTemplate func(name string, body io.Reader, o ...func(*IndicesPutTemplateRequest)) (*Response, error)
  42. // IndicesPutTemplateRequest configures the Indices Put Template API request.
  43. type IndicesPutTemplateRequest struct {
  44. Body io.Reader
  45. Name string
  46. Create *bool
  47. IncludeTypeName *bool
  48. MasterTimeout time.Duration
  49. Order *int
  50. Pretty bool
  51. Human bool
  52. ErrorTrace bool
  53. FilterPath []string
  54. Header http.Header
  55. ctx context.Context
  56. }
  57. // Do executes the request and returns response or error.
  58. func (r IndicesPutTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  59. var (
  60. method string
  61. path strings.Builder
  62. params map[string]string
  63. )
  64. method = "PUT"
  65. path.Grow(1 + len("_template") + 1 + len(r.Name))
  66. path.WriteString("/")
  67. path.WriteString("_template")
  68. path.WriteString("/")
  69. path.WriteString(r.Name)
  70. params = make(map[string]string)
  71. if r.Create != nil {
  72. params["create"] = strconv.FormatBool(*r.Create)
  73. }
  74. if r.IncludeTypeName != nil {
  75. params["include_type_name"] = strconv.FormatBool(*r.IncludeTypeName)
  76. }
  77. if r.MasterTimeout != 0 {
  78. params["master_timeout"] = formatDuration(r.MasterTimeout)
  79. }
  80. if r.Order != nil {
  81. params["order"] = strconv.FormatInt(int64(*r.Order), 10)
  82. }
  83. if r.Pretty {
  84. params["pretty"] = "true"
  85. }
  86. if r.Human {
  87. params["human"] = "true"
  88. }
  89. if r.ErrorTrace {
  90. params["error_trace"] = "true"
  91. }
  92. if len(r.FilterPath) > 0 {
  93. params["filter_path"] = strings.Join(r.FilterPath, ",")
  94. }
  95. req, err := newRequest(method, path.String(), r.Body)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if len(params) > 0 {
  100. q := req.URL.Query()
  101. for k, v := range params {
  102. q.Set(k, v)
  103. }
  104. req.URL.RawQuery = q.Encode()
  105. }
  106. if len(r.Header) > 0 {
  107. if len(req.Header) == 0 {
  108. req.Header = r.Header
  109. } else {
  110. for k, vv := range r.Header {
  111. for _, v := range vv {
  112. req.Header.Add(k, v)
  113. }
  114. }
  115. }
  116. }
  117. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  118. req.Header[headerContentType] = headerContentTypeJSON
  119. }
  120. if ctx != nil {
  121. req = req.WithContext(ctx)
  122. }
  123. res, err := transport.Perform(req)
  124. if err != nil {
  125. return nil, err
  126. }
  127. response := Response{
  128. StatusCode: res.StatusCode,
  129. Body: res.Body,
  130. Header: res.Header,
  131. }
  132. return &response, nil
  133. }
  134. // WithContext sets the request context.
  135. func (f IndicesPutTemplate) WithContext(v context.Context) func(*IndicesPutTemplateRequest) {
  136. return func(r *IndicesPutTemplateRequest) {
  137. r.ctx = v
  138. }
  139. }
  140. // WithCreate - whether the index template should only be added if new or can also replace an existing one.
  141. func (f IndicesPutTemplate) WithCreate(v bool) func(*IndicesPutTemplateRequest) {
  142. return func(r *IndicesPutTemplateRequest) {
  143. r.Create = &v
  144. }
  145. }
  146. // WithIncludeTypeName - whether a type should be returned in the body of the mappings..
  147. func (f IndicesPutTemplate) WithIncludeTypeName(v bool) func(*IndicesPutTemplateRequest) {
  148. return func(r *IndicesPutTemplateRequest) {
  149. r.IncludeTypeName = &v
  150. }
  151. }
  152. // WithMasterTimeout - specify timeout for connection to master.
  153. func (f IndicesPutTemplate) WithMasterTimeout(v time.Duration) func(*IndicesPutTemplateRequest) {
  154. return func(r *IndicesPutTemplateRequest) {
  155. r.MasterTimeout = v
  156. }
  157. }
  158. // WithOrder - the order for this template when merging multiple matching ones (higher numbers are merged later, overriding the lower numbers).
  159. func (f IndicesPutTemplate) WithOrder(v int) func(*IndicesPutTemplateRequest) {
  160. return func(r *IndicesPutTemplateRequest) {
  161. r.Order = &v
  162. }
  163. }
  164. // WithPretty makes the response body pretty-printed.
  165. func (f IndicesPutTemplate) WithPretty() func(*IndicesPutTemplateRequest) {
  166. return func(r *IndicesPutTemplateRequest) {
  167. r.Pretty = true
  168. }
  169. }
  170. // WithHuman makes statistical values human-readable.
  171. func (f IndicesPutTemplate) WithHuman() func(*IndicesPutTemplateRequest) {
  172. return func(r *IndicesPutTemplateRequest) {
  173. r.Human = true
  174. }
  175. }
  176. // WithErrorTrace includes the stack trace for errors in the response body.
  177. func (f IndicesPutTemplate) WithErrorTrace() func(*IndicesPutTemplateRequest) {
  178. return func(r *IndicesPutTemplateRequest) {
  179. r.ErrorTrace = true
  180. }
  181. }
  182. // WithFilterPath filters the properties of the response body.
  183. func (f IndicesPutTemplate) WithFilterPath(v ...string) func(*IndicesPutTemplateRequest) {
  184. return func(r *IndicesPutTemplateRequest) {
  185. r.FilterPath = v
  186. }
  187. }
  188. // WithHeader adds the headers to the HTTP request.
  189. func (f IndicesPutTemplate) WithHeader(h map[string]string) func(*IndicesPutTemplateRequest) {
  190. return func(r *IndicesPutTemplateRequest) {
  191. if r.Header == nil {
  192. r.Header = make(http.Header)
  193. }
  194. for k, v := range h {
  195. r.Header.Add(k, v)
  196. }
  197. }
  198. }
  199. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  200. func (f IndicesPutTemplate) WithOpaqueID(s string) func(*IndicesPutTemplateRequest) {
  201. return func(r *IndicesPutTemplateRequest) {
  202. if r.Header == nil {
  203. r.Header = make(http.Header)
  204. }
  205. r.Header.Set("X-Opaque-Id", s)
  206. }
  207. }