diff --git a/internal/ctrl/api/rules-registry-http.go b/internal/ctrl/api/rules-registry-http.go index 0293115..98c142c 100644 --- a/internal/ctrl/api/rules-registry-http.go +++ b/internal/ctrl/api/rules-registry-http.go @@ -14,5 +14,6 @@ type RulesRegistryHTTP interface { DeleteRule(c *gin.Context) EnableRule(c *gin.Context) DisableRule(c *gin.Context) + SwitchRule(c *gin.Context) PostRule(c *gin.Context) } diff --git a/internal/ctrl/rules-registry.go b/internal/ctrl/rules-registry.go index f22edbc..32a770f 100644 --- a/internal/ctrl/rules-registry.go +++ b/internal/ctrl/rules-registry.go @@ -110,6 +110,32 @@ func (rr *RulesRegistry) DisableRule(c *gin.Context) { c.Status(http.StatusNoContent) } +func (rr *RulesRegistry) SwitchRule(c *gin.Context) { + idEnable := c.Param("enable_uuid") + idDisable := c.Param("disable_uuid") + iduuidEnable, err := uuid.FromString(idEnable) + if err != nil { + logrus.WithError(err).Error("Bad UUID") + c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "bad uuid", Error: err}) + return + } + iduuidDisable, err := uuid.FromString(idDisable) + if err != nil { + logrus.WithError(err).Error("Bad UUID") + c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "bad uuid", Error: err}) + return + } + c.Header("Cache-Control", "no-cache") + err = rr.db.SwitchRule(c, iduuidEnable, iduuidDisable) + if err != nil { + logrus.WithError(err).Error("Could not Switch rule in the database") + c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not switch rule in the database", Error: err}) + return + //TODO: check if rule not found + } + c.Status(http.StatusNoContent) +} + // Post a new rule func (rr *RulesRegistry) PostRule(c *gin.Context) { var rule jsonapi.Rule diff --git a/internal/database/database.go b/internal/database/database.go index 398d4b4..3f56e4a 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -258,6 +258,15 @@ func (db *Database) DisableRule(ctx context.Context, uuid uuid.UUID) error { } } +func (db *Database) SwitchRule(ctx context.Context, uuidEnable uuid.UUID, uuidDisable uuid.UUID) error { + if stmt, ok := db.stmt["switch_rule"]; ok { + _, err := stmt.ExecContext(ctx, uuidEnable.String(), uuidDisable.String()) + return err + } else { + return fmt.Errorf("Procedure not registered") + } +} + func (db *Database) DeleteRule(ctx context.Context, uuid uuid.UUID) error { if stmt, ok := db.stmt["delete_rule"]; ok { _, err := stmt.ExecContext(ctx, uuid.String()) diff --git a/internal/database/database.sql b/internal/database/database.sql index 804d988..be6bb0a 100644 --- a/internal/database/database.sql +++ b/internal/database/database.sql @@ -55,6 +55,16 @@ BEGIN UPDATE rule SET enabled = false WHERE rule.uuid = in_uuid; END;$$; +CREATE OR REPLACE PROCEDURE switch_rule( + IN in_uuid_enable UUID, + IN in_uuid_disable UUID +) +LANGUAGE plpgsql AS $$ +BEGIN + UPDATE rule SET enabled = true WHERE rule.uuid = in_uuid_enable; + UPDATE rule SET enabled = false WHERE rule.uuid = in_uuid_disable; +END;$$; + CREATE OR REPLACE PROCEDURE delete_rule( IN in_uuid UUID ) diff --git a/internal/database/database_gen.go b/internal/database/database_gen.go index a3e3a63..751d279 100644 --- a/internal/database/database_gen.go +++ b/internal/database/database_gen.go @@ -17,6 +17,7 @@ var procedures = map[string]procedureOrFunction{ "insert_downlink_rule": {is_procedure: true, num_in: 4, num_out: 1}, "enable_rule": {is_procedure: true, num_in: 1, num_out: 0}, "disable_rule": {is_procedure: true, num_in: 1, num_out: 0}, + "switch_rule": {is_procedure: true, num_in: 2, num_out: 0}, "delete_rule": {is_procedure: true, num_in: 1, num_out: 0}, "get_uplink_action": {is_procedure: false, num_in: 2, num_out: 0}, "get_downlink_action": {is_procedure: false, num_in: 1, num_out: 0}, diff --git a/internal/tasks/http-server.go b/internal/tasks/http-server.go index 43839de..6b01393 100644 --- a/internal/tasks/http-server.go +++ b/internal/tasks/http-server.go @@ -63,6 +63,7 @@ func (t *HttpServerTask) RunInit(ctx context.Context) error { r.GET("/rules", t.rulesRegistryHTTP.GetRules) r.PATCH("/rules/:uuid/enable", t.rulesRegistryHTTP.EnableRule) r.PATCH("/rules/:uuid/disable", t.rulesRegistryHTTP.DisableRule) + r.PATCH("/rules/switch/:enable_uuid/:disable_uuid", t.rulesRegistryHTTP.SwitchRule) r.DELETE("/rules/:uuid", t.rulesRegistryHTTP.DeleteRule) t.srv = &http.Server{ Addr: t.httpAddr,