Forum Discussion
7 years ago
@Michael Rodrigues to give you an example, I copied the PURE datasource groovy script (as I have to cache a cookie). It's the hostname verification that seems to be the issue and Viptela is a little weird in their self signed certs, having one of our engineers look at it and trying to figure out if we can sign it with the fqdn for the friendly name. It looks like the link you provided has an easier way to get the cookie data, so i'll look and rewrite this code below to follow those examples.
import groovy.json.JsonSlurper;
hostName = hostProps.get("system.hostname");
user = hostProps.get("viptela.user");
pass = hostProps.get("viptela.pass");
// init some stuff
base_url = "https://" + hostName;
slurper = new JsonSlurper()
cookie = getSession();
// now run a command to get info from the server
api_commmand = "/dataservice/alarms/count";
api_url = new URL(base_url + api_commmand);
connection = api_url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Cookie", cookie);
arrayLines = slurper.parseText(connection.content.text);
println "count:" + arrayLines.data.count[0];
println "cleared_count:" + arrayLines.data.cleared_count[0];
return(0);
/*
* getSession - get session cookie
*
* @return string cookie
*/
def getSession()
{
def cookie;
api_commmand = "/j_security_check";
api_url = new URL(base_url + api_commmand);
connection = api_url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// write out apitoken as a HTTP POST
def out = new OutputStreamWriter(connection.getOutputStream());
out.write('j_username=' + user + '&j_password=' + pass);
out.close();
// loop through http header fields
for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++)
{
// is this the cookie field?
if (headerName.equals("Set-Cookie"))
{
// yes. get the cookie data
cookie = connection.getHeaderField(i);
}
}
return(cookie);
}
Related Content
- 5 months ago
- 10 months ago
- 2 years ago
- 9 months ago