...
1 package server
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7
8 "github.com/gin-gonic/gin"
9
10 "edge-infra.dev/pkg/lib/fog"
11 rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
12 )
13
14 func (res RulesEngineService) deleteCommand(c *gin.Context) {
15 log := fog.FromContext(c).WithName("rulesengineservice")
16 name := c.Param("name")
17
18 log = log.WithValues("commandName", name)
19 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
20
21 result, err := res.RulesEngine.DeleteCommand(c.Request.Context(), name)
22 if err != nil {
23 log.Error(err, "Deletion error", "command", name)
24 c.Status(http.StatusInternalServerError)
25 return
26 }
27 if len(result.Errors) > 0 {
28 for _, err := range result.Errors {
29 if err.Type == rulesengine.Conflict {
30 log.Error(fmt.Errorf("database query conflict"), "Conflict with database")
31 c.JSON(http.StatusConflict, result)
32 return
33 }
34 }
35
36 log.Error(fmt.Errorf("error deleting command"), "Requested data not found. No deletion performend")
37 c.JSON(http.StatusNotFound, result)
38 return
39 }
40 c.Status(http.StatusOK)
41 }
42
43 func (res RulesEngineService) readCommands(c *gin.Context) {
44 log := fog.FromContext(c).WithName("rulesengineservice")
45 vals, err := res.RulesEngine.ReadCommands(c.Request.Context())
46 if err != nil {
47 log.Error(err, "Read error")
48 c.Status(http.StatusInternalServerError)
49 return
50 }
51 if len(vals) == 0 {
52 log.Info("No commands returned")
53 c.JSON(http.StatusOK, nil)
54 return
55 }
56 c.JSON(http.StatusOK, vals)
57 }
58
59
60 func (res RulesEngineService) postCommands(c *gin.Context) {
61 log := fog.FromContext(c).WithName("rulesengineservice")
62 var payload []rulesengine.PostCommandPayload
63 if err := c.ShouldBindJSON(&payload); err != nil {
64 log.Error(err, "bind error")
65 c.Status(http.StatusBadRequest)
66 return
67 }
68 if len(payload) == 0 {
69 log.Error(errors.New("payload was nil"), "validation error")
70 c.Status(http.StatusBadRequest)
71 return
72 }
73 result, err := res.RulesEngine.AddCommands(c.Request.Context(), payload)
74 if err != nil {
75 log.Error(err, "post commands error")
76 c.Status(http.StatusInternalServerError)
77 return
78 }
79 if len(result.Conflicts) > 0 {
80 log.Info("Conflicts with query")
81 c.JSON(http.StatusConflict, result)
82 return
83 }
84 c.Status(http.StatusOK)
85 }
86
87 func (res RulesEngineService) readCommand(c *gin.Context) {
88 log := fog.FromContext(c).WithName("rulesengineservice")
89 name := c.Param("name")
90 comm, err := res.RulesEngine.ReadCommand(c.Request.Context(), name)
91 if err != nil {
92 log.Error(err, "read error")
93 c.Status(http.StatusInternalServerError)
94 return
95 }
96 if comm.Name == "" || comm.ID == "" {
97 log.Info("No command returned")
98 c.JSON(http.StatusOK, nil)
99 return
100 }
101 c.JSON(http.StatusOK, comm)
102 }
103
View as plain text