Add /spawn command to let an account spawn it's primary instance

This commit is contained in:
Marc Di Luzio 2020-06-02 17:44:39 +01:00
parent 0a1f7a37c4
commit 50c970fea2
6 changed files with 151 additions and 11 deletions

View file

@ -17,13 +17,16 @@ type Account struct {
// Name simply describes the account and must be unique
Name string `json:"name"`
// id represents a unique ID per account and is set one registered
// Id represents a unique ID per account and is set one registered
Id uuid.UUID `json:"id"`
// Primary represents the primary instance that this account owns
Primary uuid.UUID `json:"primary"`
}
// Represents the accountant data to store
type accountantData struct {
Accounts []Account `json:"accounts"`
Accounts map[uuid.UUID]Account `json:"accounts"`
}
// Accountant manages a set of accounts
@ -36,6 +39,9 @@ type Accountant struct {
func NewAccountant(dataPath string) *Accountant {
return &Accountant{
dataPath: dataPath,
data: accountantData{
Accounts: make(map[uuid.UUID]Account),
},
}
}
@ -54,8 +60,8 @@ func (a *Accountant) RegisterAccount(acc Account) (Account, error) {
}
}
// Simply add the account to the list
a.data.Accounts = append(a.data.Accounts, acc)
// Simply add the account to the map
a.data.Accounts[acc.Id] = acc
return acc, nil
}
@ -84,7 +90,7 @@ func (a *Accountant) Load() error {
// Save will save the accountant data out
func (a *Accountant) Save() error {
if b, err := json.Marshal(a.data); err != nil {
if b, err := json.MarshalIndent(a.data, "", "\t"); err != nil {
return err
} else {
if err := ioutil.WriteFile(a.path(), b, os.ModePerm); err != nil {
@ -93,3 +99,17 @@ func (a *Accountant) Save() error {
}
return nil
}
// AssignPrimary assigns primary ownership of an instance to an account
func (a *Accountant) AssignPrimary(account uuid.UUID, instance uuid.UUID) error {
// Find the account matching the ID
if this, ok := a.data.Accounts[account]; ok {
this.Primary = instance
a.data.Accounts[account] = this
} else {
return fmt.Errorf("no account found for id: %s", account)
}
return nil
}

View file

@ -3,6 +3,8 @@ package accounts
import (
"os"
"testing"
"github.com/google/uuid"
)
func TestNewAccountant(t *testing.T) {
@ -64,7 +66,7 @@ func TestAccountant_LoadSave(t *testing.T) {
if len(accountant.data.Accounts) != 1 {
t.Error("No new account made")
} else if accountant.data.Accounts[0].Name != name {
} else if accountant.data.Accounts[a.Id].Name != name {
t.Error("New account created with wrong name")
}
@ -87,7 +89,30 @@ func TestAccountant_LoadSave(t *testing.T) {
// Verify we have the same account again
if len(accountant.data.Accounts) != 1 {
t.Error("No account after load")
} else if accountant.data.Accounts[0].Name != name {
} else if accountant.data.Accounts[a.Id].Name != name {
t.Error("New account created with wrong name")
}
}
func TestAccountant_AssignPrimary(t *testing.T) {
accountant := NewAccountant(os.TempDir())
if len(accountant.data.Accounts) != 0 {
t.Error("New accountant created with non-zero account number")
}
name := "one"
a := Account{Name: name}
a, err := accountant.RegisterAccount(a)
if err != nil {
t.Error(err)
}
inst := uuid.New()
err = accountant.AssignPrimary(a.Id, inst)
if err != nil {
t.Error("Failed to set primary for created account")
} else if accountant.data.Accounts[a.Id].Primary != inst {
t.Error("Primary for assigned account is incorrect")
}
}