|
| 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 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// ClosePointInTimeService removes a point in time. |
| 16 | +// |
| 17 | +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.x/point-in-time-api.html |
| 18 | +// for details. |
| 19 | +type ClosePointInTimeService struct { |
| 20 | + client *Client |
| 21 | + |
| 22 | + pretty *bool // pretty format the returned JSON response |
| 23 | + human *bool // return human readable values for statistics |
| 24 | + errorTrace *bool // include the stack trace of returned errors |
| 25 | + filterPath []string // list of filters used to reduce the response |
| 26 | + headers http.Header // custom request-level HTTP headers |
| 27 | + |
| 28 | + id string |
| 29 | + bodyJson interface{} |
| 30 | + bodyString string |
| 31 | +} |
| 32 | + |
| 33 | +// NewClosePointInTimeService creates a new ClosePointInTimeService. |
| 34 | +func NewClosePointInTimeService(client *Client) *ClosePointInTimeService { |
| 35 | + return &ClosePointInTimeService{ |
| 36 | + client: client, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Pretty tells Elasticsearch whether to return a formatted JSON response. |
| 41 | +func (s *ClosePointInTimeService) Pretty(pretty bool) *ClosePointInTimeService { |
| 42 | + s.pretty = &pretty |
| 43 | + return s |
| 44 | +} |
| 45 | + |
| 46 | +// Human specifies whether human readable values should be returned in |
| 47 | +// the JSON response, e.g. "7.5mb". |
| 48 | +func (s *ClosePointInTimeService) Human(human bool) *ClosePointInTimeService { |
| 49 | + s.human = &human |
| 50 | + return s |
| 51 | +} |
| 52 | + |
| 53 | +// ErrorTrace specifies whether to include the stack trace of returned errors. |
| 54 | +func (s *ClosePointInTimeService) ErrorTrace(errorTrace bool) *ClosePointInTimeService { |
| 55 | + s.errorTrace = &errorTrace |
| 56 | + return s |
| 57 | +} |
| 58 | + |
| 59 | +// FilterPath specifies a list of filters used to reduce the response. |
| 60 | +func (s *ClosePointInTimeService) FilterPath(filterPath ...string) *ClosePointInTimeService { |
| 61 | + s.filterPath = filterPath |
| 62 | + return s |
| 63 | +} |
| 64 | + |
| 65 | +// Header adds a header to the request. |
| 66 | +func (s *ClosePointInTimeService) Header(name string, value string) *ClosePointInTimeService { |
| 67 | + if s.headers == nil { |
| 68 | + s.headers = http.Header{} |
| 69 | + } |
| 70 | + s.headers.Add(name, value) |
| 71 | + return s |
| 72 | +} |
| 73 | + |
| 74 | +// Headers specifies the headers of the request. |
| 75 | +func (s *ClosePointInTimeService) Headers(headers http.Header) *ClosePointInTimeService { |
| 76 | + s.headers = headers |
| 77 | + return s |
| 78 | +} |
| 79 | + |
| 80 | +// ID to close. |
| 81 | +func (s *ClosePointInTimeService) ID(id string) *ClosePointInTimeService { |
| 82 | + s.id = id |
| 83 | + return s |
| 84 | +} |
| 85 | + |
| 86 | +// BodyJson is the document as a serializable JSON interface. |
| 87 | +func (s *ClosePointInTimeService) BodyJson(body interface{}) *ClosePointInTimeService { |
| 88 | + s.bodyJson = body |
| 89 | + return s |
| 90 | +} |
| 91 | + |
| 92 | +// BodyString is the document encoded as a string. |
| 93 | +func (s *ClosePointInTimeService) BodyString(body string) *ClosePointInTimeService { |
| 94 | + s.bodyString = body |
| 95 | + return s |
| 96 | +} |
| 97 | + |
| 98 | +// buildURL builds the URL for the operation. |
| 99 | +func (s *ClosePointInTimeService) buildURL() (string, string, url.Values, error) { |
| 100 | + var ( |
| 101 | + method = "DELETE" |
| 102 | + path = "/_pit" |
| 103 | + ) |
| 104 | + |
| 105 | + // Add query string parameters |
| 106 | + params := url.Values{} |
| 107 | + if v := s.pretty; v != nil { |
| 108 | + params.Set("pretty", fmt.Sprint(*v)) |
| 109 | + } |
| 110 | + if v := s.human; v != nil { |
| 111 | + params.Set("human", fmt.Sprint(*v)) |
| 112 | + } |
| 113 | + if v := s.errorTrace; v != nil { |
| 114 | + params.Set("error_trace", fmt.Sprint(*v)) |
| 115 | + } |
| 116 | + if len(s.filterPath) > 0 { |
| 117 | + params.Set("filter_path", strings.Join(s.filterPath, ",")) |
| 118 | + } |
| 119 | + return method, path, params, nil |
| 120 | +} |
| 121 | + |
| 122 | +// Validate checks if the operation is valid. |
| 123 | +func (s *ClosePointInTimeService) Validate() error { |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// Do executes the operation. |
| 128 | +func (s *ClosePointInTimeService) Do(ctx context.Context) (*ClosePointInTimeResponse, error) { |
| 129 | + // Check pre-conditions |
| 130 | + if err := s.Validate(); err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + // Get URL for request |
| 135 | + method, path, params, err := s.buildURL() |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + // Setup HTTP request body |
| 141 | + var body interface{} |
| 142 | + if s.id != "" { |
| 143 | + body = map[string]interface{}{ |
| 144 | + "id": s.id, |
| 145 | + } |
| 146 | + } else if s.bodyJson != nil { |
| 147 | + body = s.bodyJson |
| 148 | + } else { |
| 149 | + body = s.bodyString |
| 150 | + } |
| 151 | + |
| 152 | + // Get HTTP response |
| 153 | + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ |
| 154 | + Method: method, |
| 155 | + Path: path, |
| 156 | + Params: params, |
| 157 | + Body: body, |
| 158 | + Headers: s.headers, |
| 159 | + }) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + // Return operation response |
| 165 | + ret := new(ClosePointInTimeResponse) |
| 166 | + if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + return ret, nil |
| 170 | +} |
| 171 | + |
| 172 | +// ClosePointInTimeResponse is the result of closing a point in time. |
| 173 | +type ClosePointInTimeResponse struct { |
| 174 | + Succeeded bool `json:"succeeded,omitempty"` |
| 175 | + NumFreed int `json:"num_freed,omitempty"` |
| 176 | +} |
0 commit comments