Skip to content

Commit de9f877

Browse files
committed
fix: [GoSDK] Pass base64 passwd content instead of raw data (#40268)
Related to #40261 Also add some options for create collection options and refine some behavior Signed-off-by: Congqi Xia <[email protected]>
1 parent 1df4631 commit de9f877

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-12
lines changed

client/index/index.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ func (gi GenericIndex) Params() map[string]string {
6868
return m
6969
}
7070

71+
func (gi GenericIndex) WithMetricType(metricType MetricType) {
72+
gi.baseIndex.metricType = metricType
73+
}
74+
7175
// NewGenericIndex create generic index instance
72-
func NewGenericIndex(name string, params map[string]string) Index {
76+
func NewGenericIndex(name string, params map[string]string) GenericIndex {
7377
return GenericIndex{
7478
baseIndex: baseIndex{
7579
name: name,

client/milvusclient/collection_example_test.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,67 @@ func ExampleClient_CreateCollection_ttl() {
154154
}
155155
}
156156

157+
func ExampleClient_CreateCollection_quickSetup() {
158+
ctx, cancel := context.WithCancel(context.Background())
159+
defer cancel()
160+
161+
collectionName := `quick_setup_1`
162+
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
163+
Address: milvusAddr,
164+
})
165+
if err != nil {
166+
// handle err
167+
}
168+
169+
err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512))
170+
if err != nil {
171+
// handle error
172+
}
173+
}
174+
175+
func ExampleClient_CreateCollection_quickSetupWithIndexParams() {
176+
ctx, cancel := context.WithCancel(context.Background())
177+
defer cancel()
178+
179+
collectionName := `quick_setup_2`
180+
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
181+
Address: milvusAddr,
182+
})
183+
if err != nil {
184+
// handle err
185+
}
186+
187+
err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).WithIndexOptions(
188+
milvusclient.NewCreateIndexOption(collectionName, "vector", index.NewHNSWIndex(entity.L2, 64, 128)),
189+
))
190+
if err != nil {
191+
log.Println(err.Error())
192+
// handle error
193+
}
194+
}
195+
196+
func ExampleClient_CreateCollection_quickSetupCustomize() {
197+
ctx, cancel := context.WithCancel(context.Background())
198+
defer cancel()
199+
200+
collectionName := `quick_setup_3`
201+
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
202+
Address: milvusAddr,
203+
})
204+
if err != nil {
205+
// handle err
206+
}
207+
208+
err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).
209+
WithVarcharPK(true, 64).
210+
WithShardNum(1),
211+
)
212+
if err != nil {
213+
log.Println(err.Error())
214+
// handle error
215+
}
216+
}
217+
157218
func ExampleClient_CreateCollection_consistencyLevel() {
158219
ctx, cancel := context.WithCancel(context.Background())
159220
defer cancel()
@@ -245,7 +306,7 @@ func ExampleClient_RenameCollection() {
245306
}
246307
}
247308

248-
func ExampleClient_AlterCollection_setTTL() {
309+
func ExampleClient_AlterCollectionProperties_setTTL() {
249310
ctx, cancel := context.WithCancel(context.Background())
250311
defer cancel()
251312

client/milvusclient/collection_options.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (opt *createCollectionOption) WithVarcharPK(varcharPK bool, maxLen int) *cr
8888
}
8989

9090
func (opt *createCollectionOption) WithIndexOptions(indexOpts ...CreateIndexOption) *createCollectionOption {
91-
opt.indexOptions = append(opt.indexOptions, indexOpts...)
91+
opt.indexOptions = indexOpts
9292
return opt
9393
}
9494

@@ -102,6 +102,26 @@ func (opt *createCollectionOption) WithConsistencyLevel(cl entity.ConsistencyLev
102102
return opt
103103
}
104104

105+
func (opt *createCollectionOption) WithMetricType(metricType entity.MetricType) *createCollectionOption {
106+
opt.metricType = metricType
107+
return opt
108+
}
109+
110+
func (opt *createCollectionOption) WithPKFieldName(name string) *createCollectionOption {
111+
opt.pkFieldName = name
112+
return opt
113+
}
114+
115+
func (opt *createCollectionOption) WithVectorFieldName(name string) *createCollectionOption {
116+
opt.vectorFieldName = name
117+
return opt
118+
}
119+
120+
func (opt *createCollectionOption) WithNumPartitions(numPartitions int64) *createCollectionOption {
121+
opt.numPartitions = numPartitions
122+
return opt
123+
}
124+
105125
func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest {
106126
// fast create collection
107127
if opt.isFast {
@@ -140,12 +160,12 @@ func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest {
140160

141161
func (opt *createCollectionOption) Indexes() []CreateIndexOption {
142162
// fast create
143-
if opt.isFast {
163+
if opt.isFast && opt.indexOptions == nil {
144164
return []CreateIndexOption{
145165
NewCreateIndexOption(opt.name, opt.vectorFieldName, index.NewGenericIndex("", map[string]string{})),
146166
}
147167
}
148-
return nil
168+
return opt.indexOptions
149169
}
150170

151171
func (opt *createCollectionOption) IsFast() bool {

client/milvusclient/collection_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ func (s *CollectionSuite) TestCreateCollectionOptions() {
104104
s.True(collSchema.GetEnableDynamicField())
105105

106106
collectionName = fmt.Sprintf("test_collection_%s", s.randString(6))
107-
opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).WithDynamicSchema(false)
107+
opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).
108+
WithPKFieldName("pk").WithVectorFieldName("embedding").WithMetricType(entity.L2).
109+
WithDynamicSchema(false)
108110
req = opt.Request()
109111
s.Equal(collectionName, req.GetCollectionName())
110112
s.EqualValues(1, req.GetShardsNum())

client/milvusclient/rbac_options.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package milvusclient
1818

1919
import (
2020
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
21+
"github.com/milvus-io/milvus/pkg/v2/util/crypto"
2122
)
2223

2324
type ListUserOption interface {
@@ -71,7 +72,7 @@ type createUserOption struct {
7172
func (opt *createUserOption) Request() *milvuspb.CreateCredentialRequest {
7273
return &milvuspb.CreateCredentialRequest{
7374
Username: opt.userName,
74-
Password: opt.password,
75+
Password: crypto.Base64Encode(opt.password),
7576
}
7677
}
7778

@@ -95,8 +96,8 @@ type updatePasswordOption struct {
9596
func (opt *updatePasswordOption) Request() *milvuspb.UpdateCredentialRequest {
9697
return &milvuspb.UpdateCredentialRequest{
9798
Username: opt.userName,
98-
OldPassword: opt.oldPassword,
99-
NewPassword: opt.newPassword,
99+
OldPassword: crypto.Base64Encode(opt.oldPassword),
100+
NewPassword: crypto.Base64Encode(opt.newPassword),
100101
}
101102
}
102103

client/milvusclient/rbac_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
2828
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
29+
"github.com/milvus-io/milvus/pkg/v2/util/crypto"
2930
"github.com/milvus-io/milvus/pkg/v2/util/merr"
3031
)
3132

@@ -102,7 +103,7 @@ func (s *UserSuite) TestCreateUser() {
102103
password := s.randString(12)
103104
s.mock.EXPECT().CreateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ccr *milvuspb.CreateCredentialRequest) (*commonpb.Status, error) {
104105
s.Equal(userName, ccr.GetUsername())
105-
s.Equal(password, ccr.GetPassword())
106+
s.Equal(crypto.Base64Encode(password), ccr.GetPassword())
106107
return merr.Success(), nil
107108
}).Once()
108109

@@ -121,8 +122,8 @@ func (s *UserSuite) TestUpdatePassword() {
121122
newPassword := s.randString(12)
122123
s.mock.EXPECT().UpdateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ucr *milvuspb.UpdateCredentialRequest) (*commonpb.Status, error) {
123124
s.Equal(userName, ucr.GetUsername())
124-
s.Equal(oldPassword, ucr.GetOldPassword())
125-
s.Equal(newPassword, ucr.GetNewPassword())
125+
s.Equal(crypto.Base64Encode(oldPassword), ucr.GetOldPassword())
126+
s.Equal(crypto.Base64Encode(newPassword), ucr.GetNewPassword())
126127
return merr.Success(), nil
127128
}).Once()
128129

0 commit comments

Comments
 (0)