From b0102f916d6bcf39a17b81ec8d0a71326503c00e Mon Sep 17 00:00:00 2001 From: Manu Srivastava Date: Wed, 8 Jul 2020 13:51:12 -0700 Subject: [PATCH 1/4] Fixes up Admin Cluster Command for adding search attributes --- tools/cli/admin.go | 5 ++-- tools/cli/adminClusterCommands.go | 41 ++++++++------------------ tools/cli/adminClusterCommands_test.go | 39 ------------------------ 3 files changed, 14 insertions(+), 71 deletions(-) diff --git a/tools/cli/admin.go b/tools/cli/admin.go index 77e075851d8..90db73acb88 100644 --- a/tools/cli/admin.go +++ b/tools/cli/admin.go @@ -798,10 +798,9 @@ func newAdminClusterCommands() []cli.Command { Name: FlagSearchAttributesKey, Usage: "Search Attribute key to be whitelisted", }, - cli.IntFlag{ + cli.StringFlag{ Name: FlagSearchAttributesType, - Value: -1, - Usage: "Search Attribute value type. [0:String, 1:Keyword, 2:Int, 3:Double, 4:Bool, 5:Datetime]", + Usage: "Search Attribute value type. [String, Keyword, Int, Double, Bool, Datetime]", }, cli.StringFlag{ Name: FlagSecurityTokenWithAlias, diff --git a/tools/cli/adminClusterCommands.go b/tools/cli/adminClusterCommands.go index 5f679b930af..cb02fffe9cd 100644 --- a/tools/cli/adminClusterCommands.go +++ b/tools/cli/adminClusterCommands.go @@ -37,13 +37,19 @@ import ( // AdminAddSearchAttribute to whitelist search attribute func AdminAddSearchAttribute(c *cli.Context) { key := getRequiredOption(c, FlagSearchAttributesKey) - valType := getRequiredIntOption(c, FlagSearchAttributesType) - if !isValueTypeValid(valType) { - ErrorAndExit("Unknown Search Attributes value type.", nil) + valTypeStr := getRequiredOption(c, FlagSearchAttributesType) + valTypeInt, err := stringToEnum(valTypeStr, enumspb.IndexedValueType_value) + if err != nil { + ErrorAndExit("Failed to parse Search Attribute Type", err) } // ask user for confirmation - promptMsg := fmt.Sprintf("Are you trying to add key [%s] with Type [%s]? Y/N", color.YellowString(key), color.YellowString(intValTypeToString(valType))) + promptMsg := fmt.Sprintf( + "Are you trying to add key [%s] with Type [%s]? Y/N", + color.YellowString(key), + color.YellowString(enumspb.IndexedValueType_name[valTypeInt]), + ) + prompt(promptMsg, c.GlobalBool(FlagAutoConfirm)) adminClient := cFactory.AdminClient(c) @@ -51,12 +57,12 @@ func AdminAddSearchAttribute(c *cli.Context) { defer cancel() request := &adminservice.AddSearchAttributeRequest{ SearchAttribute: map[string]enumspb.IndexedValueType{ - key: enumspb.IndexedValueType(valType), + key: enumspb.IndexedValueType(valTypeInt), }, SecurityToken: c.String(FlagSecurityToken), } - _, err := adminClient.AddSearchAttribute(ctx, request) + _, err = adminClient.AddSearchAttribute(ctx, request) if err != nil { ErrorAndExit("Add search attribute failed.", err) } @@ -76,26 +82,3 @@ func AdminDescribeCluster(c *cli.Context) { prettyPrintJSONObject(response) } - -func intValTypeToString(valType int) string { - switch valType { - case 0: - return "String" - case 1: - return "Keyword" - case 2: - return "Int" - case 3: - return "Double" - case 4: - return "Bool" - case 5: - return "Datetime" - default: - return "" - } -} - -func isValueTypeValid(valType int) bool { - return valType >= 0 && valType <= 5 -} diff --git a/tools/cli/adminClusterCommands_test.go b/tools/cli/adminClusterCommands_test.go index 8ca1ab53b1c..ca088c5a8f3 100644 --- a/tools/cli/adminClusterCommands_test.go +++ b/tools/cli/adminClusterCommands_test.go @@ -23,42 +23,3 @@ // THE SOFTWARE. package cli - -import ( - "testing" - - "github.com/bmizerany/assert" -) - -func TestAdminAddSearchAttribute_isValueTypeValid(t *testing.T) { - testCases := []struct { - name string - input int - expected bool - }{ - { - name: "negative", - input: -1, - expected: false, - }, - { - name: "valid", - input: 0, - expected: true, - }, - { - name: "valid", - input: 5, - expected: true, - }, - { - name: "unknown", - input: 6, - expected: false, - }, - } - - for _, testCase := range testCases { - assert.Equal(t, testCase.expected, isValueTypeValid(testCase.input)) - } -} From 5a9e9ff1ac91aba1df875509f28bc46288a1f8de Mon Sep 17 00:00:00 2001 From: Manu Srivastava Date: Wed, 8 Jul 2020 14:20:34 -0700 Subject: [PATCH 2/4] fix broken test --- tools/cli/app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cli/app_test.go b/tools/cli/app_test.go index e3f714db9d4..e534257af8e 100644 --- a/tools/cli/app_test.go +++ b/tools/cli/app_test.go @@ -548,7 +548,7 @@ func (s *cliAppSuite) TestAdminAddSearchAttribute() { } s.serverAdminClient.EXPECT().AddSearchAttribute(gomock.Any(), request).Times(1) - err := s.app.Run([]string{"", "--auto_confirm", "--ns", cliTestNamespace, "admin", "cl", "asa", "--search_attr_key", "testKey", "--search_attr_type", "1"}) + err := s.app.Run([]string{"", "--auto_confirm", "--ns", cliTestNamespace, "admin", "cl", "asa", "--search_attr_key", "testKey", "--search_attr_type", "keyword"}) s.Nil(err) } From 0544770c77a9fcf15bb1965ac08d299b7c523f02 Mon Sep 17 00:00:00 2001 From: Manu Srivastava Date: Wed, 8 Jul 2020 14:22:59 -0700 Subject: [PATCH 3/4] one more test fix --- tools/cli/app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cli/app_test.go b/tools/cli/app_test.go index e534257af8e..690a9273005 100644 --- a/tools/cli/app_test.go +++ b/tools/cli/app_test.go @@ -543,7 +543,7 @@ func (s *cliAppSuite) TestAdminDescribeWorkflow_Failed() { func (s *cliAppSuite) TestAdminAddSearchAttribute() { request := &adminservice.AddSearchAttributeRequest{ SearchAttribute: map[string]enumspb.IndexedValueType{ - "testKey": enumspb.IndexedValueType(1), + "testKey": enumspb.IndexedValueType(2), }, } s.serverAdminClient.EXPECT().AddSearchAttribute(gomock.Any(), request).Times(1) From aceb338b9e8d9aa594c5949780a6cf0705783c78 Mon Sep 17 00:00:00 2001 From: Manu Srivastava Date: Wed, 8 Jul 2020 15:09:13 -0700 Subject: [PATCH 4/4] changes casing of usage message --- tools/cli/admin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cli/admin.go b/tools/cli/admin.go index 90db73acb88..01a4b11a2b5 100644 --- a/tools/cli/admin.go +++ b/tools/cli/admin.go @@ -800,7 +800,7 @@ func newAdminClusterCommands() []cli.Command { }, cli.StringFlag{ Name: FlagSearchAttributesType, - Usage: "Search Attribute value type. [String, Keyword, Int, Double, Bool, Datetime]", + Usage: "Search Attribute value type. [string, keyword, int, double, bool, datetime]", }, cli.StringFlag{ Name: FlagSecurityTokenWithAlias,