Hi,
I am using node.js to fetch keys from a vault and I need to use those keys in the subsequent policies.
Earlier I was fetching only one key/value pair from vault like below and it was giving me its value directly in the javascript code.
orgVault.get('key',function(err, securevalue) {
apigee.setVariable(req,"var_secretvalue",securevalue);
});
Now I am trying to fetch more number of key/value pairs and calling same get function number of times and setting those values to different variables like above.
I can see that the callback function returns the values properly and those are printed in node.js logs(and also observed that they will not be shown in the trace logs immediatedly like it happens for other variables created by different policies)
But when I am trying to use these variables in a service callout policy I am getting 'unresolved variable error'.
I also tried fetching these variables in a javascript(immediately after the node call execution) and setting these to different variables from the script but that is also not working.
I read on the page http://docs.apigee.com/api-services/content/access-flow-variables-nodejs the we need to set the variable in the request object only and by using the setVariable method like I used above.
Can somebody please help and let me know what am I missing here?
Thanks,
Santosh
Solved! Go to Solution.
Hi,
I got this resolved by putting the statement resp.end('') in the last get call of the code.
Earlier I had put this at the end of createServer() method.
Thanks @rakshith for the pointer here.
Regards,
Santosh
Hi,
I nested the subsequest org.getVault calls inside the callback of first and below code is working for me as a solution here.
var http = require('http');
var apigee = require('apigee-access');
var svr = http.createServer(function(req, resp) {
var orgVault = apigee.getVault('SampleVault', 'environment');
//Call to get the HostURL orgVault.get('HostURL',function(err, HostURL) {
if(err){ resp.end('Failed to get HostURL');
} else {
apigee.setVariable(req,"HostURL",HostURL);
console.log('HostURL',HostURL);
// Next call to get the TokenURL
orgVault.get('TokenURL',function(err, TokenURL) {
if(err){
resp.end('Failed to get TokenURL');
} else {
apigee.setVariable(req,"TokenURL",TokenURL);
console.log('TokenURL',TokenURL);
// Next call to get the CallURL
orgVault.get('CallURL',function(err, CallURL) {
if(err){
resp.end('Failed to get CallURL');
} else {
apigee.setVariable(req,"CallURL",CallURL);
console.log('CallURL',CallURL);
// Next call to get the Username
orgVault.get('Username',function(err, Username) {
if(err){
resp.end('Failed to get Username');
} else {
apigee.setVariable(req,"Username",Username);
console.log('Username',Username);
// Next call to get the Password
orgVault.get('Password',function(err, Password) {
if(err){
resp.end('Failed to get Password');
} else {
apigee.setVariable(req,"Password",Password);
console.log('Password',Password);
resp.end('All Calls were successful');
}
});
}
});
}
});
}
}):
}
});
});
svr.listen(9000, function() {
console.log('The server is listening on port 9000');
});
This is because there is no other logic in node.js than callback to check if the call got completed or not.
Using 'serial' execution model of express will be a overkill here as we are not doing anything but fetching values from vault.
Please let me know if this works fine or need any modifications/changes to make it better.
Thanks,
Santosh