Skip to content

Commit

Permalink
Merge pull request #14 from github/manage_snapshots
Browse files Browse the repository at this point in the history
Manage snapshots
  • Loading branch information
Nick Canzoneri authored Oct 29, 2018
2 parents 1246506 + 68c17fc commit 4eaecb5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ Functionality:
* Enable or disable cluster allocation entirely


### v0.2.0 - Proposed
## v0.2.0 - [Released](https://github.com/github/vulcanizer/releases/tag/v0.2.0)

Handle more cases around repositories and snapshots.

Functionality:
* List repositories
* Verify a repository
* Create a repository
* Delete a snapshot

#### v0.2.1 - Proposed

Functionality:
* List repositories
* Create a repository

### v0.3.0 - Proposed

Show more information around shard allocation and recovery.
Expand Down
45 changes: 45 additions & 0 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type snapshotWrapper struct {
Snapshots []Snapshot `json:"snapshots"`
}

type acknowledgedResponse struct {
Acknowledged bool `json:"acknowledged"`
}

//Holds information about an Elasticsearch snapshot, based on the snapshot API: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html
type Snapshot struct {
State string `json:"state"`
Expand Down Expand Up @@ -169,6 +173,14 @@ func (c *Client) buildPutRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Put(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}

func (c *Client) buildDeleteRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Delete(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}

func (c *Client) buildPostRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Post(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}

// Get current cluster settings for shard allocation exclusion rules.
func (c *Client) GetClusterExcludeSettings() (ExcludeSettings, error) {
body, err := handleErrWithBytes(c.buildGetRequest(clusterSettingsPath))
Expand Down Expand Up @@ -434,3 +446,36 @@ func (c *Client) GetSnapshotStatus(repository string, snapshot string) (Snapshot

return snapshotWrapper.Snapshots[0], nil
}

//Delete a snapshot
//
//Use case: You want to delete older snapshots so that they don't take up extra space.
func (c *Client) DeleteSnapshot(repository string, snapshot string) error {
var response acknowledgedResponse

err := handleErrWithStruct(c.buildDeleteRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)).Timeout(10*time.Minute), &response)

if err != nil {
return err
}

if !response.Acknowledged {
return fmt.Errorf(`Request to delete snapshot "%s" on respository "%s" was not acknowledged. %+v`, snapshot, repository, response)
}

return nil
}

//Verify a snapshot repository
//
//Use case: Have Elasticsearch verify a repository to make sure that all nodes can access the snapshot location correctly.
func (c *Client) VerifyRepository(repository string) (bool, error) {

_, err := handleErrWithBytes(c.buildPostRequest(fmt.Sprintf("_snapshot/%s/_verify", repository)))

if err != nil {
return false, err
}

return true, nil
}
38 changes: 38 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,41 @@ func TestGetSnapshotStatus(t *testing.T) {
t.Errorf("Unexpected name, got %+v", snapshot)
}
}

func TestDeleteSnapshot(t *testing.T) {
testSetup := &ServerSetup{
Method: "DELETE",
Path: "/_snapshot/octocat/snapshot1",
Response: `{"acknowledged": true}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

err := client.DeleteSnapshot("octocat", "snapshot1")
if err != nil {
t.Errorf("Unexpected error, got %s", err)
}
}

func TestVerifyRepository(t *testing.T) {
testSetup := &ServerSetup{
Method: "POST",
Path: "/_snapshot/octocat/_verify",
Response: `{"nodes":{"YaTBa_BtRmOoz1bHKJeQ8w":{"name":"YaTBa_B"}}}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

verified, err := client.VerifyRepository("octocat")
if err != nil {
t.Errorf("Unexpected error, got %s", err)
}

if !verified {
t.Errorf("Expected repository to be verified, got %v", verified)
}
}

0 comments on commit 4eaecb5

Please sign in to comment.