- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2017 11:47 AM
Hello fellow developers.
I'm fairly new and dont have much REST or Web Services experience. But i think after all the hours I spent i'm very close so i just need a little bit of help.
------------BACKGROUND-----------------
We are integrating with Foglight. Foglight uses Rest to create a ServiceNow Incident. They populate two additional fields in the Incident
u_reference : which contains an 'alarmID'
u_foglight_alert_ server: which contains the target server of the alarm.
They way their API works, you have to run a POST login command to get a 'Auth-Token' using basic authentication. Then you have to take that token you get, and pass it in a "Get" (along with the alarmID and alert server) to clear the alarm. This Token only lasts about 10 mins, so you have to generate a new token before every time you want to run a "GET"...(seems really dumb way of doing things to me, but that's how they say I have to do it)
I created both end points and have tested successfully using the "test" ink and the "Value Substitutions" list.
I run the post, and get the 'token' back.
I then take that token, put is as a variable substitution in the GET endpoint, and get a successful message back.
All is confirmed as working
-------MY SOLUTION-------------------
I needed to trigger this 'clear alarm' rest call whenever these incidents were put in a resolved state..To accomplish this, I created a business rule that triggers when these particular incidents are moved to resolve state. Then got the code for the "POST" and "GET using the "preview Script Usage" link on each rest call respectively to put in to my business rule.
- I then put POST message first (passing the foglight_alert_server) to log in and get a token
- I put a "JSON.parse(responceBody) to get the 'token' value out of the JSON string and assigned that a variable
- I then put the GET message after that and set the alarmID, serverhost, and token that i got from the POST
The response of the raw post rest message looks something like this:
{"status":1,"data":{"user":{"id":"8e94015b-8ec0-46d6-aff3-c098dbfd77f5","lastLogonTS":1504196220230,"name":"foglight_srvnow","groups":["Foglight Operators","Foglight Users","Foglight Administrators","Cartridge Developers"],"roles":["Operator","Dashboard User","VMware QuickView User","Support","VMware Automation User","General Access","Core Reports","Advanced Operator","Console User","Dashboard Designer","VMware Administrator","Cartridge Developer","Command Line Access","VMware Report User","Anybody","Capacity Management Administrator","Chargeback Administrator","Administrator","API Access","Report Manager"]},"token":"jjhju8e0sfjv9basa7rkknpqlt"}}
---------ISSUE --------------
When I put the incident in "resolved" state, i know the BR is running because my note (that is the last commend in the BR) is added to the worknotes that shows "Sent clear request to Foglight for server: '+ serverName1 + ' & Alarm ID: ' + alarm;" where serverName1 and alarm are populated with those fields form the incident.
We are however seeing a 401 error on the Foglight system for the GET which is an authentication issue. This leads me to believe that the token isn't getting passed.
---------MY QUESTION---------------
Is it possible to pass the variable "authCode" that i created from the POST, in to the setString Parameter of the GET as I am attempting to do or should I be doing this differently?
Here is my business rule
try {
var serverName = current.u_foglight_alert_server;
var r = new sn_ws.RESTMessageV2('Foglight Integration', 'post');
r.setStringParameter('serverhost', serverName);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var getAuth = JSON.parse(responseBody);
var authCode = getAuth.token;
}
catch(ex) {
var message = ex.getMessage();
}
try {
var r = new sn_ws.RESTMessageV2('Foglight Integration', 'get');
var alarm = current.u_reference;
var serverName1 = current.u_foglight_alert_server;
r.setStringParameter('alarmID', alarm);
r.setStringParameter('serverhost', serverName1);
r.setStringParameter('token', authCode);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
current.work_notes ='Sent clear request to Foglight for server: '+ serverName1 + ' & Alarm ID: ' + alarm;
current.update();
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2017 06:01 AM
Ok. I figured it out. My code works perfectly....the issue is that the JSON data was structure such that it was an object inside an object so when i would try to parse, it couldn't find it. It wasn't until i put my JSON in to a JSON "Format" that i realized the element i was looking for was inside another element, so I had to change my line 11 to be:
var authCode = getAuth.data.token;
Once i did that, it found it just fine, and passed it to the next rest call just fine.
Thanks Michael for pointing out the obvious that i totally missed and that was to make sure the value was even being captured. Troubleshooting 101. duh...
Cheers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2017 12:08 PM
I would suggest putting in the following code after line 11 of your script to make sure you are getting the token back:
gs.info("token: " + authCode + " httpStatus: " + httpStatus);
Then navigate to System Logs\System Log\All to get the values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2017 02:31 PM
Good call... I'll test that. but there is no reason that i can't pass that to the GET statement if it does infact have a value, right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2017 06:01 AM
Ok. I figured it out. My code works perfectly....the issue is that the JSON data was structure such that it was an object inside an object so when i would try to parse, it couldn't find it. It wasn't until i put my JSON in to a JSON "Format" that i realized the element i was looking for was inside another element, so I had to change my line 11 to be:
var authCode = getAuth.data.token;
Once i did that, it found it just fine, and passed it to the next rest call just fine.
Thanks Michael for pointing out the obvious that i totally missed and that was to make sure the value was even being captured. Troubleshooting 101. duh...
Cheers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2017 06:28 AM
Sorry for not replying sooner. Awesome happy you figured it out. Yes the structure can bite you so I usually use PostMan or something to see the data first and then build my code around it afterwards.