From 3bb750fc71e676239eb12b2139db9416e803c81a Mon Sep 17 00:00:00 2001 From: Nick Canzoneri Date: Thu, 23 May 2019 14:05:52 -0400 Subject: [PATCH 1/2] Rename `GetAliases` to `GetAllAliases` --- cmd/vulcanizer/aliases.go | 9 ++++++++- es.go | 18 +++++++++++++++++- es_test.go | 34 ++++++++++++++++++++++++++++++++-- integration_test.go | 8 ++++---- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/cmd/vulcanizer/aliases.go b/cmd/vulcanizer/aliases.go index 5a8ab55..6ef9480 100644 --- a/cmd/vulcanizer/aliases.go +++ b/cmd/vulcanizer/aliases.go @@ -91,10 +91,17 @@ var cmdAliasesList = &cobra.Command{ Use: "list", Short: "Display the aliases of the cluster", Long: `Show what aliases are created on the given cluster.`, + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { v := getClient() - aliases, err := v.GetAliases() + var err error + var aliases []vulcanizer.Alias + if len(args) > 0 { + aliases, err = v.GetAllAliases() + } else { + aliases, err = v.GetAliases(args[0]) + } if err != nil { fmt.Printf("Error getting aliases: %s\n", err) diff --git a/es.go b/es.go index 4d81113..eb4ca28 100644 --- a/es.go +++ b/es.go @@ -519,7 +519,7 @@ func (c *Client) GetIndices(index string) ([]Index, error) { //Get all the aliases in the cluster. // //Use case: You want to see some basic info on all the aliases of the cluster -func (c *Client) GetAliases() ([]Alias, error) { +func (c *Client) GetAllAliases() ([]Alias, error) { var aliases []Alias err := handleErrWithStruct(c.buildGetRequest("_cat/aliases?h=alias,index,filter,routing.index,routing.search"), &aliases) @@ -531,6 +531,22 @@ func (c *Client) GetAliases() ([]Alias, error) { return aliases, nil } +//Get a subset the aliases in the cluster. +// +//Use case: You want to see some basic info on a subset of the aliases of the cluster +func (c *Client) GetAliases(alias string) ([]Alias, error) { + var aliases []Alias + + path := fmt.Sprintf("_cat/aliases/%s?h=alias,index,filter,routing.index,routing.search", alias) + err := handleErrWithStruct(c.buildGetRequest(path), &aliases) + + if err != nil { + return nil, err + } + + return aliases, nil +} + //Interact with aliases in the cluster. // //Use case: You want to add, delete or update an index alias diff --git a/es_test.go b/es_test.go index e790d4a..d7b300b 100644 --- a/es_test.go +++ b/es_test.go @@ -335,7 +335,7 @@ func TestGetIndices(t *testing.T) { } } -func TestGetAliases(t *testing.T) { +func TestGetAllAliases(t *testing.T) { testSetup := &ServerSetup{ Method: "GET", Path: "/_cat/aliases", @@ -346,7 +346,37 @@ func TestGetAliases(t *testing.T) { defer ts.Close() client := NewClient(host, port) - aliases, err := client.GetAliases() + aliases, err := client.GetAllAliases() + + if err != nil { + t.Errorf("Unexpected error expected nil, got %s", err) + } + + if len(aliases) != 2 { + t.Errorf("Unexpected aliases, got %v", aliases) + } + + if aliases[0].Name != "test" || aliases[0].IndexName != "test_v1" || aliases[0].Filter != "-filter" || aliases[0].RoutingIndex != "-routing.index" || aliases[0].RoutingSearch != "-routing.search" { + t.Errorf("Unexpected index values, got %v", aliases[0]) + } + + if aliases[1].Name != "test_again" || aliases[1].IndexName != "test_again_v1" || aliases[1].Filter != "--filter" || aliases[1].RoutingIndex != "--routing.index" || aliases[1].RoutingSearch != "--routing.search" { + t.Errorf("Unexpected index values, got %v", aliases[1]) + } +} + +func TestGetAliases(t *testing.T) { + testSetup := &ServerSetup{ + Method: "GET", + Path: "/_cat/aliases/test*", + Response: `[{"alias": "test","index": "test_v1","filter": "-filter","routing.index": "-routing.index","routing.search": "-routing.search"},{"alias": "test_again","index": "test_again_v1","filter": "--filter","routing.index": "--routing.index","routing.search": "--routing.search"}]`, + } + + host, port, ts := setupTestServers(t, []*ServerSetup{testSetup}) + defer ts.Close() + client := NewClient(host, port) + + aliases, err := client.GetAliases("test*") if err != nil { t.Errorf("Unexpected error expected nil, got %s", err) diff --git a/integration_test.go b/integration_test.go index 99bdd91..de73deb 100644 --- a/integration_test.go +++ b/integration_test.go @@ -73,7 +73,7 @@ func TestIndices(t *testing.T) { func TestAliases(t *testing.T) { c := vulcanizer.NewClient("localhost", 49200) - aliases, err := c.GetAliases() + aliases, err := c.GetAllAliases() if err != nil { t.Fatalf("Error getting aliases: %s", err) @@ -114,7 +114,7 @@ func TestAliasesAddDeleteUpdate(t *testing.T) { t.Fatalf("Error modifying aliases: %s", err) } - aliases, err := c.GetAliases() + aliases, err := c.GetAllAliases() if err != nil { t.Fatalf("Error getting aliases: %s", err) } @@ -146,7 +146,7 @@ func TestAliasesAddDeleteUpdate(t *testing.T) { t.Fatalf("Error modifying aliases: %s", err) } - aliases, err := c.GetAliases() + aliases, err := c.GetAllAliases() if err != nil { t.Fatalf("Error getting aliases: %s", err) } @@ -170,7 +170,7 @@ func TestAliasesAddDeleteUpdate(t *testing.T) { t.Fatalf("Error modifying aliases: %s", err) } - aliases, err := c.GetAliases() + aliases, err := c.GetAllAliases() if err != nil { t.Fatalf("Error getting aliases: %s", err) } From 23eaf30e8a4cf2426bf0a788d771370a2592854e Mon Sep 17 00:00:00 2001 From: Nick Canzoneri Date: Thu, 23 May 2019 14:19:44 -0400 Subject: [PATCH 2/2] Add a call to `GetAliases(string)` to the integration tests --- integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_test.go b/integration_test.go index de73deb..0c4b5e8 100644 --- a/integration_test.go +++ b/integration_test.go @@ -170,7 +170,7 @@ func TestAliasesAddDeleteUpdate(t *testing.T) { t.Fatalf("Error modifying aliases: %s", err) } - aliases, err := c.GetAllAliases() + aliases, err := c.GetAliases("integration_test*") if err != nil { t.Fatalf("Error getting aliases: %s", err) }