UNPKG

node-red-contrib-powerbi

Version:

A PowerBI API interaction node-red module which allows to interact with PowerBI service.

442 lines (371 loc) 14.3 kB
var request = require('request'); module.exports = function(RED) { function PowerBIGateways(config) { RED.nodes.createNode(this,config); // Retrieve the connection node. this.connection = RED.nodes.getNode(config.connection); var node = this; function IsInvalidParameter(p) { return (p === undefined || p.id === undefined || p.id.length === 0); } node.on('input', function(msg) { function ProcessResponse(error, response, body) { msg.status = response.statusCode; msg.headers = response.headers; msg.payload = body; response !== undefined && response.statusCode !== undefined && response.statusCode === 200 ? node.send(msg) : node.send([null, msg]); } switch (config.operation) { case "setcredentials": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource id"; node.send([null, msg]); return; } if (msg.datasource.username === undefined || msg.datasource.username.length === 0) { msg.payload = "Missing target datasouce username"; node.send([null, msg]); return; } if (msg.datasource.password === undefined || msg.datasource.password.length === 0) { msg.payload = "Missing target dataset password"; node.send([null, msg]); return; } request({ json: true, method:'PATCH', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id, body: { "credentialType": "Basic", "basicCredentials": { "username": msg.datasource.username, "password": msg.datasource.password } }, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getgateways": request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways', headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getgateway": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getdatasource": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target gateway datasource id"; node.send([null, msg]); return; } request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getdatasources": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/dataSources', headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getdatasourcestatus": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target gateway datasource id"; node.send([null, msg]); return; } request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id + '/status', headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "getdatasourceusers": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target gateway datasource id"; node.send([null, msg]); return; } request({ method:'GET', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id + '/users', headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "createdatasource": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource name"; node.send([null, msg]); return; } if (msg.datasource.connectionDetails === undefined || msg.datasource.connectionDetails.server === undefined || msg.datasource.connectionDetails.server.length === 0) { msg.payload = "Missing target datasouce server"; node.send([null, msg]); return; } if (msg.datasource.connectionDetails === undefined || msg.datasource.connectionDetails.database === undefined || msg.datasource.connectionDetails.database.length === 0) { msg.payload = "Missing target datasouce database"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.credentials === undefined || msg.datasource.credentialDetails.credentials.length === 0) { msg.payload = "Missing target datasouce credentials"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.encryptionAlgorithm === undefined || msg.datasource.credentialDetails.encryptionAlgorithm.length === 0) { msg.payload = "Missing target datasouce encryptionAlgorithm"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.encryptionConnection === undefined || msg.datasource.credentialDetails.encryptionConnection.length === 0) { msg.payload = "Missing target datasouce encryptionConnection"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.privacyLevel === undefined || msg.datasource.credentialDetails.privacyLevel.length === 0) { msg.payload = "Missing target datasouce privacyLevel"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.credentialType === undefined || msg.datasource.credentialDetails.credentialType.length === 0) { msg.payload = "Missing target datasouce credentialType"; node.send([null, msg]); return; } request({ json: true, method:'POST', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources', body: { "name": msg.datasource.name, "connectionDetails": { "server": msg.datasource.connectionDetails.server, "database": msg.datasource.connectionDetails.database }, "credentialDetails": { "credentials": msg.datasource.credentialDetails.credentials, "encryptionAlgorithm": "RSA-OAEP", "encryptionConnection": msg.datasource.credentialDetails.encryptionConnection, // Encrypted|NotEncrypted "privacyLevel": msg.datasource.credentialDetails.privacyLevel, // None|Public|Organizational|Private "credentialType": msg.datasource.credentialDetails.credentialType // Basic|Windows|Anonymous|... } }, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "patchdatasource": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource id"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.credentials === undefined || msg.datasource.credentialDetails.credentials.length === 0) { msg.payload = "Missing target datasouce credentials"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.encryptionAlgorithm === undefined || msg.datasource.credentialDetails.encryptionAlgorithm.length === 0) { msg.payload = "Missing target datasouce encryptionAlgorithm"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.encryptionConnection === undefined || msg.datasource.credentialDetails.encryptionConnection.length === 0) { msg.payload = "Missing target datasouce encryptionConnection"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.privacyLevel === undefined || msg.datasource.credentialDetails.privacyLevel.length === 0) { msg.payload = "Missing target datasouce privacyLevel"; node.send([null, msg]); return; } if (msg.datasource.credentialDetails === undefined || msg.datasource.credentialDetails.credentialType === undefined || msg.datasource.credentialDetails.credentialType.length === 0) { msg.payload = "Missing target datasouce credentialType"; node.send([null, msg]); return; } request({ json: true, method:'PATCH', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id, body: { "credentialDetails": { "credentials": msg.datasource.credentialDetails.credentials, "encryptionAlgorithm": "RSA-OAEP", "encryptionConnection": msg.datasource.credentialDetails.encryptionConnection, // Encrypted|NotEncrypted "privacyLevel": msg.datasource.credentialDetails.privacyLevel, // None|Public|Organizational|Private "credentialType": msg.datasource.credentialDetails.credentialType // Basic|Windows|Anonymous|... } }, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "adddatasourceuser": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource id"; node.send([null, msg]); return; } if (msg.datasource.user === undefined || msg.datasource.user.emailAddress === undefined || msg.datasource.user.emailAddress.length === 0) { msg.payload = "Missing target datasouce user emailAddress"; node.send([null, msg]); return; } request({ json: true, method:'POST', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id + '/users', body: { "accessRight": "Read", "emailAddress": msg.datasource.user.emailAddress }, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "deletedatasourceuser": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource id"; node.send([null, msg]); return; } if (msg.datasource.user === undefined || msg.datasource.user.emailAddress === undefined || msg.datasource.user.emailAddress.lenght === 0) { msg.payload = "Missing target datasouce user emailAddress"; node.send([null, msg]); return; } request({ method:'DELETE', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id + '/users/' + msg.datasource.user.emailAddress, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; case "deletedatasource": if (IsInvalidParameter(msg.gateway)) { msg.payload = "Missing target gateway id"; node.send([null, msg]); return; } if (IsInvalidParameter(msg.datasource)) { msg.payload = "Missing target datasource id"; node.send([null, msg]); return; } request({ method:'DELETE', url: 'https://api.powerbi.com/v1.0/myorg/gateways/' + msg.gateway.id + '/datasources/' + msg.datasource.id, headers: { 'Authorization': 'Bearer ' + node.connection.accesstoken }}, ProcessResponse); break; }; }); } RED.nodes.registerType('powerbi-gateways', PowerBIGateways); }