api.indices.delete_alias.go 5.7 KB

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