4
0

api_log_arguments.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package api
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "connectrpc.com/connect"
  7. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  8. "github.com/OliveTin/OliveTin/internal/executor"
  9. )
  10. func logEntryArgumentsToProto(args map[string]string) []*apiv1.StartActionArgument {
  11. if len(args) == 0 {
  12. return nil
  13. }
  14. names := make([]string, 0, len(args))
  15. for name := range args {
  16. names = append(names, name)
  17. }
  18. sort.Strings(names)
  19. out := make([]*apiv1.StartActionArgument, 0, len(names))
  20. for _, name := range names {
  21. out = append(out, &apiv1.StartActionArgument{
  22. Name: name,
  23. Value: args[name],
  24. })
  25. }
  26. return out
  27. }
  28. func copyStringMap(source map[string]string) map[string]string {
  29. copied := make(map[string]string, len(source))
  30. for key, value := range source {
  31. copied[key] = value
  32. }
  33. return copied
  34. }
  35. func restartArgumentsIncompleteError() error {
  36. return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("stored arguments are incomplete for restart; use StartAction with the required arguments instead"))
  37. }
  38. func validateRestartLogEntry(entry *executor.InternalLogEntry) error {
  39. if entry.Binding.Action.Justification && strings.TrimSpace(entry.Justification) == "" {
  40. return restartRequiresJustificationError()
  41. }
  42. if executor.RestartArgumentsIncomplete(entry.Binding.Action, entry.Binding.Entity, entry.Arguments) {
  43. return restartArgumentsIncompleteError()
  44. }
  45. return nil
  46. }