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 5package elastic 6 7import ( 8 "context" 9 "encoding/json" 10 "fmt" 11 "net/url" 12 "strings" 13 14 "github.com/olivere/elastic/uritemplates" 15) 16 17// SnapshotDeleteRepositoryService deletes a snapshot repository. 18// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/modules-snapshots.html 19// for details. 20type 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. 29func 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. 37func (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. 43func (s *SnapshotDeleteRepositoryService) MasterTimeout(masterTimeout string) *SnapshotDeleteRepositoryService { 44 s.masterTimeout = masterTimeout 45 return s 46} 47 48// Timeout is an explicit operation timeout. 49func (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. 55func (s *SnapshotDeleteRepositoryService) Pretty(pretty bool) *SnapshotDeleteRepositoryService { 56 s.pretty = pretty 57 return s 58} 59 60// buildURL builds the URL for the operation. 61func (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", "true") 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. 85func (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. 97func (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, PerformRequestOptions{ 111 Method: "DELETE", 112 Path: path, 113 Params: params, 114 }) 115 if err != nil { 116 return nil, err 117 } 118 119 // Return operation response 120 ret := new(SnapshotDeleteRepositoryResponse) 121 if err := json.Unmarshal(res.Body, ret); err != nil { 122 return nil, err 123 } 124 return ret, nil 125} 126 127// SnapshotDeleteRepositoryResponse is the response of SnapshotDeleteRepositoryService.Do. 128type SnapshotDeleteRepositoryResponse struct { 129 Acknowledged bool `json:"acknowledged"` 130 ShardsAcknowledged bool `json:"shards_acknowledged"` 131 Index string `json:"index,omitempty"` 132} 133