| 1 | package hostclaim |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // Adopt is the one adoption routine (CC8) for a host that has never had a |
| 6 | // claim. It writes discovery chores for every other region and box (CC4). |
| 7 | func Adopt(nh NewHolder, servingBox string, otherRegions, otherBoxes []string, opID string, newID func() string) func(*Claim) error { |
| 8 | return func(c *Claim) error { |
| 9 | if c.Gen != 0 || c.Holder != nil { |
| 10 | return fmt.Errorf("%w: adoption is only for a host that never had a claim", ErrWrongState) |
| 11 | } |
| 12 | if nh.Host != c.Host || !keyMatches(nh) || nh.Region == "" || servingBox == "" || opID == "" { |
| 13 | return fmt.Errorf("%w: adoption inputs are not this host's", ErrWrongState) |
| 14 | } |
| 15 | inc, err := Incarnation(nh.Account, nh.App, nh.Host, nh.CreatedAt) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | c.Holder = &Holder{Acquisition: nh.Acquisition, Family: nh.Family, Key: nh.Key, Account: nh.Account, App: nh.App, Name: nh.Name, |
| 20 | Incarnation: inc, CreatedAt: nh.CreatedAt, Location: "instances/" + nh.Host, Stamp: c.Gen + 1, WriterBox: servingBox} |
| 21 | c.Op = Op{Kind: KindServing, Exec: servingBox, Participants: []string{servingBox}} |
| 22 | c.Touched = Touched{Complete: true, Regions: []string{nh.Region}, Boxes: []string{servingBox}} |
| 23 | appendParkObligations(c, opID, otherRegions, otherBoxes, newID) |
| 24 | if len(c.Obligations) > 0 { |
| 25 | c.Touched.Complete, c.Touched.DiscoveryOp = false, opID |
| 26 | c.Touched.DiscoveryBound = true |
| 27 | c.Touched.DiscoveryBoxes = append([]string(nil), otherBoxes...) |
| 28 | } |
| 29 | return nil |
| 30 | } |
| 31 | } |
| 32 | |