package hostclaim import "fmt" // Adopt is the one adoption routine (CC8) for a host that has never had a // claim. It writes discovery chores for every other region and box (CC4). func Adopt(nh NewHolder, servingBox string, otherRegions, otherBoxes []string, opID string, newID func() string) func(*Claim) error { return func(c *Claim) error { if c.Gen != 0 || c.Holder != nil { return fmt.Errorf("%w: adoption is only for a host that never had a claim", ErrWrongState) } if nh.Host != c.Host || !keyMatches(nh) || nh.Region == "" || servingBox == "" || opID == "" { return fmt.Errorf("%w: adoption inputs are not this host's", ErrWrongState) } inc, err := Incarnation(nh.Account, nh.App, nh.Host, nh.CreatedAt) if err != nil { return err } c.Holder = &Holder{Acquisition: nh.Acquisition, Family: nh.Family, Key: nh.Key, Account: nh.Account, App: nh.App, Name: nh.Name, Incarnation: inc, CreatedAt: nh.CreatedAt, Location: "instances/" + nh.Host, Stamp: c.Gen + 1, WriterBox: servingBox} c.Op = Op{Kind: KindServing, Exec: servingBox, Participants: []string{servingBox}} c.Touched = Touched{Complete: true, Regions: []string{nh.Region}, Boxes: []string{servingBox}} appendParkObligations(c, opID, otherRegions, otherBoxes, newID) if len(c.Obligations) > 0 { c.Touched.Complete, c.Touched.DiscoveryOp = false, opID c.Touched.DiscoveryBound = true c.Touched.DiscoveryBoxes = append([]string(nil), otherBoxes...) } return nil } }