Commit 4684e851 authored by Akshay Mahendrakar's avatar Akshay Mahendrakar
Browse files

List users and list databases feature, Creating specified (hidden) directory...

List users and list databases feature, Creating specified (hidden) directory while init connection and TestCbConnection bug fix.
parent 2a3f9265
Branches
Tags
3 merge requests!270Core ImmuDB package and TOTP plugin - Implementation,!269Core ImmuDB package and TOTP plugin - Implementation,!267List users and list databases feature, Creating specified (hidden) directory…
Showing with 126 additions and 16 deletions
......@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"time"
......@@ -94,6 +95,19 @@ func InitUsingJson(filePath string) error {
opts.Address = h.Server
opts.Port = h.Port
if h.Dir != "" {
// Checking if folder name contains extension
if filepath.Ext(h.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION " + filepath.Ext(h.Dir))
}
path := strings.Split(h.Dir, "/")
// Hiding folder by adding prefix '.'
if !strings.HasPrefix(path[len(path)-1], ".") {
path[len(path)-1] = "." + path[len(path)-1]
}
h.Dir = strings.Join(path, "/")
if _, err := os.Stat(h.Dir); os.IsNotExist(err) { // Checking directory is present and accessible
if err = os.Mkdir(h.Dir, 0740); err != nil {
return err
......@@ -134,6 +148,18 @@ func InitImmuDBConnections(hosts []ImmuHost) error {
opts.Address = v.Server
opts.Port = v.Port
if v.Dir != "" {
// Checking if folder name contains extension
if filepath.Ext(v.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION " + filepath.Ext(v.Dir))
}
path := strings.Split(v.Dir, "/")
// Hiding folder by adding prefix '.'
if !strings.HasPrefix(path[len(path)-1], ".") {
path[len(path)-1] = "." + path[len(path)-1]
}
v.Dir = strings.Join(path, "/")
if _, err := os.Stat(v.Dir); os.IsNotExist(err) { // Checking directory is present and accessible
if err = os.Mkdir(v.Dir, 0740); err != nil {
return err
......@@ -172,28 +198,27 @@ func DeleteImmuDbSession(hostName string) error {
}
// TestImmuDbConnection - Connects to Immudb with provided Database host details, returns false - if not able to connect ImmuDB
func TestDbConnection(host ImmuHost) bool {
func TestDbConnection(host ImmuHost) error {
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = host.Server
opts.Port = host.Port
if host.Dir != "" {
if _, err := os.ReadDir(host.Dir); err != nil { // Checking directory is present and accessible
return false
// Checking if folder name contains extension
if filepath.Ext(host.Dir) != "" {
return errormdl.Wrap("DIRECTORY NAME CONTAINS EXTENSION: " + filepath.Ext(host.Dir))
}
opts.Dir = host.Dir
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
if err != nil {
return false
return err
}
if ok := client.IsConnected(); !ok {
return false
return errormdl.Wrap("ImmuDB is not connected")
}
return true // Sending true if connection successfully established to ImmuDB
return nil // Sending true if connection successfully established to ImmuDB
}
// Get ImmuDB connection - immudb.ImmuClient
......@@ -407,6 +432,16 @@ func (i *ImmuDAO) DeleteDatabase(dbName string) (*schema.DeleteDatabaseResponse,
})
}
// ListDatabases - returns a list of databases the user has access.
func (i *ImmuDAO) ListDatabases() (*schema.DatabaseListResponseV2, error) {
client, err := GetImmuDbConnection(i.HostName)
if err != nil {
return nil, err
}
return client.DatabaseListV2(context.TODO())
}
/*
CreateUser - Creates new user
......@@ -428,6 +463,17 @@ func (i *ImmuDAO) CreateUser(username, password, dbName string, permission int)
return client.CreateUser(context.TODO(), []byte(username), []byte(password), uint32(permission), dbName)
}
/*
ListUsers - List all database users
*/
func (i *ImmuDAO) ListUsers() (*schema.UserList, error) {
client, err := GetImmuDbConnection(i.HostName)
if err != nil {
return nil, err
}
return client.ListUsers(context.TODO())
}
func prepareOptions(getOpts GetOptions) []immudb.GetOption {
var opts []immudb.GetOption
if getOpts.AtTx != 0 {
......
......@@ -19,7 +19,7 @@ func init() {
// }
// Establishing ImmuDB connection using dbconfig.json file
// err := InitUsingJsonFile("./dbconfig.json")
// err := InitUsingJson("./dbconfig.json")
// if err != nil {
// panic(err)
// }
......@@ -100,17 +100,17 @@ func TestImmuDbConnection(t *testing.T) {
host ImmuHost
}
tests := []struct {
name string
args args
want bool
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{host: ImmuHost{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", "", 0, true, false}}, want: true}, // PASS
{name: "Test 2", args: args{host: ImmuHost{"ImmuDbHost2", "localhost", 3324, "defaultdb", "immudb", "immudb", "", 0, false, false}}, want: false}, // FAIL - Immudb not running at provided port
{name: "Test 1", args: args{host: ImmuHost{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", "", 0, true, false}}, wantErr: true}, // PASS
{name: "Test 2", args: args{host: ImmuHost{"ImmuDbHost2", "localhost", 3324, "defaultdb", "immudb", "immudb", "", 0, false, false}}, wantErr: false}, // FAIL - Immudb not running at provided port
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TestDbConnection(tt.args.host); got != tt.want {
t.Errorf("TestImmuDbConnection() = %v, want %v", got, tt.want)
if err := TestDbConnection(tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("TestDbConnection() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
......@@ -623,3 +623,67 @@ func TestImmuDAO_CreateUser(t *testing.T) {
})
}
}
func TestImmuDAO_ListUsers(t *testing.T) {
type fields struct {
HostName string
MetaData MetaData
}
tests := []struct {
name string
fields fields
want *schema.UserList
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost10"}, wantErr: true}, // FAIL
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
MetaData: tt.fields.MetaData,
}
got, err := i.ListUsers()
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.ListUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) && err != nil {
t.Errorf("ImmuDAO.ListUsers() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_ListDatabases(t *testing.T) {
type fields struct {
HostName string
MetaData MetaData
}
tests := []struct {
name string
fields fields
want *schema.DatabaseListResponseV2
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost10"}, wantErr: true}, // FAIL
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
MetaData: tt.fields.MetaData,
}
got, err := i.ListDatabases()
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.ListDatabases() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) && err != nil {
t.Errorf("ImmuDAO.ListDatabases() = %v, want %v", got, tt.want)
}
})
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment