Skip to content

Commit

Permalink
Add update_action api
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Jan 15, 2025
1 parent 9fd8d9e commit ce2077c
Show file tree
Hide file tree
Showing 6 changed files with 50 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 @@ -17,4 +17,5 @@ type RulesRegistryHTTP interface {
DisableRule(c *gin.Context)
SwitchRule(c *gin.Context)
PostRule(c *gin.Context)
UpdateAction(c *gin.Context)
}
25 changes: 25 additions & 0 deletions internal/ctrl/rules-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,28 @@ func (rr *RulesRegistry) PostRule(c *gin.Context) {
c.Header("Location", fmt.Sprintf("/rules/%s", id))
c.JSON(http.StatusCreated, rule)
}

// Update action of a rule
func (rr *RulesRegistry) UpdateAction(c *gin.Context) {
id_rule := c.Param("uuid")
iduuid_rule, err := uuid.FromString(id_rule)
if err != nil {
logrus.WithError(err).Error("Bad UUID")
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "bad uuid", Error: err})
return
}
var action n4tosrv6.Action
if err := c.BindJSON(&action); err != nil {
logrus.WithError(err).Error("could not deserialize")
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
return
}
c.Header("Cache-Control", "no-cache")
err = rr.db.UpdateAction(c, iduuid_rule, action)
if err != nil {
logrus.WithError(err).Error("Could not update Action for this rule in the database")
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not update Action for this rule in the database", Error: err})
return
}
c.Status(http.StatusNoContent)
}
13 changes: 13 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,16 @@ func (db *Database) GetDownlinkAction(ctx context.Context, ueIp netip.Addr) (n4t
return n4tosrv6.Action{}, fmt.Errorf("Procedure not registered")
}
}

func (db *Database) UpdateAction(ctx context.Context, uuidRule uuid.UUID, action n4tosrv6.Action) error {
srh := []string{}
for _, ip := range action.SRH {
srh = append(srh, ip.String())
}
if stmt, ok := db.stmt["update_action"]; ok {
_, err := stmt.ExecContext(ctx, uuidRule.String(), pq.Array(srh))
return err
} else {
return fmt.Errorf("Procedure not registered")
}
}
9 changes: 9 additions & 0 deletions internal/database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ BEGIN
DELETE FROM rule WHERE uuid = in_uuid;
END;$$;

CREATE OR REPLACE PROCEDURE update_action(
IN in_uuid UUID,
IN in_srh INET ARRAY
)
LANGUAGE plpgsql AS $$
BEGIN
UPDATE rule SET action_srh = in_srh WHERE rule.uuid = in_uuid;
END;$$;

CREATE OR REPLACE FUNCTION get_uplink_action(
IN in_uplink_teid BIGINT, IN in_uplink_upf INET,
IN in_gnb_ip INET,
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 @@ -68,6 +68,7 @@ func (t *HttpServerTask) RunInit(ctx context.Context) error {
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)
r.PATCH("/rules/:uuid/update-action", t.rulesRegistryHTTP.UpdateAction)
t.srv = &http.Server{
Addr: t.httpAddr.String(),
Handler: r,
Expand Down

0 comments on commit ce2077c

Please sign in to comment.