Skip to content

Commit 37f1b77

Browse files
authored
Fixes up admin cluster command for adding search attributes to be indexed (temporalio#514)
Changed the --search_attr_type flag to take in an English readable name (e.g. Keyword, Int, String, etc) instead of an integer (which is less intuitive and should be hidden from the end user anyway). Fixes confusion for the user around which integer to map to the type of field they want indexed. Removes the need to document the attribute type integer in our temporal docs (which just causes confusion)
1 parent ca33ba4 commit 37f1b77

File tree

4 files changed

+16
-73
lines changed

4 files changed

+16
-73
lines changed

tools/cli/admin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,9 @@ func newAdminClusterCommands() []cli.Command {
798798
Name: FlagSearchAttributesKey,
799799
Usage: "Search Attribute key to be whitelisted",
800800
},
801-
cli.IntFlag{
801+
cli.StringFlag{
802802
Name: FlagSearchAttributesType,
803-
Value: -1,
804-
Usage: "Search Attribute value type. [0:String, 1:Keyword, 2:Int, 3:Double, 4:Bool, 5:Datetime]",
803+
Usage: "Search Attribute value type. [string, keyword, int, double, bool, datetime]",
805804
},
806805
cli.StringFlag{
807806
Name: FlagSecurityTokenWithAlias,

tools/cli/adminClusterCommands.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,32 @@ import (
3737
// AdminAddSearchAttribute to whitelist search attribute
3838
func AdminAddSearchAttribute(c *cli.Context) {
3939
key := getRequiredOption(c, FlagSearchAttributesKey)
40-
valType := getRequiredIntOption(c, FlagSearchAttributesType)
41-
if !isValueTypeValid(valType) {
42-
ErrorAndExit("Unknown Search Attributes value type.", nil)
40+
valTypeStr := getRequiredOption(c, FlagSearchAttributesType)
41+
valTypeInt, err := stringToEnum(valTypeStr, enumspb.IndexedValueType_value)
42+
if err != nil {
43+
ErrorAndExit("Failed to parse Search Attribute Type", err)
4344
}
4445

4546
// ask user for confirmation
46-
promptMsg := fmt.Sprintf("Are you trying to add key [%s] with Type [%s]? Y/N", color.YellowString(key), color.YellowString(intValTypeToString(valType)))
47+
promptMsg := fmt.Sprintf(
48+
"Are you trying to add key [%s] with Type [%s]? Y/N",
49+
color.YellowString(key),
50+
color.YellowString(enumspb.IndexedValueType_name[valTypeInt]),
51+
)
52+
4753
prompt(promptMsg, c.GlobalBool(FlagAutoConfirm))
4854

4955
adminClient := cFactory.AdminClient(c)
5056
ctx, cancel := newContext(c)
5157
defer cancel()
5258
request := &adminservice.AddSearchAttributeRequest{
5359
SearchAttribute: map[string]enumspb.IndexedValueType{
54-
key: enumspb.IndexedValueType(valType),
60+
key: enumspb.IndexedValueType(valTypeInt),
5561
},
5662
SecurityToken: c.String(FlagSecurityToken),
5763
}
5864

59-
_, err := adminClient.AddSearchAttribute(ctx, request)
65+
_, err = adminClient.AddSearchAttribute(ctx, request)
6066
if err != nil {
6167
ErrorAndExit("Add search attribute failed.", err)
6268
}
@@ -76,26 +82,3 @@ func AdminDescribeCluster(c *cli.Context) {
7682

7783
prettyPrintJSONObject(response)
7884
}
79-
80-
func intValTypeToString(valType int) string {
81-
switch valType {
82-
case 0:
83-
return "String"
84-
case 1:
85-
return "Keyword"
86-
case 2:
87-
return "Int"
88-
case 3:
89-
return "Double"
90-
case 4:
91-
return "Bool"
92-
case 5:
93-
return "Datetime"
94-
default:
95-
return ""
96-
}
97-
}
98-
99-
func isValueTypeValid(valType int) bool {
100-
return valType >= 0 && valType <= 5
101-
}

tools/cli/adminClusterCommands_test.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,3 @@
2323
// THE SOFTWARE.
2424

2525
package cli
26-
27-
import (
28-
"testing"
29-
30-
"github.com/bmizerany/assert"
31-
)
32-
33-
func TestAdminAddSearchAttribute_isValueTypeValid(t *testing.T) {
34-
testCases := []struct {
35-
name string
36-
input int
37-
expected bool
38-
}{
39-
{
40-
name: "negative",
41-
input: -1,
42-
expected: false,
43-
},
44-
{
45-
name: "valid",
46-
input: 0,
47-
expected: true,
48-
},
49-
{
50-
name: "valid",
51-
input: 5,
52-
expected: true,
53-
},
54-
{
55-
name: "unknown",
56-
input: 6,
57-
expected: false,
58-
},
59-
}
60-
61-
for _, testCase := range testCases {
62-
assert.Equal(t, testCase.expected, isValueTypeValid(testCase.input))
63-
}
64-
}

tools/cli/app_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ func (s *cliAppSuite) TestAdminDescribeWorkflow_Failed() {
543543
func (s *cliAppSuite) TestAdminAddSearchAttribute() {
544544
request := &adminservice.AddSearchAttributeRequest{
545545
SearchAttribute: map[string]enumspb.IndexedValueType{
546-
"testKey": enumspb.IndexedValueType(1),
546+
"testKey": enumspb.IndexedValueType(2),
547547
},
548548
}
549549
s.serverAdminClient.EXPECT().AddSearchAttribute(gomock.Any(), request).Times(1)
550550

551-
err := s.app.Run([]string{"", "--auto_confirm", "--ns", cliTestNamespace, "admin", "cl", "asa", "--search_attr_key", "testKey", "--search_attr_type", "1"})
551+
err := s.app.Run([]string{"", "--auto_confirm", "--ns", cliTestNamespace, "admin", "cl", "asa", "--search_attr_key", "testKey", "--search_attr_type", "keyword"})
552552
s.Nil(err)
553553
}
554554

0 commit comments

Comments
 (0)