api.xpack.indices.freeze.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. "net/http"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. func newIndicesFreezeFunc(t Transport) IndicesFreeze {
  28. return func(index string, o ...func(*IndicesFreezeRequest)) (*Response, error) {
  29. var r = IndicesFreezeRequest{Index: index}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // IndicesFreeze - Freezes an index. A frozen index has almost no overhead on the cluster (except for maintaining its metadata in memory) and is read-only.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/freeze-index-api.html.
  40. type IndicesFreeze func(index string, o ...func(*IndicesFreezeRequest)) (*Response, error)
  41. // IndicesFreezeRequest configures the Indices Freeze API request.
  42. type IndicesFreezeRequest struct {
  43. Index string
  44. AllowNoIndices *bool
  45. ExpandWildcards string
  46. IgnoreUnavailable *bool
  47. MasterTimeout time.Duration
  48. Timeout time.Duration
  49. WaitForActiveShards string
  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 IndicesFreezeRequest) 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 = "POST"
  65. path.Grow(1 + len(r.Index) + 1 + len("_freeze"))
  66. path.WriteString("/")
  67. path.WriteString(r.Index)
  68. path.WriteString("/")
  69. path.WriteString("_freeze")
  70. params = make(map[string]string)
  71. if r.AllowNoIndices != nil {
  72. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  73. }
  74. if r.ExpandWildcards != "" {
  75. params["expand_wildcards"] = r.ExpandWildcards
  76. }
  77. if r.IgnoreUnavailable != nil {
  78. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  79. }
  80. if r.MasterTimeout != 0 {
  81. params["master_timeout"] = formatDuration(r.MasterTimeout)
  82. }
  83. if r.Timeout != 0 {
  84. params["timeout"] = formatDuration(r.Timeout)
  85. }
  86. if r.WaitForActiveShards != "" {
  87. params["wait_for_active_shards"] = r.WaitForActiveShards
  88. }
  89. if r.Pretty {
  90. params["pretty"] = "true"
  91. }
  92. if r.Human {
  93. params["human"] = "true"
  94. }
  95. if r.ErrorTrace {
  96. params["error_trace"] = "true"
  97. }
  98. if len(r.FilterPath) > 0 {
  99. params["filter_path"] = strings.Join(r.FilterPath, ",")
  100. }
  101. req, err := newRequest(method, path.String(), nil)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if len(params) > 0 {
  106. q := req.URL.Query()
  107. for k, v := range params {
  108. q.Set(k, v)
  109. }
  110. req.URL.RawQuery = q.Encode()
  111. }
  112. if len(r.Header) > 0 {
  113. if len(req.Header) == 0 {
  114. req.Header = r.Header
  115. } else {
  116. for k, vv := range r.Header {
  117. for _, v := range vv {
  118. req.Header.Add(k, v)
  119. }
  120. }
  121. }
  122. }
  123. if ctx != nil {
  124. req = req.WithContext(ctx)
  125. }
  126. res, err := transport.Perform(req)
  127. if err != nil {
  128. return nil, err
  129. }
  130. response := Response{
  131. StatusCode: res.StatusCode,
  132. Body: res.Body,
  133. Header: res.Header,
  134. }
  135. return &response, nil
  136. }
  137. // WithContext sets the request context.
  138. func (f IndicesFreeze) WithContext(v context.Context) func(*IndicesFreezeRequest) {
  139. return func(r *IndicesFreezeRequest) {
  140. r.ctx = v
  141. }
  142. }
  143. // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
  144. func (f IndicesFreeze) WithAllowNoIndices(v bool) func(*IndicesFreezeRequest) {
  145. return func(r *IndicesFreezeRequest) {
  146. r.AllowNoIndices = &v
  147. }
  148. }
  149. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  150. func (f IndicesFreeze) WithExpandWildcards(v string) func(*IndicesFreezeRequest) {
  151. return func(r *IndicesFreezeRequest) {
  152. r.ExpandWildcards = v
  153. }
  154. }
  155. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  156. func (f IndicesFreeze) WithIgnoreUnavailable(v bool) func(*IndicesFreezeRequest) {
  157. return func(r *IndicesFreezeRequest) {
  158. r.IgnoreUnavailable = &v
  159. }
  160. }
  161. // WithMasterTimeout - specify timeout for connection to master.
  162. func (f IndicesFreeze) WithMasterTimeout(v time.Duration) func(*IndicesFreezeRequest) {
  163. return func(r *IndicesFreezeRequest) {
  164. r.MasterTimeout = v
  165. }
  166. }
  167. // WithTimeout - explicit operation timeout.
  168. func (f IndicesFreeze) WithTimeout(v time.Duration) func(*IndicesFreezeRequest) {
  169. return func(r *IndicesFreezeRequest) {
  170. r.Timeout = v
  171. }
  172. }
  173. // WithWaitForActiveShards - sets the number of active shards to wait for before the operation returns..
  174. func (f IndicesFreeze) WithWaitForActiveShards(v string) func(*IndicesFreezeRequest) {
  175. return func(r *IndicesFreezeRequest) {
  176. r.WaitForActiveShards = v
  177. }
  178. }
  179. // WithPretty makes the response body pretty-printed.
  180. func (f IndicesFreeze) WithPretty() func(*IndicesFreezeRequest) {
  181. return func(r *IndicesFreezeRequest) {
  182. r.Pretty = true
  183. }
  184. }
  185. // WithHuman makes statistical values human-readable.
  186. func (f IndicesFreeze) WithHuman() func(*IndicesFreezeRequest) {
  187. return func(r *IndicesFreezeRequest) {
  188. r.Human = true
  189. }
  190. }
  191. // WithErrorTrace includes the stack trace for errors in the response body.
  192. func (f IndicesFreeze) WithErrorTrace() func(*IndicesFreezeRequest) {
  193. return func(r *IndicesFreezeRequest) {
  194. r.ErrorTrace = true
  195. }
  196. }
  197. // WithFilterPath filters the properties of the response body.
  198. func (f IndicesFreeze) WithFilterPath(v ...string) func(*IndicesFreezeRequest) {
  199. return func(r *IndicesFreezeRequest) {
  200. r.FilterPath = v
  201. }
  202. }
  203. // WithHeader adds the headers to the HTTP request.
  204. func (f IndicesFreeze) WithHeader(h map[string]string) func(*IndicesFreezeRequest) {
  205. return func(r *IndicesFreezeRequest) {
  206. if r.Header == nil {
  207. r.Header = make(http.Header)
  208. }
  209. for k, v := range h {
  210. r.Header.Add(k, v)
  211. }
  212. }
  213. }
  214. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  215. func (f IndicesFreeze) WithOpaqueID(s string) func(*IndicesFreezeRequest) {
  216. return func(r *IndicesFreezeRequest) {
  217. if r.Header == nil {
  218. r.Header = make(http.Header)
  219. }
  220. r.Header.Set("X-Opaque-Id", s)
  221. }
  222. }