Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronMcCaughan/b590ef068d45b055b49be778ac0e0489 to your computer and use it in GitHub Desktop.
Save AaronMcCaughan/b590ef068d45b055b49be778ac0e0489 to your computer and use it in GitHub Desktop.
Connect to Plutora using parameters entered in the UI and GET a list of Releases
/*
This script will connect to Plutora using Oauth 2.0 Authentication and retrieve a list of Releases using an API request.
It will then console.log the response to the execution log in Plutora.
This script uses credentials that are defined in Plutora UI as Job Parameters. Refer to the knowledge base in Plutora for more information on adding parameters
To execute this script on your own instance of Plutora, create a new Job, upload the script and define the 'Credentials' in the Plutora UI as parameters.
The parameters should be named exactly as they are named in the script. For example, 'args.arguments.oauthurl' should a parameter call 'oauthurl' in the UI
*/
/********************************************************
*************STEP 1: Import npm's************************
********************************************************/
const https = require("https");
var querystring = require('querystring');
/********************************************************
*******STEP 2: Define you API and SSH parameters*********
********************************************************/
const params = {
auth: {
server: {
hostname: '',
},
client_id: '',
client_secret: '',
grant_type: '',
username: '',
password: '',
},
api: {
server: {
hostname: '',
},
pagination: {
pagenum: 0,
limit: 100
}
},
};
/********************************************************
****STEP 3: Create an API Request function********
********************************************************/
const makeRequest = function (options, payload = '') {
return new Promise(function (resolve, reject) {
let req = https.request(options, (res) => {
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(body);
}
else {
reject(body);
}
});
});
console.log('Request has been made for ' + JSON.stringify(options));
req.on('error', (e) => {
console.error(e.message);
reject(e);
});
req.write(payload);
req.end();
console.log('Request made...');
});
};
/********************************************************
****STEP 4: Define all the API requests required ********
********************************************************/
//Get Plutora oauth token
const getAuthToken = function (authParams) {
const postData = `client_id=${authParams.client_id}&client_secret=${authParams.client_secret}&grant_type=${authParams.grant_type}&username=${authParams.username}&password=${authParams.password}`
const options = {
hostname: authParams.server.hostname,
path: '/oauth/token',
method: 'POST',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
return makeRequest(options, postData).then((data) => {
return JSON.parse(data);
});
};
//Get the list of existing Release
const getReleases = function (apiParams) {
const options = {
hostname: apiParams.server.hostname,
path: `/Releases?pagenum=${apiParams.pagination.pagenum}&limit=${apiParams.pagination.limit}`,
method: 'GET',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiParams.auth_token
}
};
return makeRequest(options).then((data) => {
const releases = JSON.parse(data);
return releases.resultSet.reduce(function (map, obj) {
map[obj.identifier] = obj;
return map;
}, {});
});
};
/********************************************************
********STEP 5: Define the main run function ************
********************************************************/
const run = async function (args) {
const params = {
auth: {
server: {
hostname: args.arguments.oauthurl,
},
client_id: args.arguments.clientId,
client_secret: args.arguments.clientSecret,
grant_type: 'password',
username: args.arguments.username,
password: args.arguments.password,
},
api: {
server: {
hostname: args.arguments.apiUrl,
},
pagination: {
pagenum: 0,
limit: 100
}
},
};
return new Promise(async (resolve, reject) => {
console.log(`retrieving auth token.`);
const authToken = await getAuthToken(params.auth);
const authApiParams = {...params.api, ...{auth_token: authToken.access_token}};
console.log(`loading releases.`);
const existingReleases = await getReleases(authApiParams);
console.log(JSON.stringify(existingReleases));
resolve();
});
};
module.exports = {
run: run
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment