Skip to content

Commit

Permalink
fix: fixed mongo device deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbrache committed Oct 12, 2023
1 parent 1b30f38 commit d011490
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 38 deletions.
1 change: 1 addition & 0 deletions data/initMPS.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS devices(
tenantid varchar(36) NOT NULL,
friendlyname varchar(256),
dnssuffix varchar(256),
deviceinfo JSON,
CONSTRAINT device_guid UNIQUE(guid),
PRIMARY KEY (guid,tenantid)
);
4 changes: 2 additions & 2 deletions src/data/mongo/collections/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ describe('MongoDeviceTable', () => {
it('should delete document by name and tenantId', async () => {
collection.deleteOne.mockResolvedValue({ deletedCount: 1 } as any)

const result = await mongoDeviceTable.delete('someName', 'someTenantId')
const result = await mongoDeviceTable.delete('someGuid', 'someTenantId')

expect(result).toBe(true)
expect(collection.deleteOne).toHaveBeenCalledWith({ friendlyName: 'someName', tenantId: 'someTenantId' })
expect(collection.deleteOne).toHaveBeenCalledWith({ guid: 'someGuid', tenantId: 'someTenantId' })
})

it('should insert a device', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/data/mongo/collections/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class MongoDeviceTable implements IDeviceTable {
return this.collection.findOne({ guid: id, tenantId }) as unknown as WithId<Device>
}

async delete (name: string, tenantId: string = ''): Promise<boolean> {
const result = await this.collection.deleteOne({ friendlyName: name, tenantId })
async delete (id: string, tenantId: string = ''): Promise<boolean> {
const result = await this.collection.deleteOne({ guid: id, tenantId })
return result.deletedCount && result.deletedCount > 0
}

Expand Down
106 changes: 86 additions & 20 deletions src/data/postgres/tables/device.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tenantid = $3
ORDER BY guid
Expand Down Expand Up @@ -88,7 +89,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tenantid = $3
ORDER BY guid
Expand All @@ -111,7 +113,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE guid = $1`, ['4c4c4544-004b-4210-8033-b6c04f504633'])
})
Expand Down Expand Up @@ -161,7 +164,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE guid = $1 and tenantid = $2`, ['4c4c4544-004b-4210-8033-b6c04f504633', 'tenantId'])
})
Expand Down Expand Up @@ -190,7 +194,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE hostname = $1 and tenantid = $2`, ['hostname', 'tenantId'])
})
Expand All @@ -210,7 +215,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE hostname = $1 and tenantid = $2`, ['hostname', ''])
})
Expand Down Expand Up @@ -264,7 +270,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tags @> $1 and tenantId = $4
ORDER BY guid
Expand Down Expand Up @@ -295,7 +302,8 @@ describe('device tests', () => {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tags && $1 and tenantId = $4
ORDER BY guid
Expand Down Expand Up @@ -336,15 +344,23 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtBuild: '1111',
amtSku: '16392',
amtVersion: '16.1',
currentMode: '0',
dnsSuffixOs: '',
ipAddress: ''
}
}
querySpy.mockResolvedValueOnce({ rows: [{ device }], command: '', fields: null, rowCount: 1, oid: 0 })
getById.mockResolvedValueOnce(device)
const result = await deviceTable.insert(device)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(`
INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix)
values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9)`,
INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix, deviceinfo)
values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9, $10)`,
[
device.guid,
device.hostname,
Expand All @@ -354,7 +370,8 @@ describe('device tests', () => {
device.mpsusername,
device.tenantId,
device.friendlyName,
device.dnsSuffix
device.dnsSuffix,
JSON.stringify(device.deviceInfo)
])
expect(getById).toBeCalledTimes(1)
expect(result).toBe(device)
Expand All @@ -370,7 +387,15 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockResolvedValueOnce({ rows: [], command: '', fields: null, rowCount: 0, oid: 0 })
const result = await deviceTable.insert(device)
Expand All @@ -388,7 +413,15 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockRejectedValueOnce(() => {
throw new Error()
Expand All @@ -412,7 +445,15 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockRejectedValueOnce({ code: '23505' })
try {
Expand All @@ -434,15 +475,23 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockResolvedValueOnce({ rows: [{ device }], command: '', fields: null, rowCount: 1, oid: 0 })
getById.mockResolvedValueOnce(device)
const result = await deviceTable.update(device)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(`
UPDATE devices
SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9
SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9, deviceinfo=$10
WHERE guid=$1 and tenantid = $7`,
[
device.guid,
Expand All @@ -453,7 +502,8 @@ describe('device tests', () => {
device.mpsusername,
device.tenantId,
device.friendlyName,
device.dnsSuffix
device.dnsSuffix,
JSON.stringify(device.deviceInfo)
])
expect(getById).toBeCalledTimes(1)
expect(result).toBe(device)
Expand All @@ -470,7 +520,15 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockResolvedValueOnce({ rows: [], command: '', fields: null, rowCount: 0, oid: 0 })
try {
Expand All @@ -492,7 +550,15 @@ describe('device tests', () => {
mpsusername: 'admin',
tenantId: null,
friendlyName: null,
dnsSuffix: null
dnsSuffix: null,
deviceInfo: {
amtVersion: '16.1',
amtBuild: '1111',
amtSku: '16392',
currentMode: '0',
ipAddress: '',
dnsSuffixOs: ''
}
}
querySpy.mockRejectedValueOnce(() => {
throw new Error()
Expand Down
30 changes: 19 additions & 11 deletions src/data/postgres/tables/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tenantid = $3
ORDER BY guid
Expand Down Expand Up @@ -82,7 +83,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE guid = $1 and tenantid = $2`
let params = [id, tenantId]
Expand All @@ -96,7 +98,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE guid = $1`
params = [id]
Expand All @@ -116,7 +119,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE ${columnName} = $1 and tenantid = $2`, [queryValue, tenantId])

Expand Down Expand Up @@ -144,7 +148,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tags @> $1 and tenantId = $4
ORDER BY guid
Expand All @@ -160,7 +165,8 @@ export class DeviceTable implements IDeviceTable {
mpsusername as "mpsusername",
tenantid as "tenantId",
friendlyname as "friendlyName",
dnssuffix as "dnsSuffix"
dnssuffix as "dnsSuffix",
deviceinfo as "deviceInfo"
FROM devices
WHERE tags && $1 and tenantId = $4
ORDER BY guid
Expand All @@ -185,8 +191,8 @@ export class DeviceTable implements IDeviceTable {
async insert (device: Device): Promise<Device> {
try {
const results = await this.db.query(`
INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix)
values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9)`,
INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix, deviceinfo)
values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9, $10)`,
[
device.guid,
device.hostname,
Expand All @@ -196,7 +202,8 @@ export class DeviceTable implements IDeviceTable {
device.mpsusername,
device.tenantId,
device.friendlyName,
device.dnsSuffix
device.dnsSuffix,
JSON.stringify(device.deviceInfo)
])
if (results.rowCount > 0) {
return await this.getById(device.guid)
Expand All @@ -220,7 +227,7 @@ export class DeviceTable implements IDeviceTable {
try {
const results = await this.db.query(`
UPDATE devices
SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9
SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9, deviceinfo=$10
WHERE guid=$1 and tenantid = $7`,
[
device.guid,
Expand All @@ -231,7 +238,8 @@ export class DeviceTable implements IDeviceTable {
device.mpsusername,
device.tenantId,
device.friendlyName,
device.dnsSuffix
device.dnsSuffix,
JSON.stringify(device.deviceInfo)
])
if (results.rowCount > 0) {
return await this.getById(device.guid)
Expand Down
9 changes: 9 additions & 0 deletions src/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export interface Device {
tenantId: string
friendlyName: string
dnsSuffix: string
deviceInfo: DeviceInfo
}
export interface DeviceInfo {
amtVersion: string
amtBuild: string
amtSku: string
currentMode: string
ipAddress: string
dnsSuffixOs: string
}
export type Credentials = Record<string, AMTCredential>
export interface AMTCredential {
Expand Down
Loading

0 comments on commit d011490

Please sign in to comment.