mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-25 19:07:47 +02:00
feat: add SYNC_SYSTEM_NAMES env var to sync system names with hostname (#1917)
This commit is contained in:
@@ -279,6 +279,10 @@ func (sys *System) createRecords(data *system.CombinedData) (*core.Record, error
|
|||||||
if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil {
|
if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// sync display name with hostname if enabled (details are fetched once per agent connection)
|
||||||
|
if syncNames, _ := utils.GetEnv("SYNC_SYSTEM_NAMES"); syncNames == "true" && data.Details.Hostname != "" {
|
||||||
|
systemRecord.Set("name", data.Details.Hostname)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Monitors != nil {
|
if data.Monitors != nil {
|
||||||
|
|||||||
34
internal/hub/systems/system_sync_name_test.go
Normal file
34
internal/hub/systems/system_sync_name_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
//go:build testing
|
||||||
|
|
||||||
|
package systems
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/henrygd/beszel/internal/entities/system"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRecordsSyncSystemNames(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
env string
|
||||||
|
hostname string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"disabled", "", "new-host", "test-system"},
|
||||||
|
{"enabled", "true", "new-host", "new-host"},
|
||||||
|
{"enabled with empty hostname", "true", "", "test-system"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Setenv("SYNC_SYSTEM_NAMES", tc.env)
|
||||||
|
sys, app := newTestSystemWithHub(t)
|
||||||
|
_, err := sys.createRecords(&system.CombinedData{Details: &system.Details{Hostname: tc.hostname}})
|
||||||
|
require.NoError(t, err)
|
||||||
|
record, err := app.FindRecordById("systems", sys.Id)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, tc.expected, record.GetString("name"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user