fix(api): platform check type switch binds the platform's checkers only
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m25s
ci / test (pull_request) Successful in 12m34s
ci / build (pull_request) Successful in 18m7s
ci / image (pull_request) Successful in 5m22s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m25s
ci / test (pull_request) Successful in 12m34s
ci / build (pull_request) Successful in 18m7s
ci / image (pull_request) Successful in 5m22s
Switching a check type off in the admin area removed it for every organization, own checkers included, and hid it from the organization's own choice. The platform switch now only keeps such monitors off the platform's regions: organizations with regions of their own still create them there (422 check_type_own_checkers_only on a platform region), and the editor marks them "own checkers only". Passive types, which no checker runs, stay switched off entirely. The organization's check types are chosen independently of the platform's: its settings list every type, and the platform switch no longer rewrites that choice. 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
12e6de77aa
commit
bd887523de
12 files changed
+2296
-2096
No files matched your search
+115
-27
@@ -8,6 +8,7 @@ import (
|
||||
"slices"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
|
||||
@@ -15,10 +16,12 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
// Platform admins can switch off check types the installation cannot
|
||||
// offer, such as Docker on shared cloud checkers. Switched-off types are
|
||||
// not offered for new monitors; existing ones keep running and stay
|
||||
// editable.
|
||||
// Platform admins can switch off check types the platform's checkers
|
||||
// cannot offer, such as Docker on shared cloud checkers. That only binds
|
||||
// the platform's checkers: organizations keep such types for their own.
|
||||
// Organizations separately choose the types they offer their members.
|
||||
// Switched-off types are not offered for new monitors; existing ones keep
|
||||
// running and stay editable.
|
||||
|
||||
const settingDisabledChecks = "disabled_check_types"
|
||||
|
||||
@@ -48,34 +51,87 @@ func (s *Server) disabledChecks(ctx context.Context) map[string]bool {
|
||||
return set
|
||||
}
|
||||
|
||||
// offered reports whether org may create monitors of type t: neither the
|
||||
// platform nor the organization switched it off.
|
||||
func (s *Server) offered(ctx context.Context, org store.Org, t string) bool {
|
||||
return slices.Contains(s.enabledCheckTypes(ctx, org, ""), t)
|
||||
// checkTypeChoice is a type an organization can pick for a monitor.
|
||||
type checkTypeChoice struct {
|
||||
Type string
|
||||
// OwnOnly is set when the platform switched the type off for its
|
||||
// checkers, leaving the organization's own.
|
||||
OwnOnly bool
|
||||
}
|
||||
|
||||
// enabledCheckTypes lists the types offered to org for new monitors, plus
|
||||
// keep when a monitor of a switched-off type is being edited. The
|
||||
// organization's choice is ignored if it would leave nothing, which can
|
||||
// happen when the platform later switches off what it kept.
|
||||
func (s *Server) enabledCheckTypes(ctx context.Context, org store.Org, keep string) []string {
|
||||
// checkTypeChoices lists the types offered to org, plus keep when a
|
||||
// monitor of a switched-off type is being edited. Types the platform
|
||||
// switched off for its checkers are offered only to organizations with
|
||||
// regions of their own; passive ones, which no checker runs, not at all.
|
||||
func (s *Server) checkTypeChoices(ctx context.Context, org store.Org, keep string, ownRegions bool) []checkTypeChoice {
|
||||
off := s.disabledChecks(ctx)
|
||||
var platform, out []string
|
||||
for _, t := range check.Types {
|
||||
if off[string(t)] && string(t) != keep {
|
||||
var out, platform []checkTypeChoice
|
||||
for _, ty := range check.Types {
|
||||
t := string(ty)
|
||||
passive := check.Passive(ty)
|
||||
if off[t] && t != keep && (passive || !ownRegions) {
|
||||
continue
|
||||
}
|
||||
platform = append(platform, string(t))
|
||||
if !slices.Contains(org.DisabledCheckTypes, string(t)) || string(t) == keep {
|
||||
out = append(out, string(t))
|
||||
choice := checkTypeChoice{Type: t, OwnOnly: off[t] && !passive}
|
||||
if !off[t] || t == keep {
|
||||
platform = append(platform, choice)
|
||||
}
|
||||
if !slices.Contains(org.DisabledCheckTypes, t) || t == keep {
|
||||
out = append(out, choice)
|
||||
}
|
||||
}
|
||||
// The organization's choice can leave nothing usable when it only
|
||||
// kept types the platform's checkers no longer run.
|
||||
if len(out) == 0 {
|
||||
return platform
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
var (
|
||||
errCheckTypeDisabled = errors.New("api: check type switched off")
|
||||
errOwnCheckersOnly = errors.New("api: check type switched off for platform checkers")
|
||||
)
|
||||
|
||||
// checkTypeAllowed fails unless org may give a monitor, previously of
|
||||
// type keep in the regions previous, type t in regions; platform marks
|
||||
// the platform's regions. Monitors of a type the platform switched off
|
||||
// keep the platform regions they had but gain none.
|
||||
func (s *Server) checkTypeAllowed(ctx context.Context, org store.Org, t, keep string, regions, previous []uuid.UUID, platform map[uuid.UUID]bool) error {
|
||||
own := false
|
||||
for _, p := range platform {
|
||||
own = own || !p
|
||||
}
|
||||
choices := s.checkTypeChoices(ctx, org, keep, own)
|
||||
i := slices.IndexFunc(choices, func(c checkTypeChoice) bool { return c.Type == t })
|
||||
if i < 0 {
|
||||
return errCheckTypeDisabled
|
||||
}
|
||||
if !s.disabledChecks(ctx)[t] || check.Passive(check.Type(t)) {
|
||||
return nil
|
||||
}
|
||||
for _, r := range regions {
|
||||
if platform[r] && (t != keep || !slices.Contains(previous, r)) {
|
||||
return errOwnCheckersOnly
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// failCheckType answers a checkTypeAllowed error and reports whether it
|
||||
// did.
|
||||
func failCheckType(c *gin.Context, err error) bool {
|
||||
switch {
|
||||
case errors.Is(err, errCheckTypeDisabled):
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_disabled")
|
||||
case errors.Is(err, errOwnCheckersOnly):
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_own_checkers_only")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type checkTypeView struct {
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
@@ -132,13 +188,40 @@ func (s *Server) adminSetCheckTypes(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// orgCheckTypes tells API clients which monitor types they can create.
|
||||
// orgCheckTypes tells API clients which monitor types they can create;
|
||||
// own_checkers_only are those the platform's checkers do not run.
|
||||
func (s *Server) orgCheckTypes(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"types": s.enabledCheckTypes(c.Request.Context(), current(c).Org, "")})
|
||||
p := current(c)
|
||||
ctx := c.Request.Context()
|
||||
own, err := s.hasOwnRegions(ctx, p)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
types, ownOnly := []string{}, []string{}
|
||||
for _, ch := range s.checkTypeChoices(ctx, p.Org, "", own) {
|
||||
types = append(types, ch.Type)
|
||||
if ch.OwnOnly {
|
||||
ownOnly = append(ownOnly, ch.Type)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"types": types, "own_checkers_only": ownOnly})
|
||||
}
|
||||
|
||||
// orgSetCheckTypes chooses which of the platform's types the organization
|
||||
// offers its members.
|
||||
func (s *Server) hasOwnRegions(ctx context.Context, p *principal) (bool, error) {
|
||||
var own bool
|
||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
regions, err := q.ListVisibleRegions(ctx)
|
||||
own = slices.ContainsFunc(regions, func(r store.Region) bool { return r.OrgID != nil })
|
||||
return err
|
||||
})
|
||||
return own, err
|
||||
}
|
||||
|
||||
// orgSetCheckTypes chooses which types the organization offers its
|
||||
// members, independently of what the platform's checkers run. Passive
|
||||
// types the platform switched off cannot be chosen and keep their
|
||||
// setting.
|
||||
func (s *Server) orgSetCheckTypes(c *gin.Context) {
|
||||
var req checkTypesRequest
|
||||
if !bind(c, &req) {
|
||||
@@ -149,12 +232,17 @@ func (s *Server) orgSetCheckTypes(c *gin.Context) {
|
||||
off := s.disabledChecks(ctx)
|
||||
disabled := []string{}
|
||||
enabled := 0
|
||||
for _, t := range check.Types {
|
||||
for _, ty := range check.Types {
|
||||
t := string(ty)
|
||||
switch {
|
||||
case slices.Contains(req.Enabled, string(t)):
|
||||
case off[t] && check.Passive(ty):
|
||||
if slices.Contains(p.Org.DisabledCheckTypes, t) {
|
||||
disabled = append(disabled, t)
|
||||
}
|
||||
case slices.Contains(req.Enabled, t):
|
||||
enabled++
|
||||
case !off[string(t)]:
|
||||
disabled = append(disabled, string(t))
|
||||
default:
|
||||
disabled = append(disabled, t)
|
||||
}
|
||||
}
|
||||
for _, t := range req.Enabled {
|
||||
|
||||
Reference in new issue
Block a user