mirror of
https://github.com/go-gitea/gitea
synced 2024-11-01 15:54:25 +00:00
12a1f914f4
* update github.com/alecthomas/chroma v0.8.0 -> v0.8.1 * github.com/blevesearch/bleve v1.0.10 -> v1.0.12 * editorconfig-core-go v2.1.1 -> v2.3.7 * github.com/gliderlabs/ssh v0.2.2 -> v0.3.1 * migrate editorconfig.ParseBytes to Parse * github.com/shurcooL/vfsgen to 0d455de96546 * github.com/go-git/go-git/v5 v5.1.0 -> v5.2.0 * github.com/google/uuid v1.1.1 -> v1.1.2 * github.com/huandu/xstrings v1.3.0 -> v1.3.2 * github.com/klauspost/compress v1.10.11 -> v1.11.1 * github.com/markbates/goth v1.61.2 -> v1.65.0 * github.com/mattn/go-sqlite3 v1.14.0 -> v1.14.4 * github.com/mholt/archiver v3.3.0 -> v3.3.2 * github.com/microcosm-cc/bluemonday 4f7140c49acb -> v1.0.4 * github.com/minio/minio-go v7.0.4 -> v7.0.5 * github.com/olivere/elastic v7.0.9 -> v7.0.20 * github.com/urfave/cli v1.20.0 -> v1.22.4 * github.com/prometheus/client_golang v1.1.0 -> v1.8.0 * github.com/xanzy/go-gitlab v0.37.0 -> v0.38.1 * mvdan.cc/xurls v2.1.0 -> v2.2.0 Co-authored-by: Lauris BH <lauris@nix.lv>
299 lines
8.5 KiB
Go
Vendored
299 lines
8.5 KiB
Go
Vendored
// Copyright 2012-present Oliver Eilhard. All rights reserved.
|
|
// Use of this source code is governed by a MIT-license.
|
|
// See http://olivere.mit-license.org/license.txt for details.
|
|
|
|
package elastic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/olivere/elastic/v7/uritemplates"
|
|
)
|
|
|
|
// SnapshotRestoreService restores a snapshot from a snapshot repository.
|
|
//
|
|
// It is documented at
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/7.1/modules-snapshots.html#_restore.
|
|
type SnapshotRestoreService struct {
|
|
client *Client
|
|
|
|
pretty *bool // pretty format the returned JSON response
|
|
human *bool // return human readable values for statistics
|
|
errorTrace *bool // include the stack trace of returned errors
|
|
filterPath []string // list of filters used to reduce the response
|
|
headers http.Header // custom request-level HTTP headers
|
|
|
|
repository string
|
|
snapshot string
|
|
masterTimeout string
|
|
waitForCompletion *bool
|
|
ignoreUnavailable *bool
|
|
partial *bool
|
|
includeAliases *bool
|
|
includeGlobalState *bool
|
|
bodyString string
|
|
renamePattern string
|
|
renameReplacement string
|
|
indices []string
|
|
indexSettings map[string]interface{}
|
|
}
|
|
|
|
// NewSnapshotCreateService creates a new SnapshotRestoreService.
|
|
func NewSnapshotRestoreService(client *Client) *SnapshotRestoreService {
|
|
return &SnapshotRestoreService{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// Pretty tells Elasticsearch whether to return a formatted JSON response.
|
|
func (s *SnapshotRestoreService) Pretty(pretty bool) *SnapshotRestoreService {
|
|
s.pretty = &pretty
|
|
return s
|
|
}
|
|
|
|
// Human specifies whether human readable values should be returned in
|
|
// the JSON response, e.g. "7.5mb".
|
|
func (s *SnapshotRestoreService) Human(human bool) *SnapshotRestoreService {
|
|
s.human = &human
|
|
return s
|
|
}
|
|
|
|
// ErrorTrace specifies whether to include the stack trace of returned errors.
|
|
func (s *SnapshotRestoreService) ErrorTrace(errorTrace bool) *SnapshotRestoreService {
|
|
s.errorTrace = &errorTrace
|
|
return s
|
|
}
|
|
|
|
// FilterPath specifies a list of filters used to reduce the response.
|
|
func (s *SnapshotRestoreService) FilterPath(filterPath ...string) *SnapshotRestoreService {
|
|
s.filterPath = filterPath
|
|
return s
|
|
}
|
|
|
|
// Header adds a header to the request.
|
|
func (s *SnapshotRestoreService) Header(name string, value string) *SnapshotRestoreService {
|
|
if s.headers == nil {
|
|
s.headers = http.Header{}
|
|
}
|
|
s.headers.Add(name, value)
|
|
return s
|
|
}
|
|
|
|
// Headers specifies the headers of the request.
|
|
func (s *SnapshotRestoreService) Headers(headers http.Header) *SnapshotRestoreService {
|
|
s.headers = headers
|
|
return s
|
|
}
|
|
|
|
// Repository name.
|
|
func (s *SnapshotRestoreService) Repository(repository string) *SnapshotRestoreService {
|
|
s.repository = repository
|
|
return s
|
|
}
|
|
|
|
// Snapshot name.
|
|
func (s *SnapshotRestoreService) Snapshot(snapshot string) *SnapshotRestoreService {
|
|
s.snapshot = snapshot
|
|
return s
|
|
}
|
|
|
|
// MasterTimeout specifies an explicit operation timeout for connection to master node.
|
|
func (s *SnapshotRestoreService) MasterTimeout(masterTimeout string) *SnapshotRestoreService {
|
|
s.masterTimeout = masterTimeout
|
|
return s
|
|
}
|
|
|
|
// WaitForCompletion indicates whether this request should wait until the operation has
|
|
// completed before returning.
|
|
func (s *SnapshotRestoreService) WaitForCompletion(waitForCompletion bool) *SnapshotRestoreService {
|
|
s.waitForCompletion = &waitForCompletion
|
|
return s
|
|
}
|
|
|
|
// Indices sets the name of the indices that should be restored from the snapshot.
|
|
func (s *SnapshotRestoreService) Indices(indices ...string) *SnapshotRestoreService {
|
|
s.indices = indices
|
|
return s
|
|
}
|
|
|
|
// IncludeGlobalState allows the global cluster state to be restored, defaults to false.
|
|
func (s *SnapshotRestoreService) IncludeGlobalState(includeGlobalState bool) *SnapshotRestoreService {
|
|
s.includeGlobalState = &includeGlobalState
|
|
return s
|
|
}
|
|
|
|
// RenamePattern helps rename indices on restore using regular expressions.
|
|
func (s *SnapshotRestoreService) RenamePattern(renamePattern string) *SnapshotRestoreService {
|
|
s.renamePattern = renamePattern
|
|
return s
|
|
}
|
|
|
|
// RenameReplacement as RenamePattern, helps rename indices on restore using regular expressions.
|
|
func (s *SnapshotRestoreService) RenameReplacement(renameReplacement string) *SnapshotRestoreService {
|
|
s.renameReplacement = renameReplacement
|
|
return s
|
|
}
|
|
|
|
// Partial indicates whether to restore indices that where partially snapshoted, defaults to false.
|
|
func (s *SnapshotRestoreService) Partial(partial bool) *SnapshotRestoreService {
|
|
s.partial = &partial
|
|
return s
|
|
}
|
|
|
|
// BodyString allows the user to specify the body of the HTTP request manually.
|
|
func (s *SnapshotRestoreService) BodyString(body string) *SnapshotRestoreService {
|
|
s.bodyString = body
|
|
return s
|
|
}
|
|
|
|
// IndexSettings sets the settings to be overwritten during the restore process
|
|
func (s *SnapshotRestoreService) IndexSettings(indexSettings map[string]interface{}) *SnapshotRestoreService {
|
|
s.indexSettings = indexSettings
|
|
return s
|
|
}
|
|
|
|
// IncludeAliases flags whether indices should be restored with their respective aliases,
|
|
// defaults to false.
|
|
func (s *SnapshotRestoreService) IncludeAliases(includeAliases bool) *SnapshotRestoreService {
|
|
s.includeAliases = &includeAliases
|
|
return s
|
|
}
|
|
|
|
// IgnoreUnavailable specifies whether to ignore unavailable snapshots, defaults to false.
|
|
func (s *SnapshotRestoreService) IgnoreUnavailable(ignoreUnavailable bool) *SnapshotRestoreService {
|
|
s.ignoreUnavailable = &ignoreUnavailable
|
|
return s
|
|
}
|
|
|
|
// Do executes the operation.
|
|
func (s *SnapshotRestoreService) Do(ctx context.Context) (*SnapshotRestoreResponse, error) {
|
|
if err := s.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
path, params, err := s.buildURL()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var body interface{}
|
|
if len(s.bodyString) > 0 {
|
|
body = s.bodyString
|
|
} else {
|
|
body = s.buildBody()
|
|
}
|
|
|
|
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
|
|
Method: "POST",
|
|
Path: path,
|
|
Params: params,
|
|
Body: body,
|
|
Headers: s.headers,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := new(SnapshotRestoreResponse)
|
|
if err := json.Unmarshal(res.Body, ret); err != nil {
|
|
return nil, err
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
// Validate checks if the operation is valid.
|
|
func (s *SnapshotRestoreService) Validate() error {
|
|
var invalid []string
|
|
if s.repository == "" {
|
|
invalid = append(invalid, "Repository")
|
|
}
|
|
if s.snapshot == "" {
|
|
invalid = append(invalid, "Snapshot")
|
|
}
|
|
if len(invalid) > 0 {
|
|
return fmt.Errorf("missing required fields: %v", invalid)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SnapshotRestoreService) buildURL() (string, url.Values, error) {
|
|
path, err := uritemplates.Expand("/_snapshot/{repository}/{snapshot}/_restore", map[string]string{
|
|
"snapshot": s.snapshot,
|
|
"repository": s.repository,
|
|
})
|
|
if err != nil {
|
|
return "", url.Values{}, err
|
|
}
|
|
|
|
params := url.Values{}
|
|
if v := s.pretty; v != nil {
|
|
params.Set("pretty", fmt.Sprint(*v))
|
|
}
|
|
if v := s.human; v != nil {
|
|
params.Set("human", fmt.Sprint(*v))
|
|
}
|
|
if v := s.errorTrace; v != nil {
|
|
params.Set("error_trace", fmt.Sprint(*v))
|
|
}
|
|
if len(s.filterPath) > 0 {
|
|
params.Set("filter_path", strings.Join(s.filterPath, ","))
|
|
}
|
|
if s.masterTimeout != "" {
|
|
params.Set("master_timeout", s.masterTimeout)
|
|
}
|
|
if v := s.waitForCompletion; v != nil {
|
|
params.Set("wait_for_completion", fmt.Sprint(*v))
|
|
}
|
|
if v := s.ignoreUnavailable; v != nil {
|
|
params.Set("ignore_unavailable", fmt.Sprint(*v))
|
|
}
|
|
return path, params, nil
|
|
}
|
|
|
|
func (s *SnapshotRestoreService) buildBody() interface{} {
|
|
body := map[string]interface{}{}
|
|
|
|
if s.includeGlobalState != nil {
|
|
body["include_global_state"] = *s.includeGlobalState
|
|
}
|
|
if s.partial != nil {
|
|
body["partial"] = *s.partial
|
|
}
|
|
if s.includeAliases != nil {
|
|
body["include_aliases"] = *s.includeAliases
|
|
}
|
|
if len(s.indices) > 0 {
|
|
body["indices"] = strings.Join(s.indices, ",")
|
|
}
|
|
if len(s.renamePattern) > 0 {
|
|
body["rename_pattern"] = s.renamePattern
|
|
}
|
|
if len(s.renamePattern) > 0 {
|
|
body["rename_replacement"] = s.renameReplacement
|
|
}
|
|
if len(s.indexSettings) > 0 {
|
|
body["index_settings"] = s.indexSettings
|
|
}
|
|
return body
|
|
}
|
|
|
|
// SnapshotRestoreResponse represents the response for SnapshotRestoreService.Do
|
|
type SnapshotRestoreResponse struct {
|
|
// Accepted indicates whether the request was accepted by Elasticsearch.
|
|
Accepted *bool `json:"accepted"`
|
|
|
|
// Snapshot information.
|
|
Snapshot *RestoreInfo `json:"snapshot"`
|
|
}
|
|
|
|
// RestoreInfo represents information about the restored snapshot.
|
|
type RestoreInfo struct {
|
|
Snapshot string `json:"snapshot"`
|
|
Indices []string `json:"indices"`
|
|
Shards ShardsInfo `json:"shards"`
|
|
}
|