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 @@ -1148,7 +1148,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"guid\": \"143e4567-e89b-12d3-a456-426614174000\",\r\n \"friendlyName\": \"friendlyName\",\r\n \"hostname\": \"hostname\",\r\n \"tags\": [],\r\n \"mpsusername\": \"admin\"\r\n}",
"raw": "{\r\n \"guid\": \"143e4567-e89b-12d3-a456-426614174000\",\r\n \"friendlyName\": \"friendlyName\",\r\n \"hostname\": \"hostname\",\r\n \"tags\": [],\r\n \"mpsusername\": \"admin\",\r\n \"deviceInfo\": {\r\n \"fwVersion\": \"16.1.30\",\r\n \"fwBuild\": \"3400\",\r\n \"fwSku\": \"11\",\r\n \"currentMode\": \"Admin\",\r\n \"features\": \"SOL,IDER,KVM\",\r\n \"ipAddress\": \"10.0.0.12\",\r\n \"lastUpdated\": \"2026-05-21T00:00:00Z\",\r\n \"tlsMode\": \"TLS 1.2\",\r\n \"upid\": {\r\n \"oemPlatformIdType\": \"Not Set (0)\",\r\n \"oemId\": \"\",\r\n \"csmeId\": \"4A45A39C5ED9462082510000\"\r\n },\r\n \"amtEnabledInBIOS\": true,\r\n \"meInterfaceVersion\": \"16.1.25.2124\",\r\n \"dhcpEnabled\": true,\r\n \"certHashes\": [\r\n \"a1b2c3\",\r\n \"d4e5f6\"\r\n ],\r\n \"lmsInstalled\": true,\r\n \"lmsVersion\": \"2410.5.0.0\",\r\n \"osName\": \"linux\",\r\n \"osVersion\": \"6.8.0-51-generic\",\r\n \"osDistro\": \"Ubuntu 24.04 LTS\",\r\n \"cpuModel\": \"Intel(R) Core(TM) Ultra 7 165H\",\r\n \"osIpAddress\": \"10.49.76.163\",\r\n \"ethernetAdapterCount\": 2,\r\n \"monitorConnected\": true,\r\n \"ieee8021xEnabled\": false\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
Expand Down Expand Up @@ -1217,7 +1217,7 @@
"});\r",
"pm.test(\"Expect an error with wrong device guid format\",function(){\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.error).to.eq(\"Error not found\")\r",
" pm.expect(jsonData.error).to.eq(\"device not found\")\r",
"})"
],
"type": "text/javascript",
Expand Down Expand Up @@ -1307,7 +1307,7 @@
"});\r",
"pm.test(\"Expect to return device info\",function(){\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.error).to.be.equal(\"Error not found\")\r",
" pm.expect(jsonData.error).to.be.equal(\"device not found\")\r",
"})"
],
"type": "text/javascript",
Expand Down Expand Up @@ -1828,7 +1828,7 @@
"});\r",
"pm.test(\"Expect an error for invalid guid\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.error).to.eq(\"Error not found\")\r",
" pm.expect(jsonData.error).to.eq(\"device not found\")\r",
"});"
],
"type": "text/javascript",
Expand Down Expand Up @@ -1927,7 +1927,7 @@
"});\r",
"pm.test(\"Expect an error when there is no device\", function () {\r",
" var jsonData = pm.response.json();\r",
" pm.expect(jsonData.error).to.eq(\"Error not found\")\r",
" pm.expect(jsonData.error).to.eq(\"device not found\")\r",
"});"
],
"type": "text/javascript",
Expand Down
27 changes: 25 additions & 2 deletions internal/controller/httpapi/v1/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,43 @@ func (dr *deviceRoutes) insert(c *gin.Context) {

// Keys are lowercased so callers can match against setter maps regardless of
// client casing (encoding/json unmarshals case-insensitively).
// Nested objects are flattened with dot notation (for example,
// "deviceinfo.fwversion") so PATCH handlers can deep-merge object fields.
func providedJSONFields(c *gin.Context) (map[string]bool, error) {
var raw map[string]json.RawMessage
if err := c.ShouldBindBodyWithJSON(&raw); err != nil {
return nil, err
}

fields := make(map[string]bool, len(raw))
for k := range raw {
fields[strings.ToLower(k)] = true
for k, v := range raw {
key := strings.ToLower(k)
fields[key] = true
collectNestedJSONFields(key, v, fields, 0)
}

return fields, nil
}

const maxNestedJSONFieldDepth = 16

func collectNestedJSONFields(prefix string, raw json.RawMessage, fields map[string]bool, depth int) {
if depth >= maxNestedJSONFieldDepth {
return
}

var obj map[string]json.RawMessage
if err := json.Unmarshal(raw, &obj); err != nil {
return
}

for k, v := range obj {
path := prefix + "." + strings.ToLower(k)
fields[path] = true
collectNestedJSONFields(path, v, fields, depth+1)
}
}
Comment thread
ShradhaGupta31 marked this conversation as resolved.

func (dr *deviceRoutes) update(c *gin.Context) {
var device dto.Device
if err := c.ShouldBindBodyWithJSON(&device); err != nil {
Expand Down
131 changes: 131 additions & 0 deletions internal/controller/httpapi/v1/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,137 @@ func TestDevicesUpdatePartialPatchMixedCaseKeys(t *testing.T) {
require.Equal(t, http.StatusOK, w.Code)
}

func TestDevicesUpdatePartialPatchTracksDeviceInfoSubfields(t *testing.T) {
t.Parallel()

guid := testDeviceGUID

incoming := &dto.Device{
GUID: guid,
DeviceInfo: &dto.DeviceInfo{
FWVersion: "16.1.30",
},
}

expectedFields := map[string]bool{
"guid": true,
"deviceinfo": true,
"deviceinfo.fwversion": true,
}

devicesFeature, engine := devicesTest(t)

devicesFeature.EXPECT().
Update(context.Background(), incoming, expectedFields).
Return(incoming, nil)

body := []byte(`{"guid":"` + guid + `","deviceInfo":{"fwVersion":"16.1.30"}}`)

req, err := http.NewRequestWithContext(context.Background(), http.MethodPatch, "/api/v1/devices", bytes.NewBuffer(body))
require.NoError(t, err)

w := httptest.NewRecorder()
engine.ServeHTTP(w, req)

require.Equal(t, http.StatusOK, w.Code)
}

func TestDevicesInsertAcceptsFullDeviceInfo(t *testing.T) {
t.Parallel()

lmsInstalled := false
amtEnabledInBIOS := true
dhcpEnabled := true
ethernetAdapterCount := 2
monitorConnected := true
ieee8021xEnabled := false

incoming := &dto.Device{
GUID: testDeviceGUID,
Hostname: "test-device",
DeviceInfo: &dto.DeviceInfo{
FWVersion: "16.1.30",
FWBuild: "3400",
FWSku: "11",
CurrentMode: "Admin",
Features: "SOL,IDER,KVM",
IPAddress: "10.0.0.12",
LastUpdated: &timeNow,
TLSMode: "TLS 1.2",
UPID: map[string]json.RawMessage{
"oemPlatformIdType": json.RawMessage(`"Not Set (0)"`),
"oemId": json.RawMessage(`""`),
"csmeId": json.RawMessage(`"4A45A39C5ED9462082510000"`),
},
AMTEnabledInBIOS: &amtEnabledInBIOS,
MEInterfaceVersion: "16.1.25.2124",
DHCPEnabled: &dhcpEnabled,
CertHashes: []string{"a1b2c3", "d4e5f6"},
LMSInstalled: &lmsInstalled,
LMSVersion: "2410.5.0.0",
OSName: "linux",
OSVersion: "6.8.0-51-generic",
OSDistro: "Ubuntu 24.04 LTS",
CPUModel: "Intel(R) Core(TM) Ultra 7 165H",
OSIPAddress: "10.49.76.163",
EthernetAdapterCount: &ethernetAdapterCount,
MonitorConnected: &monitorConnected,
IEEE8021XEnabled: &ieee8021xEnabled,
},
}

devicesFeature, engine := devicesTest(t)

devicesFeature.EXPECT().
Insert(context.Background(), incoming).
Return(incoming, nil)

body := []byte(`{
"guid":"` + testDeviceGUID + `",
"hostname":"test-device",
"deviceInfo":{
"fwVersion":"16.1.30",
"fwBuild":"3400",
"fwSku":"11",
"currentMode":"Admin",
"features":"SOL,IDER,KVM",
"ipAddress":"10.0.0.12",
"lastUpdated":"` + timeNow.Format(time.RFC3339Nano) + `",
"tlsMode":"TLS 1.2",
"upid":{
"oemPlatformIdType":"Not Set (0)",
"oemId":"",
"csmeId":"4A45A39C5ED9462082510000"
},
"amtEnabledInBIOS":true,
"meInterfaceVersion":"16.1.25.2124",
"dhcpEnabled":true,
"certHashes":["a1b2c3","d4e5f6"],
"lmsInstalled":false,
"lmsVersion":"2410.5.0.0",
"osName":"linux",
"osVersion":"6.8.0-51-generic",
"osDistro":"Ubuntu 24.04 LTS",
"cpuModel":"Intel(R) Core(TM) Ultra 7 165H",
"osIpAddress":"10.49.76.163",
"ethernetAdapterCount":2,
"monitorConnected":true,
"ieee8021xEnabled":false
}
}`)

req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/api/v1/devices", bytes.NewBuffer(body))
require.NoError(t, err)

w := httptest.NewRecorder()
engine.ServeHTTP(w, req)

require.Equal(t, http.StatusCreated, w.Code)

expected, _ := json.Marshal(incoming)
require.Equal(t, string(expected), w.Body.String())
}

// TestLoginRedirection verifies the device redirection token endpoint
func TestLoginRedirection(t *testing.T) {
t.Parallel()
Expand Down
32 changes: 24 additions & 8 deletions internal/entity/dto/v1/device.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dto

import (
"encoding/json"
"time"
)

Expand Down Expand Up @@ -37,14 +38,29 @@ type Device struct {
}

type DeviceInfo struct {
Comment thread
ShradhaGupta31 marked this conversation as resolved.
FWVersion string `json:"fwVersion"`
FWBuild string `json:"fwBuild"`
FWSku string `json:"fwSku"`
CurrentMode string `json:"currentMode"`
Features string `json:"features"`
IPAddress string `json:"ipAddress"`
LastUpdated time.Time `json:"lastUpdated"`
LMSInstalled *bool `json:"lmsInstalled,omitempty"`
FWVersion string `json:"fwVersion"`
FWBuild string `json:"fwBuild"`
FWSku string `json:"fwSku"`
CurrentMode string `json:"currentMode"`
Features string `json:"features"`
IPAddress string `json:"ipAddress"`
LastUpdated *time.Time `json:"lastUpdated,omitempty"`
LMSInstalled *bool `json:"lmsInstalled,omitempty"`
Comment thread
ShradhaGupta31 marked this conversation as resolved.
Comment thread
ShradhaGupta31 marked this conversation as resolved.
LMSVersion string `json:"lmsVersion,omitempty"`
TLSMode string `json:"tlsMode,omitempty"`
UPID map[string]json.RawMessage `json:"upid,omitempty"`
AMTEnabledInBIOS *bool `json:"amtEnabledInBIOS,omitempty"`
MEInterfaceVersion string `json:"meInterfaceVersion,omitempty"`
DHCPEnabled *bool `json:"dhcpEnabled,omitempty"`
CertHashes []string `json:"certHashes,omitempty"`
OSName string `json:"osName,omitempty"`
OSVersion string `json:"osVersion,omitempty"`
OSDistro string `json:"osDistro,omitempty"`
CPUModel string `json:"cpuModel,omitempty"`
OSIPAddress string `json:"osIpAddress,omitempty"`
EthernetAdapterCount *int `json:"ethernetAdapterCount,omitempty"`
MonitorConnected *bool `json:"monitorConnected,omitempty"`
IEEE8021XEnabled *bool `json:"ieee8021xEnabled,omitempty"`
}

type Explorer struct {
Expand Down
64 changes: 64 additions & 0 deletions internal/entity/dto/v1/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dto

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestDeviceInfoJSONRoundTrip(t *testing.T) {
t.Parallel()

amtEnabled := true
dhcpEnabled := true
lmsInstalled := true
ethernetAdapterCount := 2
monitorConnected := true
ieee8021xEnabled := false
lastUpdated := time.Date(2026, 5, 21, 0, 0, 0, 0, time.UTC)

info := DeviceInfo{
FWVersion: "16.1.30",
FWBuild: "3400",
FWSku: "11",
CurrentMode: "Admin",
Features: "SOL,IDER,KVM",
IPAddress: "10.0.0.12",
LastUpdated: &lastUpdated,
TLSMode: "TLS 1.2",
UPID: map[string]json.RawMessage{
"oemPlatformIdType": json.RawMessage(`"Not Set (0)"`),
"oemId": json.RawMessage(`""`),
"csmeId": json.RawMessage(`"4A45A39C5ED94620"`),
},
AMTEnabledInBIOS: &amtEnabled,
MEInterfaceVersion: "16.1.25.2124",
DHCPEnabled: &dhcpEnabled,
CertHashes: []string{"a1b2c3", "d4e5f6"},
LMSInstalled: &lmsInstalled,
LMSVersion: "2410.5.0.0",
OSName: "linux",
OSVersion: "6.8.0-51-generic",
OSDistro: "Ubuntu 24.04 LTS",
CPUModel: "Intel(R) Core(TM) Ultra 7 165H",
OSIPAddress: "10.49.76.163",
EthernetAdapterCount: &ethernetAdapterCount,
MonitorConnected: &monitorConnected,
IEEE8021XEnabled: &ieee8021xEnabled,
}

encoded, err := json.Marshal(info)
require.NoError(t, err)

var decoded DeviceInfo
require.NoError(t, json.Unmarshal(encoded, &decoded))

require.Equal(t, info.TLSMode, decoded.TLSMode)
require.Equal(t, info.MEInterfaceVersion, decoded.MEInterfaceVersion)
require.Equal(t, info.CertHashes, decoded.CertHashes)
require.Equal(t, info.LMSVersion, decoded.LMSVersion)
require.NotNil(t, decoded.LMSInstalled)
require.Equal(t, *info.LMSInstalled, *decoded.LMSInstalled)
}
8 changes: 5 additions & 3 deletions internal/usecase/devices/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
ErrCancelled = dto.CanceledError{Console: ErrDeviceUseCase}
)

const deviceNotFoundMessage = "device not found"

// History - getting translate history from store.
func (uc *UseCase) GetCount(ctx context.Context, tenantID string) (int, error) {
count, err := uc.repo.GetCount(ctx, tenantID)
Expand Down Expand Up @@ -83,7 +85,7 @@ func (uc *UseCase) GetByID(ctx context.Context, guid, tenantID string, includeSe
}

if data == nil || data.GUID == "" {
return nil, ErrNotFound
return nil, ErrNotFound.WrapWithMessage("GetByID", "uc.repo.GetByID", deviceNotFoundMessage)
}

d2, err := uc.entityToDTO(data)
Expand Down Expand Up @@ -192,7 +194,7 @@ func (uc *UseCase) Delete(ctx context.Context, guid, tenantID string) error {
}

if !isSuccessful {
return ErrNotFound
return ErrNotFound.WrapWithMessage("Delete", "uc.repo.Delete", deviceNotFoundMessage)
}

return nil
Expand Down Expand Up @@ -222,7 +224,7 @@ func (uc *UseCase) Update(ctx context.Context, d *dto.Device, fields map[string]
}

if !updated {
return nil, ErrNotFound.Wrap("Update", "uc.repo.Update", nil)
return nil, ErrNotFound.WrapWithMessage("Update", "uc.repo.Update", deviceNotFoundMessage)
}

updateDevice, err := uc.repo.GetByID(ctx, d1.GUID, d1.TenantID)
Expand Down
Loading
Loading