api.cluster.state.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 newClusterStateFunc(t Transport) ClusterState {
  28. return func(o ...func(*ClusterStateRequest)) (*Response, error) {
  29. var r = ClusterStateRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // ClusterState returns a comprehensive information about the state of the cluster.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-state.html.
  40. type ClusterState func(o ...func(*ClusterStateRequest)) (*Response, error)
  41. // ClusterStateRequest configures the Cluster State API request.
  42. type ClusterStateRequest struct {
  43. Index []string
  44. Metric []string
  45. AllowNoIndices *bool
  46. ExpandWildcards string
  47. FlatSettings *bool
  48. IgnoreUnavailable *bool
  49. Local *bool
  50. MasterTimeout time.Duration
  51. WaitForMetadataVersion *int
  52. WaitForTimeout time.Duration
  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 ClusterStateRequest) 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 = "GET"
  68. path.Grow(1 + len("_cluster") + 1 + len("state") + 1 + len(strings.Join(r.Metric, ",")) + 1 + len(strings.Join(r.Index, ",")))
  69. path.WriteString("/")
  70. path.WriteString("_cluster")
  71. path.WriteString("/")
  72. path.WriteString("state")
  73. if len(r.Metric) > 0 {
  74. path.WriteString("/")
  75. path.WriteString(strings.Join(r.Metric, ","))
  76. }
  77. if len(r.Index) > 0 {
  78. path.WriteString("/")
  79. path.WriteString(strings.Join(r.Index, ","))
  80. }
  81. params = make(map[string]string)
  82. if r.AllowNoIndices != nil {
  83. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  84. }
  85. if r.ExpandWildcards != "" {
  86. params["expand_wildcards"] = r.ExpandWildcards
  87. }
  88. if r.FlatSettings != nil {
  89. params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
  90. }
  91. if r.IgnoreUnavailable != nil {
  92. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  93. }
  94. if r.Local != nil {
  95. params["local"] = strconv.FormatBool(*r.Local)
  96. }
  97. if r.MasterTimeout != 0 {
  98. params["master_timeout"] = formatDuration(r.MasterTimeout)
  99. }
  100. if r.WaitForMetadataVersion != nil {
  101. params["wait_for_metadata_version"] = strconv.FormatInt(int64(*r.WaitForMetadataVersion), 10)
  102. }
  103. if r.WaitForTimeout != 0 {
  104. params["wait_for_timeout"] = formatDuration(r.WaitForTimeout)
  105. }
  106. if r.Pretty {
  107. params["pretty"] = "true"
  108. }
  109. if r.Human {
  110. params["human"] = "true"
  111. }
  112. if r.ErrorTrace {
  113. params["error_trace"] = "true"
  114. }
  115. if len(r.FilterPath) > 0 {
  116. params["filter_path"] = strings.Join(r.FilterPath, ",")
  117. }
  118. req, err := newRequest(method, path.String(), nil)
  119. if err != nil {
  120. return nil, err
  121. }
  122. if len(params) > 0 {
  123. q := req.URL.Query()
  124. for k, v := range params {
  125. q.Set(k, v)
  126. }
  127. req.URL.RawQuery = q.Encode()
  128. }
  129. if len(r.Header) > 0 {
  130. if len(req.Header) == 0 {
  131. req.Header = r.Header
  132. } else {
  133. for k, vv := range r.Header {
  134. for _, v := range vv {
  135. req.Header.Add(k, v)
  136. }
  137. }
  138. }
  139. }
  140. if ctx != nil {
  141. req = req.WithContext(ctx)
  142. }
  143. res, err := transport.Perform(req)
  144. if err != nil {
  145. return nil, err
  146. }
  147. response := Response{
  148. StatusCode: res.StatusCode,
  149. Body: res.Body,
  150. Header: res.Header,
  151. }
  152. return &response, nil
  153. }
  154. // WithContext sets the request context.
  155. func (f ClusterState) WithContext(v context.Context) func(*ClusterStateRequest) {
  156. return func(r *ClusterStateRequest) {
  157. r.ctx = v
  158. }
  159. }
  160. // WithIndex - a list of index names; use _all to perform the operation on all indices.
  161. func (f ClusterState) WithIndex(v ...string) func(*ClusterStateRequest) {
  162. return func(r *ClusterStateRequest) {
  163. r.Index = v
  164. }
  165. }
  166. // WithMetric - limit the information returned to the specified metrics.
  167. func (f ClusterState) WithMetric(v ...string) func(*ClusterStateRequest) {
  168. return func(r *ClusterStateRequest) {
  169. r.Metric = v
  170. }
  171. }
  172. // 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).
  173. func (f ClusterState) WithAllowNoIndices(v bool) func(*ClusterStateRequest) {
  174. return func(r *ClusterStateRequest) {
  175. r.AllowNoIndices = &v
  176. }
  177. }
  178. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  179. func (f ClusterState) WithExpandWildcards(v string) func(*ClusterStateRequest) {
  180. return func(r *ClusterStateRequest) {
  181. r.ExpandWildcards = v
  182. }
  183. }
  184. // WithFlatSettings - return settings in flat format (default: false).
  185. func (f ClusterState) WithFlatSettings(v bool) func(*ClusterStateRequest) {
  186. return func(r *ClusterStateRequest) {
  187. r.FlatSettings = &v
  188. }
  189. }
  190. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  191. func (f ClusterState) WithIgnoreUnavailable(v bool) func(*ClusterStateRequest) {
  192. return func(r *ClusterStateRequest) {
  193. r.IgnoreUnavailable = &v
  194. }
  195. }
  196. // WithLocal - return local information, do not retrieve the state from master node (default: false).
  197. func (f ClusterState) WithLocal(v bool) func(*ClusterStateRequest) {
  198. return func(r *ClusterStateRequest) {
  199. r.Local = &v
  200. }
  201. }
  202. // WithMasterTimeout - specify timeout for connection to master.
  203. func (f ClusterState) WithMasterTimeout(v time.Duration) func(*ClusterStateRequest) {
  204. return func(r *ClusterStateRequest) {
  205. r.MasterTimeout = v
  206. }
  207. }
  208. // WithWaitForMetadataVersion - wait for the metadata version to be equal or greater than the specified metadata version.
  209. func (f ClusterState) WithWaitForMetadataVersion(v int) func(*ClusterStateRequest) {
  210. return func(r *ClusterStateRequest) {
  211. r.WaitForMetadataVersion = &v
  212. }
  213. }
  214. // WithWaitForTimeout - the maximum time to wait for wait_for_metadata_version before timing out.
  215. func (f ClusterState) WithWaitForTimeout(v time.Duration) func(*ClusterStateRequest) {
  216. return func(r *ClusterStateRequest) {
  217. r.WaitForTimeout = v
  218. }
  219. }
  220. // WithPretty makes the response body pretty-printed.
  221. func (f ClusterState) WithPretty() func(*ClusterStateRequest) {
  222. return func(r *ClusterStateRequest) {
  223. r.Pretty = true
  224. }
  225. }
  226. // WithHuman makes statistical values human-readable.
  227. func (f ClusterState) WithHuman() func(*ClusterStateRequest) {
  228. return func(r *ClusterStateRequest) {
  229. r.Human = true
  230. }
  231. }
  232. // WithErrorTrace includes the stack trace for errors in the response body.
  233. func (f ClusterState) WithErrorTrace() func(*ClusterStateRequest) {
  234. return func(r *ClusterStateRequest) {
  235. r.ErrorTrace = true
  236. }
  237. }
  238. // WithFilterPath filters the properties of the response body.
  239. func (f ClusterState) WithFilterPath(v ...string) func(*ClusterStateRequest) {
  240. return func(r *ClusterStateRequest) {
  241. r.FilterPath = v
  242. }
  243. }
  244. // WithHeader adds the headers to the HTTP request.
  245. func (f ClusterState) WithHeader(h map[string]string) func(*ClusterStateRequest) {
  246. return func(r *ClusterStateRequest) {
  247. if r.Header == nil {
  248. r.Header = make(http.Header)
  249. }
  250. for k, v := range h {
  251. r.Header.Add(k, v)
  252. }
  253. }
  254. }
  255. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  256. func (f ClusterState) WithOpaqueID(s string) func(*ClusterStateRequest) {
  257. return func(r *ClusterStateRequest) {
  258. if r.Header == nil {
  259. r.Header = make(http.Header)
  260. }
  261. r.Header.Set("X-Opaque-Id", s)
  262. }
  263. }