api.cluster.health.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 newClusterHealthFunc(t Transport) ClusterHealth {
  28. return func(o ...func(*ClusterHealthRequest)) (*Response, error) {
  29. var r = ClusterHealthRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // ClusterHealth returns basic information about the health of the cluster.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-health.html.
  40. type ClusterHealth func(o ...func(*ClusterHealthRequest)) (*Response, error)
  41. // ClusterHealthRequest configures the Cluster Health API request.
  42. type ClusterHealthRequest struct {
  43. Index []string
  44. ExpandWildcards string
  45. Level string
  46. Local *bool
  47. MasterTimeout time.Duration
  48. Timeout time.Duration
  49. WaitForActiveShards string
  50. WaitForEvents string
  51. WaitForNoInitializingShards *bool
  52. WaitForNoRelocatingShards *bool
  53. WaitForNodes string
  54. WaitForStatus string
  55. Pretty bool
  56. Human bool
  57. ErrorTrace bool
  58. FilterPath []string
  59. Header http.Header
  60. ctx context.Context
  61. }
  62. // Do executes the request and returns response or error.
  63. func (r ClusterHealthRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  64. var (
  65. method string
  66. path strings.Builder
  67. params map[string]string
  68. )
  69. method = "GET"
  70. path.Grow(1 + len("_cluster") + 1 + len("health") + 1 + len(strings.Join(r.Index, ",")))
  71. path.WriteString("/")
  72. path.WriteString("_cluster")
  73. path.WriteString("/")
  74. path.WriteString("health")
  75. if len(r.Index) > 0 {
  76. path.WriteString("/")
  77. path.WriteString(strings.Join(r.Index, ","))
  78. }
  79. params = make(map[string]string)
  80. if r.ExpandWildcards != "" {
  81. params["expand_wildcards"] = r.ExpandWildcards
  82. }
  83. if r.Level != "" {
  84. params["level"] = r.Level
  85. }
  86. if r.Local != nil {
  87. params["local"] = strconv.FormatBool(*r.Local)
  88. }
  89. if r.MasterTimeout != 0 {
  90. params["master_timeout"] = formatDuration(r.MasterTimeout)
  91. }
  92. if r.Timeout != 0 {
  93. params["timeout"] = formatDuration(r.Timeout)
  94. }
  95. if r.WaitForActiveShards != "" {
  96. params["wait_for_active_shards"] = r.WaitForActiveShards
  97. }
  98. if r.WaitForEvents != "" {
  99. params["wait_for_events"] = r.WaitForEvents
  100. }
  101. if r.WaitForNoInitializingShards != nil {
  102. params["wait_for_no_initializing_shards"] = strconv.FormatBool(*r.WaitForNoInitializingShards)
  103. }
  104. if r.WaitForNoRelocatingShards != nil {
  105. params["wait_for_no_relocating_shards"] = strconv.FormatBool(*r.WaitForNoRelocatingShards)
  106. }
  107. if r.WaitForNodes != "" {
  108. params["wait_for_nodes"] = r.WaitForNodes
  109. }
  110. if r.WaitForStatus != "" {
  111. params["wait_for_status"] = r.WaitForStatus
  112. }
  113. if r.Pretty {
  114. params["pretty"] = "true"
  115. }
  116. if r.Human {
  117. params["human"] = "true"
  118. }
  119. if r.ErrorTrace {
  120. params["error_trace"] = "true"
  121. }
  122. if len(r.FilterPath) > 0 {
  123. params["filter_path"] = strings.Join(r.FilterPath, ",")
  124. }
  125. req, err := newRequest(method, path.String(), nil)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if len(params) > 0 {
  130. q := req.URL.Query()
  131. for k, v := range params {
  132. q.Set(k, v)
  133. }
  134. req.URL.RawQuery = q.Encode()
  135. }
  136. if len(r.Header) > 0 {
  137. if len(req.Header) == 0 {
  138. req.Header = r.Header
  139. } else {
  140. for k, vv := range r.Header {
  141. for _, v := range vv {
  142. req.Header.Add(k, v)
  143. }
  144. }
  145. }
  146. }
  147. if ctx != nil {
  148. req = req.WithContext(ctx)
  149. }
  150. res, err := transport.Perform(req)
  151. if err != nil {
  152. return nil, err
  153. }
  154. response := Response{
  155. StatusCode: res.StatusCode,
  156. Body: res.Body,
  157. Header: res.Header,
  158. }
  159. return &response, nil
  160. }
  161. // WithContext sets the request context.
  162. func (f ClusterHealth) WithContext(v context.Context) func(*ClusterHealthRequest) {
  163. return func(r *ClusterHealthRequest) {
  164. r.ctx = v
  165. }
  166. }
  167. // WithIndex - limit the information returned to a specific index.
  168. func (f ClusterHealth) WithIndex(v ...string) func(*ClusterHealthRequest) {
  169. return func(r *ClusterHealthRequest) {
  170. r.Index = v
  171. }
  172. }
  173. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  174. func (f ClusterHealth) WithExpandWildcards(v string) func(*ClusterHealthRequest) {
  175. return func(r *ClusterHealthRequest) {
  176. r.ExpandWildcards = v
  177. }
  178. }
  179. // WithLevel - specify the level of detail for returned information.
  180. func (f ClusterHealth) WithLevel(v string) func(*ClusterHealthRequest) {
  181. return func(r *ClusterHealthRequest) {
  182. r.Level = v
  183. }
  184. }
  185. // WithLocal - return local information, do not retrieve the state from master node (default: false).
  186. func (f ClusterHealth) WithLocal(v bool) func(*ClusterHealthRequest) {
  187. return func(r *ClusterHealthRequest) {
  188. r.Local = &v
  189. }
  190. }
  191. // WithMasterTimeout - explicit operation timeout for connection to master node.
  192. func (f ClusterHealth) WithMasterTimeout(v time.Duration) func(*ClusterHealthRequest) {
  193. return func(r *ClusterHealthRequest) {
  194. r.MasterTimeout = v
  195. }
  196. }
  197. // WithTimeout - explicit operation timeout.
  198. func (f ClusterHealth) WithTimeout(v time.Duration) func(*ClusterHealthRequest) {
  199. return func(r *ClusterHealthRequest) {
  200. r.Timeout = v
  201. }
  202. }
  203. // WithWaitForActiveShards - wait until the specified number of shards is active.
  204. func (f ClusterHealth) WithWaitForActiveShards(v string) func(*ClusterHealthRequest) {
  205. return func(r *ClusterHealthRequest) {
  206. r.WaitForActiveShards = v
  207. }
  208. }
  209. // WithWaitForEvents - wait until all currently queued events with the given priority are processed.
  210. func (f ClusterHealth) WithWaitForEvents(v string) func(*ClusterHealthRequest) {
  211. return func(r *ClusterHealthRequest) {
  212. r.WaitForEvents = v
  213. }
  214. }
  215. // WithWaitForNoInitializingShards - whether to wait until there are no initializing shards in the cluster.
  216. func (f ClusterHealth) WithWaitForNoInitializingShards(v bool) func(*ClusterHealthRequest) {
  217. return func(r *ClusterHealthRequest) {
  218. r.WaitForNoInitializingShards = &v
  219. }
  220. }
  221. // WithWaitForNoRelocatingShards - whether to wait until there are no relocating shards in the cluster.
  222. func (f ClusterHealth) WithWaitForNoRelocatingShards(v bool) func(*ClusterHealthRequest) {
  223. return func(r *ClusterHealthRequest) {
  224. r.WaitForNoRelocatingShards = &v
  225. }
  226. }
  227. // WithWaitForNodes - wait until the specified number of nodes is available.
  228. func (f ClusterHealth) WithWaitForNodes(v string) func(*ClusterHealthRequest) {
  229. return func(r *ClusterHealthRequest) {
  230. r.WaitForNodes = v
  231. }
  232. }
  233. // WithWaitForStatus - wait until cluster is in a specific state.
  234. func (f ClusterHealth) WithWaitForStatus(v string) func(*ClusterHealthRequest) {
  235. return func(r *ClusterHealthRequest) {
  236. r.WaitForStatus = v
  237. }
  238. }
  239. // WithPretty makes the response body pretty-printed.
  240. func (f ClusterHealth) WithPretty() func(*ClusterHealthRequest) {
  241. return func(r *ClusterHealthRequest) {
  242. r.Pretty = true
  243. }
  244. }
  245. // WithHuman makes statistical values human-readable.
  246. func (f ClusterHealth) WithHuman() func(*ClusterHealthRequest) {
  247. return func(r *ClusterHealthRequest) {
  248. r.Human = true
  249. }
  250. }
  251. // WithErrorTrace includes the stack trace for errors in the response body.
  252. func (f ClusterHealth) WithErrorTrace() func(*ClusterHealthRequest) {
  253. return func(r *ClusterHealthRequest) {
  254. r.ErrorTrace = true
  255. }
  256. }
  257. // WithFilterPath filters the properties of the response body.
  258. func (f ClusterHealth) WithFilterPath(v ...string) func(*ClusterHealthRequest) {
  259. return func(r *ClusterHealthRequest) {
  260. r.FilterPath = v
  261. }
  262. }
  263. // WithHeader adds the headers to the HTTP request.
  264. func (f ClusterHealth) WithHeader(h map[string]string) func(*ClusterHealthRequest) {
  265. return func(r *ClusterHealthRequest) {
  266. if r.Header == nil {
  267. r.Header = make(http.Header)
  268. }
  269. for k, v := range h {
  270. r.Header.Add(k, v)
  271. }
  272. }
  273. }
  274. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  275. func (f ClusterHealth) WithOpaqueID(s string) func(*ClusterHealthRequest) {
  276. return func(r *ClusterHealthRequest) {
  277. if r.Header == nil {
  278. r.Header = make(http.Header)
  279. }
  280. r.Header.Set("X-Opaque-Id", s)
  281. }
  282. }