From 121db02e3dd7bd847aa9661f183bbad7e96a9dfa Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 15:32:56 +0100 Subject: [PATCH 1/6] AddAlias implementation --- goes.go | 25 +++++++++++++++++++++++++ goes_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/goes.go b/goes.go index a85cd7f..ce6bc29 100644 --- a/goes.go +++ b/goes.go @@ -528,3 +528,28 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response, return r.Run() } + +// AddAlias creates an alias to one or more indexes +func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) { + command := map[string]interface{}{ + "actions": make([]map[string]interface{}, 1), + } + + for _, index := range indexes { + command["actions"] = append(command["actions"].([]map[string]interface{}), map[string]interface{}{ + "add": map[string]interface{}{ + "index": index, + "alias": alias, + }, + }) + } + + r := Request{ + Conn: c, + Query: command, + method: "POST", + api: "_aliases", + } + + return r.Run() +} diff --git a/goes_test.go b/goes_test.go index fdb6023..30f34d0 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1272,3 +1272,52 @@ func (s *GoesTestSuite) TestDeleteMapping(c *C) { c.Assert(response.Acknowledged, Equals, true) c.Assert(response.TimedOut, Equals, false) } + +func (s *GoesTestSuite) TestAddAlias(c *C) { + aliasName := "testAlias" + indexName := "testalias_1" + docType := "testDoc" + docId := "1234" + source := map[string]interface{}{ + "user": "foo", + "message": "bar", + } + + conn := NewConnection(ES_HOST, ES_PORT) + defer conn.DeleteIndex(indexName) + + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(indexName) + + d := Document{ + Index: indexName, + Type: docType, + Id: docId, + Fields: source, + } + + // Index data + _, err = conn.Index(d, url.Values{}) + c.Assert(err, IsNil) + + // Add alias + _, err = conn.AddAlias(aliasName, []string{indexName}) + c.Assert(err, IsNil) + + // Get document via alias + response, err := conn.Get(aliasName, docType, docId, url.Values{}) + c.Assert(err, IsNil) + + expectedResponse := Response{ + Index: indexName, + Type: docType, + Id: docId, + Version: 1, + Found: true, + Source: source, + } + + response.Raw = nil + c.Assert(response, DeepEquals, expectedResponse) +} From 13bf94fc2e1672a9da59cae371bc597a35a280c3 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 15:33:06 +0100 Subject: [PATCH 2/6] New gocheck location --- goes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goes_test.go b/goes_test.go index 30f34d0..dea9428 100644 --- a/goes_test.go +++ b/goes_test.go @@ -5,7 +5,7 @@ package goes import ( - . "launchpad.net/gocheck" + . "gopkg.in/check.v1" "net/http" "net/url" "os" From f6ba59e55a2d34f20413d10d6c521920cd765a20 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 15:48:00 +0100 Subject: [PATCH 3/6] RemoveAlias implementation, reusing code --- goes.go | 15 ++++++++++++--- goes_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/goes.go b/goes.go index ce6bc29..076bcb7 100644 --- a/goes.go +++ b/goes.go @@ -529,15 +529,14 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response, return r.Run() } -// AddAlias creates an alias to one or more indexes -func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) { +func (c *Connection) modifyAlias(action string, alias string, indexes []string) (Response, error) { command := map[string]interface{}{ "actions": make([]map[string]interface{}, 1), } for _, index := range indexes { command["actions"] = append(command["actions"].([]map[string]interface{}), map[string]interface{}{ - "add": map[string]interface{}{ + action: map[string]interface{}{ "index": index, "alias": alias, }, @@ -553,3 +552,13 @@ func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) return r.Run() } + +// AddAlias creates an alias to one or more indexes +func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) { + return c.modifyAlias("add", alias, indexes) +} + +// RemoveAlias removes an alias to one or more indexes +func (c *Connection) RemoveAlias(alias string, indexes []string) (Response, error) { + return c.modifyAlias("remove", alias, indexes) +} diff --git a/goes_test.go b/goes_test.go index dea9428..3d03297 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1321,3 +1321,44 @@ func (s *GoesTestSuite) TestAddAlias(c *C) { response.Raw = nil c.Assert(response, DeepEquals, expectedResponse) } + +func (s *GoesTestSuite) TestRemoveAlias(c *C) { + aliasName := "testAlias" + indexName := "testalias_1" + docType := "testDoc" + docId := "1234" + source := map[string]interface{}{ + "user": "foo", + "message": "bar", + } + + conn := NewConnection(ES_HOST, ES_PORT) + defer conn.DeleteIndex(indexName) + + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(indexName) + + d := Document{ + Index: indexName, + Type: docType, + Id: docId, + Fields: source, + } + + // Index data + _, err = conn.Index(d, url.Values{}) + c.Assert(err, IsNil) + + // Add alias + _, err = conn.AddAlias(aliasName, []string{indexName}) + c.Assert(err, IsNil) + + // Remove alias + _, err = conn.RemoveAlias(aliasName, []string{indexName}) + c.Assert(err, IsNil) + + // Get document via alias + _, err = conn.Get(aliasName, docType, docId, url.Values{}) + c.Assert(err.Error(), Equals, "[404] IndexMissingException[["+aliasName+"] missing]") +} From aedb5f6b85ed17a82ebcbbb37a2b65b7003fcee1 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 15:48:17 +0100 Subject: [PATCH 4/6] Delete index before test, just in case --- goes_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/goes_test.go b/goes_test.go index 3d03297..e402f5b 100644 --- a/goes_test.go +++ b/goes_test.go @@ -240,6 +240,7 @@ func (s *GoesTestSuite) TestBulkSend(c *C) { conn := NewConnection(ES_HOST, ES_PORT) + conn.DeleteIndex(indexName) _, err := conn.CreateIndex(indexName, nil) c.Assert(err, IsNil) From 5e4a4d663a9b9e5e9a220561fdddca16a18561b4 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 16:46:08 +0100 Subject: [PATCH 5/6] AliasExists implementation --- goes.go | 14 ++++++++++++++ goes_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/goes.go b/goes.go index 076bcb7..5637228 100644 --- a/goes.go +++ b/goes.go @@ -562,3 +562,17 @@ func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) func (c *Connection) RemoveAlias(alias string, indexes []string) (Response, error) { return c.modifyAlias("remove", alias, indexes) } + +// AliasExists checks whether alias is defined on the server +func (c *Connection) AliasExists(alias string) (bool, error) { + + r := Request{ + Conn: c, + method: "HEAD", + api: "_alias/" + alias, + } + + resp, err := r.Run() + + return resp.Status == 200, err +} diff --git a/goes_test.go b/goes_test.go index e402f5b..3334d30 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1363,3 +1363,28 @@ func (s *GoesTestSuite) TestRemoveAlias(c *C) { _, err = conn.Get(aliasName, docType, docId, url.Values{}) c.Assert(err.Error(), Equals, "[404] IndexMissingException[["+aliasName+"] missing]") } + +func (s *GoesTestSuite) TestAliasExists(c *C) { + index := "testaliasexist_1" + alias := "testaliasexists" + + conn := NewConnection(ES_HOST, ES_PORT) + // just in case + conn.DeleteIndex(index) + + exists, err := conn.AliasExists(alias) + c.Assert(exists, Equals, false) + + _, err = conn.CreateIndex(index, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(index) + time.Sleep(200 * time.Millisecond) + + _, err = conn.AddAlias(alias, []string{index}) + c.Assert(err, IsNil) + time.Sleep(200 * time.Millisecond) + defer conn.RemoveAlias(alias, []string{index}) + + exists, err = conn.AliasExists(alias) + c.Assert(exists, Equals, true) +} From 906234e92088edd88055dc5a126b4a7736a0ac6d Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Mon, 2 Mar 2015 08:59:23 +0100 Subject: [PATCH 6/6] install gocheck on travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 69123d3..18e7b30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,5 +18,8 @@ before_script: - tar -xzf elasticsearch-${ES_VERSION}.tar.gz -C ${HOME}/elasticsearch - ${HOME}/elasticsearch/elasticsearch-${ES_VERSION}/bin/elasticsearch >/dev/null & +install: + - go get gopkg.in/check.v1 + script: - make test