api.cluster.put_settings.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "io"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. func newClusterPutSettingsFunc(t Transport) ClusterPutSettings {
  29. return func(body io.Reader, o ...func(*ClusterPutSettingsRequest)) (*Response, error) {
  30. var r = ClusterPutSettingsRequest{Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // ClusterPutSettings updates the cluster settings.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-update-settings.html.
  41. type ClusterPutSettings func(body io.Reader, o ...func(*ClusterPutSettingsRequest)) (*Response, error)
  42. // ClusterPutSettingsRequest configures the Cluster Put Settings API request.
  43. type ClusterPutSettingsRequest struct {
  44. Body io.Reader
  45. FlatSettings *bool
  46. MasterTimeout time.Duration
  47. Timeout time.Duration
  48. Pretty bool
  49. Human bool
  50. ErrorTrace bool
  51. FilterPath []string
  52. Header http.Header
  53. ctx context.Context
  54. }
  55. // Do executes the request and returns response or error.
  56. func (r ClusterPutSettingsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  57. var (
  58. method string
  59. path strings.Builder
  60. params map[string]string
  61. )
  62. method = "PUT"
  63. path.Grow(len("/_cluster/settings"))
  64. path.WriteString("/_cluster/settings")
  65. params = make(map[string]string)
  66. if r.FlatSettings != nil {
  67. params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
  68. }
  69. if r.MasterTimeout != 0 {
  70. params["master_timeout"] = formatDuration(r.MasterTimeout)
  71. }
  72. if r.Timeout != 0 {
  73. params["timeout"] = formatDuration(r.Timeout)
  74. }
  75. if r.Pretty {
  76. params["pretty"] = "true"
  77. }
  78. if r.Human {
  79. params["human"] = "true"
  80. }
  81. if r.ErrorTrace {
  82. params["error_trace"] = "true"
  83. }
  84. if len(r.FilterPath) > 0 {
  85. params["filter_path"] = strings.Join(r.FilterPath, ",")
  86. }
  87. req, err := newRequest(method, path.String(), r.Body)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if len(params) > 0 {
  92. q := req.URL.Query()
  93. for k, v := range params {
  94. q.Set(k, v)
  95. }
  96. req.URL.RawQuery = q.Encode()
  97. }
  98. if len(r.Header) > 0 {
  99. if len(req.Header) == 0 {
  100. req.Header = r.Header
  101. } else {
  102. for k, vv := range r.Header {
  103. for _, v := range vv {
  104. req.Header.Add(k, v)
  105. }
  106. }
  107. }
  108. }
  109. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  110. req.Header[headerContentType] = headerContentTypeJSON
  111. }
  112. if ctx != nil {
  113. req = req.WithContext(ctx)
  114. }
  115. res, err := transport.Perform(req)
  116. if err != nil {
  117. return nil, err
  118. }
  119. response := Response{
  120. StatusCode: res.StatusCode,
  121. Body: res.Body,
  122. Header: res.Header,
  123. }
  124. return &response, nil
  125. }
  126. // WithContext sets the request context.
  127. func (f ClusterPutSettings) WithContext(v context.Context) func(*ClusterPutSettingsRequest) {
  128. return func(r *ClusterPutSettingsRequest) {
  129. r.ctx = v
  130. }
  131. }
  132. // WithFlatSettings - return settings in flat format (default: false).
  133. func (f ClusterPutSettings) WithFlatSettings(v bool) func(*ClusterPutSettingsRequest) {
  134. return func(r *ClusterPutSettingsRequest) {
  135. r.FlatSettings = &v
  136. }
  137. }
  138. // WithMasterTimeout - explicit operation timeout for connection to master node.
  139. func (f ClusterPutSettings) WithMasterTimeout(v time.Duration) func(*ClusterPutSettingsRequest) {
  140. return func(r *ClusterPutSettingsRequest) {
  141. r.MasterTimeout = v
  142. }
  143. }
  144. // WithTimeout - explicit operation timeout.
  145. func (f ClusterPutSettings) WithTimeout(v time.Duration) func(*ClusterPutSettingsRequest) {
  146. return func(r *ClusterPutSettingsRequest) {
  147. r.Timeout = v
  148. }
  149. }
  150. // WithPretty makes the response body pretty-printed.
  151. func (f ClusterPutSettings) WithPretty() func(*ClusterPutSettingsRequest) {
  152. return func(r *ClusterPutSettingsRequest) {
  153. r.Pretty = true
  154. }
  155. }
  156. // WithHuman makes statistical values human-readable.
  157. func (f ClusterPutSettings) WithHuman() func(*ClusterPutSettingsRequest) {
  158. return func(r *ClusterPutSettingsRequest) {
  159. r.Human = true
  160. }
  161. }
  162. // WithErrorTrace includes the stack trace for errors in the response body.
  163. func (f ClusterPutSettings) WithErrorTrace() func(*ClusterPutSettingsRequest) {
  164. return func(r *ClusterPutSettingsRequest) {
  165. r.ErrorTrace = true
  166. }
  167. }
  168. // WithFilterPath filters the properties of the response body.
  169. func (f ClusterPutSettings) WithFilterPath(v ...string) func(*ClusterPutSettingsRequest) {
  170. return func(r *ClusterPutSettingsRequest) {
  171. r.FilterPath = v
  172. }
  173. }
  174. // WithHeader adds the headers to the HTTP request.
  175. func (f ClusterPutSettings) WithHeader(h map[string]string) func(*ClusterPutSettingsRequest) {
  176. return func(r *ClusterPutSettingsRequest) {
  177. if r.Header == nil {
  178. r.Header = make(http.Header)
  179. }
  180. for k, v := range h {
  181. r.Header.Add(k, v)
  182. }
  183. }
  184. }
  185. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  186. func (f ClusterPutSettings) WithOpaqueID(s string) func(*ClusterPutSettingsRequest) {
  187. return func(r *ClusterPutSettingsRequest) {
  188. if r.Header == nil {
  189. r.Header = make(http.Header)
  190. }
  191. r.Header.Set("X-Opaque-Id", s)
  192. }
  193. }