Download this file
| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | var ErrClaimAdmission = errors.New("provision: claim admission unavailable") |
| 11 | |
| 12 | type ClaimRecord struct { |
| 13 | Host, Key, Family, Account, App string |
| 14 | CreatedAt time.Time |
| 15 | Value any |
| 16 | Route registry.Route |
| 17 | DryRun bool |
| 18 | } |
| 19 | |
| 20 | func (cfg ReconcileConfig) claimInstall(ctx context.Context, selected ClaimRecord, existing *registry.Route, write func(registry.ClaimOwnership) error) error { |
| 21 | if !cfg.ClaimEnabled { |
| 22 | return write(registry.ClaimOwnership{}) |
| 23 | } |
| 24 | if cfg.ClaimInstall == nil { |
| 25 | return ErrClaimAdmission |
| 26 | } |
| 27 | // An existing row without a valid claim owner can never be kept: the |
| 28 | // write below would refuse it. Decide that before the hook, whose guard |
| 29 | // and inventory effects a refused row must not cause. |
| 30 | if existing != nil && !existing.ClaimOwnership.Valid() { |
| 31 | return registry.ErrClaimOwnership |
| 32 | } |
| 33 | return cfg.ClaimInstall(ctx, selected, func(owner registry.ClaimOwnership) error { |
| 34 | if !owner.Valid() || (existing != nil && existing.ClaimOwnership != owner) { |
| 35 | return registry.ErrClaimOwnership |
| 36 | } |
| 37 | return write(owner) |
| 38 | }) |
| 39 | } |
| 40 | func (cfg ReconcileConfig) claimRetire(ctx context.Context, reg *registry.Registry, route registry.Route) error { |
| 41 | if !cfg.ClaimEnabled { |
| 42 | return reg.Delete(route.Host) |
| 43 | } |
| 44 | if cfg.ClaimRetire == nil { |
| 45 | return ErrClaimAdmission |
| 46 | } |
| 47 | return cfg.ClaimRetire(ctx, route) |
| 48 | } |
| 49 | |