api.indices.delete.go 6.3 KB

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