api.cat.allocation.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 newCatAllocationFunc(t Transport) CatAllocation {
  28. return func(o ...func(*CatAllocationRequest)) (*Response, error) {
  29. var r = CatAllocationRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // CatAllocation provides a snapshot of how many shards are allocated to each data node and how much disk space they are using.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cat-allocation.html.
  40. type CatAllocation func(o ...func(*CatAllocationRequest)) (*Response, error)
  41. // CatAllocationRequest configures the Cat Allocation API request.
  42. type CatAllocationRequest struct {
  43. NodeID []string
  44. Bytes string
  45. Format string
  46. H []string
  47. Help *bool
  48. Local *bool
  49. MasterTimeout time.Duration
  50. S []string
  51. V *bool
  52. Pretty bool
  53. Human bool
  54. ErrorTrace bool
  55. FilterPath []string
  56. Header http.Header
  57. ctx context.Context
  58. }
  59. // Do executes the request and returns response or error.
  60. func (r CatAllocationRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  61. var (
  62. method string
  63. path strings.Builder
  64. params map[string]string
  65. )
  66. method = "GET"
  67. path.Grow(1 + len("_cat") + 1 + len("allocation") + 1 + len(strings.Join(r.NodeID, ",")))
  68. path.WriteString("/")
  69. path.WriteString("_cat")
  70. path.WriteString("/")
  71. path.WriteString("allocation")
  72. if len(r.NodeID) > 0 {
  73. path.WriteString("/")
  74. path.WriteString(strings.Join(r.NodeID, ","))
  75. }
  76. params = make(map[string]string)
  77. if r.Bytes != "" {
  78. params["bytes"] = r.Bytes
  79. }
  80. if r.Format != "" {
  81. params["format"] = r.Format
  82. }
  83. if len(r.H) > 0 {
  84. params["h"] = strings.Join(r.H, ",")
  85. }
  86. if r.Help != nil {
  87. params["help"] = strconv.FormatBool(*r.Help)
  88. }
  89. if r.Local != nil {
  90. params["local"] = strconv.FormatBool(*r.Local)
  91. }
  92. if r.MasterTimeout != 0 {
  93. params["master_timeout"] = formatDuration(r.MasterTimeout)
  94. }
  95. if len(r.S) > 0 {
  96. params["s"] = strings.Join(r.S, ",")
  97. }
  98. if r.V != nil {
  99. params["v"] = strconv.FormatBool(*r.V)
  100. }
  101. if r.Pretty {
  102. params["pretty"] = "true"
  103. }
  104. if r.Human {
  105. params["human"] = "true"
  106. }
  107. if r.ErrorTrace {
  108. params["error_trace"] = "true"
  109. }
  110. if len(r.FilterPath) > 0 {
  111. params["filter_path"] = strings.Join(r.FilterPath, ",")
  112. }
  113. req, err := newRequest(method, path.String(), nil)
  114. if err != nil {
  115. return nil, err
  116. }
  117. if len(params) > 0 {
  118. q := req.URL.Query()
  119. for k, v := range params {
  120. q.Set(k, v)
  121. }
  122. req.URL.RawQuery = q.Encode()
  123. }
  124. if len(r.Header) > 0 {
  125. if len(req.Header) == 0 {
  126. req.Header = r.Header
  127. } else {
  128. for k, vv := range r.Header {
  129. for _, v := range vv {
  130. req.Header.Add(k, v)
  131. }
  132. }
  133. }
  134. }
  135. if ctx != nil {
  136. req = req.WithContext(ctx)
  137. }
  138. res, err := transport.Perform(req)
  139. if err != nil {
  140. return nil, err
  141. }
  142. response := Response{
  143. StatusCode: res.StatusCode,
  144. Body: res.Body,
  145. Header: res.Header,
  146. }
  147. return &response, nil
  148. }
  149. // WithContext sets the request context.
  150. func (f CatAllocation) WithContext(v context.Context) func(*CatAllocationRequest) {
  151. return func(r *CatAllocationRequest) {
  152. r.ctx = v
  153. }
  154. }
  155. // WithNodeID - a list of node ids or names to limit the returned information.
  156. func (f CatAllocation) WithNodeID(v ...string) func(*CatAllocationRequest) {
  157. return func(r *CatAllocationRequest) {
  158. r.NodeID = v
  159. }
  160. }
  161. // WithBytes - the unit in which to display byte values.
  162. func (f CatAllocation) WithBytes(v string) func(*CatAllocationRequest) {
  163. return func(r *CatAllocationRequest) {
  164. r.Bytes = v
  165. }
  166. }
  167. // WithFormat - a short version of the accept header, e.g. json, yaml.
  168. func (f CatAllocation) WithFormat(v string) func(*CatAllocationRequest) {
  169. return func(r *CatAllocationRequest) {
  170. r.Format = v
  171. }
  172. }
  173. // WithH - comma-separated list of column names to display.
  174. func (f CatAllocation) WithH(v ...string) func(*CatAllocationRequest) {
  175. return func(r *CatAllocationRequest) {
  176. r.H = v
  177. }
  178. }
  179. // WithHelp - return help information.
  180. func (f CatAllocation) WithHelp(v bool) func(*CatAllocationRequest) {
  181. return func(r *CatAllocationRequest) {
  182. r.Help = &v
  183. }
  184. }
  185. // WithLocal - return local information, do not retrieve the state from master node (default: false).
  186. func (f CatAllocation) WithLocal(v bool) func(*CatAllocationRequest) {
  187. return func(r *CatAllocationRequest) {
  188. r.Local = &v
  189. }
  190. }
  191. // WithMasterTimeout - explicit operation timeout for connection to master node.
  192. func (f CatAllocation) WithMasterTimeout(v time.Duration) func(*CatAllocationRequest) {
  193. return func(r *CatAllocationRequest) {
  194. r.MasterTimeout = v
  195. }
  196. }
  197. // WithS - comma-separated list of column names or column aliases to sort by.
  198. func (f CatAllocation) WithS(v ...string) func(*CatAllocationRequest) {
  199. return func(r *CatAllocationRequest) {
  200. r.S = v
  201. }
  202. }
  203. // WithV - verbose mode. display column headers.
  204. func (f CatAllocation) WithV(v bool) func(*CatAllocationRequest) {
  205. return func(r *CatAllocationRequest) {
  206. r.V = &v
  207. }
  208. }
  209. // WithPretty makes the response body pretty-printed.
  210. func (f CatAllocation) WithPretty() func(*CatAllocationRequest) {
  211. return func(r *CatAllocationRequest) {
  212. r.Pretty = true
  213. }
  214. }
  215. // WithHuman makes statistical values human-readable.
  216. func (f CatAllocation) WithHuman() func(*CatAllocationRequest) {
  217. return func(r *CatAllocationRequest) {
  218. r.Human = true
  219. }
  220. }
  221. // WithErrorTrace includes the stack trace for errors in the response body.
  222. func (f CatAllocation) WithErrorTrace() func(*CatAllocationRequest) {
  223. return func(r *CatAllocationRequest) {
  224. r.ErrorTrace = true
  225. }
  226. }
  227. // WithFilterPath filters the properties of the response body.
  228. func (f CatAllocation) WithFilterPath(v ...string) func(*CatAllocationRequest) {
  229. return func(r *CatAllocationRequest) {
  230. r.FilterPath = v
  231. }
  232. }
  233. // WithHeader adds the headers to the HTTP request.
  234. func (f CatAllocation) WithHeader(h map[string]string) func(*CatAllocationRequest) {
  235. return func(r *CatAllocationRequest) {
  236. if r.Header == nil {
  237. r.Header = make(http.Header)
  238. }
  239. for k, v := range h {
  240. r.Header.Add(k, v)
  241. }
  242. }
  243. }
  244. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  245. func (f CatAllocation) WithOpaqueID(s string) func(*CatAllocationRequest) {
  246. return func(r *CatAllocationRequest) {
  247. if r.Header == nil {
  248. r.Header = make(http.Header)
  249. }
  250. r.Header.Set("X-Opaque-Id", s)
  251. }
  252. }