From 7169c877cf201aad644c74afdc431e7d14f7e42b Mon Sep 17 00:00:00 2001 From: Oliver Eilhard Date: Mon, 10 Apr 2017 11:26:36 +0200 Subject: [PATCH] Syntactic sugar on the Snapshot Repository Create API You can now build the body via the builder. See #508 --- snapshot_create_repository.go | 63 ++++++++++++++++++++++++------ snapshot_create_repository_test.go | 36 ++++++++++++++++- snapshot_delete_repository.go | 12 +++--- snapshot_delete_repository_test.go | 4 ++ snapshot_get_repository.go | 22 ++++++----- snapshot_get_repository_test.go | 4 ++ snapshot_verify_repository.go | 12 +++--- snapshot_verify_repository_test.go | 4 ++ 8 files changed, 126 insertions(+), 31 deletions(-) diff --git a/snapshot_create_repository.go b/snapshot_create_repository.go index 6701bbbb3..9fc0a32a6 100644 --- a/snapshot_create_repository.go +++ b/snapshot_create_repository.go @@ -1,4 +1,4 @@ -// Copyright 2012-2017 Oliver Eilhard. All rights reserved. +// 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. @@ -13,7 +13,9 @@ import ( "gopkg.in/olivere/elastic.v5/uritemplates" ) -// SnapshotCreateRepositoryService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.x/modules-snapshots.html. +// SnapshotCreateRepositoryService creates a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html +// for details. type SnapshotCreateRepositoryService struct { client *Client pretty bool @@ -21,6 +23,8 @@ type SnapshotCreateRepositoryService struct { masterTimeout string timeout string verify *bool + typ string + settings map[string]interface{} bodyJson interface{} bodyString string } @@ -32,25 +36,25 @@ func NewSnapshotCreateRepositoryService(client *Client) *SnapshotCreateRepositor } } -// Repository is documented as: A repository name. +// Repository is the repository name. func (s *SnapshotCreateRepositoryService) Repository(repository string) *SnapshotCreateRepositoryService { s.repository = repository return s } -// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +// MasterTimeout specifies an explicit operation timeout for connection to master node. func (s *SnapshotCreateRepositoryService) MasterTimeout(masterTimeout string) *SnapshotCreateRepositoryService { s.masterTimeout = masterTimeout return s } -// Timeout is documented as: Explicit operation timeout. +// Timeout is an explicit operation timeout. func (s *SnapshotCreateRepositoryService) Timeout(timeout string) *SnapshotCreateRepositoryService { s.timeout = timeout return s } -// Verify is documented as: Whether to verify the repository after creation. +// Verify indicates whether to verify the repository after creation. func (s *SnapshotCreateRepositoryService) Verify(verify bool) *SnapshotCreateRepositoryService { s.verify = &verify return s @@ -62,6 +66,27 @@ func (s *SnapshotCreateRepositoryService) Pretty(pretty bool) *SnapshotCreateRep return s } +// Type sets the snapshot repository type, e.g. "fs". +func (s *SnapshotCreateRepositoryService) Type(typ string) *SnapshotCreateRepositoryService { + s.typ = typ + return s +} + +// Settings sets all settings of the snapshot repository. +func (s *SnapshotCreateRepositoryService) Settings(settings map[string]interface{}) *SnapshotCreateRepositoryService { + s.settings = settings + return s +} + +// Setting sets a single settings of the snapshot repository. +func (s *SnapshotCreateRepositoryService) Setting(name string, value interface{}) *SnapshotCreateRepositoryService { + if s.settings == nil { + s.settings = make(map[string]interface{}) + } + s.settings[name] = value + return s +} + // BodyJson is documented as: The repository definition. func (s *SnapshotCreateRepositoryService) BodyJson(body interface{}) *SnapshotCreateRepositoryService { s.bodyJson = body @@ -101,6 +126,24 @@ func (s *SnapshotCreateRepositoryService) buildURL() (string, url.Values, error) return path, params, nil } +// buildBody builds the body for the operation. +func (s *SnapshotCreateRepositoryService) buildBody() (interface{}, error) { + if s.bodyJson != nil { + return s.bodyJson, nil + } + if s.bodyString != "" { + return s.bodyString, nil + } + + body := map[string]interface{}{ + "type": s.typ, + } + if len(s.settings) > 0 { + body["settings"] = s.settings + } + return body, nil +} + // Validate checks if the operation is valid. func (s *SnapshotCreateRepositoryService) Validate() error { var invalid []string @@ -130,11 +173,9 @@ func (s *SnapshotCreateRepositoryService) Do(ctx context.Context) (*SnapshotCrea } // Setup HTTP request body - var body interface{} - if s.bodyJson != nil { - body = s.bodyJson - } else { - body = s.bodyString + body, err := s.buildBody() + if err != nil { + return nil, err } // Get HTTP response diff --git a/snapshot_create_repository_test.go b/snapshot_create_repository_test.go index 6b835c489..2045c700d 100644 --- a/snapshot_create_repository_test.go +++ b/snapshot_create_repository_test.go @@ -1,6 +1,13 @@ +// 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 "testing" +import ( + "encoding/json" + "testing" +) func TestSnapshotPutRepositoryURL(t *testing.T) { client := setupTestClient(t) @@ -25,3 +32,30 @@ func TestSnapshotPutRepositoryURL(t *testing.T) { } } } + +func TestSnapshotPutRepositoryBody(t *testing.T) { + client := setupTestClient(t) + + service := client.SnapshotCreateRepository("my_backup") + service = service.Type("fs"). + Settings(map[string]interface{}{ + "location": "my_backup_location", + "compress": false, + }). + Setting("compress", true). + Setting("chunk_size", 16*1024*1024) + + src, err := service.buildBody() + if err != nil { + t.Fatal(err) + } + data, err := json.Marshal(src) + if err != nil { + t.Fatalf("marshaling to JSON failed: %v", err) + } + got := string(data) + expected := `{"settings":{"chunk_size":16777216,"compress":true,"location":"my_backup_location"},"type":"fs"}` + if got != expected { + t.Errorf("expected\n%s\n,got:\n%s", expected, got) + } +} diff --git a/snapshot_delete_repository.go b/snapshot_delete_repository.go index dc05cfa8b..1f402fba5 100644 --- a/snapshot_delete_repository.go +++ b/snapshot_delete_repository.go @@ -1,4 +1,4 @@ -// Copyright 2012-2017 Oliver Eilhard. All rights reserved. +// 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. @@ -14,7 +14,9 @@ import ( "gopkg.in/olivere/elastic.v5/uritemplates" ) -// SnapshotDeleteRepositoryService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.x/modules-snapshots.html. +// SnapshotDeleteRepositoryService deletes a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html +// for details. type SnapshotDeleteRepositoryService struct { client *Client pretty bool @@ -31,19 +33,19 @@ func NewSnapshotDeleteRepositoryService(client *Client) *SnapshotDeleteRepositor } } -// Repository is documented as: A comma-separated list of repository names. +// Repository is the list of repository names. func (s *SnapshotDeleteRepositoryService) Repository(repositories ...string) *SnapshotDeleteRepositoryService { s.repository = append(s.repository, repositories...) return s } -// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +// MasterTimeout specifies an explicit operation timeout for connection to master node. func (s *SnapshotDeleteRepositoryService) MasterTimeout(masterTimeout string) *SnapshotDeleteRepositoryService { s.masterTimeout = masterTimeout return s } -// Timeout is documented as: Explicit operation timeout. +// Timeout is an explicit operation timeout. func (s *SnapshotDeleteRepositoryService) Timeout(timeout string) *SnapshotDeleteRepositoryService { s.timeout = timeout return s diff --git a/snapshot_delete_repository_test.go b/snapshot_delete_repository_test.go index d2c673e5e..aec793a60 100644 --- a/snapshot_delete_repository_test.go +++ b/snapshot_delete_repository_test.go @@ -1,3 +1,7 @@ +// 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 "testing" diff --git a/snapshot_get_repository.go b/snapshot_get_repository.go index 948deb138..10b2d0b9c 100644 --- a/snapshot_get_repository.go +++ b/snapshot_get_repository.go @@ -1,4 +1,4 @@ -// Copyright 2012-2017 Oliver Eilhard. All rights reserved. +// 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. @@ -14,7 +14,9 @@ import ( "gopkg.in/olivere/elastic.v5/uritemplates" ) -// SnapshotGetRepositoryService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.x/modules-snapshots.html. +// SnapshotGetRepositoryService reads a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html +// for details. type SnapshotGetRepositoryService struct { client *Client pretty bool @@ -31,19 +33,19 @@ func NewSnapshotGetRepositoryService(client *Client) *SnapshotGetRepositoryServi } } -// Repository is documented as: A comma-separated list of repository names. +// Repository is the list of repository names. func (s *SnapshotGetRepositoryService) Repository(repositories ...string) *SnapshotGetRepositoryService { s.repository = append(s.repository, repositories...) return s } -// Local is documented as: Return local information, do not retrieve the state from master node (default: false). +// Local indicates whether to return local information, i.e. do not retrieve the state from master node (default: false). func (s *SnapshotGetRepositoryService) Local(local bool) *SnapshotGetRepositoryService { s.local = &local return s } -// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +// MasterTimeout specifies an explicit operation timeout for connection to master node. func (s *SnapshotGetRepositoryService) MasterTimeout(masterTimeout string) *SnapshotGetRepositoryService { s.masterTimeout = masterTimeout return s @@ -118,9 +120,11 @@ func (s *SnapshotGetRepositoryService) Do(ctx context.Context) (SnapshotGetRepos } // SnapshotGetRepositoryResponse is the response of SnapshotGetRepositoryService.Do. -type SnapshotGetRepositoryResponse map[string]*SnapshotRepository +type SnapshotGetRepositoryResponse map[string]*SnapshotRepositoryMetaData -type SnapshotRepository struct { - Type string `json:"type"` - Settings interface{} `json:"settings,omitempty"` +// SnapshotRepositoryMetaData contains all information about +// a single snapshot repository. +type SnapshotRepositoryMetaData struct { + Type string `json:"type"` + Settings map[string]interface{} `json:"settings,omitempty"` } diff --git a/snapshot_get_repository_test.go b/snapshot_get_repository_test.go index 607cc930b..0dcd0bb90 100644 --- a/snapshot_get_repository_test.go +++ b/snapshot_get_repository_test.go @@ -1,3 +1,7 @@ +// 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 "testing" diff --git a/snapshot_verify_repository.go b/snapshot_verify_repository.go index acedd3da2..4e8c25a24 100644 --- a/snapshot_verify_repository.go +++ b/snapshot_verify_repository.go @@ -1,4 +1,4 @@ -// Copyright 2012-2017 Oliver Eilhard. All rights reserved. +// 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. @@ -13,7 +13,9 @@ import ( "gopkg.in/olivere/elastic.v5/uritemplates" ) -// SnapshotVerifyRepositoryService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.x/modules-snapshots.html. +// SnapshotVerifyRepositoryService verifies a snapshop repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html +// for details. type SnapshotVerifyRepositoryService struct { client *Client pretty bool @@ -29,19 +31,19 @@ func NewSnapshotVerifyRepositoryService(client *Client) *SnapshotVerifyRepositor } } -// Repository is documented as: A repository name. +// Repository specifies the repository name. func (s *SnapshotVerifyRepositoryService) Repository(repository string) *SnapshotVerifyRepositoryService { s.repository = repository return s } -// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +// MasterTimeout is the explicit operation timeout for connection to master node. func (s *SnapshotVerifyRepositoryService) MasterTimeout(masterTimeout string) *SnapshotVerifyRepositoryService { s.masterTimeout = masterTimeout return s } -// Timeout is documented as: Explicit operation timeout. +// Timeout is an explicit operation timeout. func (s *SnapshotVerifyRepositoryService) Timeout(timeout string) *SnapshotVerifyRepositoryService { s.timeout = timeout return s diff --git a/snapshot_verify_repository_test.go b/snapshot_verify_repository_test.go index 99839979d..9776782d2 100644 --- a/snapshot_verify_repository_test.go +++ b/snapshot_verify_repository_test.go @@ -1,3 +1,7 @@ +// 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 "testing"