mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-11 18:17:40 +00:00
state: introduce state
this commit moves all of the read and write logic, and all different parts of headscale that manages some sort of persistent and in memory state into a separate package. The goal of this is to clearly define the boundry between parts of the app which accesses and modifies data, and where it happens. Previously, different state (routes, policy, db and so on) was used directly, and sometime passed to functions as pointers. Now all access has to go through state. In the initial implementation, most of the same functions exists and have just been moved. In the future centralising this will allow us to optimise bottle necks with the database (in memory state) and make the different parts talking to eachother do so in the same way across headscale components. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:

committed by
Kristoffer Dalby

parent
a975b6a8b1
commit
1553f0ab53
@@ -587,6 +587,9 @@ func ensureUniqueGivenName(
|
||||
return givenName, nil
|
||||
}
|
||||
|
||||
// ExpireExpiredNodes checks for nodes that have expired since the last check
|
||||
// and returns a time to be used for the next check, a StateUpdate
|
||||
// containing the expired nodes, and a boolean indicating if any nodes were found.
|
||||
func ExpireExpiredNodes(tx *gorm.DB,
|
||||
lastCheck time.Time,
|
||||
) (time.Time, types.StateUpdate, bool) {
|
||||
|
@@ -199,19 +199,18 @@ func ListNodesByUser(tx *gorm.DB, uid types.UserID) (types.Nodes, error) {
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (hsdb *HSDatabase) AssignNodeToUser(node *types.Node, uid types.UserID) error {
|
||||
return hsdb.Write(func(tx *gorm.DB) error {
|
||||
return AssignNodeToUser(tx, node, uid)
|
||||
})
|
||||
}
|
||||
|
||||
// AssignNodeToUser assigns a Node to a user.
|
||||
func AssignNodeToUser(tx *gorm.DB, node *types.Node, uid types.UserID) error {
|
||||
func AssignNodeToUser(tx *gorm.DB, nodeID types.NodeID, uid types.UserID) error {
|
||||
node, err := GetNodeByID(tx, nodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user, err := GetUserByID(tx, uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
node.User = *user
|
||||
node.UserID = user.ID
|
||||
if result := tx.Save(&node); result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
@@ -108,7 +108,7 @@ func (s *Suite) TestSetMachineUser(c *check.C) {
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
node := types.Node{
|
||||
ID: 0,
|
||||
ID: 12,
|
||||
Hostname: "testnode",
|
||||
UserID: oldUser.ID,
|
||||
RegisterMethod: util.RegisterMethodAuthKey,
|
||||
@@ -118,16 +118,28 @@ func (s *Suite) TestSetMachineUser(c *check.C) {
|
||||
c.Assert(trx.Error, check.IsNil)
|
||||
c.Assert(node.UserID, check.Equals, oldUser.ID)
|
||||
|
||||
err = db.AssignNodeToUser(&node, types.UserID(newUser.ID))
|
||||
err = db.Write(func(tx *gorm.DB) error {
|
||||
return AssignNodeToUser(tx, 12, types.UserID(newUser.ID))
|
||||
})
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(node.UserID, check.Equals, newUser.ID)
|
||||
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
||||
// Reload node from database to see updated values
|
||||
updatedNode, err := db.GetNodeByID(12)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(updatedNode.UserID, check.Equals, newUser.ID)
|
||||
c.Assert(updatedNode.User.Name, check.Equals, newUser.Name)
|
||||
|
||||
err = db.AssignNodeToUser(&node, 9584849)
|
||||
err = db.Write(func(tx *gorm.DB) error {
|
||||
return AssignNodeToUser(tx, 12, 9584849)
|
||||
})
|
||||
c.Assert(err, check.Equals, ErrUserNotFound)
|
||||
|
||||
err = db.AssignNodeToUser(&node, types.UserID(newUser.ID))
|
||||
err = db.Write(func(tx *gorm.DB) error {
|
||||
return AssignNodeToUser(tx, 12, types.UserID(newUser.ID))
|
||||
})
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(node.UserID, check.Equals, newUser.ID)
|
||||
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
||||
// Reload node from database again to see updated values
|
||||
finalNode, err := db.GetNodeByID(12)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(finalNode.UserID, check.Equals, newUser.ID)
|
||||
c.Assert(finalNode.User.Name, check.Equals, newUser.Name)
|
||||
}
|
||||
|
Reference in New Issue
Block a user