working commit

This commit is contained in:
2026-06-07 18:19:15 +02:00
parent d3556d397c
commit 0506b35cd1
26 changed files with 199 additions and 1299 deletions
+29 -29
View File
@@ -11,14 +11,14 @@ import (
"mbase/pkg/mbctl"
)
func (lg *Logic) ValidateAccount(ctx context.Context, username, password string) (bool, string, error) {
func (oper *Operator) ValidateAccount(ctx context.Context, username, password string) (bool, string, error) {
var err error
var accountID string
var valid bool
lg.WaitRestoring()
oper.WaitRestoring()
accountExists, accountDescr, err := lg.db.GetAccountByUsername(ctx, username)
accountExists, accountDescr, err := oper.db.GetAccountByUsername(ctx, username)
if !accountExists {
err := fmt.Errorf("Account not exists")
return valid, accountID, err
@@ -32,14 +32,14 @@ func (lg *Logic) ValidateAccount(ctx context.Context, username, password string)
return valid, accountID, err
}
func (lg *Logic) CreateAccount(ctx context.Context, accountID string, params *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) {
func (oper *Operator) CreateAccount(ctx context.Context, accountID string, params *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) {
var err error
res := &mbctl.CreateAccountResult{}
lg.WaitDumping()
lg.WaitRestoring()
oper.WaitDumping()
oper.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -57,7 +57,7 @@ func (lg *Logic) CreateAccount(ctx context.Context, accountID string, params *mb
return res, err
}
accountExists, _, err := lg.db.GetAccountByUsername(ctx, params.Username)
accountExists, _, err := oper.db.GetAccountByUsername(ctx, params.Username)
if err != nil {
return res, err
}
@@ -75,7 +75,7 @@ func (lg *Logic) CreateAccount(ctx context.Context, accountID string, params *mb
CreatedAt: now,
UpdatedAt: now,
}
err = lg.db.InsertAccount(ctx, accountDescr)
err = oper.db.InsertAccount(ctx, accountDescr)
if err != nil {
return res, err
}
@@ -83,14 +83,14 @@ func (lg *Logic) CreateAccount(ctx context.Context, accountID string, params *mb
return res, err
}
func (lg *Logic) UpdateAccount(ctx context.Context, accountID string, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) {
func (oper *Operator) UpdateAccount(ctx context.Context, accountID string, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) {
var err error
res := &mbctl.UpdateAccountResult{}
lg.WaitRestoring()
lg.WaitDumping()
oper.WaitRestoring()
oper.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -103,12 +103,12 @@ func (lg *Logic) UpdateAccount(ctx context.Context, accountID string, params *mb
var accountExists bool
switch {
case params.AccountID != "":
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
accountExists, accountDescr, err = oper.db.GetAccountByID(ctx, params.AccountID)
if err != nil {
return res, err
}
case params.Username != "":
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
accountExists, accountDescr, err = oper.db.GetAccountByUsername(ctx, params.Username)
if err != nil {
return res, err
}
@@ -133,21 +133,21 @@ func (lg *Logic) UpdateAccount(ctx context.Context, accountID string, params *mb
accountDescr.Disabled = params.Disabled
}
err = lg.db.UpdateAccountByID(ctx, accountDescr.ID, accountDescr)
err = oper.db.UpdateAccountByID(ctx, accountDescr.ID, accountDescr)
if err != nil {
return res, err
}
return res, err
}
func (lg *Logic) DeleteAccount(ctx context.Context, accountID string, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) {
func (oper *Operator) DeleteAccount(ctx context.Context, accountID string, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) {
var err error
res := &mbctl.DeleteAccountResult{}
lg.WaitDumping()
lg.WaitRestoring()
oper.WaitDumping()
oper.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -160,12 +160,12 @@ func (lg *Logic) DeleteAccount(ctx context.Context, accountID string, params *mb
var accountExists bool
switch {
case params.AccountID != "":
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
accountExists, accountDescr, err = oper.db.GetAccountByID(ctx, params.AccountID)
if err != nil {
return res, err
}
case params.Username != "":
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
accountExists, accountDescr, err = oper.db.GetAccountByUsername(ctx, params.Username)
if err != nil {
return res, err
}
@@ -175,26 +175,26 @@ func (lg *Logic) DeleteAccount(ctx context.Context, accountID string, params *mb
return res, err
}
err = lg.db.DeleteAllGrantsForAccountID(ctx, accountDescr.ID)
err = oper.db.DeleteAllGrantsForAccountID(ctx, accountDescr.ID)
if err != nil {
return res, err
}
err = lg.db.DeleteAccountByID(ctx, accountDescr.ID)
err = oper.db.DeleteAccountByID(ctx, accountDescr.ID)
if err != nil {
return res, err
}
return res, err
}
func (lg *Logic) ListAccounts(ctx context.Context, accountID string, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) {
func (oper *Operator) ListAccounts(ctx context.Context, accountID string, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) {
var err error
res := &mbctl.ListAccountsResult{
Accounts: make([]*mbctl.AccountShortDescr, 0),
}
lg.WaitRestoring()
oper.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -203,7 +203,7 @@ func (lg *Logic) ListAccounts(ctx context.Context, accountID string, params *mbc
return res, err
}
accountDescrs, err := lg.db.ReducedListAccounts(ctx)
accountDescrs, err := oper.db.ReducedListAccounts(ctx)
if err != nil {
return res, err
}
@@ -215,7 +215,7 @@ func (lg *Logic) ListAccounts(ctx context.Context, accountID string, params *mbc
UpdatedAt: accountDescr.UpdatedAt,
Grants: make([]*mbctl.GrantShortDescr, 0),
}
grantDescrs, err := lg.db.ListGrantsByAccountID(ctx, accountDescr.ID)
grantDescrs, err := oper.db.ListGrantsByAccountID(ctx, accountDescr.ID)
if err != nil {
return res, err
}
+7 -7
View File
@@ -14,23 +14,23 @@ const (
defaultSeedPassword = "mbase"
)
func (lg *Logic) CleanDatabase(ctx context.Context) error {
func (oper *Operator) CleanDatabase(ctx context.Context) error {
var err error
err = lg.db.CleanDatabase(ctx)
err = oper.db.CleanDatabase(ctx)
if err != nil {
return err
}
return err
}
func (lg *Logic) SeedAccount(ctx context.Context) (string, error) {
func (oper *Operator) SeedAccount(ctx context.Context) (string, error) {
var err error
var accountID string
accountDescrs, err := lg.db.ReducedListAccounts(ctx)
accountDescrs, err := oper.db.ReducedListAccounts(ctx)
if err != nil {
return accountID, err
}
lg.log.Debugf("Seed account")
oper.log.Debugf("Seed account")
if len(accountDescrs) == 0 {
now := time.Now().Format(time.RFC3339)
passhash := auxpwd.MakeSHA256Hash([]byte(defaultSeedPassword))
@@ -42,7 +42,7 @@ func (lg *Logic) SeedAccount(ctx context.Context) (string, error) {
CreatedAt: now,
UpdatedAt: now,
}
err = lg.db.InsertAccount(ctx, accountDescr)
err = oper.db.InsertAccount(ctx, accountDescr)
if err != nil {
return accountID, err
}
@@ -57,7 +57,7 @@ func (lg *Logic) SeedAccount(ctx context.Context) (string, error) {
Operation: grantType,
CreatedAt: now,
}
err = lg.db.InsertGrant(ctx, grantDescr)
err = oper.db.InsertGrant(ctx, grantDescr)
if err != nil {
return accountID, err
}
+22 -22
View File
@@ -11,11 +11,11 @@ import (
"go.yaml.in/yaml/v4"
)
func (lg *Logic) GetDump(ctx context.Context, accountID string, params *mbctl.GetDumpParams) (*mbctl.GetDumpResult, error) {
func (oper *Operator) GetDump(ctx context.Context, accountID string, params *mbctl.GetDumpParams) (*mbctl.GetDumpResult, error) {
var err error
res := &mbctl.GetDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
if err != nil {
return res, err
}
@@ -24,21 +24,21 @@ func (lg *Logic) GetDump(ctx context.Context, accountID string, params *mbctl.Ge
return res, err
}
lg.WaitRestoring()
lg.WaitDumping()
oper.WaitRestoring()
oper.WaitDumping()
lg.DumpingSemUp()
defer lg.DumpingSemDown()
oper.DumpingSemUp()
defer oper.DumpingSemDown()
listAccounts, err := lg.db.CompletedListAccounts(ctx)
listAccounts, err := oper.db.CompletedListAccounts(ctx)
if err != nil {
return res, err
}
listGrants, err := lg.db.ListGrants(ctx)
listGrants, err := oper.db.ListGrants(ctx)
if err != nil {
return res, err
}
lg.DumpingSemDown()
oper.DumpingSemDown()
dump := descr.Dump{
Timestamp: time.Now().Format(time.RFC3339),
@@ -55,11 +55,11 @@ func (lg *Logic) GetDump(ctx context.Context, accountID string, params *mbctl.Ge
return res, err
}
func (lg *Logic) RestoreDump(ctx context.Context, accountID string, params *mbctl.RestoreDumpParams) (*mbctl.RestoreDumpResult, error) {
func (oper *Operator) RestoreDump(ctx context.Context, accountID string, params *mbctl.RestoreDumpParams) (*mbctl.RestoreDumpResult, error) {
var err error
res := &mbctl.RestoreDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
if err != nil {
return res, err
}
@@ -68,8 +68,8 @@ func (lg *Logic) RestoreDump(ctx context.Context, accountID string, params *mbct
return res, err
}
lg.WaitDumping()
lg.WaitRestoring()
oper.WaitDumping()
oper.WaitRestoring()
var dump descr.Dump
@@ -78,28 +78,28 @@ func (lg *Logic) RestoreDump(ctx context.Context, accountID string, params *mbct
return res, err
}
lg.RestoringSemUp()
defer lg.RestoringSemDown()
oper.RestoringSemUp()
defer oper.RestoringSemDown()
if params.DeleteAllRecords {
err = lg.db.CleanDatabase(ctx)
err = oper.db.CleanDatabase(ctx)
if err != nil {
return res, err
}
}
for _, account := range dump.Accounts {
lg.log.Infof("Insert account %s", account.Username)
err = lg.db.InsertAccount(ctx, &account)
oper.log.Infof("Insert account %s", account.Username)
err = oper.db.InsertAccount(ctx, &account)
if err != nil {
lg.log.Errorf("Insert account error: %v", err)
oper.log.Errorf("Insert account error: %v", err)
}
}
for _, grant := range dump.Grants {
lg.log.Infof("Insert grant %s for account %d", grant.Operation, grant.AccountID)
err = lg.db.InsertGrant(ctx, &grant)
oper.log.Infof("Insert grant %s for account %d", grant.Operation, grant.AccountID)
err = oper.db.InsertGrant(ctx, &grant)
if err != nil {
lg.log.Errorf("Insert account error: %v", err)
oper.log.Errorf("Insert account error: %v", err)
}
}
+14 -14
View File
@@ -10,13 +10,13 @@ import (
"mbase/pkg/mbctl"
)
func (lg *Logic) SetGrant(ctx context.Context, accountID string, params *mbctl.SetGrantParams) (*mbctl.SetGrantResult, error) {
func (oper *Operator) SetGrant(ctx context.Context, accountID string, params *mbctl.SetGrantParams) (*mbctl.SetGrantResult, error) {
var err error
res := &mbctl.SetGrantResult{}
lg.WaitDumping()
oper.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -45,12 +45,12 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID string, params *mbctl.S
var accountExists bool
switch {
case params.AccountID != "":
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
accountExists, accountDescr, err = oper.db.GetAccountByID(ctx, params.AccountID)
if err != nil {
return res, err
}
case params.Username != "":
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
accountExists, accountDescr, err = oper.db.GetAccountByUsername(ctx, params.Username)
if err != nil {
return res, err
}
@@ -60,7 +60,7 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID string, params *mbctl.S
return res, err
}
grantExists, _, err = lg.db.GetGrant(ctx, accountDescr.ID, params.Operation)
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
@@ -75,20 +75,20 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID string, params *mbctl.S
CreatedAt: now,
Operation: params.Operation,
}
err = lg.db.InsertGrant(ctx, grantDescr)
err = oper.db.InsertGrant(ctx, grantDescr)
if err != nil {
return res, err
}
return res, err
}
func (lg *Logic) DeleteGrant(ctx context.Context, accountID string, params *mbctl.DeleteGrantParams) (*mbctl.DeleteGrantResult, error) {
func (oper *Operator) DeleteGrant(ctx context.Context, accountID string, params *mbctl.DeleteGrantParams) (*mbctl.DeleteGrantResult, error) {
var err error
res := &mbctl.DeleteGrantResult{}
lg.WaitDumping()
oper.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
grantExists, _, err := oper.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil {
return res, err
}
@@ -117,12 +117,12 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID string, params *mbct
var accountExists bool
switch {
case params.AccountID != "":
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
accountExists, accountDescr, err = oper.db.GetAccountByID(ctx, params.AccountID)
if err != nil {
return res, err
}
case params.Username != "":
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
accountExists, accountDescr, err = oper.db.GetAccountByUsername(ctx, params.Username)
if err != nil {
return res, err
}
@@ -132,7 +132,7 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID string, params *mbct
return res, err
}
grantExists, _, err = lg.db.GetGrant(ctx, accountDescr.ID, params.Operation)
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
@@ -140,7 +140,7 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID string, params *mbct
err := fmt.Errorf("Requested grant for the user not found")
return res, err
}
err = lg.db.DeleteGrantByAccountID(ctx, accountDescr.ID, params.Operation)
err = oper.db.DeleteGrantByAccountID(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"mbase/pkg/mbctl"
)
func (lg *Logic) GetHello(ctx context.Context, params *mbctl.GetHelloParams) (*mbctl.GetHelloResult, error) {
func (oper *Operator) GetHello(ctx context.Context, params *mbctl.GetHelloParams) (*mbctl.GetHelloResult, error) {
var err error
res := &mbctl.GetHelloResult{
Message: "hello",
+18 -18
View File
@@ -8,54 +8,54 @@ import (
"mbase/pkg/logger"
)
type LogicConfig struct {
type OperatorConfig struct {
Database *maindb.Database
}
type Logic struct {
type Operator struct {
log *logger.Logger
db *maindb.Database
dumpingSem atomic.Bool
restoringSem atomic.Bool
}
func NewLogic(conf *LogicConfig) (*Logic, error) {
func NewOperator(conf *OperatorConfig) (*Operator, error) {
var err error
lg := &Logic{
oper := &Operator{
db: conf.Database,
}
lg.log = logger.NewLogger("logic")
return lg, err
oper.log = logger.NewLogger("Operator")
return oper, err
}
func (lg *Logic) DumpingSemUp() {
lg.dumpingSem.Store(true)
func (oper *Operator) DumpingSemUp() {
oper.dumpingSem.Store(true)
}
func (lg *Logic) DumpingSemDown() {
lg.dumpingSem.Store(false)
func (oper *Operator) DumpingSemDown() {
oper.dumpingSem.Store(false)
}
func (lg *Logic) WaitDumping() {
func (oper *Operator) WaitDumping() {
for {
if !lg.dumpingSem.Load() {
if !oper.dumpingSem.Load() {
return
}
time.Sleep(1 * time.Millisecond)
}
}
func (lg *Logic) RestoringSemUp() {
lg.restoringSem.Store(true)
func (oper *Operator) RestoringSemUp() {
oper.restoringSem.Store(true)
}
func (lg *Logic) RestoringSemDown() {
lg.restoringSem.Store(false)
func (oper *Operator) RestoringSemDown() {
oper.restoringSem.Store(false)
}
func (lg *Logic) WaitRestoring() {
func (oper *Operator) WaitRestoring() {
for {
if !lg.restoringSem.Load() {
if !oper.restoringSem.Load() {
return
}
time.Sleep(1 * time.Millisecond)