package park import ( "github.com/carlosframework/platform/internal/regionname" "time" ) // HomePlacementCode is the console door's home-name rule. It consumes a // roster/API name, not an engine region; @home is never a placement code. func HomePlacementCode(home, region string) string { return regionname.HomePlacementCode(home, region) } // SameRegion compares nonempty engine identities under the verified home binding. func SameRegion(home, a, b string) bool { return regionname.SameRegion(home, a, b) } func QuietUntil(bounds map[string]time.Time, home, region string) time.Time { var latest time.Time for r, until := range bounds { if SameRegion(home, r, region) && latest.Before(until) { latest = until } } return latest } func (e Engine) OwnsRegion(region string) bool { return SameRegion(e.HomeRegion, e.Region, region) } func (v View) sameRegion(a, b string) bool { return SameRegion(v.HomeRegion, a, b) } // PlacementMatches compares a record code with a nonempty engine identity. func PlacementMatches(home, code, engine string) bool { return SameRegion(home, PlacementRegion(code), engine) } // BoundHome joins the verified local binding and the admitted operation's // binding for history reads. It never changes an engine's ownership. func BoundHome(local, operation string) (string, error) { for _, home := range []string{local, operation} { if home != "" && PlacementRegion(home) != home { return "", ErrUnsafe } } if local != "" && operation != "" && local != operation { return "", ErrUnsafe } if local != "" { return local, nil } return operation, nil } // EnginePlacement projects a verified engine identity into record vocabulary. func EnginePlacement(home, engine string) (string, bool) { if engine == "" { return "", false } if SameRegion(home, engine, HomeRegion) { return "", true } return engine, PlacementRegion(engine) == engine } func VerifiedHome(fleet, local, region, operation string) (string, error) { if (fleet != "" && PlacementRegion(fleet) != fleet) || (operation != "" && operation != fleet) || (local != "" && (local != fleet || local != region)) || (local == "" && operation != "" && operation == region) { return "", ErrUnsafe } return BoundHome(local, operation) }