Skip to content

Commit

Permalink
Check all errors for db.Save
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed May 30, 2022
1 parent 405de9e commit 52cc3bc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 4 additions & 1 deletion api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func (h *Headscale) CreateAPIKey(
Hash: hash,
Expiration: expiration,
}
h.db.Save(&key)

if err := h.db.Save(&key).Error; err != nil {
return "", nil, fmt.Errorf("failed to save API key to database: %w", err)
}

return keyStr, &key, nil
}
Expand Down
32 changes: 24 additions & 8 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,31 +367,42 @@ func (h *Headscale) SetTags(machine *Machine, tags []string) error {
return err
}
h.setLastStateChangeToNow(machine.Namespace.Name)
h.db.Save(machine)

if err := h.db.Save(machine).Error; err != nil {
return fmt.Errorf("failed to update tags for machine in the database: %w", err)
}

return nil
}

// ExpireMachine takes a Machine struct and sets the expire field to now.
func (h *Headscale) ExpireMachine(machine *Machine) {
func (h *Headscale) ExpireMachine(machine *Machine) error {
now := time.Now()
machine.Expiry = &now

h.setLastStateChangeToNow(machine.Namespace.Name)

h.db.Save(machine)
if err := h.db.Save(machine).Error; err != nil {
return fmt.Errorf("failed to expire machine in the database: %w", err)
}

return nil
}

// RefreshMachine takes a Machine struct and sets the expire field to now.
func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) {
// RefreshMachine takes a Machine struct and sets the expire field.
func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) error {
now := time.Now()

machine.LastSuccessfulUpdate = &now
machine.Expiry = &expiry

h.setLastStateChangeToNow(machine.Namespace.Name)

h.db.Save(machine)
if err := h.db.Save(machine).Error; err != nil {
return fmt.Errorf("failed to refresh machine (update expiration) in the database: %w", err)
}

return nil
}

// DeleteMachine softs deletes a Machine from the database.
Expand Down Expand Up @@ -761,7 +772,9 @@ func (h *Headscale) RegisterMachine(machine Machine,

machine.IPAddresses = ips

h.db.Save(&machine)
if err := h.db.Save(machine).Error; err != nil {
return nil, fmt.Errorf("failed register(save) machine in the database: %w", err)
}

log.Trace().
Caller().
Expand Down Expand Up @@ -821,7 +834,10 @@ func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error {
}

machine.EnabledRoutes = newRoutes
h.db.Save(&machine)

if err := h.db.Save(machine).Error; err != nil {
return fmt.Errorf("failed enable routes for machine in the database: %w", err)
}

return nil
}
Expand Down
14 changes: 11 additions & 3 deletions preauth_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -60,7 +61,10 @@ func (h *Headscale) CreatePreAuthKey(
CreatedAt: &now,
Expiration: expiration,
}
h.db.Save(&key)

if err := h.db.Save(&key).Error; err != nil {
return nil, fmt.Errorf("failed to create key in the database: %w", err)
}

return &key, nil
}
Expand Down Expand Up @@ -114,9 +118,13 @@ func (h *Headscale) ExpirePreAuthKey(k *PreAuthKey) error {
}

// UsePreAuthKey marks a PreAuthKey as used.
func (h *Headscale) UsePreAuthKey(k *PreAuthKey) {
func (h *Headscale) UsePreAuthKey(k *PreAuthKey) error {
k.Used = true
h.db.Save(k)
if err := h.db.Save(k).Error; err != nil {
return fmt.Errorf("failed to update key used status in the database: %w", err)
}

return nil
}

// checkKeyValidity does the heavy lifting for validation of the PreAuthKey coming from a node
Expand Down
7 changes: 6 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package headscale

import (
"fmt"

"inet.af/netaddr"
)

Expand Down Expand Up @@ -108,7 +110,10 @@ func (h *Headscale) EnableNodeRoute(
}

machine.EnabledRoutes = enabledRoutes
h.db.Save(&machine)

if err := h.db.Save(&machine).Error; err != nil {
return fmt.Errorf("failed to update node routes in the database: %w", err)
}

return nil
}

0 comments on commit 52cc3bc

Please sign in to comment.