-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3157 from FlowFuse/2997-device-groups-backend
Implement API for Device Groups
- Loading branch information
Showing
63 changed files
with
4,358 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* eslint-disable no-unused-vars */ | ||
/** | ||
* Add DeviceGroups table that has an FK association with Applications and Devices | ||
*/ | ||
|
||
const { Sequelize, QueryInterface } = require('sequelize') | ||
|
||
module.exports = { | ||
/** | ||
* upgrade database | ||
* @param {QueryInterface} context QueryInterface | ||
*/ | ||
up: async (context) => { | ||
await context.createTable('DeviceGroups', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING(255), | ||
allowNull: false | ||
}, | ||
description: { | ||
type: Sequelize.TEXT | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
ApplicationId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Applications', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
} | ||
}) | ||
|
||
// add a column to Devices table that references DeviceGroups | ||
await context.addColumn('Devices', 'DeviceGroupId', { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'DeviceGroups', | ||
key: 'id' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL' | ||
}) | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* A DeviceGroup. | ||
* A logical grouping of devices for the primary intent of group deployments in the pipeline stages. | ||
* @namespace forge.db.models.DeviceGroup | ||
*/ | ||
|
||
const { DataTypes, literal } = require('sequelize') | ||
|
||
const { buildPaginationSearchClause } = require('../utils') | ||
const nameValidator = { msg: 'Device Group name cannot be empty' } | ||
|
||
module.exports = { | ||
name: 'DeviceGroup', | ||
schema: { | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
validate: { | ||
notEmpty: nameValidator, | ||
notNull: nameValidator | ||
} | ||
}, | ||
description: { type: DataTypes.TEXT } | ||
}, | ||
associations: function (M) { | ||
this.belongsTo(M.Application, { onDelete: 'CASCADE' }) | ||
this.hasMany(M.Device) | ||
}, | ||
finders: function (M) { | ||
const self = this | ||
const deviceCountLiteral = literal(`( | ||
SELECT COUNT(*) | ||
FROM "Devices" AS "device" | ||
WHERE | ||
"device"."DeviceGroupId" = "DeviceGroup"."id" | ||
AND | ||
"device"."ApplicationId" = "DeviceGroup"."ApplicationId" | ||
)`) | ||
return { | ||
static: { | ||
byId: async function (id) { | ||
if (typeof id === 'string') { | ||
id = M.DeviceGroup.decodeHashid(id) | ||
} | ||
// find one, include application, devices and device count | ||
return self.findOne({ | ||
where: { id }, | ||
include: [ | ||
{ model: M.Application, attributes: ['hashid', 'id', 'name', 'TeamId'] }, | ||
{ | ||
model: M.Device, | ||
attributes: ['hashid', 'id', 'name', 'type', 'TeamId', 'ApplicationId', 'ProjectId', 'ownerType'], | ||
where: { | ||
ApplicationId: literal('"Devices"."ApplicationId" = "Application"."id"') | ||
}, | ||
required: false | ||
} | ||
], | ||
attributes: { | ||
include: [ | ||
[ | ||
deviceCountLiteral, | ||
'deviceCount' | ||
] | ||
] | ||
} | ||
}) | ||
}, | ||
getAll: async (pagination = {}, where = {}) => { | ||
const limit = parseInt(pagination.limit) || 1000 | ||
if (pagination.cursor) { | ||
pagination.cursor = M.DeviceGroup.decodeHashid(pagination.cursor) | ||
} | ||
if (where.ApplicationId && typeof where.ApplicationId === 'string') { | ||
where.ApplicationId = M.Application.decodeHashid(where.ApplicationId) | ||
} | ||
const [rows, count] = await Promise.all([ | ||
this.findAll({ | ||
where: buildPaginationSearchClause(pagination, where, ['DeviceGroup.name', 'DeviceGroup.description']), | ||
attributes: { | ||
include: [ | ||
[ | ||
deviceCountLiteral, | ||
'deviceCount' | ||
] | ||
] | ||
}, | ||
order: [['id', 'ASC']], | ||
limit | ||
}), | ||
this.count({ where }) | ||
]) | ||
return { | ||
meta: { | ||
next_cursor: rows.length === limit ? rows[rows.length - 1].hashid : undefined | ||
}, | ||
count, | ||
groups: rows | ||
} | ||
} | ||
}, | ||
instance: { | ||
deviceCount: async function () { | ||
return await M.Device.count({ where: { DeviceGroupId: this.id, ApplicationId: this.ApplicationId } }) | ||
}, | ||
getDevices: async function () { | ||
return await M.Device.findAll({ | ||
where: { | ||
DeviceGroupId: this.id, | ||
ApplicationId: this.ApplicationId | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.