api.xpack.searchable_snapshots.mount.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 newSearchableSnapshotsMountFunc(t Transport) SearchableSnapshotsMount {
  29. return func(repository string, snapshot string, body io.Reader, o ...func(*SearchableSnapshotsMountRequest)) (*Response, error) {
  30. var r = SearchableSnapshotsMountRequest{Repository: repository, Snapshot: snapshot, Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // SearchableSnapshotsMount - Mount a snapshot as a searchable index.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/searchable-snapshots-api-mount-snapshot.html.
  41. type SearchableSnapshotsMount func(repository string, snapshot string, body io.Reader, o ...func(*SearchableSnapshotsMountRequest)) (*Response, error)
  42. // SearchableSnapshotsMountRequest configures the Searchable Snapshots Mount API request.
  43. type SearchableSnapshotsMountRequest struct {
  44. Body io.Reader
  45. Repository string
  46. Snapshot string
  47. MasterTimeout time.Duration
  48. Storage string
  49. WaitForCompletion *bool
  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 SearchableSnapshotsMountRequest) 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 = "POST"
  65. path.Grow(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(r.Snapshot) + 1 + len("_mount"))
  66. path.WriteString("/")
  67. path.WriteString("_snapshot")
  68. path.WriteString("/")
  69. path.WriteString(r.Repository)
  70. path.WriteString("/")
  71. path.WriteString(r.Snapshot)
  72. path.WriteString("/")
  73. path.WriteString("_mount")
  74. params = make(map[string]string)
  75. if r.MasterTimeout != 0 {
  76. params["master_timeout"] = formatDuration(r.MasterTimeout)
  77. }
  78. if r.Storage != "" {
  79. params["storage"] = r.Storage
  80. }
  81. if r.WaitForCompletion != nil {
  82. params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
  83. }
  84. if r.Pretty {
  85. params["pretty"] = "true"
  86. }
  87. if r.Human {
  88. params["human"] = "true"
  89. }
  90. if r.ErrorTrace {
  91. params["error_trace"] = "true"
  92. }
  93. if len(r.FilterPath) > 0 {
  94. params["filter_path"] = strings.Join(r.FilterPath, ",")
  95. }
  96. req, err := newRequest(method, path.String(), r.Body)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if len(params) > 0 {
  101. q := req.URL.Query()
  102. for k, v := range params {
  103. q.Set(k, v)
  104. }
  105. req.URL.RawQuery = q.Encode()
  106. }
  107. if len(r.Header) > 0 {
  108. if len(req.Header) == 0 {
  109. req.Header = r.Header
  110. } else {
  111. for k, vv := range r.Header {
  112. for _, v := range vv {
  113. req.Header.Add(k, v)
  114. }
  115. }
  116. }
  117. }
  118. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  119. req.Header[headerContentType] = headerContentTypeJSON
  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 SearchableSnapshotsMount) WithContext(v context.Context) func(*SearchableSnapshotsMountRequest) {
  137. return func(r *SearchableSnapshotsMountRequest) {
  138. r.ctx = v
  139. }
  140. }
  141. // WithMasterTimeout - explicit operation timeout for connection to master node.
  142. func (f SearchableSnapshotsMount) WithMasterTimeout(v time.Duration) func(*SearchableSnapshotsMountRequest) {
  143. return func(r *SearchableSnapshotsMountRequest) {
  144. r.MasterTimeout = v
  145. }
  146. }
  147. // WithStorage - selects the kind of local storage used to accelerate searches. experimental, and defaults to `full_copy`.
  148. func (f SearchableSnapshotsMount) WithStorage(v string) func(*SearchableSnapshotsMountRequest) {
  149. return func(r *SearchableSnapshotsMountRequest) {
  150. r.Storage = v
  151. }
  152. }
  153. // WithWaitForCompletion - should this request wait until the operation has completed before returning.
  154. func (f SearchableSnapshotsMount) WithWaitForCompletion(v bool) func(*SearchableSnapshotsMountRequest) {
  155. return func(r *SearchableSnapshotsMountRequest) {
  156. r.WaitForCompletion = &v
  157. }
  158. }
  159. // WithPretty makes the response body pretty-printed.
  160. func (f SearchableSnapshotsMount) WithPretty() func(*SearchableSnapshotsMountRequest) {
  161. return func(r *SearchableSnapshotsMountRequest) {
  162. r.Pretty = true
  163. }
  164. }
  165. // WithHuman makes statistical values human-readable.
  166. func (f SearchableSnapshotsMount) WithHuman() func(*SearchableSnapshotsMountRequest) {
  167. return func(r *SearchableSnapshotsMountRequest) {
  168. r.Human = true
  169. }
  170. }
  171. // WithErrorTrace includes the stack trace for errors in the response body.
  172. func (f SearchableSnapshotsMount) WithErrorTrace() func(*SearchableSnapshotsMountRequest) {
  173. return func(r *SearchableSnapshotsMountRequest) {
  174. r.ErrorTrace = true
  175. }
  176. }
  177. // WithFilterPath filters the properties of the response body.
  178. func (f SearchableSnapshotsMount) WithFilterPath(v ...string) func(*SearchableSnapshotsMountRequest) {
  179. return func(r *SearchableSnapshotsMountRequest) {
  180. r.FilterPath = v
  181. }
  182. }
  183. // WithHeader adds the headers to the HTTP request.
  184. func (f SearchableSnapshotsMount) WithHeader(h map[string]string) func(*SearchableSnapshotsMountRequest) {
  185. return func(r *SearchableSnapshotsMountRequest) {
  186. if r.Header == nil {
  187. r.Header = make(http.Header)
  188. }
  189. for k, v := range h {
  190. r.Header.Add(k, v)
  191. }
  192. }
  193. }
  194. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  195. func (f SearchableSnapshotsMount) WithOpaqueID(s string) func(*SearchableSnapshotsMountRequest) {
  196. return func(r *SearchableSnapshotsMountRequest) {
  197. if r.Header == nil {
  198. r.Header = make(http.Header)
  199. }
  200. r.Header.Set("X-Opaque-Id", s)
  201. }
  202. }