Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes up admin cluster command for adding search attributes to be indexed #514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 12 additions & 29 deletions tools/cli/adminClusterCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,32 @@ 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)
ctx, cancel := newContext(c)
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)
}
Expand All @@ -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
}
39 changes: 0 additions & 39 deletions tools/cli/adminClusterCommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
4 changes: 2 additions & 2 deletions tools/cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ 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)

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)
}

Expand Down