| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "github.com/carlosframework/platform/internal/regionname" |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | // ClaimOriginProof implements moves' Proven contract. HomeRegion must come from |
| 12 | // the verified roster/placement binding, and Claims is the deployment store. |
| 13 | // Admission.NewGeneration is deliberately not evidence of admission. |
| 14 | type ClaimOriginProof struct { |
| 15 | Claims release.Store |
| 16 | HomeRegion, Region string |
| 17 | } |
| 18 | |
| 19 | func (p ClaimOriginProof) Proven(ctx context.Context, o hostclaim.Observed, _ hostclaim.Admission, generations []string) (map[string]bool, error) { |
| 20 | if o.Absent || o.Claim == nil || o.Claim.Holder == nil || p.Region == "" { |
| 21 | return nil, hostclaim.ErrWrongState |
| 22 | } |
| 23 | h := o.Claim.Holder |
| 24 | regions := []string{p.Region} |
| 25 | if p.HomeRegion != "" && regionname.PlacementRegion(p.HomeRegion) == p.HomeRegion && regionname.SameRegion(p.HomeRegion, p.Region, regionname.HomeRegion) { |
| 26 | if p.Region == regionname.HomeRegion { |
| 27 | regions = append(regions, p.HomeRegion) |
| 28 | } else { |
| 29 | regions = append(regions, regionname.HomeRegion) |
| 30 | } |
| 31 | } |
| 32 | set := make([]hostclaim.OriginTuple, 0, len(regions)*len(generations)) |
| 33 | for _, generation := range generations { |
| 34 | for _, region := range regions { |
| 35 | set = append(set, hostclaim.OriginTuple{Incarnation: h.Incarnation, Location: h.Location, Region: region, Generation: generation}) |
| 36 | } |
| 37 | } |
| 38 | proven, err := hostclaim.AdmittedOrigins(ctx, p.Claims, o, set) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | result := make(map[string]bool, len(generations)) |
| 43 | for _, tuple := range set { |
| 44 | result[tuple.Generation] = result[tuple.Generation] || proven[tuple] |
| 45 | } |
| 46 | return result, nil |
| 47 | } |
| 48 | |