| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type RegistryRows struct { |
| 11 | Registry *registry.Registry |
| 12 | Inventory RetirementInventory |
| 13 | } |
| 14 | |
| 15 | func (r RegistryRows) RetireRows(context.Context, string, []Artifact) error { return ErrTypedJournal } |
| 16 | func (r RegistryRows) RetireRowsJournal(ctx context.Context, id, owner string, rows []Artifact, archive func(json.RawMessage) error) error { |
| 17 | if r.Registry == nil || owner != r.Inventory.Ownership { |
| 18 | return ErrTypedInventory |
| 19 | } |
| 20 | targets := make([]registry.ClaimRow, 0, len(rows)) |
| 21 | for _, a := range rows { |
| 22 | if a.Root != "registry" || a.Owner != owner { |
| 23 | return ErrTypedInventory |
| 24 | } |
| 25 | target := registry.ClaimRow{AllowAbsent: a.AllowAbsent, Kind: string(a.Kind), Host: r.Inventory.Host, Ownership: registry.ClaimOwnership{ClaimIncarnation: r.Inventory.Captured.Incarnation, ClaimStamp: r.Inventory.Captured.Stamp, ClaimOwner: owner, ClaimRowID: a.RowID}} |
| 26 | if a.Kind == ArtifactRoute { |
| 27 | if a.Name != target.Host { |
| 28 | return ErrTypedInventory |
| 29 | } |
| 30 | } else if a.Kind == ArtifactTimer { |
| 31 | prefix := target.Host + "/" |
| 32 | if !strings.HasPrefix(a.Name, prefix) { |
| 33 | return ErrTypedInventory |
| 34 | } |
| 35 | target.Name = strings.TrimPrefix(a.Name, prefix) |
| 36 | target.Registration = a.RowID |
| 37 | if id, registration, ok := strings.Cut(a.RowID, "~"); ok { |
| 38 | if id == "" || registration == "" || strings.Contains(registration, "~") { |
| 39 | return ErrTypedInventory |
| 40 | } |
| 41 | target.Ownership.ClaimRowID = id |
| 42 | target.Registration = registration |
| 43 | } |
| 44 | } else { |
| 45 | return ErrTypedInventory |
| 46 | } |
| 47 | targets = append(targets, target) |
| 48 | } |
| 49 | return r.Registry.RetireClaimRows(ctx, id, targets, func(rows []registry.ClaimRowArchive) error { |
| 50 | b, err := json.Marshal(rows) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | return archive(b) |
| 55 | }) |
| 56 | } |
| 57 | |