apiActions.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package api
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. log "github.com/sirupsen/logrus"
  7. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  8. acl "github.com/OliveTin/OliveTin/internal/acl"
  9. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  10. config "github.com/OliveTin/OliveTin/internal/config"
  11. entities "github.com/OliveTin/OliveTin/internal/entities"
  12. executor "github.com/OliveTin/OliveTin/internal/executor"
  13. "github.com/OliveTin/OliveTin/internal/tpl"
  14. )
  15. type DashboardRenderRequest struct {
  16. AuthenticatedUser *authpublic.AuthenticatedUser
  17. cfg *config.Config
  18. ex *executor.Executor
  19. EntityType string
  20. EntityKey string
  21. }
  22. func (rr *DashboardRenderRequest) findAction(title string) *apiv1.Action {
  23. return rr.findActionForEntity(title, nil)
  24. }
  25. func (rr *DashboardRenderRequest) findActionForEntity(title string, entity *entities.Entity) *apiv1.Action {
  26. rr.ex.MapActionBindingsLock.RLock()
  27. defer rr.ex.MapActionBindingsLock.RUnlock()
  28. for _, binding := range rr.ex.MapActionBindings {
  29. if binding.Action.Title != title {
  30. continue
  31. }
  32. if matchesEntity(binding, entity) {
  33. return buildAction(binding, rr)
  34. }
  35. }
  36. return nil
  37. }
  38. func matchesEntity(binding *executor.ActionBinding, entity *entities.Entity) bool {
  39. if entity == nil {
  40. return binding.Entity == nil
  41. }
  42. return binding.Entity != nil && binding.Entity.UniqueKey == entity.UniqueKey
  43. }
  44. func buildEffectivePolicy(policy *config.ConfigurationPolicy) *apiv1.EffectivePolicy {
  45. ret := &apiv1.EffectivePolicy{
  46. ShowDiagnostics: policy.ShowDiagnostics,
  47. ShowLogList: policy.ShowLogList,
  48. }
  49. return ret
  50. }
  51. func evaluateEnabledExpression(action *config.Action, entity *entities.Entity) bool {
  52. if action.EnabledExpression == "" {
  53. return true
  54. }
  55. result := tpl.ParseTemplateOfActionBeforeExec(action.EnabledExpression, entity)
  56. result = strings.TrimSpace(result)
  57. if result == "" {
  58. return false
  59. }
  60. if isTemplateError(result, action) {
  61. return false
  62. }
  63. return evaluateResultValue(result)
  64. }
  65. func isTemplateError(result string, action *config.Action) bool {
  66. if !strings.HasPrefix(result, "tpl ") || !strings.Contains(result, "error") {
  67. return false
  68. }
  69. log.WithFields(log.Fields{
  70. "actionTitle": action.Title,
  71. "enabledExpression": action.EnabledExpression,
  72. "result": result,
  73. }).Warn("enabledExpression template evaluation failed, treating as disabled")
  74. return true
  75. }
  76. func evaluateResultValue(result string) bool {
  77. if strings.EqualFold(result, "true") {
  78. return true
  79. }
  80. if num, err := strconv.Atoi(result); err == nil {
  81. return num != 0
  82. }
  83. return false
  84. }
  85. func getDefaultArgumentValue(cfgArg config.ActionArgument, entity *entities.Entity) string {
  86. defaultValue := cfgArg.Default
  87. if defaultValue != "" {
  88. defaultValue = tpl.ParseTemplateOfActionBeforeExec(defaultValue, entity)
  89. }
  90. return defaultValue
  91. }
  92. func buildAction(actionBinding *executor.ActionBinding, rr *DashboardRenderRequest) *apiv1.Action {
  93. action := actionBinding.Action
  94. aclCanExec := acl.IsAllowedExec(rr.cfg, rr.AuthenticatedUser, action)
  95. enabledExprCanExec := evaluateEnabledExpression(action, actionBinding.Entity)
  96. // Calculate rate limit expiry time
  97. expiryUnix := rr.ex.GetTimeUntilAvailable(actionBinding)
  98. datetimeRateLimitExpires := ""
  99. if expiryUnix > 0 {
  100. datetimeRateLimitExpires = time.Unix(expiryUnix, 0).Format("2006-01-02 15:04:05")
  101. }
  102. btn := apiv1.Action{
  103. BindingId: actionBinding.ID,
  104. Title: tpl.ParseTemplateOfActionBeforeExec(action.Title, actionBinding.Entity),
  105. Icon: tpl.ParseTemplateOfActionBeforeExec(action.Icon, actionBinding.Entity),
  106. CanExec: aclCanExec && enabledExprCanExec,
  107. PopupOnStart: action.PopupOnStart,
  108. Order: int32(actionBinding.ConfigOrder),
  109. Timeout: int32(action.Timeout),
  110. DatetimeRateLimitExpires: datetimeRateLimitExpires,
  111. }
  112. for _, cfgArg := range action.Arguments {
  113. pbArg := apiv1.ActionArgument{
  114. Name: cfgArg.Name,
  115. Title: cfgArg.Title,
  116. Type: cfgArg.Type,
  117. Description: cfgArg.Description,
  118. DefaultValue: getDefaultArgumentValue(cfgArg, actionBinding.Entity),
  119. Choices: buildChoices(cfgArg),
  120. Suggestions: cfgArg.Suggestions,
  121. SuggestionsBrowserKey: cfgArg.SuggestionsBrowserKey,
  122. }
  123. btn.Arguments = append(btn.Arguments, &pbArg)
  124. }
  125. return &btn
  126. }
  127. func buildChoices(arg config.ActionArgument) []*apiv1.ActionArgumentChoice {
  128. if arg.Entity != "" && len(arg.Choices) == 1 {
  129. return buildChoicesEntity(arg.Choices[0], arg.Entity)
  130. } else {
  131. return buildChoicesSimple(arg.Choices)
  132. }
  133. }
  134. func buildChoicesEntity(firstChoice config.ActionArgumentChoice, entityTitle string) []*apiv1.ActionArgumentChoice {
  135. ret := []*apiv1.ActionArgumentChoice{}
  136. entList := entities.GetEntityInstances(entityTitle)
  137. for _, ent := range entList {
  138. ret = append(ret, &apiv1.ActionArgumentChoice{
  139. Value: tpl.ParseTemplateOfActionBeforeExec(firstChoice.Value, ent),
  140. Title: tpl.ParseTemplateOfActionBeforeExec(firstChoice.Title, ent),
  141. })
  142. }
  143. return ret
  144. }
  145. func buildChoicesSimple(choices []config.ActionArgumentChoice) []*apiv1.ActionArgumentChoice {
  146. ret := []*apiv1.ActionArgumentChoice{}
  147. for _, cfgChoice := range choices {
  148. pbChoice := apiv1.ActionArgumentChoice{
  149. Value: cfgChoice.Value,
  150. Title: cfgChoice.Title,
  151. }
  152. ret = append(ret, &pbChoice)
  153. }
  154. return ret
  155. }