Debian Codegraph @

snapshot_delete_repository.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "context"
9 "encoding/json"
10 "fmt"
11 "net/url"
12 "strings"
13
14 "gopkg.in/olivere/elastic.v5/uritemplates"
15 )
16
17 // SnapshotDeleteRepositoryService deletes a snapshot repository.
18 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html
19 // for details.
20 type SnapshotDeleteRepositoryService struct {
21 client *Client
22 pretty bool
23 repository []string
24 masterTimeout string
25 timeout string
26 }
27
28 // NewSnapshotDeleteRepositoryService creates a new SnapshotDeleteRepositoryService.
29 func NewSnapshotDeleteRepositoryService(client *Client) *SnapshotDeleteRepositoryService {
30 return &SnapshotDeleteRepositoryService{
31 client: client,
32 repository: make([]string, 0),
33 }
34 }
35
36 // Repository is the list of repository names.
37 func (s *SnapshotDeleteRepositoryService) Repository(repositories ...string) *SnapshotDeleteRepositoryService {
38 s.repository = append(s.repository, repositories...)
39 return s
40 }
41
42 // MasterTimeout specifies an explicit operation timeout for connection to master node.
43 func (s *SnapshotDeleteRepositoryService) MasterTimeout(masterTimeout string) *SnapshotDeleteRepositoryService {
44 s.masterTimeout = masterTimeout
45 return s
46 }
47
48 // Timeout is an explicit operation timeout.
49 func (s *SnapshotDeleteRepositoryService) Timeout(timeout string) *SnapshotDeleteRepositoryService {
50 s.timeout = timeout
51 return s
52 }
53
54 // Pretty indicates that the JSON response be indented and human readable.
55 func (s *SnapshotDeleteRepositoryService) Pretty(pretty bool) *SnapshotDeleteRepositoryService {
56 s.pretty = pretty
57 return s
58 }
59
60 // buildURL builds the URL for the operation.
61 func (s *SnapshotDeleteRepositoryService) buildURL() (string, url.Values, error) {
62 // Build URL
63 path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{
64 "repository": strings.Join(s.repository, ","),
65 })
66 if err != nil {
67 return "", url.Values{}, err
68 }
69
70 // Add query string parameters
71 params := url.Values{}
72 if s.pretty {
73 params.Set("pretty", "1")
74 }
75 if s.masterTimeout != "" {
76 params.Set("master_timeout", s.masterTimeout)
77 }
78 if s.timeout != "" {
79 params.Set("timeout", s.timeout)
80 }
81 return path, params, nil
82 }
83
84 // Validate checks if the operation is valid.
85 func (s *SnapshotDeleteRepositoryService) Validate() error {
86 var invalid []string
87 if len(s.repository) == 0 {
88 invalid = append(invalid, "Repository")
89 }
90 if len(invalid) > 0 {
91 return fmt.Errorf("missing required fields: %v", invalid)
92 }
93 return nil
94 }
95
96 // Do executes the operation.
97 func (s *SnapshotDeleteRepositoryService) Do(ctx context.Context) (*SnapshotDeleteRepositoryResponse, error) {
98 // Check pre-conditions
99 if err := s.Validate(); err != nil {
100 return nil, err
101 }
102
103 // Get URL for request
104 path, params, err := s.buildURL()
105 if err != nil {
106 return nil, err
107 }
108
109 // Get HTTP response
110 res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil)
111 if err != nil {
112 return nil, err
113 }
114
115 // Return operation response
116 ret := new(SnapshotDeleteRepositoryResponse)
117 if err := json.Unmarshal(res.Body, ret); err != nil {
118 return nil, err
119 }
120 return ret, nil
121 }
122
123 // SnapshotDeleteRepositoryResponse is the response of SnapshotDeleteRepositoryService.Do.
124 type SnapshotDeleteRepositoryResponse struct {
125 Acknowledged bool `json:"acknowledged"`
126 }