apiActions.go 4.7 KB

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