Skip to content

Commit

Permalink
Merge pull request #1572 from ydb-platform/login-password
Browse files Browse the repository at this point in the history
Added WithStaticCredentialsLogin+WithStaticCredentialsPassword
  • Loading branch information
asmyasnikov authored Dec 2, 2024
2 parents a63ea4f + 7694274 commit 0b3d8ca
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added `ydb.WithStaticCredentialsLogin` and `ydb.WithStaticCredentialsPassword` options

## v3.92.6
* Fixed string representation of `TzTimestamp`, `TzDatetime` and `TzDate` type values
* Added `database/sql/driver.Value` as type destination for almost ydb values
Expand Down
28 changes: 28 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ func WithStaticCredentials(user, password string) Option {
}
}

func WithStaticCredentialsLogin(login string) Option {
return func(ctx context.Context, d *Driver) error {
if d.userInfo == nil {
d.userInfo = &dsn.UserInfo{
User: login,
}
} else {
d.userInfo.User = login
}

return nil
}
}

func WithStaticCredentialsPassword(password string) Option {
return func(ctx context.Context, d *Driver) error {
if d.userInfo == nil {
d.userInfo = &dsn.UserInfo{
Password: password,
}
} else {
d.userInfo.Password = password
}

return nil
}
}

// WithNodeAddressMutator applies mutator for node addresses from discovery.ListEndpoints response
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
Expand Down
121 changes: 85 additions & 36 deletions tests/integration/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,49 +259,98 @@ func TestDriver(sourceTest *testing.T) {
})
t.RunSynced("UsingExplicitStaticCredentials", func(t *xtest.SyncedTest) {
t.RunSynced("HappyWay", func(t *xtest.SyncedTest) {
db, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithStaticCredentials("test", "password"),
)
require.NoError(t, err)
defer func() {
_ = db.Close(ctx)
}()
tableName := path.Join(db.Name(), t.Name(), "test")
t.RunSynced("CreateTable", func(t *xtest.SyncedTest) {
err := db.Query().Exec(ctx, fmt.Sprintf(`
t.RunSynced("WithStaticCredentials", func(t *xtest.SyncedTest) {
db, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithStaticCredentials("test", "password"),
)
require.NoError(t, err)
defer func() {
_ = db.Close(ctx)
}()
tableName := path.Join(db.Name(), t.Name(), "test")
t.RunSynced("CreateTable", func(t *xtest.SyncedTest) {
err := db.Query().Exec(ctx, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
id Uint64,
value Utf8,
PRIMARY KEY (id)
)`, "`"+tableName+"`"),
)
require.NoError(t, err)
})
t.RunSynced("Query", func(t *xtest.SyncedTest) {
row, err := db.Query().QueryRow(ctx, `SELECT 1`)
require.NoError(t, err)
var v int
err = row.Scan(&v)
require.NoError(t, err)
})
t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) {
var d options.Description
err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
d, err = s.DescribeTable(ctx, tableName)
if err != nil {
return err
}

return nil
})
require.NoError(t, err)
require.Equal(t, "test", d.Name)
require.Equal(t, 2, len(d.Columns))
require.Equal(t, "id", d.Columns[0].Name)
require.Equal(t, "value", d.Columns[1].Name)
require.Equal(t, []string{"id"}, d.PrimaryKey)
})
})
t.RunSynced("WithStaticCredentialsLogin+WithStaticCredentialsPassword",
func(t *xtest.SyncedTest) {
db, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithStaticCredentialsLogin("test"),
ydb.WithStaticCredentialsPassword("password"),
)
require.NoError(t, err)
defer func() {
_ = db.Close(ctx)
}()
tableName := path.Join(db.Name(), t.Name(), "test")
t.RunSynced("CreateTable", func(t *xtest.SyncedTest) {
err := db.Query().Exec(ctx, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
id Uint64,
value Utf8,
PRIMARY KEY (id)
)`, "`"+tableName+"`"),
)
require.NoError(t, err)
})
t.RunSynced("Query", func(t *xtest.SyncedTest) {
row, err := db.Query().QueryRow(ctx, `SELECT 1`)
require.NoError(t, err)
var v int
err = row.Scan(&v)
require.NoError(t, err)
})
t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) {
var d options.Description
err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
d, err = s.DescribeTable(ctx, tableName)
if err != nil {
return err
}
)
require.NoError(t, err)
})
t.RunSynced("Query", func(t *xtest.SyncedTest) {
row, err := db.Query().QueryRow(ctx, `SELECT 1`)
require.NoError(t, err)
var v int
err = row.Scan(&v)
require.NoError(t, err)
})
t.RunSynced("DescribeTable", func(t *xtest.SyncedTest) {
var d options.Description
err := db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
d, err = s.DescribeTable(ctx, tableName)
if err != nil {
return err
}

return nil
return nil
})
require.NoError(t, err)
require.Equal(t, "test", d.Name)
require.Equal(t, 2, len(d.Columns))
require.Equal(t, "id", d.Columns[0].Name)
require.Equal(t, "value", d.Columns[1].Name)
require.Equal(t, []string{"id"}, d.PrimaryKey)
})
})
require.NoError(t, err)
require.Equal(t, "test", d.Name)
require.Equal(t, 2, len(d.Columns))
require.Equal(t, "id", d.Columns[0].Name)
require.Equal(t, "value", d.Columns[1].Name)
require.Equal(t, []string{"id"}, d.PrimaryKey)
})
})
t.RunSynced("WrongLogin", func(t *xtest.SyncedTest) {
db, err := ydb.Open(ctx,
Expand Down

0 comments on commit 0b3d8ca

Please sign in to comment.