| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "github.com/carlosframework/platform/internal/schedule" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | func (h ClaimRootHTTP) verifyTimerMutationRow(host string, owner registry.ClaimOwnership, e hostbox.StopInventoryEntry, m *hostbox.TimerMutation) error { |
| 12 | if m == nil || e.RelativePath != m.Timer.Host+"/"+m.Timer.Name { |
| 13 | return h.verifyInventoryTimer(host, owner, e) |
| 14 | } |
| 15 | t := m.Timer |
| 16 | if t.Host != host || !schedule.ValidName(t.Name) || !t.ClaimOwnership.Valid() || t.ClaimIncarnation != owner.ClaimIncarnation || t.ClaimStamp != owner.ClaimStamp || t.ClaimOwner != owner.ClaimOwner || t.ClaimRowID != t.Registration || t.Registration == "" || e.Kind != "timer" || e.ManagedRoot != "registry" || e.Ownership != owner.ClaimOwner || e.RowIdentity != t.Registration || e.Materialization != "materialized" || e.TimerRetired != m.Remove || e.Device != 0 || e.Inode != 0 || e.Type != 0 { |
| 17 | return hostbox.ErrTypedInventory |
| 18 | } |
| 19 | if t.Kind != schedule.KindOnce && t.Kind != schedule.KindSchedule { |
| 20 | return hostbox.ErrTypedInventory |
| 21 | } |
| 22 | if schedule.ValidPath(t.Path) != nil { |
| 23 | return hostbox.ErrTypedInventory |
| 24 | } |
| 25 | row, err := h.Reconcile.Registry.GetTimer(host, t.Name) |
| 26 | if errors.Is(err, os.ErrNotExist) { |
| 27 | if m.Expected == "" || m.Remove { |
| 28 | return nil |
| 29 | } |
| 30 | return hostbox.ErrChanged |
| 31 | } |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if row == nil || row.ClaimIncarnation != t.ClaimIncarnation || row.ClaimStamp != t.ClaimStamp || row.ClaimOwner != t.ClaimOwner || row.ClaimRowID != row.Registration || row.Kind != t.Kind { |
| 36 | return hostbox.ErrChanged |
| 37 | } |
| 38 | if *row == t || row.Registration == m.Expected { |
| 39 | return nil |
| 40 | } |
| 41 | return hostbox.ErrChanged |
| 42 | } |
| 43 | |
| 44 | func (h ClaimRootHTTP) verifyTimerMutation(prior, next hostbox.StopInventory, m *hostbox.TimerMutation) error { |
| 45 | if m == nil { |
| 46 | return h.verifyNewTimerIntentions(prior, next) |
| 47 | } |
| 48 | var old, entry *hostbox.StopInventoryEntry |
| 49 | name := m.Timer.Host + "/" + m.Timer.Name |
| 50 | for i := range prior.Entries { |
| 51 | e := &prior.Entries[i] |
| 52 | if e.Kind == "timer" && e.RelativePath == name { |
| 53 | if old != nil { |
| 54 | return hostbox.ErrTypedInventory |
| 55 | } |
| 56 | old = e |
| 57 | } |
| 58 | } |
| 59 | for i := range next.Entries { |
| 60 | e := &next.Entries[i] |
| 61 | if e.Kind == "timer" && e.RelativePath == name { |
| 62 | if entry != nil { |
| 63 | return hostbox.ErrTypedInventory |
| 64 | } |
| 65 | entry = e |
| 66 | } |
| 67 | } |
| 68 | if entry == nil { |
| 69 | return hostbox.ErrTypedInventory |
| 70 | } |
| 71 | if old == nil || old.TimerRetired { |
| 72 | if m.Expected != "" || m.Remove { |
| 73 | return hostbox.ErrChanged |
| 74 | } |
| 75 | } else if old.RowIdentity != m.Expected { |
| 76 | return hostbox.ErrChanged |
| 77 | } |
| 78 | if old != nil && (old.ArtifactID != entry.ArtifactID || old.Ownership != entry.Ownership || old.ManagedRoot != entry.ManagedRoot) { |
| 79 | return hostbox.ErrChanged |
| 80 | } |
| 81 | if m.Remove && (old == nil || old.RowIdentity != entry.RowIdentity) { |
| 82 | return hostbox.ErrChanged |
| 83 | } |
| 84 | if !m.Remove && m.Expected != "" && m.Expected == entry.RowIdentity { |
| 85 | return hostbox.ErrChanged |
| 86 | } |
| 87 | // Check every other new entry through the ordinary intention rules. |
| 88 | filtered := next |
| 89 | filtered.Entries = nil |
| 90 | for _, e := range next.Entries { |
| 91 | if e.RelativePath != name || e.Kind != "timer" { |
| 92 | filtered.Entries = append(filtered.Entries, e) |
| 93 | } |
| 94 | } |
| 95 | return h.verifyNewTimerIntentions(prior, filtered) |
| 96 | } |
| 97 | |
| 98 | func (h ClaimRootHTTP) applyTimerMutation(m hostbox.TimerMutation) error { |
| 99 | if m.Remove { |
| 100 | err := h.Reconcile.Registry.DeleteTimerOwnedRegistration(m.Timer) |
| 101 | if errors.Is(err, registry.ErrTimerChanged) { |
| 102 | _, err = h.Reconcile.Registry.GetTimer(m.Timer.Host, m.Timer.Name) |
| 103 | if errors.Is(err, os.ErrNotExist) { |
| 104 | return nil |
| 105 | } |
| 106 | if err == nil { |
| 107 | return hostbox.ErrChanged |
| 108 | } |
| 109 | } |
| 110 | return err |
| 111 | } |
| 112 | _, err := h.Reconcile.Registry.MaterializeTimerRegistration(m.Timer, m.Expected) |
| 113 | return err |
| 114 | } |
| 115 | |