Skip to content

Commit

Permalink
Merge pull request #5081 from FlowFuse/fix-db-views-add-status
Browse files Browse the repository at this point in the history
Add 3rd Party broker endpoint
  • Loading branch information
hardillb authored Feb 7, 2025
2 parents 7ce94e9 + a215e0f commit 00be7f6
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 60 deletions.
66 changes: 38 additions & 28 deletions forge/db/views/BrokerCredentials.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
module.exports = {
credentials: function (app, credentials) {
const filtered = []
credentials.forEach(c => {
filtered.push(this.clean(app, c))
})
return filtered
},
clean: function (app, cred) {
const result = cred.toJSON()
const cleaned = {
id: result.hashid,
name: result.name,
host: result.host,
port: result.port,
protocol: result.protocol,
ssl: result.ssl,
verifySSL: result.verifySSL,
clientId: result.clientId
module.exports = function (app) {
app.addSchema({
$id: 'MQTTBroker',
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
host: { type: 'string' },
port: { type: 'number' },
protocol: { type: 'string' },
ssl: { type: 'boolean' },
verifySSL: { type: 'boolean' },
clientId: { type: 'string' }
},
additionalProperties: true
})
return {
clean: function (cred) {
const result = cred.toJSON()
const cleaned = {
id: result.hashid,
name: result.name,
host: result.host,
port: result.port,
protocol: result.protocol,
ssl: result.ssl,
verifySSL: result.verifySSL,
clientId: result.clientId
}
return cleaned
},
cleanList: function (list) {
const filtered = []
list.brokers.forEach(u => {
filtered.push(this.clean(u))
})
list.brokers = filtered
return list
}
return cleaned
},
cleanList: function (app, list) {
const filtered = []
list.brokers.forEach(u => {
filtered.push(this.clean(app, u))
})
list.brokers = filtered
return list
}
}
88 changes: 56 additions & 32 deletions forge/ee/routes/teamBroker/3rdPartyBroker.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ module.exports = async function (app) {
200: {
type: 'object',
properties: {

meta: { $ref: 'PaginationMeta' },
count: { type: 'number' },
brokers: {
type: 'array',
items: {
$ref: 'MQTTBroker'
}
}
},
additionalProperties: true
},
Expand Down Expand Up @@ -98,28 +105,11 @@ module.exports = async function (app) {
}
},
body: {
type: 'object',
properties: {
name: { type: 'string' },
host: { type: 'string' },
port: { type: 'number' },
protocol: { type: 'string' },
protocolVersion: { type: 'number' },
ssl: { type: 'boolean' },
verifySSL: { type: 'boolean' },
clientId: { type: 'string' },
credentials: {
type: 'object'
}
}
$ref: 'MQTTBroker'
},
response: {
201: {
type: 'object',
properties: {

},
additionalProperties: true
$ref: 'MQTTBroker'
},
'4xx': {
$ref: 'APIError'
Expand Down Expand Up @@ -190,11 +180,7 @@ module.exports = async function (app) {
},
response: {
200: {
type: 'object',
properties: {

},
additionalProperties: true
$ref: 'MQTTBroker'
},
'4xx': {
$ref: 'APIError'
Expand Down Expand Up @@ -262,11 +248,7 @@ module.exports = async function (app) {
},
response: {
200: {
type: 'object',
properties: {

},
additionalProperties: true
$ref: 'MQTTBroker'
},
'4xx': {
$ref: 'APIError'
Expand All @@ -293,6 +275,48 @@ module.exports = async function (app) {
}
})

/**
* Get details and status of a 3rd Party Broker
*/
app.get('/:brokerId', {
preHandler: app.needsPermission('broker:credentials:list'),
schema: {
summary: 'Get 3rd Party Broker details and status',
tags: ['MQTT Broker'],
params: {
type: 'object',
properties: {
teamId: { type: 'string' },
brokerId: { type: 'string' }
}
},
response: {
200: {
$ref: 'MQTTBroker'
},
'4xx': {
$ref: 'APIError'
},
500: {
$ref: 'APIError'
}
}
}
}, async (request, reply) => {
if (request.params.brokerId !== 'team-broker') {
try {
const state = await app.containers.getBrokerAgentState(request.broker)
const clean = app.db.views.BrokerCredentials.clean(request.broker)
clean.state = state
reply.send(clean)
} catch (err) {
reply.status(500).send({ error: 'unknown_error', message: err.toString() })
}
} else {
reply.status(40).send({ error: 'not_supported', message: 'not supported' })
}
})

/**
* Remove 3rd Party Broker credentials
* @name /api/v1/teams/:teamId/brokers/:brokerId
Expand Down Expand Up @@ -336,10 +360,10 @@ module.exports = async function (app) {
await request.broker.destroy()
reply.send({})
} catch (err) {
reply.status(500).send({ error: 'unknown_erorr', message: err.toString() })
reply.status(500).send({ error: 'unknown_error', message: err.toString() })
}
} else {
reply.status(404).send({})
reply.status(404).send({ error: 'not_found', message: 'not found' })
}
})

Expand Down

0 comments on commit 00be7f6

Please sign in to comment.