Skip to content

Commit

Permalink
feat(api): add unit-openvpn endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Sep 18, 2024
1 parent 4a9667b commit f9d08a5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func main() {
reports.POST("/ovpnrw-connections", methods.UpdateOvpnConnections)
reports.POST("/dpi-stats", methods.UpdateDpiStats)
reports.POST("/unit-name", methods.SetUnitName)
reports.POST("/unit-openvpnrw", methods.SetUnitOpenVPNRW)
reports.POST("/unit-openvpn", methods.SetUnitOpenVPNRW)
reports.POST("/unit-wan", methods.SetUnitWan)
}

// handle missing endpoint
Expand Down
49 changes: 49 additions & 0 deletions api/methods/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,55 @@ func SetUnitOpenVPNRW(c *gin.Context) {
}
}

func SetUnitWan(c *gin.Context) {
// bind json
var req models.UnitWanRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, structs.Map(response.StatusBadRequest{
Code: 400,
Message: "Invalid request",
Data: err.Error(),
}))
return
}

if checkUnitId(c.MustGet("UnitId").(string)) != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Unit not found",
Data: nil,
Code: 404,
}))
return
}

dbpool, dbctx := storage.ReportInstance()
// Remove all previous data
_, err := dbpool.Exec(dbctx, "DELETE FROM wan_config WHERE uuid = $1", c.MustGet("UnitId").(string))
if err != nil {
logs.Logs.Println("[ERR][UNITWAN] error deleting previous data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Code: 500,
Message: "Error deleting previous data",
Data: err.Error(),
}))
return
}
// Insert inside WAN table
for _, wan := range req.Data {
_, err := dbpool.Exec(dbctx, "INSERT INTO wan_config (uuid, interface, device) VALUES ($1, $2, $3)", c.MustGet("UnitId").(string), wan.Interface, wan.Device)
if err != nil {
logs.Logs.Println("[ERR][UNITWAN] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Code: 500,
Message: "Error inserting data",
Data: err.Error(),
}))
return
}
}

}

func checkUnitId(unitId string) error {
if unitId == "" {
return errors.New("uuid is empty")
Expand Down
11 changes: 11 additions & 0 deletions api/models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@ type OpenVPNConfigurations []OpenVPNConfiguration
type UnitOpenVPNRWRequest struct {
Data OpenVPNConfigurations `json:"data" binding:"required"`
}

type Wan struct {
Interface string `json:"interface" binding:"required"`
Device string `json:"device" binding:"required"`
}

type Wans []Wan

type UnitWanRequest struct {
Data Wans `json:"data" binding:"required"`
}
7 changes: 7 additions & 0 deletions api/storage/report_schema.sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ CREATE TABLE IF NOT EXISTS openvpn_config (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS wan_config (
uuid UUID NOT NULL references units(uuid),
interface TEXT NOT NULL,
device TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- General retention policies

-- Keep raw data for 30 days
Expand Down

0 comments on commit f9d08a5

Please sign in to comment.