api.indices.rollover.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 newIndicesRolloverFunc(t Transport) IndicesRollover {
  29. return func(alias string, o ...func(*IndicesRolloverRequest)) (*Response, error) {
  30. var r = IndicesRolloverRequest{Alias: alias}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesRollover updates an alias to point to a new index when the existing index
  39. // is considered to be too large or too old.
  40. //
  41. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-rollover-index.html.
  42. type IndicesRollover func(alias string, o ...func(*IndicesRolloverRequest)) (*Response, error)
  43. // IndicesRolloverRequest configures the Indices Rollover API request.
  44. type IndicesRolloverRequest struct {
  45. Body io.Reader
  46. Alias string
  47. NewIndex string
  48. DryRun *bool
  49. IncludeTypeName *bool
  50. MasterTimeout time.Duration
  51. Timeout time.Duration
  52. WaitForActiveShards string
  53. Pretty bool
  54. Human bool
  55. ErrorTrace bool
  56. FilterPath []string
  57. Header http.Header
  58. ctx context.Context
  59. }
  60. // Do executes the request and returns response or error.
  61. func (r IndicesRolloverRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  62. var (
  63. method string
  64. path strings.Builder
  65. params map[string]string
  66. )
  67. method = "POST"
  68. path.Grow(1 + len(r.Alias) + 1 + len("_rollover") + 1 + len(r.NewIndex))
  69. path.WriteString("/")
  70. path.WriteString(r.Alias)
  71. path.WriteString("/")
  72. path.WriteString("_rollover")
  73. if r.NewIndex != "" {
  74. path.WriteString("/")
  75. path.WriteString(r.NewIndex)
  76. }
  77. params = make(map[string]string)
  78. if r.DryRun != nil {
  79. params["dry_run"] = strconv.FormatBool(*r.DryRun)
  80. }
  81. if r.IncludeTypeName != nil {
  82. params["include_type_name"] = strconv.FormatBool(*r.IncludeTypeName)
  83. }
  84. if r.MasterTimeout != 0 {
  85. params["master_timeout"] = formatDuration(r.MasterTimeout)
  86. }
  87. if r.Timeout != 0 {
  88. params["timeout"] = formatDuration(r.Timeout)
  89. }
  90. if r.WaitForActiveShards != "" {
  91. params["wait_for_active_shards"] = r.WaitForActiveShards
  92. }
  93. if r.Pretty {
  94. params["pretty"] = "true"
  95. }
  96. if r.Human {
  97. params["human"] = "true"
  98. }
  99. if r.ErrorTrace {
  100. params["error_trace"] = "true"
  101. }
  102. if len(r.FilterPath) > 0 {
  103. params["filter_path"] = strings.Join(r.FilterPath, ",")
  104. }
  105. req, err := newRequest(method, path.String(), r.Body)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if len(params) > 0 {
  110. q := req.URL.Query()
  111. for k, v := range params {
  112. q.Set(k, v)
  113. }
  114. req.URL.RawQuery = q.Encode()
  115. }
  116. if len(r.Header) > 0 {
  117. if len(req.Header) == 0 {
  118. req.Header = r.Header
  119. } else {
  120. for k, vv := range r.Header {
  121. for _, v := range vv {
  122. req.Header.Add(k, v)
  123. }
  124. }
  125. }
  126. }
  127. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  128. req.Header[headerContentType] = headerContentTypeJSON
  129. }
  130. if ctx != nil {
  131. req = req.WithContext(ctx)
  132. }
  133. res, err := transport.Perform(req)
  134. if err != nil {
  135. return nil, err
  136. }
  137. response := Response{
  138. StatusCode: res.StatusCode,
  139. Body: res.Body,
  140. Header: res.Header,
  141. }
  142. return &response, nil
  143. }
  144. // WithContext sets the request context.
  145. func (f IndicesRollover) WithContext(v context.Context) func(*IndicesRolloverRequest) {
  146. return func(r *IndicesRolloverRequest) {
  147. r.ctx = v
  148. }
  149. }
  150. // WithBody - The conditions that needs to be met for executing rollover.
  151. func (f IndicesRollover) WithBody(v io.Reader) func(*IndicesRolloverRequest) {
  152. return func(r *IndicesRolloverRequest) {
  153. r.Body = v
  154. }
  155. }
  156. // WithNewIndex - the name of the rollover index.
  157. func (f IndicesRollover) WithNewIndex(v string) func(*IndicesRolloverRequest) {
  158. return func(r *IndicesRolloverRequest) {
  159. r.NewIndex = v
  160. }
  161. }
  162. // WithDryRun - if set to true the rollover action will only be validated but not actually performed even if a condition matches. the default is false.
  163. func (f IndicesRollover) WithDryRun(v bool) func(*IndicesRolloverRequest) {
  164. return func(r *IndicesRolloverRequest) {
  165. r.DryRun = &v
  166. }
  167. }
  168. // WithIncludeTypeName - whether a type should be included in the body of the mappings..
  169. func (f IndicesRollover) WithIncludeTypeName(v bool) func(*IndicesRolloverRequest) {
  170. return func(r *IndicesRolloverRequest) {
  171. r.IncludeTypeName = &v
  172. }
  173. }
  174. // WithMasterTimeout - specify timeout for connection to master.
  175. func (f IndicesRollover) WithMasterTimeout(v time.Duration) func(*IndicesRolloverRequest) {
  176. return func(r *IndicesRolloverRequest) {
  177. r.MasterTimeout = v
  178. }
  179. }
  180. // WithTimeout - explicit operation timeout.
  181. func (f IndicesRollover) WithTimeout(v time.Duration) func(*IndicesRolloverRequest) {
  182. return func(r *IndicesRolloverRequest) {
  183. r.Timeout = v
  184. }
  185. }
  186. // WithWaitForActiveShards - set the number of active shards to wait for on the newly created rollover index before the operation returns..
  187. func (f IndicesRollover) WithWaitForActiveShards(v string) func(*IndicesRolloverRequest) {
  188. return func(r *IndicesRolloverRequest) {
  189. r.WaitForActiveShards = v
  190. }
  191. }
  192. // WithPretty makes the response body pretty-printed.
  193. func (f IndicesRollover) WithPretty() func(*IndicesRolloverRequest) {
  194. return func(r *IndicesRolloverRequest) {
  195. r.Pretty = true
  196. }
  197. }
  198. // WithHuman makes statistical values human-readable.
  199. func (f IndicesRollover) WithHuman() func(*IndicesRolloverRequest) {
  200. return func(r *IndicesRolloverRequest) {
  201. r.Human = true
  202. }
  203. }
  204. // WithErrorTrace includes the stack trace for errors in the response body.
  205. func (f IndicesRollover) WithErrorTrace() func(*IndicesRolloverRequest) {
  206. return func(r *IndicesRolloverRequest) {
  207. r.ErrorTrace = true
  208. }
  209. }
  210. // WithFilterPath filters the properties of the response body.
  211. func (f IndicesRollover) WithFilterPath(v ...string) func(*IndicesRolloverRequest) {
  212. return func(r *IndicesRolloverRequest) {
  213. r.FilterPath = v
  214. }
  215. }
  216. // WithHeader adds the headers to the HTTP request.
  217. func (f IndicesRollover) WithHeader(h map[string]string) func(*IndicesRolloverRequest) {
  218. return func(r *IndicesRolloverRequest) {
  219. if r.Header == nil {
  220. r.Header = make(http.Header)
  221. }
  222. for k, v := range h {
  223. r.Header.Add(k, v)
  224. }
  225. }
  226. }
  227. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  228. func (f IndicesRollover) WithOpaqueID(s string) func(*IndicesRolloverRequest) {
  229. return func(r *IndicesRolloverRequest) {
  230. if r.Header == nil {
  231. r.Header = make(http.Header)
  232. }
  233. r.Header.Set("X-Opaque-Id", s)
  234. }
  235. }