| 1 | package park |
| 2 | |
| 3 | import ( |
| 4 | "github.com/carlosframework/platform/internal/regionname" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // HomePlacementCode is the console door's home-name rule. It consumes a |
| 9 | // roster/API name, not an engine region; @home is never a placement code. |
| 10 | func HomePlacementCode(home, region string) string { return regionname.HomePlacementCode(home, region) } |
| 11 | |
| 12 | // SameRegion compares nonempty engine identities under the verified home binding. |
| 13 | func SameRegion(home, a, b string) bool { return regionname.SameRegion(home, a, b) } |
| 14 | |
| 15 | func QuietUntil(bounds map[string]time.Time, home, region string) time.Time { |
| 16 | var latest time.Time |
| 17 | for r, until := range bounds { |
| 18 | if SameRegion(home, r, region) && latest.Before(until) { |
| 19 | latest = until |
| 20 | } |
| 21 | } |
| 22 | return latest |
| 23 | } |
| 24 | |
| 25 | func (e Engine) OwnsRegion(region string) bool { |
| 26 | return SameRegion(e.HomeRegion, e.Region, region) |
| 27 | } |
| 28 | |
| 29 | func (v View) sameRegion(a, b string) bool { return SameRegion(v.HomeRegion, a, b) } |
| 30 | |
| 31 | // PlacementMatches compares a record code with a nonempty engine identity. |
| 32 | func PlacementMatches(home, code, engine string) bool { |
| 33 | return SameRegion(home, PlacementRegion(code), engine) |
| 34 | } |
| 35 | |
| 36 | // BoundHome joins the verified local binding and the admitted operation's |
| 37 | // binding for history reads. It never changes an engine's ownership. |
| 38 | func BoundHome(local, operation string) (string, error) { |
| 39 | for _, home := range []string{local, operation} { |
| 40 | if home != "" && PlacementRegion(home) != home { |
| 41 | return "", ErrUnsafe |
| 42 | } |
| 43 | } |
| 44 | if local != "" && operation != "" && local != operation { |
| 45 | return "", ErrUnsafe |
| 46 | } |
| 47 | if local != "" { |
| 48 | return local, nil |
| 49 | } |
| 50 | return operation, nil |
| 51 | } |
| 52 | |
| 53 | // EnginePlacement projects a verified engine identity into record vocabulary. |
| 54 | func EnginePlacement(home, engine string) (string, bool) { |
| 55 | if engine == "" { |
| 56 | return "", false |
| 57 | } |
| 58 | if SameRegion(home, engine, HomeRegion) { |
| 59 | return "", true |
| 60 | } |
| 61 | return engine, PlacementRegion(engine) == engine |
| 62 | } |
| 63 | |
| 64 | func VerifiedHome(fleet, local, region, operation string) (string, error) { |
| 65 | if (fleet != "" && PlacementRegion(fleet) != fleet) || (operation != "" && operation != fleet) || (local != "" && (local != fleet || local != region)) || (local == "" && operation != "" && operation == region) { |
| 66 | return "", ErrUnsafe |
| 67 | } |
| 68 | return BoundHome(local, operation) |
| 69 | } |
| 70 | |