Skip to content

Commit ce2077c

Browse files
committed
Add update_action api
1 parent 9fd8d9e commit ce2077c

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

internal/ctrl/api/rules-registry-http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type RulesRegistryHTTP interface {
1717
DisableRule(c *gin.Context)
1818
SwitchRule(c *gin.Context)
1919
PostRule(c *gin.Context)
20+
UpdateAction(c *gin.Context)
2021
}

internal/ctrl/rules-registry.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,28 @@ func (rr *RulesRegistry) PostRule(c *gin.Context) {
158158
c.Header("Location", fmt.Sprintf("/rules/%s", id))
159159
c.JSON(http.StatusCreated, rule)
160160
}
161+
162+
// Update action of a rule
163+
func (rr *RulesRegistry) UpdateAction(c *gin.Context) {
164+
id_rule := c.Param("uuid")
165+
iduuid_rule, err := uuid.FromString(id_rule)
166+
if err != nil {
167+
logrus.WithError(err).Error("Bad UUID")
168+
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "bad uuid", Error: err})
169+
return
170+
}
171+
var action n4tosrv6.Action
172+
if err := c.BindJSON(&action); err != nil {
173+
logrus.WithError(err).Error("could not deserialize")
174+
c.JSON(http.StatusBadRequest, jsonapi.MessageWithError{Message: "could not deserialize", Error: err})
175+
return
176+
}
177+
c.Header("Cache-Control", "no-cache")
178+
err = rr.db.UpdateAction(c, iduuid_rule, action)
179+
if err != nil {
180+
logrus.WithError(err).Error("Could not update Action for this rule in the database")
181+
c.JSON(http.StatusInternalServerError, jsonapi.MessageWithError{Message: "could not update Action for this rule in the database", Error: err})
182+
return
183+
}
184+
c.Status(http.StatusNoContent)
185+
}

internal/database/database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,16 @@ func (db *Database) GetDownlinkAction(ctx context.Context, ueIp netip.Addr) (n4t
373373
return n4tosrv6.Action{}, fmt.Errorf("Procedure not registered")
374374
}
375375
}
376+
377+
func (db *Database) UpdateAction(ctx context.Context, uuidRule uuid.UUID, action n4tosrv6.Action) error {
378+
srh := []string{}
379+
for _, ip := range action.SRH {
380+
srh = append(srh, ip.String())
381+
}
382+
if stmt, ok := db.stmt["update_action"]; ok {
383+
_, err := stmt.ExecContext(ctx, uuidRule.String(), pq.Array(srh))
384+
return err
385+
} else {
386+
return fmt.Errorf("Procedure not registered")
387+
}
388+
}

internal/database/database.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ BEGIN
7676
DELETE FROM rule WHERE uuid = in_uuid;
7777
END;$$;
7878

79+
CREATE OR REPLACE PROCEDURE update_action(
80+
IN in_uuid UUID,
81+
IN in_srh INET ARRAY
82+
)
83+
LANGUAGE plpgsql AS $$
84+
BEGIN
85+
UPDATE rule SET action_srh = in_srh WHERE rule.uuid = in_uuid;
86+
END;$$;
87+
7988
CREATE OR REPLACE FUNCTION get_uplink_action(
8089
IN in_uplink_teid BIGINT, IN in_uplink_upf INET,
8190
IN in_gnb_ip INET,

internal/database/database_gen.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/tasks/http-server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (t *HttpServerTask) RunInit(ctx context.Context) error {
6868
r.PATCH("/rules/:uuid/disable", t.rulesRegistryHTTP.DisableRule)
6969
r.PATCH("/rules/switch/:enable_uuid/:disable_uuid", t.rulesRegistryHTTP.SwitchRule)
7070
r.DELETE("/rules/:uuid", t.rulesRegistryHTTP.DeleteRule)
71+
r.PATCH("/rules/:uuid/update-action", t.rulesRegistryHTTP.UpdateAction)
7172
t.srv = &http.Server{
7273
Addr: t.httpAddr.String(),
7374
Handler: r,

0 commit comments

Comments
 (0)