Move DB call of pollmap to Machine inside a function

This commit is contained in:
Kristoffer Dalby 2021-10-02 21:58:28 +01:00
parent 42913e2c37
commit 0475eb6ef7
No known key found for this signature in database
GPG key ID: 09F62DC067465735
4 changed files with 24 additions and 8 deletions

18
poll.go
View file

@ -51,13 +51,19 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
return
}
var m Machine
if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
log.Warn().
m, err := h.GetMachineByMachineKey(mKey.HexString())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn().
Str("handler", "PollNetMap").
Msgf("Ignoring request, cannot find machine with key %s", mKey.HexString())
c.String(http.StatusUnauthorized, "")
return
}
log.Error().
Str("handler", "PollNetMap").
Msgf("Ignoring request, cannot find machine with key %s", mKey.HexString())
c.String(http.StatusUnauthorized, "")
return
Msgf("Failed to fetch machine from the database with Machine key: %s", mKey.HexString())
c.String(http.StatusInternalServerError, "")
}
log.Trace().
Str("handler", "PollNetMap").