api.snapshot.get.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. "fmt"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. "time"
  28. )
  29. func newSnapshotGetFunc(t Transport) SnapshotGet {
  30. return func(repository string, snapshot []string, o ...func(*SnapshotGetRequest)) (*Response, error) {
  31. var r = SnapshotGetRequest{Repository: repository, Snapshot: snapshot}
  32. for _, f := range o {
  33. f(&r)
  34. }
  35. return r.Do(r.ctx, t)
  36. }
  37. }
  38. // ----- API Definition -------------------------------------------------------
  39. // SnapshotGet returns information about a snapshot.
  40. //
  41. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-snapshots.html.
  42. type SnapshotGet func(repository string, snapshot []string, o ...func(*SnapshotGetRequest)) (*Response, error)
  43. // SnapshotGetRequest configures the Snapshot Get API request.
  44. type SnapshotGetRequest struct {
  45. Repository string
  46. Snapshot []string
  47. After string
  48. FromSortValue string
  49. IgnoreUnavailable *bool
  50. IncludeRepository *bool
  51. IndexDetails *bool
  52. MasterTimeout time.Duration
  53. Offset interface{}
  54. Order string
  55. Size interface{}
  56. SlmPolicyFilter string
  57. Sort string
  58. Verbose *bool
  59. Pretty bool
  60. Human bool
  61. ErrorTrace bool
  62. FilterPath []string
  63. Header http.Header
  64. ctx context.Context
  65. }
  66. // Do executes the request and returns response or error.
  67. func (r SnapshotGetRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  68. var (
  69. method string
  70. path strings.Builder
  71. params map[string]string
  72. )
  73. method = "GET"
  74. if len(r.Snapshot) == 0 {
  75. return nil, errors.New("snapshot is required and cannot be nil or empty")
  76. }
  77. path.Grow(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(strings.Join(r.Snapshot, ",")))
  78. path.WriteString("/")
  79. path.WriteString("_snapshot")
  80. path.WriteString("/")
  81. path.WriteString(r.Repository)
  82. path.WriteString("/")
  83. path.WriteString(strings.Join(r.Snapshot, ","))
  84. params = make(map[string]string)
  85. if r.After != "" {
  86. params["after"] = r.After
  87. }
  88. if r.FromSortValue != "" {
  89. params["from_sort_value"] = r.FromSortValue
  90. }
  91. if r.IgnoreUnavailable != nil {
  92. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  93. }
  94. if r.IncludeRepository != nil {
  95. params["include_repository"] = strconv.FormatBool(*r.IncludeRepository)
  96. }
  97. if r.IndexDetails != nil {
  98. params["index_details"] = strconv.FormatBool(*r.IndexDetails)
  99. }
  100. if r.MasterTimeout != 0 {
  101. params["master_timeout"] = formatDuration(r.MasterTimeout)
  102. }
  103. if r.Offset != nil {
  104. params["offset"] = fmt.Sprintf("%v", r.Offset)
  105. }
  106. if r.Order != "" {
  107. params["order"] = r.Order
  108. }
  109. if r.Size != nil {
  110. params["size"] = fmt.Sprintf("%v", r.Size)
  111. }
  112. if r.SlmPolicyFilter != "" {
  113. params["slm_policy_filter"] = r.SlmPolicyFilter
  114. }
  115. if r.Sort != "" {
  116. params["sort"] = r.Sort
  117. }
  118. if r.Verbose != nil {
  119. params["verbose"] = strconv.FormatBool(*r.Verbose)
  120. }
  121. if r.Pretty {
  122. params["pretty"] = "true"
  123. }
  124. if r.Human {
  125. params["human"] = "true"
  126. }
  127. if r.ErrorTrace {
  128. params["error_trace"] = "true"
  129. }
  130. if len(r.FilterPath) > 0 {
  131. params["filter_path"] = strings.Join(r.FilterPath, ",")
  132. }
  133. req, err := newRequest(method, path.String(), nil)
  134. if err != nil {
  135. return nil, err
  136. }
  137. if len(params) > 0 {
  138. q := req.URL.Query()
  139. for k, v := range params {
  140. q.Set(k, v)
  141. }
  142. req.URL.RawQuery = q.Encode()
  143. }
  144. if len(r.Header) > 0 {
  145. if len(req.Header) == 0 {
  146. req.Header = r.Header
  147. } else {
  148. for k, vv := range r.Header {
  149. for _, v := range vv {
  150. req.Header.Add(k, v)
  151. }
  152. }
  153. }
  154. }
  155. if ctx != nil {
  156. req = req.WithContext(ctx)
  157. }
  158. res, err := transport.Perform(req)
  159. if err != nil {
  160. return nil, err
  161. }
  162. response := Response{
  163. StatusCode: res.StatusCode,
  164. Body: res.Body,
  165. Header: res.Header,
  166. }
  167. return &response, nil
  168. }
  169. // WithContext sets the request context.
  170. func (f SnapshotGet) WithContext(v context.Context) func(*SnapshotGetRequest) {
  171. return func(r *SnapshotGetRequest) {
  172. r.ctx = v
  173. }
  174. }
  175. // WithAfter - offset identifier to start pagination from as returned by the 'next' field in the response body..
  176. func (f SnapshotGet) WithAfter(v string) func(*SnapshotGetRequest) {
  177. return func(r *SnapshotGetRequest) {
  178. r.After = v
  179. }
  180. }
  181. // WithFromSortValue - value of the current sort column at which to start retrieval..
  182. func (f SnapshotGet) WithFromSortValue(v string) func(*SnapshotGetRequest) {
  183. return func(r *SnapshotGetRequest) {
  184. r.FromSortValue = v
  185. }
  186. }
  187. // WithIgnoreUnavailable - whether to ignore unavailable snapshots, defaults to false which means a snapshotmissingexception is thrown.
  188. func (f SnapshotGet) WithIgnoreUnavailable(v bool) func(*SnapshotGetRequest) {
  189. return func(r *SnapshotGetRequest) {
  190. r.IgnoreUnavailable = &v
  191. }
  192. }
  193. // WithIncludeRepository - whether to include the repository name in the snapshot info. defaults to true..
  194. func (f SnapshotGet) WithIncludeRepository(v bool) func(*SnapshotGetRequest) {
  195. return func(r *SnapshotGetRequest) {
  196. r.IncludeRepository = &v
  197. }
  198. }
  199. // WithIndexDetails - whether to include details of each index in the snapshot, if those details are available. defaults to false..
  200. func (f SnapshotGet) WithIndexDetails(v bool) func(*SnapshotGetRequest) {
  201. return func(r *SnapshotGetRequest) {
  202. r.IndexDetails = &v
  203. }
  204. }
  205. // WithMasterTimeout - explicit operation timeout for connection to master node.
  206. func (f SnapshotGet) WithMasterTimeout(v time.Duration) func(*SnapshotGetRequest) {
  207. return func(r *SnapshotGetRequest) {
  208. r.MasterTimeout = v
  209. }
  210. }
  211. // WithOffset - numeric offset to start pagination based on the snapshots matching the request. defaults to 0.
  212. func (f SnapshotGet) WithOffset(v interface{}) func(*SnapshotGetRequest) {
  213. return func(r *SnapshotGetRequest) {
  214. r.Offset = v
  215. }
  216. }
  217. // WithOrder - sort order.
  218. func (f SnapshotGet) WithOrder(v string) func(*SnapshotGetRequest) {
  219. return func(r *SnapshotGetRequest) {
  220. r.Order = v
  221. }
  222. }
  223. // WithSize - maximum number of snapshots to return. defaults to 0 which means return all that match without limit..
  224. func (f SnapshotGet) WithSize(v interface{}) func(*SnapshotGetRequest) {
  225. return func(r *SnapshotGetRequest) {
  226. r.Size = v
  227. }
  228. }
  229. // WithSlmPolicyFilter - filter snapshots by a list of slm policy names that snapshots belong to. accepts wildcards. use the special pattern '_none' to match snapshots without an slm policy.
  230. func (f SnapshotGet) WithSlmPolicyFilter(v string) func(*SnapshotGetRequest) {
  231. return func(r *SnapshotGetRequest) {
  232. r.SlmPolicyFilter = v
  233. }
  234. }
  235. // WithSort - allows setting a sort order for the result. defaults to start_time.
  236. func (f SnapshotGet) WithSort(v string) func(*SnapshotGetRequest) {
  237. return func(r *SnapshotGetRequest) {
  238. r.Sort = v
  239. }
  240. }
  241. // WithVerbose - whether to show verbose snapshot info or only show the basic info found in the repository index blob.
  242. func (f SnapshotGet) WithVerbose(v bool) func(*SnapshotGetRequest) {
  243. return func(r *SnapshotGetRequest) {
  244. r.Verbose = &v
  245. }
  246. }
  247. // WithPretty makes the response body pretty-printed.
  248. func (f SnapshotGet) WithPretty() func(*SnapshotGetRequest) {
  249. return func(r *SnapshotGetRequest) {
  250. r.Pretty = true
  251. }
  252. }
  253. // WithHuman makes statistical values human-readable.
  254. func (f SnapshotGet) WithHuman() func(*SnapshotGetRequest) {
  255. return func(r *SnapshotGetRequest) {
  256. r.Human = true
  257. }
  258. }
  259. // WithErrorTrace includes the stack trace for errors in the response body.
  260. func (f SnapshotGet) WithErrorTrace() func(*SnapshotGetRequest) {
  261. return func(r *SnapshotGetRequest) {
  262. r.ErrorTrace = true
  263. }
  264. }
  265. // WithFilterPath filters the properties of the response body.
  266. func (f SnapshotGet) WithFilterPath(v ...string) func(*SnapshotGetRequest) {
  267. return func(r *SnapshotGetRequest) {
  268. r.FilterPath = v
  269. }
  270. }
  271. // WithHeader adds the headers to the HTTP request.
  272. func (f SnapshotGet) WithHeader(h map[string]string) func(*SnapshotGetRequest) {
  273. return func(r *SnapshotGetRequest) {
  274. if r.Header == nil {
  275. r.Header = make(http.Header)
  276. }
  277. for k, v := range h {
  278. r.Header.Add(k, v)
  279. }
  280. }
  281. }
  282. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  283. func (f SnapshotGet) WithOpaqueID(s string) func(*SnapshotGetRequest) {
  284. return func(r *SnapshotGetRequest) {
  285. if r.Header == nil {
  286. r.Header = make(http.Header)
  287. }
  288. r.Header.Set("X-Opaque-Id", s)
  289. }
  290. }