api.indices.add_block.go 6.8 KB

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