feat(api): organization-owned regions, checkers and status page servers
Organizations create regions and agents for themselves under Regions & checkers; an org checker only receives its org's monitors. Platform admins manage shared regions and agents in the admin panel or with the CLI, which gains 'api admin grant|revoke'. The first user of a self-hosted installation becomes a platform admin. Region lists show the org's own regions first. Closes #52 Refs #29 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
607798d742
commit
22f0740056
19 files changed
+1932
-301
No files matched your search
+27
-47
@@ -27,7 +27,7 @@ type monitorRequest struct {
|
||||
TimeoutMS int `json:"timeout_ms"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
Regions []string `json:"regions" binding:"required,min=1,max=50"`
|
||||
Regions []uuid.UUID `json:"regions" binding:"required,min=1,max=50"`
|
||||
// Confirmations is how many failures in a row make a region count as
|
||||
// down; 0 keeps the default of 2.
|
||||
Confirmations int `json:"confirmations" binding:"min=0,max=10"`
|
||||
@@ -52,11 +52,11 @@ type monitorView struct {
|
||||
Config json.RawMessage `json:"config"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Confirmations int32 `json:"confirmations"`
|
||||
Regions []string `json:"regions"`
|
||||
Regions []uuid.UUID `json:"regions"`
|
||||
Latest []latestView `json:"latest"`
|
||||
}
|
||||
|
||||
func viewMonitor(m store.Monitor, regions []string) monitorView {
|
||||
func viewMonitor(m store.Monitor, regions []uuid.UUID) monitorView {
|
||||
return monitorView{
|
||||
ID: m.ID, Name: m.Name, Type: m.Type, Target: m.Target, IntervalSeconds: m.IntervalSeconds,
|
||||
TimeoutMS: m.TimeoutMs, Config: m.Config, Enabled: m.Enabled, Confirmations: m.Confirmations, Regions: regions, Latest: []latestView{},
|
||||
@@ -105,13 +105,13 @@ func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
|
||||
return false
|
||||
}
|
||||
req.Config = cfg
|
||||
slices.Sort(req.Regions)
|
||||
slices.SortFunc(req.Regions, func(a, b uuid.UUID) int { return strings.Compare(a.String(), b.String()) })
|
||||
req.Regions = slices.Compact(req.Regions)
|
||||
ctx := c.Request.Context()
|
||||
var known []store.Region
|
||||
err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
known, err = q.ListRegions(ctx)
|
||||
known, err = q.ListVisibleRegions(ctx)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -119,7 +119,9 @@ func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
|
||||
return false
|
||||
}
|
||||
for _, r := range req.Regions {
|
||||
if !slices.ContainsFunc(known, func(k store.Region) bool { return k.Key == r }) {
|
||||
// Row-level security limits known to the org's own and the
|
||||
// platform regions.
|
||||
if !slices.ContainsFunc(known, func(k store.Region) bool { return k.ID == r }) {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_region")
|
||||
return false
|
||||
}
|
||||
@@ -144,9 +146,9 @@ func (s *Server) listMonitors(c *gin.Context) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
byMonitor := map[uuid.UUID][]string{}
|
||||
byMonitor := map[uuid.UUID][]uuid.UUID{}
|
||||
for _, r := range regions {
|
||||
byMonitor[r.MonitorID] = append(byMonitor[r.MonitorID], r.Region)
|
||||
byMonitor[r.MonitorID] = append(byMonitor[r.MonitorID], r.RegionID)
|
||||
}
|
||||
latestBy := map[uuid.UUID][]latestView{}
|
||||
for _, l := range latest {
|
||||
@@ -201,12 +203,12 @@ func (s *Server) createMonitor(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, viewMonitor(m, req.Regions))
|
||||
}
|
||||
|
||||
func setRegions(ctx context.Context, q *store.Queries, m store.Monitor, regions []string) error {
|
||||
func setRegions(ctx context.Context, q *store.Queries, m store.Monitor, regions []uuid.UUID) error {
|
||||
if err := q.DeleteMonitorRegions(ctx, m.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range regions {
|
||||
if err := q.AddMonitorRegion(ctx, store.AddMonitorRegionParams{MonitorID: m.ID, OrgID: m.OrgID, Region: r}); err != nil {
|
||||
if err := q.AddMonitorRegion(ctx, store.AddMonitorRegionParams{MonitorID: m.ID, OrgID: m.OrgID, RegionID: r}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -215,13 +217,21 @@ func setRegions(ctx context.Context, q *store.Queries, m store.Monitor, regions
|
||||
|
||||
// syncSchedule applies a committed monitor change to the scheduler. Should it
|
||||
// fail, the periodic reconciliation repairs the schedule.
|
||||
func (s *Server) syncSchedule(ctx context.Context, m store.Monitor, regions, previous []string) error {
|
||||
func (s *Server) syncSchedule(ctx context.Context, m store.Monitor, regions, previous []uuid.UUID) error {
|
||||
if !m.Enabled {
|
||||
return s.sched.Remove(ctx, m.ID, append(regions, previous...))
|
||||
return s.sched.Remove(ctx, m.ID, idStrings(append(regions, previous...)))
|
||||
}
|
||||
return s.sched.Upsert(ctx, schedule.Monitor{
|
||||
ID: m.ID, Interval: time.Duration(m.IntervalSeconds) * time.Second, Regions: regions,
|
||||
}, previous)
|
||||
ID: m.ID, Interval: time.Duration(m.IntervalSeconds) * time.Second, Regions: idStrings(regions),
|
||||
}, idStrings(previous))
|
||||
}
|
||||
|
||||
func idStrings(ids []uuid.UUID) []string {
|
||||
out := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
out[i] = id.String()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) updateMonitor(c *gin.Context) {
|
||||
@@ -238,7 +248,7 @@ func (s *Server) updateMonitor(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
var (
|
||||
m store.Monitor
|
||||
previous []string
|
||||
previous []uuid.UUID
|
||||
)
|
||||
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
@@ -279,7 +289,7 @@ func (s *Server) deleteMonitor(c *gin.Context) {
|
||||
p := current(c)
|
||||
ctx := c.Request.Context()
|
||||
var (
|
||||
regions []string
|
||||
regions []uuid.UUID
|
||||
n int64
|
||||
)
|
||||
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
@@ -298,7 +308,7 @@ func (s *Server) deleteMonitor(c *gin.Context) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
if err := s.sched.Remove(ctx, id, regions); err != nil {
|
||||
if err := s.sched.Remove(ctx, id, idStrings(regions)); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -306,36 +316,6 @@ func (s *Server) deleteMonitor(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
type regionView struct {
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
Instances int64 `json:"instances"`
|
||||
}
|
||||
|
||||
func (s *Server) listRegions(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
var rows []store.Region
|
||||
err := s.db.Tx(ctx, tenant.Global(), func(q *store.Queries) error {
|
||||
var err error
|
||||
rows, err = q.ListRegions(ctx)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
out := make([]regionView, len(rows))
|
||||
for i, r := range rows {
|
||||
n, err := s.sched.Instances(ctx, r.Key)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
out[i] = regionView{Key: r.Key, Name: r.Name, Instances: n}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"regions": out})
|
||||
}
|
||||
|
||||
// RunScheduler dispatches due checks and periodically reconciles the
|
||||
// schedule with the database until ctx ends. Every API replica runs it;
|
||||
// dispatch is atomic in Redis and reconciliation is idempotent.
|
||||
|
||||
Reference in new issue
Block a user