api.indices.open.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 newIndicesOpenFunc(t Transport) IndicesOpen {
  29. return func(index []string, o ...func(*IndicesOpenRequest)) (*Response, error) {
  30. var r = IndicesOpenRequest{Index: index}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesOpen opens an index.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html.
  41. type IndicesOpen func(index []string, o ...func(*IndicesOpenRequest)) (*Response, error)
  42. // IndicesOpenRequest configures the Indices Open API request.
  43. type IndicesOpenRequest struct {
  44. Index []string
  45. AllowNoIndices *bool
  46. ExpandWildcards string
  47. IgnoreUnavailable *bool
  48. MasterTimeout time.Duration
  49. Timeout time.Duration
  50. WaitForActiveShards string
  51. Pretty bool
  52. Human bool
  53. ErrorTrace bool
  54. FilterPath []string
  55. Header http.Header
  56. ctx context.Context
  57. }
  58. // Do executes the request and returns response or error.
  59. func (r IndicesOpenRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  60. var (
  61. method string
  62. path strings.Builder
  63. params map[string]string
  64. )
  65. method = "POST"
  66. if len(r.Index) == 0 {
  67. return nil, errors.New("index is required and cannot be nil or empty")
  68. }
  69. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_open"))
  70. path.WriteString("/")
  71. path.WriteString(strings.Join(r.Index, ","))
  72. path.WriteString("/")
  73. path.WriteString("_open")
  74. params = make(map[string]string)
  75. if r.AllowNoIndices != nil {
  76. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  77. }
  78. if r.ExpandWildcards != "" {
  79. params["expand_wildcards"] = r.ExpandWildcards
  80. }
  81. if r.IgnoreUnavailable != nil {
  82. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  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(), nil)
  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 ctx != nil {
  128. req = req.WithContext(ctx)
  129. }
  130. res, err := transport.Perform(req)
  131. if err != nil {
  132. return nil, err
  133. }
  134. response := Response{
  135. StatusCode: res.StatusCode,
  136. Body: res.Body,
  137. Header: res.Header,
  138. }
  139. return &response, nil
  140. }
  141. // WithContext sets the request context.
  142. func (f IndicesOpen) WithContext(v context.Context) func(*IndicesOpenRequest) {
  143. return func(r *IndicesOpenRequest) {
  144. r.ctx = v
  145. }
  146. }
  147. // 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).
  148. func (f IndicesOpen) WithAllowNoIndices(v bool) func(*IndicesOpenRequest) {
  149. return func(r *IndicesOpenRequest) {
  150. r.AllowNoIndices = &v
  151. }
  152. }
  153. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  154. func (f IndicesOpen) WithExpandWildcards(v string) func(*IndicesOpenRequest) {
  155. return func(r *IndicesOpenRequest) {
  156. r.ExpandWildcards = v
  157. }
  158. }
  159. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  160. func (f IndicesOpen) WithIgnoreUnavailable(v bool) func(*IndicesOpenRequest) {
  161. return func(r *IndicesOpenRequest) {
  162. r.IgnoreUnavailable = &v
  163. }
  164. }
  165. // WithMasterTimeout - specify timeout for connection to master.
  166. func (f IndicesOpen) WithMasterTimeout(v time.Duration) func(*IndicesOpenRequest) {
  167. return func(r *IndicesOpenRequest) {
  168. r.MasterTimeout = v
  169. }
  170. }
  171. // WithTimeout - explicit operation timeout.
  172. func (f IndicesOpen) WithTimeout(v time.Duration) func(*IndicesOpenRequest) {
  173. return func(r *IndicesOpenRequest) {
  174. r.Timeout = v
  175. }
  176. }
  177. // WithWaitForActiveShards - sets the number of active shards to wait for before the operation returns..
  178. func (f IndicesOpen) WithWaitForActiveShards(v string) func(*IndicesOpenRequest) {
  179. return func(r *IndicesOpenRequest) {
  180. r.WaitForActiveShards = v
  181. }
  182. }
  183. // WithPretty makes the response body pretty-printed.
  184. func (f IndicesOpen) WithPretty() func(*IndicesOpenRequest) {
  185. return func(r *IndicesOpenRequest) {
  186. r.Pretty = true
  187. }
  188. }
  189. // WithHuman makes statistical values human-readable.
  190. func (f IndicesOpen) WithHuman() func(*IndicesOpenRequest) {
  191. return func(r *IndicesOpenRequest) {
  192. r.Human = true
  193. }
  194. }
  195. // WithErrorTrace includes the stack trace for errors in the response body.
  196. func (f IndicesOpen) WithErrorTrace() func(*IndicesOpenRequest) {
  197. return func(r *IndicesOpenRequest) {
  198. r.ErrorTrace = true
  199. }
  200. }
  201. // WithFilterPath filters the properties of the response body.
  202. func (f IndicesOpen) WithFilterPath(v ...string) func(*IndicesOpenRequest) {
  203. return func(r *IndicesOpenRequest) {
  204. r.FilterPath = v
  205. }
  206. }
  207. // WithHeader adds the headers to the HTTP request.
  208. func (f IndicesOpen) WithHeader(h map[string]string) func(*IndicesOpenRequest) {
  209. return func(r *IndicesOpenRequest) {
  210. if r.Header == nil {
  211. r.Header = make(http.Header)
  212. }
  213. for k, v := range h {
  214. r.Header.Add(k, v)
  215. }
  216. }
  217. }
  218. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  219. func (f IndicesOpen) WithOpaqueID(s string) func(*IndicesOpenRequest) {
  220. return func(r *IndicesOpenRequest) {
  221. if r.Header == nil {
  222. r.Header = make(http.Header)
  223. }
  224. r.Header.Set("X-Opaque-Id", s)
  225. }
  226. }