| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "github.com/carlosframework/platform/internal/moves" |
| 8 | "github.com/carlosframework/platform/internal/park" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | ) |
| 11 | |
| 12 | // CommittedLandingRecords is hostclaim.CommittedLandingGuard over the |
| 13 | // authoritative instance record: the move named by the claim's op has |
| 14 | // committed for this incarnation (provision.MoveCommitted, no rollback), |
| 15 | // and the regions and park scopes are the commit's (moves.CommitScopes), |
| 16 | // the regions as engine regions. |
| 17 | type CommittedLandingRecords struct{ Records MovesRecordReader } |
| 18 | |
| 19 | var _ hostclaim.CommittedLandingGuard = CommittedLandingRecords{} |
| 20 | |
| 21 | func (g CommittedLandingRecords) CheckLandingCommitted(ctx context.Context, o hostclaim.Observed, opID string) (hostclaim.CommittedLanding, error) { |
| 22 | c := o.Claim |
| 23 | if g.Records == nil || c == nil || c.Holder == nil || c.Op.ID != opID { |
| 24 | return hostclaim.CommittedLanding{}, hostclaim.ErrWrongState |
| 25 | } |
| 26 | rec, err := g.Records(ctx, c.Holder.Account, c.Holder.App, o.Host) |
| 27 | if err != nil { |
| 28 | return hostclaim.CommittedLanding{}, err |
| 29 | } |
| 30 | if rec == nil || rec.Move == nil { |
| 31 | return hostclaim.CommittedLanding{}, hostclaim.ErrWrongState |
| 32 | } |
| 33 | m := rec.Move |
| 34 | if m.ID != opID || m.Incarnation != c.Holder.Incarnation || m.State != provision.MoveCommitted || m.Rollback != nil || m.CommittedAt == nil { |
| 35 | return hostclaim.CommittedLanding{}, hostclaim.ErrWrongState |
| 36 | } |
| 37 | source, staging, err := moves.CommitScopes(c, m.FlippedAt) |
| 38 | if err != nil { |
| 39 | return hostclaim.CommittedLanding{}, err |
| 40 | } |
| 41 | // The record's From and To are placement codes. The commit's parks are |
| 42 | // queued in engine regions, as moves' commitClaim queues them |
| 43 | // (park.PlacementRegion, so the home is park.HomeRegion, never ""); a |
| 44 | // code that is no placement refuses. |
| 45 | from, to := park.PlacementRegion(m.From), park.PlacementRegion(m.To) |
| 46 | if from == "" || to == "" { |
| 47 | return hostclaim.CommittedLanding{}, hostclaim.ErrWrongState |
| 48 | } |
| 49 | return hostclaim.CommittedLanding{FromRegion: from, ToRegion: to, SourceScope: source, StagingScope: staging}, nil |
| 50 | } |
| 51 | |