action_factory.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package device
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. hthv1 "hth/internal/gen/hth/api/v1"
  7. "hth/internal/grpcserver"
  8. "hth/internal/protocol"
  9. "hth/internal/safety"
  10. )
  11. func terminalDispatch(s hthv1.ActionDispatchState) bool {
  12. switch s {
  13. case hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_UNCONFIRMED,
  14. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_CONFIRMED,
  15. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_OUTCOME_UNKNOWN,
  16. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_NOT_DELIVERED,
  17. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED,
  18. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_SUPERSEDED:
  19. return true
  20. default:
  21. return false
  22. }
  23. }
  24. func internalOrigin(reason string) string {
  25. if strings.HasPrefix(reason, "daemon/internal") {
  26. return reason
  27. }
  28. if reason == "" {
  29. return "daemon/internal"
  30. }
  31. return "daemon/internal:" + reason
  32. }
  33. func capturePeer(ctx context.Context, r *req) {
  34. if r == nil || ctx == nil {
  35. return
  36. }
  37. if cred, ok := grpcserver.PeerFromContext(ctx); ok {
  38. r.peerUID, r.peerGID, r.peerPID = cred.UID, cred.GID, cred.PID
  39. }
  40. }
  41. func (d *Device) actionContextFromReq(commandID, origin string, mode hthv1.ActionMode, frame protocol.SetAll, r req) ActionExecutionContext {
  42. ax := d.newActionContext(commandID, origin, mode, frame, r.rpcCtx)
  43. if r.peerUID != 0 || r.peerGID != 0 || r.peerPID != 0 {
  44. ax.PeerUID, ax.PeerGID, ax.PeerPID = r.peerUID, r.peerGID, r.peerPID
  45. }
  46. ax.LeaseID = r.leaseID
  47. ax.LeaseEpoch = r.leaseEpoch
  48. if r.batch != nil {
  49. if ax.LeaseID == "" {
  50. ax.LeaseID = r.batch.GetLeaseId()
  51. ax.LeaseEpoch = r.batch.GetLeaseEpoch()
  52. }
  53. ax.Requested = r.batch
  54. if r.batch.ValidUntil != nil {
  55. ax.ValidUntil = r.batch.ValidUntil.AsTime()
  56. }
  57. }
  58. return ax
  59. }
  60. func recordFromAction(ax ActionExecutionContext, now time.Time, conf hthv1.ConfirmationStatus, dispatch hthv1.ActionDispatchState) *hthv1.ActionRecord {
  61. client := "hthd"
  62. if ax.Requested != nil && ax.Requested.ClientId != "" {
  63. client = ax.Requested.ClientId
  64. }
  65. rec := actionRecord(&hthv1.ActionBatch{CommandId: ax.CommandID, ClientId: client}, now, conf, ax.EffectiveTarget)
  66. if rec == nil {
  67. return nil
  68. }
  69. if ax.Requested != nil {
  70. rec.FieldAudits = safety.RawFieldAudits(ax.Requested)
  71. }
  72. return bindActionRecord(rec, ax, dispatch)
  73. }
  74. func stampResultContext(res *hthv1.ActionResult, ax ActionExecutionContext) *hthv1.ActionResult {
  75. if res == nil {
  76. return nil
  77. }
  78. if res.CommandId == "" {
  79. res.CommandId = ax.CommandID
  80. }
  81. if res.ActionOrigin == "" {
  82. res.ActionOrigin = ax.Origin
  83. }
  84. if res.DeviceEpoch == 0 {
  85. res.DeviceEpoch = ax.DeviceEpoch
  86. }
  87. if res.PortGeneration == 0 {
  88. res.PortGeneration = ax.PortGeneration
  89. }
  90. return res
  91. }