| 1 | package hostactors |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | func (d Directory) Referenced(ctx context.Context, reference string) (bool, error) { |
| 12 | keys, err := d.Store.List(ctx, "control/hosts/") |
| 13 | if err != nil { |
| 14 | return false, err |
| 15 | } |
| 16 | for _, key := range keys { |
| 17 | name := strings.TrimPrefix(key, "control/hosts/") |
| 18 | if strings.Contains(name, "/") { |
| 19 | continue |
| 20 | } |
| 21 | host, ok := strings.CutSuffix(name, ".json") |
| 22 | if !ok { |
| 23 | return false, ErrRoster |
| 24 | } |
| 25 | observed, err := hostclaim.Read(ctx, d.Store, host) |
| 26 | if err != nil { |
| 27 | return false, err |
| 28 | } |
| 29 | if observed.Absent { |
| 30 | return false, ErrRoster |
| 31 | } |
| 32 | c := observed.Claim |
| 33 | _, inventory := c.Inventory[reference] |
| 34 | if inventory || c.Op.Exec == reference || slices.Contains(c.Op.Participants, reference) || slices.Contains(c.Touched.Boxes, reference) || slices.Contains(c.Lost, reference) { |
| 35 | return true, nil |
| 36 | } |
| 37 | if c.Holder != nil { |
| 38 | if c.Holder.WriterBox == reference { |
| 39 | return true, nil |
| 40 | } |
| 41 | for _, a := range c.Holder.Admissions { |
| 42 | if a.Box == reference { |
| 43 | return true, nil |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | keys, err = d.Store.List(ctx, "control/edges/") |
| 49 | if err != nil { |
| 50 | return false, err |
| 51 | } |
| 52 | for _, key := range keys { |
| 53 | raw, _, err := d.Store.Get(ctx, key) |
| 54 | if err != nil { |
| 55 | return false, err |
| 56 | } |
| 57 | var edge struct { |
| 58 | BoxID string `json:"box_id"` |
| 59 | } |
| 60 | if json.Unmarshal(raw, &edge) != nil { |
| 61 | return false, ErrRoster |
| 62 | } |
| 63 | if edge.BoxID == reference || key == "control/edges/"+reference+".json" { |
| 64 | return true, nil |
| 65 | } |
| 66 | } |
| 67 | return false, nil |
| 68 | } |
| 69 | |