Skip to content

Commit

Permalink
Add command to switch rules; close #87
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Sep 25, 2024
1 parent e8e1058 commit 9bad858
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/ctrl/api/rules-registry-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
26 changes: 26 additions & 0 deletions internal/ctrl/rules-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions internal/database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions internal/database/database_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/tasks/http-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9bad858

Please sign in to comment.