api.snapshot.clone.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "strings"
  25. "time"
  26. )
  27. func newSnapshotCloneFunc(t Transport) SnapshotClone {
  28. return func(repository string, snapshot string, body io.Reader, target_snapshot string, o ...func(*SnapshotCloneRequest)) (*Response, error) {
  29. var r = SnapshotCloneRequest{Repository: repository, Snapshot: snapshot, Body: body, TargetSnapshot: target_snapshot}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // SnapshotClone clones indices from one snapshot into another snapshot in the same repository.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-snapshots.html.
  40. type SnapshotClone func(repository string, snapshot string, body io.Reader, target_snapshot string, o ...func(*SnapshotCloneRequest)) (*Response, error)
  41. // SnapshotCloneRequest configures the Snapshot Clone API request.
  42. type SnapshotCloneRequest struct {
  43. Body io.Reader
  44. Repository string
  45. Snapshot string
  46. TargetSnapshot string
  47. MasterTimeout 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 SnapshotCloneRequest) 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(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(r.Snapshot) + 1 + len("_clone") + 1 + len(r.TargetSnapshot))
  64. path.WriteString("/")
  65. path.WriteString("_snapshot")
  66. path.WriteString("/")
  67. path.WriteString(r.Repository)
  68. path.WriteString("/")
  69. path.WriteString(r.Snapshot)
  70. path.WriteString("/")
  71. path.WriteString("_clone")
  72. path.WriteString("/")
  73. path.WriteString(r.TargetSnapshot)
  74. params = make(map[string]string)
  75. if r.MasterTimeout != 0 {
  76. params["master_timeout"] = formatDuration(r.MasterTimeout)
  77. }
  78. if r.Pretty {
  79. params["pretty"] = "true"
  80. }
  81. if r.Human {
  82. params["human"] = "true"
  83. }
  84. if r.ErrorTrace {
  85. params["error_trace"] = "true"
  86. }
  87. if len(r.FilterPath) > 0 {
  88. params["filter_path"] = strings.Join(r.FilterPath, ",")
  89. }
  90. req, err := newRequest(method, path.String(), r.Body)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if len(params) > 0 {
  95. q := req.URL.Query()
  96. for k, v := range params {
  97. q.Set(k, v)
  98. }
  99. req.URL.RawQuery = q.Encode()
  100. }
  101. if len(r.Header) > 0 {
  102. if len(req.Header) == 0 {
  103. req.Header = r.Header
  104. } else {
  105. for k, vv := range r.Header {
  106. for _, v := range vv {
  107. req.Header.Add(k, v)
  108. }
  109. }
  110. }
  111. }
  112. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  113. req.Header[headerContentType] = headerContentTypeJSON
  114. }
  115. if ctx != nil {
  116. req = req.WithContext(ctx)
  117. }
  118. res, err := transport.Perform(req)
  119. if err != nil {
  120. return nil, err
  121. }
  122. response := Response{
  123. StatusCode: res.StatusCode,
  124. Body: res.Body,
  125. Header: res.Header,
  126. }
  127. return &response, nil
  128. }
  129. // WithContext sets the request context.
  130. func (f SnapshotClone) WithContext(v context.Context) func(*SnapshotCloneRequest) {
  131. return func(r *SnapshotCloneRequest) {
  132. r.ctx = v
  133. }
  134. }
  135. // WithMasterTimeout - explicit operation timeout for connection to master node.
  136. func (f SnapshotClone) WithMasterTimeout(v time.Duration) func(*SnapshotCloneRequest) {
  137. return func(r *SnapshotCloneRequest) {
  138. r.MasterTimeout = v
  139. }
  140. }
  141. // WithPretty makes the response body pretty-printed.
  142. func (f SnapshotClone) WithPretty() func(*SnapshotCloneRequest) {
  143. return func(r *SnapshotCloneRequest) {
  144. r.Pretty = true
  145. }
  146. }
  147. // WithHuman makes statistical values human-readable.
  148. func (f SnapshotClone) WithHuman() func(*SnapshotCloneRequest) {
  149. return func(r *SnapshotCloneRequest) {
  150. r.Human = true
  151. }
  152. }
  153. // WithErrorTrace includes the stack trace for errors in the response body.
  154. func (f SnapshotClone) WithErrorTrace() func(*SnapshotCloneRequest) {
  155. return func(r *SnapshotCloneRequest) {
  156. r.ErrorTrace = true
  157. }
  158. }
  159. // WithFilterPath filters the properties of the response body.
  160. func (f SnapshotClone) WithFilterPath(v ...string) func(*SnapshotCloneRequest) {
  161. return func(r *SnapshotCloneRequest) {
  162. r.FilterPath = v
  163. }
  164. }
  165. // WithHeader adds the headers to the HTTP request.
  166. func (f SnapshotClone) WithHeader(h map[string]string) func(*SnapshotCloneRequest) {
  167. return func(r *SnapshotCloneRequest) {
  168. if r.Header == nil {
  169. r.Header = make(http.Header)
  170. }
  171. for k, v := range h {
  172. r.Header.Add(k, v)
  173. }
  174. }
  175. }
  176. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  177. func (f SnapshotClone) WithOpaqueID(s string) func(*SnapshotCloneRequest) {
  178. return func(r *SnapshotCloneRequest) {
  179. if r.Header == nil {
  180. r.Header = make(http.Header)
  181. }
  182. r.Header.Set("X-Opaque-Id", s)
  183. }
  184. }