apiActions.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package api
  2. import (
  3. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  4. acl "github.com/OliveTin/OliveTin/internal/acl"
  5. config "github.com/OliveTin/OliveTin/internal/config"
  6. entities "github.com/OliveTin/OliveTin/internal/entities"
  7. executor "github.com/OliveTin/OliveTin/internal/executor"
  8. )
  9. type DashboardRenderRequest struct {
  10. AuthenticatedUser *acl.AuthenticatedUser
  11. cfg *config.Config
  12. ex *executor.Executor
  13. }
  14. func (rr *DashboardRenderRequest) findAction(title string) *apiv1.Action {
  15. rr.ex.MapActionIdToBindingLock.RLock()
  16. defer rr.ex.MapActionIdToBindingLock.RUnlock()
  17. for _, binding := range rr.ex.MapActionIdToBinding {
  18. if binding.Action.Title == title {
  19. return buildAction(binding, rr)
  20. }
  21. }
  22. return nil
  23. }
  24. func buildEffectivePolicy(policy *config.ConfigurationPolicy) *apiv1.EffectivePolicy {
  25. ret := &apiv1.EffectivePolicy{
  26. ShowDiagnostics: policy.ShowDiagnostics,
  27. ShowLogList: policy.ShowLogList,
  28. }
  29. return ret
  30. }
  31. func buildAction(actionBinding *executor.ActionBinding, rr *DashboardRenderRequest) *apiv1.Action {
  32. action := actionBinding.Action
  33. btn := apiv1.Action{
  34. BindingId: actionBinding.ID,
  35. Title: entities.ParseTemplateWith(action.Title, actionBinding.Entity),
  36. Icon: entities.ParseTemplateWith(action.Icon, actionBinding.Entity),
  37. CanExec: acl.IsAllowedExec(rr.cfg, rr.AuthenticatedUser, action),
  38. PopupOnStart: action.PopupOnStart,
  39. Order: int32(actionBinding.ConfigOrder),
  40. Timeout: int32(action.Timeout),
  41. }
  42. for _, cfgArg := range action.Arguments {
  43. pbArg := apiv1.ActionArgument{
  44. Name: cfgArg.Name,
  45. Title: cfgArg.Title,
  46. Type: cfgArg.Type,
  47. Description: cfgArg.Description,
  48. DefaultValue: cfgArg.Default,
  49. Choices: buildChoices(cfgArg),
  50. Suggestions: cfgArg.Suggestions,
  51. }
  52. btn.Arguments = append(btn.Arguments, &pbArg)
  53. }
  54. return &btn
  55. }
  56. func buildChoices(arg config.ActionArgument) []*apiv1.ActionArgumentChoice {
  57. if arg.Entity != "" && len(arg.Choices) == 1 {
  58. return buildChoicesEntity(arg.Choices[0], arg.Entity)
  59. } else {
  60. return buildChoicesSimple(arg.Choices)
  61. }
  62. }
  63. func buildChoicesEntity(firstChoice config.ActionArgumentChoice, entityTitle string) []*apiv1.ActionArgumentChoice {
  64. ret := []*apiv1.ActionArgumentChoice{}
  65. entList := entities.GetEntityInstances(entityTitle)
  66. for _, ent := range entList {
  67. ret = append(ret, &apiv1.ActionArgumentChoice{
  68. Value: entities.ParseTemplateWith(firstChoice.Value, ent),
  69. Title: entities.ParseTemplateWith(firstChoice.Title, ent),
  70. })
  71. }
  72. return ret
  73. }
  74. func buildChoicesSimple(choices []config.ActionArgumentChoice) []*apiv1.ActionArgumentChoice {
  75. ret := []*apiv1.ActionArgumentChoice{}
  76. for _, cfgChoice := range choices {
  77. pbChoice := apiv1.ActionArgumentChoice{
  78. Value: cfgChoice.Value,
  79. Title: cfgChoice.Title,
  80. }
  81. ret = append(ret, &pbChoice)
  82. }
  83. return ret
  84. }