Add method to list backup repos

This commit is contained in:
Nicholas Canzoneri 2018-12-11 00:17:23 -05:00
Родитель 9746540668
Коммит 938270c690
2 изменённых файлов: 78 добавлений и 7 удалений

40
es.go
Просмотреть файл

@ -487,23 +487,49 @@ func (c *Client) VerifyRepository(repository string) (bool, error) {
return true, nil
}
//List snapshot respositories on the cluster
//
//Use case: Get the names and settings of all the snapshot repositories that are configured on the cluster.
func (c *Client) ListRepositories() ([]Repository, error) {
return []Repository{}, nil
type repo struct {
Type string `json:"type"`
Settings map[string]interface{} `json:"settings"`
}
//List snapshot respositories on the cluster
//
//Use case: Get the names and settings of all the snapshot repositories that are configured on the cluster.
//Use case: You want to see all of the configured backup repositories on the given cluster, what types they are and if they are verified.
func (c *Client) ListRepositories() ([]Repository, error) {
var repos map[string]repo
var repositories []Repository
err := handleErrWithStruct(c.buildGetRequest("_snapshot/_all"), &repos)
if err != nil {
return nil, err
}
for name, r := range repos {
// Sanitize AWS secrets if they exist in the settings
delete(r.Settings, "access_key")
delete(r.Settings, "secret_key")
repositories = append(repositories, Repository{
Name: name,
Type: r.Type,
Settings: r.Settings,
})
}
return repositories, nil
}
//Take a snapshot on the cluster to the given repository
//
//Use case: You want to backup all of the indices on the cluster to the given repository.
func (c *Client) TakeSnapshot(repository string, snapshot string) error {
return nil
}
//Restore an index on the cluster
//
//Use case: Restore a single index on the cluster.
//Use case: You want to restore a particular index onto your cluster with a new name.
func (c *Client) RestoreIndex(repository, snapshot, index, restoredIndex string) error {
return nil
}

Просмотреть файл

@ -697,3 +697,48 @@ func TestVerifyRepository(t *testing.T) {
t.Errorf("Expected repository to be verified, got %v", verified)
}
}
func TestListRepositories(t *testing.T) {
testSetup := &ServerSetup{
Method: "GET",
Path: "/_snapshot/_all",
Response: `{
"fileSystemRepo": { "type": "fs", "settings": { "location": "/foo/bar" } },
"s3Repo": { "type": "s3", "settings": { "bucket": "myBucket", "base_path": "foo", "access_key": "access", "secret_key": "secret" } }
}`,
}
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)
repos, err := client.ListRepositories()
if err != nil {
t.Fatalf("Got error getting repositories: %s", err)
}
if len(repos) != 2 {
t.Fatalf("Expected two repositories, got %d", len(repos))
}
fsRepo := repos[0]
if fsRepo.Name != "fileSystemRepo" || fsRepo.Type != "fs" || fsRepo.Settings["location"] != "/foo/bar" {
t.Fatalf("Unexpected fs repo settings, got: %+v", fsRepo)
}
s3Repo := repos[1]
if s3Repo.Name != "s3Repo" || s3Repo.Type != "s3" || s3Repo.Settings["bucket"] != "myBucket" {
t.Fatalf("Unexpected s3 repo settings, got: %+v", s3Repo)
}
if _, exists := s3Repo.Settings["access_key"]; exists {
t.Fatalf("Expected access_key to be scrubbed from s3Repo.")
}
if _, exists := s3Repo.Settings["secret_key"]; exists {
t.Fatalf("Expected secret_key to be scrubbed from s3Repo.")
}
}