Move accountant to it's own deployment using gRCP

This commit is contained in:
Marc Di Luzio 2020-06-10 23:23:09 +01:00
parent 8f25f55658
commit 99da6c5d67
14 changed files with 868 additions and 64 deletions

View file

@ -0,0 +1,76 @@
package internal
import (
"fmt"
)
const kAccountsFileName = "rove-accounts.json"
// Account represents a registered user
type Account struct {
// Name simply describes the account and must be unique
Name string `json:"name"`
// Data represents internal account data
Data map[string]string `json:"data"`
}
// Represents the accountant data to store
type accountantData struct {
}
// Accountant manages a set of accounts
type Accountant struct {
Accounts map[string]Account `json:"accounts"`
}
// NewAccountant creates a new accountant
func NewAccountant() *Accountant {
return &Accountant{
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
// Set the account name
acc.Name = name
acc.Data = make(map[string]string)
// Verify this acount isn't already registered
for _, a := range a.Accounts {
if a.Name == acc.Name {
return Account{}, fmt.Errorf("Account name already registered")
}
}
// Simply add the account to the map
a.Accounts[acc.Name] = acc
return
}
// AssignRover assigns data to an account
func (a *Accountant) AssignData(account string, key string, value string) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
this.Data[key] = value
a.Accounts[account] = this
} else {
return fmt.Errorf("no account found for id: %s", account)
}
return nil
}
// GetRover gets the rover rover for the account
func (a *Accountant) GetValue(account string, key string) (string, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; !ok {
return "", fmt.Errorf("no account found for id: %s", account)
} else {
return this.Data[key], nil
}
}

View file

@ -0,0 +1,66 @@
package internal
import (
"testing"
"github.com/google/uuid"
)
func TestNewAccountant(t *testing.T) {
// Very basic verify here for now
accountant := NewAccountant()
if accountant == nil {
t.Error("Failed to create accountant")
}
}
func TestAccountant_RegisterAccount(t *testing.T) {
accountant := NewAccountant()
// Start by making two accounts
namea := uuid.New().String()
acca, err := accountant.RegisterAccount(namea)
if err != nil {
t.Error(err)
} else if acca.Name != namea {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
}
nameb := uuid.New().String()
accb, err := accountant.RegisterAccount(nameb)
if err != nil {
t.Error(err)
} else if accb.Name != nameb {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
}
// Verify another request gets rejected
_, err = accountant.RegisterAccount(namea)
if err == nil {
t.Error("Duplicate account name did not produce error")
}
}
func TestAccountant_AssignGetData(t *testing.T) {
accountant := NewAccountant()
if len(accountant.Accounts) != 0 {
t.Error("New accountant created with non-zero account number")
}
name := uuid.New().String()
a, err := accountant.RegisterAccount(name)
if err != nil {
t.Error(err)
}
err = accountant.AssignData(a.Name, "key", "value")
if err != nil {
t.Error("Failed to set data for created account")
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
t.Error("Failed to get data for account")
} else if id != "value" {
t.Error("Fetched data is incorrect for account")
}
}