| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostactors" |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | ) |
| 10 | |
| 11 | func (a ProductionAuthority) StartDeleteClaim(ctx context.Context, before hostclaim.Observed, r Record, by string) (hostclaim.Observed, error) { |
| 12 | if err := a.StartDelete(ctx, before, r); err != nil { |
| 13 | return hostclaim.Observed{}, err |
| 14 | } |
| 15 | admission, err := a.admission(ctx, r.Holder.Account, r.Holder.App) |
| 16 | if err != nil { |
| 17 | return hostclaim.Observed{}, err |
| 18 | } |
| 19 | if admission.Value.Op == nil || admission.Value.Op.Kind != "deleting" || admission.Value.Op.Intent["host"] != r.Holder.Host { |
| 20 | return hostclaim.Observed{}, ErrIdentity |
| 21 | } |
| 22 | id := admission.Value.Op.ID |
| 23 | regions, boxes, err := a.Inventory(ctx, before) |
| 24 | if err != nil { |
| 25 | return hostclaim.Observed{}, err |
| 26 | } |
| 27 | roster, err := a.Directory.Read(ctx) |
| 28 | if err != nil { |
| 29 | return hostclaim.Observed{}, err |
| 30 | } |
| 31 | poolSet := map[string]bool{} |
| 32 | for _, box := range roster.Roster.Boxes { |
| 33 | if box.Namespace == a.Namespace { |
| 34 | poolSet[box.Pool] = true |
| 35 | } |
| 36 | } |
| 37 | var pools []string |
| 38 | for pool := range poolSet { |
| 39 | pools = append(pools, pool) |
| 40 | } |
| 41 | sort.Strings(pools) |
| 42 | scope := hostactors.Scope{Namespace: a.Namespace, Regions: regions, Pools: pools, Participants: before.Claim.Op.Participants, ConsoleID: a.ConsoleID} |
| 43 | if err = a.StartDelete(ctx, before, r); err != nil { |
| 44 | return hostclaim.Observed{}, err |
| 45 | } |
| 46 | // Ruling 86: a delete passes the completion box it verified dead. |
| 47 | fenced, err := a.completionFence(ctx, before.Claim) |
| 48 | if err != nil { |
| 49 | return hostclaim.Observed{}, err |
| 50 | } |
| 51 | return a.startHeldClaim(ctx, before, id, scope, func() error { |
| 52 | if err := a.StartDelete(ctx, before, r); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | fresh, err := a.admission(ctx, r.Holder.Account, r.Holder.App) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if fresh.Value.AppID != admission.Value.AppID || fresh.Value.State != "closed" || fresh.Value.Op == nil || fresh.Value.Op.ID != id || fresh.Value.Op.Kind != "deleting" || fresh.Value.Op.Intent["host"] != r.Holder.Host { |
| 60 | return ErrPending |
| 61 | } |
| 62 | return nil |
| 63 | }, hostclaim.BeginDelete(id, by, r.ETag, a.Now(), regions, boxes, fenced)) |
| 64 | } |
| 65 | |