apiActions.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  41. for _, cfgArg := range action.Arguments {
  42. pbArg := apiv1.ActionArgument{
  43. Name: cfgArg.Name,
  44. Title: cfgArg.Title,
  45. Type: cfgArg.Type,
  46. Description: cfgArg.Description,
  47. DefaultValue: cfgArg.Default,
  48. Choices: buildChoices(cfgArg),
  49. Suggestions: cfgArg.Suggestions,
  50. }
  51. btn.Arguments = append(btn.Arguments, &pbArg)
  52. }
  53. return &btn
  54. }
  55. func buildChoices(arg config.ActionArgument) []*apiv1.ActionArgumentChoice {
  56. if arg.Entity != "" && len(arg.Choices) == 1 {
  57. return buildChoicesEntity(arg.Choices[0], arg.Entity)
  58. } else {
  59. return buildChoicesSimple(arg.Choices)
  60. }
  61. }
  62. func buildChoicesEntity(firstChoice config.ActionArgumentChoice, entityTitle string) []*apiv1.ActionArgumentChoice {
  63. ret := []*apiv1.ActionArgumentChoice{}
  64. entList := entities.GetEntityInstances(entityTitle)
  65. for _, ent := range entList {
  66. ret = append(ret, &apiv1.ActionArgumentChoice{
  67. Value: entities.ParseTemplateWith(firstChoice.Value, ent),
  68. Title: entities.ParseTemplateWith(firstChoice.Title, ent),
  69. })
  70. }
  71. return ret
  72. }
  73. func buildChoicesSimple(choices []config.ActionArgumentChoice) []*apiv1.ActionArgumentChoice {
  74. ret := []*apiv1.ActionArgumentChoice{}
  75. for _, cfgChoice := range choices {
  76. pbChoice := apiv1.ActionArgumentChoice{
  77. Value: cfgChoice.Value,
  78. Title: cfgChoice.Title,
  79. }
  80. ret = append(ret, &pbChoice)
  81. }
  82. return ret
  83. }