Add config profiles with inbound assignment for nodes.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+28
-9
@@ -17,10 +17,13 @@ func scanNode(scanner interface {
|
||||
n := &models.Node{}
|
||||
var lastSeen sql.NullTime
|
||||
var status string
|
||||
var profileID uuid.NullUUID
|
||||
var profileName sql.NullString
|
||||
err := scanner.Scan(
|
||||
&n.ID, &n.Name, &n.Host, &n.SSHPort, &n.SSHUser, &n.SSHAuthType, &n.SSHSecretEnc,
|
||||
&n.NodePort, &n.SecretKey, &status, &n.InstallMode, &n.LastError, &n.InstallLog,
|
||||
&n.RuntimeLog, &n.AgentVersion, &lastSeen, &n.CreatedAt, &n.UpdatedAt,
|
||||
&profileID, &profileName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -30,13 +33,25 @@ func scanNode(scanner interface {
|
||||
t := lastSeen.Time
|
||||
n.LastSeenAt = &t
|
||||
}
|
||||
if profileID.Valid {
|
||||
id := profileID.UUID
|
||||
n.ProfileID = &id
|
||||
}
|
||||
if profileName.Valid {
|
||||
n.ProfileName = profileName.String
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
const nodeColumns = `
|
||||
id, name, host, ssh_port, ssh_user, ssh_auth_type, ssh_secret_enc,
|
||||
node_port, secret_key, status, install_mode, last_error, install_log,
|
||||
runtime_log, agent_version, last_seen_at, created_at, updated_at`
|
||||
n.id, n.name, n.host, n.ssh_port, n.ssh_user, n.ssh_auth_type, n.ssh_secret_enc,
|
||||
n.node_port, n.secret_key, n.status, n.install_mode, n.last_error, n.install_log,
|
||||
n.runtime_log, n.agent_version, n.last_seen_at, n.created_at, n.updated_at,
|
||||
n.profile_id, COALESCE(cp.name, '')`
|
||||
|
||||
const nodeFrom = `
|
||||
FROM nodes n
|
||||
LEFT JOIN config_profiles cp ON cp.id = n.profile_id`
|
||||
|
||||
func CreateNode(db *sql.DB, n *models.Node) error {
|
||||
if n.ID == uuid.Nil {
|
||||
@@ -52,19 +67,19 @@ func CreateNode(db *sql.DB, n *models.Node) error {
|
||||
INSERT INTO nodes (
|
||||
id, name, host, ssh_port, ssh_user, ssh_auth_type, ssh_secret_enc,
|
||||
node_port, secret_key, status, install_mode, last_error, install_log,
|
||||
runtime_log, agent_version, last_seen_at, created_at, updated_at
|
||||
runtime_log, agent_version, last_seen_at, created_at, updated_at, profile_id
|
||||
) VALUES (
|
||||
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18
|
||||
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19
|
||||
)`,
|
||||
n.ID, n.Name, n.Host, n.SSHPort, n.SSHUser, n.SSHAuthType, n.SSHSecretEnc,
|
||||
n.NodePort, n.SecretKey, string(n.Status), n.InstallMode, n.LastError, n.InstallLog,
|
||||
n.RuntimeLog, n.AgentVersion, n.LastSeenAt, n.CreatedAt, n.UpdatedAt,
|
||||
n.RuntimeLog, n.AgentVersion, n.LastSeenAt, n.CreatedAt, n.UpdatedAt, n.ProfileID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetNode(db *sql.DB, id uuid.UUID) (*models.Node, error) {
|
||||
row := db.QueryRow(`SELECT `+nodeColumns+` FROM nodes WHERE id = $1`, id)
|
||||
row := db.QueryRow(`SELECT `+nodeColumns+nodeFrom+` WHERE n.id = $1`, id)
|
||||
n, err := scanNode(row)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
@@ -73,7 +88,7 @@ func GetNode(db *sql.DB, id uuid.UUID) (*models.Node, error) {
|
||||
}
|
||||
|
||||
func ListNodes(db *sql.DB) ([]models.Node, error) {
|
||||
rows, err := db.Query(`SELECT ` + nodeColumns + ` FROM nodes ORDER BY created_at DESC`)
|
||||
rows, err := db.Query(`SELECT ` + nodeColumns + nodeFrom + ` ORDER BY n.created_at DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -115,7 +130,6 @@ WHERE id = $1`, id, strings.TrimRight(chunk, "\n"))
|
||||
}
|
||||
|
||||
func SetNodeRuntimeLog(db *sql.DB, id uuid.UUID, logText string) error {
|
||||
// Cap stored log size to keep DB rows reasonable.
|
||||
const max = 200_000
|
||||
if len(logText) > max {
|
||||
logText = logText[len(logText)-max:]
|
||||
@@ -133,6 +147,11 @@ WHERE id = $1`, id, version, string(status))
|
||||
return err
|
||||
}
|
||||
|
||||
func SetNodeProfile(db *sql.DB, nodeID uuid.UUID, profileID *uuid.UUID) error {
|
||||
_, err := db.Exec(`UPDATE nodes SET profile_id = $2, updated_at = NOW() WHERE id = $1`, nodeID, profileID)
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteNode(db *sql.DB, id uuid.UUID) error {
|
||||
res, err := db.Exec(`DELETE FROM nodes WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user