feat(store): let regions and agents belong to the platform or one organization

Regions are identified by id so organizations can reuse keys. Triggers
keep agents in regions of the same owner and monitors in platform or own
regions. Platform admins are a user flag.

Refs #52

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 01:36:02 +01:00
1 parent 7bb4afa04a
commit 607798d742
10 files changed
+442 -99

No files matched your search

+191 -43
View File
@@ -7,21 +7,22 @@ package store
import (
"context"
"time"
"github.com/google/uuid"
)
const createAgent = `-- name: CreateAgent :one
INSERT INTO agents (org_id, kind, name, region, token_hash, prefix)
INSERT INTO agents (org_id, kind, name, region_id, token_hash, prefix)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, org_id, kind, name, region, token_hash, prefix, last_seen_at, release, created_at
RETURNING id, org_id, kind, name, token_hash, prefix, last_seen_at, release, created_at, region_id
`
type CreateAgentParams struct {
OrgID *uuid.UUID
Kind string
Name string
Region *string
RegionID *uuid.UUID
TokenHash []byte
Prefix string
}
@@ -31,7 +32,7 @@ func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent
arg.OrgID,
arg.Kind,
arg.Name,
arg.Region,
arg.RegionID,
arg.TokenHash,
arg.Prefix,
)
@@ -41,74 +42,202 @@ func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent
&i.OrgID,
&i.Kind,
&i.Name,
&i.Region,
&i.TokenHash,
&i.Prefix,
&i.LastSeenAt,
&i.Release,
&i.CreatedAt,
&i.RegionID,
)
return i, err
}
const createRegion = `-- name: CreateRegion :one
INSERT INTO regions (org_id, key, name) VALUES ($1, $2, $3)
RETURNING key, name, created_at, id, org_id
`
type CreateRegionParams struct {
OrgID *uuid.UUID
Key string
Name string
}
func (q *Queries) CreateRegion(ctx context.Context, arg CreateRegionParams) (Region, error) {
row := q.db.QueryRow(ctx, createRegion, arg.OrgID, arg.Key, arg.Name)
var i Region
err := row.Scan(
&i.Key,
&i.Name,
&i.CreatedAt,
&i.ID,
&i.OrgID,
)
return i, err
}
const deleteAgent = `-- name: DeleteAgent :execrows
DELETE FROM agents WHERE id = $1
DELETE FROM agents WHERE id = $1 AND org_id IS NOT DISTINCT FROM $2::uuid
`
func (q *Queries) DeleteAgent(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, deleteAgent, id)
type DeleteAgentParams struct {
ID uuid.UUID
OrgID *uuid.UUID
}
func (q *Queries) DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteAgent, arg.ID, arg.OrgID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getAgentByHash = `-- name: GetAgentByHash :one
SELECT id, org_id, kind, name, region, token_hash, prefix, last_seen_at, release, created_at FROM agents WHERE token_hash = $1
const deleteRegion = `-- name: DeleteRegion :execrows
DELETE FROM regions WHERE id = $1
`
func (q *Queries) GetAgentByHash(ctx context.Context, tokenHash []byte) (Agent, error) {
func (q *Queries) DeleteRegion(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, deleteRegion, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const ensurePlatformRegion = `-- name: EnsurePlatformRegion :one
INSERT INTO regions (org_id, key, name)
VALUES (NULL, $1, coalesce(nullif($2::text, ''), $1))
ON CONFLICT ON CONSTRAINT regions_owner_key_key
DO UPDATE SET name = CASE WHEN $2::text = '' THEN regions.name ELSE EXCLUDED.name END
RETURNING key, name, created_at, id, org_id
`
type EnsurePlatformRegionParams struct {
Key string
Name string
}
// An empty name names a new region after its key and keeps an existing name.
func (q *Queries) EnsurePlatformRegion(ctx context.Context, arg EnsurePlatformRegionParams) (Region, error) {
row := q.db.QueryRow(ctx, ensurePlatformRegion, arg.Key, arg.Name)
var i Region
err := row.Scan(
&i.Key,
&i.Name,
&i.CreatedAt,
&i.ID,
&i.OrgID,
)
return i, err
}
const getAgentByHash = `-- name: GetAgentByHash :one
SELECT a.id, a.org_id, a.kind, a.name, a.token_hash, a.prefix, a.last_seen_at, a.release, a.created_at, a.region_id, r.key AS region_key, r.name AS region_name
FROM agents a
LEFT JOIN regions r ON r.id = a.region_id
WHERE a.token_hash = $1
`
type GetAgentByHashRow struct {
ID uuid.UUID
OrgID *uuid.UUID
Kind string
Name string
TokenHash []byte
Prefix string
LastSeenAt *time.Time
Release *string
CreatedAt time.Time
RegionID *uuid.UUID
RegionKey *string
RegionName *string
}
// System scope.
func (q *Queries) GetAgentByHash(ctx context.Context, tokenHash []byte) (GetAgentByHashRow, error) {
row := q.db.QueryRow(ctx, getAgentByHash, tokenHash)
var i Agent
var i GetAgentByHashRow
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Name,
&i.Region,
&i.TokenHash,
&i.Prefix,
&i.LastSeenAt,
&i.Release,
&i.CreatedAt,
&i.RegionID,
&i.RegionKey,
&i.RegionName,
)
return i, err
}
const getRegion = `-- name: GetRegion :one
SELECT key, name, created_at, id, org_id FROM regions WHERE id = $1
`
func (q *Queries) GetRegion(ctx context.Context, id uuid.UUID) (Region, error) {
row := q.db.QueryRow(ctx, getRegion, id)
var i Region
err := row.Scan(
&i.Key,
&i.Name,
&i.CreatedAt,
&i.ID,
&i.OrgID,
)
return i, err
}
const listAgents = `-- name: ListAgents :many
SELECT id, org_id, kind, name, region, token_hash, prefix, last_seen_at, release, created_at FROM agents ORDER BY kind, region, name
SELECT a.id, a.org_id, a.kind, a.name, a.token_hash, a.prefix, a.last_seen_at, a.release, a.created_at, a.region_id, r.key AS region_key, r.name AS region_name
FROM agents a
LEFT JOIN regions r ON r.id = a.region_id
WHERE a.org_id IS NOT DISTINCT FROM $1::uuid
ORDER BY a.kind, r.name, a.name
`
func (q *Queries) ListAgents(ctx context.Context) ([]Agent, error) {
rows, err := q.db.Query(ctx, listAgents)
type ListAgentsRow struct {
ID uuid.UUID
OrgID *uuid.UUID
Kind string
Name string
TokenHash []byte
Prefix string
LastSeenAt *time.Time
Release *string
CreatedAt time.Time
RegionID *uuid.UUID
RegionKey *string
RegionName *string
}
// Agents owned by org, or platform agents when org is NULL.
func (q *Queries) ListAgents(ctx context.Context, orgID *uuid.UUID) ([]ListAgentsRow, error) {
rows, err := q.db.Query(ctx, listAgents, orgID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Agent{}
items := []ListAgentsRow{}
for rows.Next() {
var i Agent
var i ListAgentsRow
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Name,
&i.Region,
&i.TokenHash,
&i.Prefix,
&i.LastSeenAt,
&i.Release,
&i.CreatedAt,
&i.RegionID,
&i.RegionKey,
&i.RegionName,
); err != nil {
return nil, err
}
@@ -120,12 +249,12 @@ func (q *Queries) ListAgents(ctx context.Context) ([]Agent, error) {
return items, nil
}
const listRegions = `-- name: ListRegions :many
SELECT key, name, created_at FROM regions ORDER BY key
const listPlatformRegions = `-- name: ListPlatformRegions :many
SELECT key, name, created_at, id, org_id FROM regions WHERE org_id IS NULL ORDER BY name, key
`
func (q *Queries) ListRegions(ctx context.Context) ([]Region, error) {
rows, err := q.db.Query(ctx, listRegions)
func (q *Queries) ListPlatformRegions(ctx context.Context) ([]Region, error) {
rows, err := q.db.Query(ctx, listPlatformRegions)
if err != nil {
return nil, err
}
@@ -133,7 +262,45 @@ func (q *Queries) ListRegions(ctx context.Context) ([]Region, error) {
items := []Region{}
for rows.Next() {
var i Region
if err := rows.Scan(&i.Key, &i.Name, &i.CreatedAt); err != nil {
if err := rows.Scan(
&i.Key,
&i.Name,
&i.CreatedAt,
&i.ID,
&i.OrgID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listVisibleRegions = `-- name: ListVisibleRegions :many
SELECT key, name, created_at, id, org_id FROM regions ORDER BY org_id IS NULL, name, key
`
// Row-level security limits this to platform regions and the scope's org;
// the org's own regions come first.
func (q *Queries) ListVisibleRegions(ctx context.Context) ([]Region, error) {
rows, err := q.db.Query(ctx, listVisibleRegions)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Region{}
for rows.Next() {
var i Region
if err := rows.Scan(
&i.Key,
&i.Name,
&i.CreatedAt,
&i.ID,
&i.OrgID,
); err != nil {
return nil, err
}
items = append(items, i)
@@ -158,22 +325,3 @@ func (q *Queries) TouchAgent(ctx context.Context, arg TouchAgentParams) error {
_, err := q.db.Exec(ctx, touchAgent, arg.ID, arg.Release)
return err
}
const upsertRegion = `-- name: UpsertRegion :one
INSERT INTO regions (key, name) VALUES ($1, coalesce(nullif($2::text, ''), $1))
ON CONFLICT (key) DO UPDATE SET name = CASE WHEN $2::text = '' THEN regions.name ELSE EXCLUDED.name END
RETURNING key, name, created_at
`
type UpsertRegionParams struct {
Key string
Name string
}
// An empty name names a new region after its key and keeps an existing name.
func (q *Queries) UpsertRegion(ctx context.Context, arg UpsertRegionParams) (Region, error) {
row := q.db.QueryRow(ctx, upsertRegion, arg.Key, arg.Name)
var i Region
err := row.Scan(&i.Key, &i.Name, &i.CreatedAt)
return i, err
}