Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,8 @@
" pm.expect(jsonData.totalCount).to.be.equal(0)\r",
" pm.expect(jsonData.connectedCount).to.be.equal(0)\r",
" pm.expect(jsonData.disconnectedCount).to.be.equal(0)\r",
" pm.expect(jsonData.activatedCount).to.be.equal(0)\r",
" pm.expect(jsonData.discoveredCount).to.be.equal(0)\r",
" \r",
"})"
],
Expand Down Expand Up @@ -2268,6 +2270,110 @@
},
"response": []
},
{
"name": "All Devices activated",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{protocol}}://{{host}}/api/v1/devices?activated=true",
"protocol": "{{protocol}}",
"host": [
"{{host}}"
],
"path": [
"api",
"v1",
"devices"
],
"query": [
{
"key": "activated",
"value": "true"
}
]
}
},
"response": []
},
{
"name": "All Devices discovered",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{protocol}}://{{host}}/api/v1/devices?discovered=true",
"protocol": "{{protocol}}",
"host": [
"{{host}}"
],
"path": [
"api",
"v1",
"devices"
],
"query": [
{
"key": "discovered",
"value": "true"
}
]
}
},
"response": []
},
{
"name": "All Devices with count set to true",
"event": [
Expand Down
24 changes: 22 additions & 2 deletions internal/controller/httpapi/v1/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ func (dr *deviceRoutes) getStats(c *gin.Context) {
return
}

activated, discovered, err := dr.t.GetDeviceStateCounts(c.Request.Context(), "")
if err != nil {
dr.l.Error(err, "http - devices - v1 - getStats")
ErrorResponse(c, err)

return
}

countResponse := dto.DeviceStatResponse{
TotalCount: count,
TotalCount: count,
ActivatedCount: activated,
DiscoveredCount: discovered,
}

c.JSON(http.StatusOK, countResponse)
Expand Down Expand Up @@ -104,11 +114,15 @@ func (dr *deviceRoutes) get(c *gin.Context) {
tags := c.Query("tags")
hostname := c.Query("hostname")
friendlyName := c.Query("friendlyName")
activated := c.Query("activated")
discovered := c.Query("discovered")

var items []dto.Device

var err error

ctx := c.Request.Context()

switch {
case hostname != "":
items, err = dr.getByColumnOrTags(c, "HostName", hostname, odata.Top, odata.Skip, "")
Expand All @@ -119,8 +133,14 @@ func (dr *deviceRoutes) get(c *gin.Context) {
case tags != "":
items, err = dr.getByColumnOrTags(c, "Tags", tags, odata.Top, odata.Skip, "")

case activated == "true":
items, err = dr.t.GetActivated(ctx, odata.Top, odata.Skip, "")

case discovered == "true":
items, err = dr.t.GetDiscovered(ctx, odata.Top, odata.Skip, "")

default:
items, err = dr.t.Get(c.Request.Context(), odata.Top, odata.Skip, "")
items, err = dr.t.Get(ctx, odata.Top, odata.Skip, "")
}

if err != nil {
Expand Down
48 changes: 47 additions & 1 deletion internal/controller/httpapi/v1/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,40 @@ func TestDevicesRoutes(t *testing.T) {
response: []dto.Device{{GUID: "guid", MPSUsername: "mpsusername", Username: "admin", Password: "password", ConnectionStatus: true, Hostname: "hostname"}},
expectedCode: http.StatusOK,
},
{
name: "get activated devices",
method: http.MethodGet,
url: "/api/v1/devices?activated=true",
mock: func(device *mocks.MockDeviceManagementFeature) {
device.EXPECT().GetActivated(context.Background(), 25, 0, "").Return([]dto.Device{{
GUID: "guid", MPSUsername: "mpsusername", Username: "admin", Password: "password", ConnectionStatus: true, Hostname: "hostname",
}}, nil)
},
response: []dto.Device{{GUID: "guid", MPSUsername: "mpsusername", Username: "admin", Password: "password", ConnectionStatus: true, Hostname: "hostname"}},
expectedCode: http.StatusOK,
},
{
name: "get discovered devices",
method: http.MethodGet,
url: "/api/v1/devices?discovered=true",
mock: func(device *mocks.MockDeviceManagementFeature) {
device.EXPECT().GetDiscovered(context.Background(), 25, 0, "").Return([]dto.Device{{
GUID: "guid", MPSUsername: "mpsusername", Username: "admin", Password: "password", ConnectionStatus: true, Hostname: "hostname",
}}, nil)
},
response: []dto.Device{{GUID: "guid", MPSUsername: "mpsusername", Username: "admin", Password: "password", ConnectionStatus: true, Hostname: "hostname"}},
expectedCode: http.StatusOK,
},
{
name: "get activated devices - failed",
method: http.MethodGet,
url: "/api/v1/devices?activated=true",
mock: func(device *mocks.MockDeviceManagementFeature) {
device.EXPECT().GetActivated(context.Background(), 25, 0, "").Return(nil, devices.ErrDatabase)
},
response: devices.ErrDatabase,
expectedCode: http.StatusBadRequest,
},
{
name: "get all devices - with count",
method: http.MethodGet,
Expand Down Expand Up @@ -317,10 +351,22 @@ func TestDevicesRoutes(t *testing.T) {
url: "/api/v1/devices/stats",
mock: func(device *mocks.MockDeviceManagementFeature) {
device.EXPECT().GetCount(context.Background(), "").Return(5, nil)
device.EXPECT().GetDeviceStateCounts(context.Background(), "").Return(4, 1, nil)
},
response: dto.DeviceStatResponse{TotalCount: 5},
response: dto.DeviceStatResponse{TotalCount: 5, ActivatedCount: 4, DiscoveredCount: 1},
expectedCode: http.StatusOK,
},
{
name: "get devices stats - failed",
method: http.MethodGet,
url: "/api/v1/devices/stats",
mock: func(device *mocks.MockDeviceManagementFeature) {
device.EXPECT().GetCount(context.Background(), "").Return(5, nil)
device.EXPECT().GetDeviceStateCounts(context.Background(), "").Return(0, 0, devices.ErrDatabase)
},
response: devices.ErrDatabase,
expectedCode: http.StatusBadRequest,
},
}

for _, tc := range tests {
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/openapi/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (f *FuegoAdapter) registerDeviceQueryRoutes() {
fuego.OptionQuery("method", "Method to filter tags (any/all)"),
fuego.OptionQuery("hostname", "Filter devices by host name"),
fuego.OptionQuery("friendlyName", "Filter devices by friendly name"),
fuego.OptionQueryBool("activated", "Return devices activated into client or admin control mode"),
fuego.OptionQueryBool("discovered", "Return devices discovered on the network but not yet activated"),
protectedRouteOptions(),
)

Expand Down Expand Up @@ -205,6 +207,8 @@ func (f *FuegoAdapter) getDeviceStats(_ fuego.ContextNoBody) (dto.DeviceStatResp
TotalCount: 5,
ConnectedCount: 3,
DisconnectedCount: 2,
ActivatedCount: 4,
DiscoveredCount: 1,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/entity/dto/v1/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type DeviceStatResponse struct {
TotalCount int `json:"totalCount"`
ConnectedCount int `json:"connectedCount"`
DisconnectedCount int `json:"disconnectedCount"`
ActivatedCount int `json:"activatedCount"`
DiscoveredCount int `json:"discoveredCount"`
}
type Device struct {
ConnectionStatus bool `json:"connectionStatus"`
Expand Down
94 changes: 93 additions & 1 deletion internal/mocks/devicemanagement_mocks.go

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

Loading
Loading