Skip to content

Commit

Permalink
Syntactic sugar on the Snapshot Repository Create API
Browse files Browse the repository at this point in the history
You can now build the body via the builder.

See #508
  • Loading branch information
olivere committed Apr 10, 2017
1 parent 1abca06 commit 7169c87
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 31 deletions.
63 changes: 52 additions & 11 deletions snapshot_create_repository.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -13,14 +13,18 @@ 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
repository string
masterTimeout string
timeout string
verify *bool
typ string
settings map[string]interface{}
bodyJson interface{}
bodyString string
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion snapshot_create_repository_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
}
12 changes: 7 additions & 5 deletions snapshot_delete_repository.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions snapshot_delete_repository_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
22 changes: 13 additions & 9 deletions snapshot_get_repository.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"`
}
4 changes: 4 additions & 0 deletions snapshot_get_repository_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 7 additions & 5 deletions snapshot_verify_repository.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions snapshot_verify_repository_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 7169c87

Please sign in to comment.