06-12-2016 04:27 AM - edited 03-01-2019 06:42 AM
Task Name | Example on how to execute XML for UCSM from UCSD |
Description | |
Prerequisites | Tested on UCSD 5.4.0.3 |
Category | Workflow |
Components | vSphere 5.x |
User Inputs |
Instructions for Regular Workflow Use:
Thank you goes out to Mark Smith.
The import:
The workflow:
The custom tasks:
UCS M Series login code:
Input variables:
Output variables:
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(java.util.regex);
importPackage(com.cloupia.lib.util);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.cookie);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.auth);
importPackage(org.apache.commons.httpclient.protocol);
importClass(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory);
var httpRequest = function () {};
httpRequest.prototype.setup = function(serverIp, transport, username, password) {
this.serverIp = serverIp;
this.transport = transport;
this.username = username;
this.password = password;
this.httpClient = new HttpClient();
// Decide whether to create an HTTP or HTTPS connection based up 'transport'.
if( this.transport == "https" ) {
this.factory = new SecureProtocolSocketFactory() {
createSocket : function(socket, host, port, autoClose) {
this.fact = new com.cloupia.lib.util.easytrust.EasySSLProtocolSocketFactory();
this.sock = this.fact.createSocket(socket, host, port, autoClose);
this.sock.setEnabledCipherSuites([
"SSL_RSA_WITH_RC4_128_MD5",
"SSL_RSA_WITH_RC4_128_SHA",
"TLS_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
"SSL_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",
"SSL_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_DSS_WITH_DES_CBC_SHA",
"SSL_RSA_EXPORT_WITH_RC4_40_MD5",
"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"]);
return this.sock;
}
};
this.easyhttps = new Protocol(
"https",
this.factory,
443);
this.httpClient.getHostConfiguration().setHost(this.serverIp,443,this.easyhttps);
} else {
// Create new HTTP connection.
this.httpClient.getHostConfiguration().setHost(this.serverIp, 80, "http");
}
this.httpClient.getParams().setCookiePolicy("default");
// If username and password supplied, then use basicAuth.
if( this.username && this.password ) {
this.httpClient.getParams().setAuthenticationPreemptive(true);
this.defaultcreds = new UsernamePasswordCredentials(this.username, this.password);
this.httpClient.getState().setCredentials(new AuthScope(this.serverIp, -1, null), this.defaultcreds);
}
};
httpRequest.prototype.contentType = function(contentType) {
this.contentType = contentType;
this.contentTypes = [
["xml","application/xml"],
["json","application/json"]
];
for( this.i=0; this.i<this.contentTypes.length; this.i++)
if(this.contentTypes[this.i][0] == this.contentType)
this.httpMethod.addRequestHeader("Content-Type", this.contentTypes[this.i][1]);
};
httpRequest.prototype.addHeader = function(headerName,headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
this.httpMethod.addRequestHeader(this.headerName, this.headerValue);
};
httpRequest.prototype.execute = function() {
// Connection:close is hard coded here in order to ensure that the TCP connection
// gets torn down immediately after the request. Comment this line out if you wish to
// experiment with HTTP persistence.
this.httpMethod.addRequestHeader("Connection", "close");
this.httpClient.executeMethod(this.httpMethod);
// Retrieve status code.
this.statusCode = this.httpMethod.getStatusCode();
return this.statusCode;
}
httpRequest.prototype.getRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new GetMethod(this.uri);
};
httpRequest.prototype.postRequest = function(uri,bodytext) {
this.uri = uri;
this.bodytext = bodytext;
logger.addInfo("uri: "+uri);
logger.addInfo("bodytext: "+bodytext);
// POST Request.
this.httpMethod = new PostMethod(this.uri);
this.httpMethod.setRequestEntity(new StringRequestEntity(this.bodytext));
};
httpRequest.prototype.getResponse = function(asType) {
this.asType = asType;
if( this.asType == "asStream" )
return this.httpMethod.getResponseBodyAsStream();
else{
return this.httpMethod.getResponseBodyAsString();
}
};
httpRequest.prototype.deleteRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new DeleteMethod(this.uri);
};
httpRequest.prototype.disconnect = function() {
// Release connection.
this.httpMethod.releaseConnection();
};
function getAttributeValueOfTagFromXML(xmlResponse,tagName,attributeName){
//var ptn = "<"+tagName+".*?"+attributeName+"=('.*?')(.*?>)";
var ptn = "<"+tagName+".*?"+attributeName+"=(.*?)(\\s+[a-zA-Z])";
var pattern = Pattern.compile(ptn);
var matcher=pattern.matcher(xmlResponse);
logger.addInfo("matcher: "+matcher);
if(matcher.find()){
var str = matcher.group(1);
var attributeValue = str. substring(1,str.length()-1);
// var attributeValue = matcher.group(1);
logger.addInfo(attributeName+":"+attributeValue);
return attributeValue;
}
logger.addInfo("No Match Found"+matcher);
return;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// main();
//
//
// Workflow Inputs.
//
var ibUser = input.UCSUser;
var ibPassword = input.UCSPassword;
var ibIP = input.UCSIP;
var request = new httpRequest();
request.setup(this.ibIP,"https",this.ibUser,this.ibPassword);
request.postRequest("/nuova/", "<aaaLogin inName='"+ibUser+"' inPassword='"+ibPassword+"'></aaaLogin>");
request.contentType("xml");
var statusCode = request.execute();
if (statusCode != 200)
{
logger.addError("Request failed. HTTP response code: "+statusCode);
var response = request.getResponse("asString");
logger.addInfo("Response: " +response);
request.disconnect();
// Set this task as failed.
ctxt.setFailed("Request failed.");
} else {
var response = request.getResponse("asString");
logger.addInfo("Response: " +response);
var tagName = "aaaLogin";
var attributeName = "outCookie";
var response2 = "<aaaLogin cookie='' response='yes' outCookie='1442183396/df8eaa3e-fc8c-4f9f-a6ad-a3ada03ae0b4' outRefreshPeriod='600' outPriv='admin,read-only' outDomains='' outChannel='noencssl' outEvtChannel='noencssl' outSessionId='web_65459_A' outVersion='2.2(3c)' outName='admin'> </aaaLogin>";
var attributeValue = getAttributeValueOfTagFromXML(response,tagName,attributeName);
logger.addInfo(attributeName+":"+attributeValue);
output.UCScookie = attributeValue;
request.disconnect();
}
Create M-Series Servise Profile:
Input:
Output:
The code (some things are hard coded (root-org) (marks-ls) ):
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(java.util.regex);
importPackage(com.cloupia.lib.util);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.cookie);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.auth);
importPackage(org.apache.commons.httpclient.protocol);
importClass(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory);
var httpRequest = function () {};
httpRequest.prototype.setup = function(serverIp, transport, username, password) {
this.serverIp = serverIp;
this.transport = transport;
this.username = username;
this.password = password;
this.httpClient = new HttpClient();
// Decide whether to create an HTTP or HTTPS connection based up 'transport'.
if( this.transport == "https" ) {
this.factory = new SecureProtocolSocketFactory() {
createSocket : function(socket, host, port, autoClose) {
this.fact = new com.cloupia.lib.util.easytrust.EasySSLProtocolSocketFactory();
this.sock = this.fact.createSocket(socket, host, port, autoClose);
this.sock.setEnabledCipherSuites([
"SSL_RSA_WITH_RC4_128_MD5",
"SSL_RSA_WITH_RC4_128_SHA",
"TLS_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
"SSL_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",
"SSL_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_DSS_WITH_DES_CBC_SHA",
"SSL_RSA_EXPORT_WITH_RC4_40_MD5",
"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"]);
return this.sock;
}
};
this.easyhttps = new Protocol(
"https",
this.factory,
443);
this.httpClient.getHostConfiguration().setHost(this.serverIp,443,this.easyhttps);
} else {
// Create new HTTP connection.
this.httpClient.getHostConfiguration().setHost(this.serverIp, 80, "http");
}
this.httpClient.getParams().setCookiePolicy("default");
// If username and password supplied, then use basicAuth.
if( this.username && this.password ) {
this.httpClient.getParams().setAuthenticationPreemptive(true);
this.defaultcreds = new UsernamePasswordCredentials(this.username, this.password);
this.httpClient.getState().setCredentials(new AuthScope(this.serverIp, -1, null), this.defaultcreds);
}
};
httpRequest.prototype.contentType = function(contentType) {
this.contentType = contentType;
this.contentTypes = [
["xml","application/xml"],
["json","application/json"]
];
for( this.i=0; this.i<this.contentTypes.length; this.i++)
if(this.contentTypes[this.i][0] == this.contentType)
this.httpMethod.addRequestHeader("Content-Type", this.contentTypes[this.i][1]);
};
httpRequest.prototype.addHeader = function(headerName,headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
this.httpMethod.addRequestHeader(this.headerName, this.headerValue);
};
httpRequest.prototype.execute = function() {
// Connection:close is hard coded here in order to ensure that the TCP connection
// gets torn down immediately after the request. Comment this line out if you wish to
// experiment with HTTP persistence.
this.httpMethod.addRequestHeader("Connection", "close");
this.httpClient.executeMethod(this.httpMethod);
// Retrieve status code.
this.statusCode = this.httpMethod.getStatusCode();
return this.statusCode;
}
httpRequest.prototype.getRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new GetMethod(this.uri);
};
httpRequest.prototype.postRequest = function(uri,bodytext) {
this.uri = uri;
this.bodytext = bodytext;
logger.addInfo("uri: "+uri);
logger.addInfo("bodytext: "+bodytext);
// POST Request.
this.httpMethod = new PostMethod(this.uri);
this.httpMethod.setRequestEntity(new StringRequestEntity(this.bodytext));
};
httpRequest.prototype.getResponse = function(asType) {
this.asType = asType;
if( this.asType == "asStream" )
return this.httpMethod.getResponseBodyAsStream();
else{
return this.httpMethod.getResponseBodyAsString();
}
};
httpRequest.prototype.deleteRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new DeleteMethod(this.uri);
};
httpRequest.prototype.disconnect = function() {
// Release connection.
this.httpMethod.releaseConnection();
};
function getAttributeValueOfTagFromXML(xmlResponse,tagName,attributeName){
//var ptn = "<"+tagName+".*?"+attributeName+"=('.*?')(.*?>)";
var ptn = "<"+tagName+".*?"+attributeName+"=(.*?)(\\s+[a-zA-Z])";
var pattern = Pattern.compile(ptn);
var matcher=pattern.matcher(xmlResponse);
logger.addInfo("matcher: "+matcher);
if(matcher.find()){
var str = matcher.group(1);
var attributeValue = str. substring(1,str.length()-1);
// var attributeValue = matcher.group(1);
logger.addInfo(attributeName+":"+attributeValue);
return attributeValue;
}
logger.addInfo("No Match Found"+matcher);
return;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// main();
//
//
// Workflow Inputs.
//
var ibUser;
var ibPassword;
var ibIP = input.UCSIP;
var cookie = input.UCSCookie;
var msgXML = "<lsInstantiateNNamedTemplate dn=\"org-root/ls-marks-temp\""
+"cookie=\""
+cookie
+"\"inTargetOrg=\"org-root\""
+"inHierarchical=\"no\">"
+"<inNameSet>"
+"<dn value=\"marks\"/>"
+"</inNameSet>"
+"</lsInstantiateNNamedTemplate>"
var msgXML2 = "<configResolveDn "
+"cookie=\""
+cookie
+"\" inHierarchical=\"true\" dn=\"org-root/ls-marks\"/>"
var request = new httpRequest();
request.setup(this.ibIP,"https",this.ibUser,this.ibPassword);
request.postRequest("/nuova/", msgXML);
request.contentType("xml");
var statusCode = request.execute();
if (statusCode != 200)
{
logger.addError("Request failed. HTTP response code: "+statusCode);
var response = request.getResponse("asString");
logger.addInfo("Response: " +response);
request.disconnect();
// Set this task as failed.
ctxt.setFailed("Request failed.");
}
var response = request.getResponse("asString");
logger.addInfo("Response1: " +response);
var request = new httpRequest();
request.setup(this.ibIP,"https",this.ibUser,this.ibPassword);
request.postRequest("/nuova/", msgXML2);
request.contentType("xml");
var statusCode = request.execute();
if (statusCode != 200)
{
logger.addError("Request failed. HTTP response code: "+statusCode);
var response = request.getResponse("asString");
logger.addInfo("Response: " +response);
request.disconnect();
// Set this task as failed.
ctxt.setFailed("Request failed.");
}
var response = request.getResponse("asString");
logger.addInfo("Response2: " +response);
var tagName = "vnicEther";
var attributeName = "addr";
var attributeValue = getAttributeValueOfTagFromXML(response,tagName,attributeName);
logger.addInfo(attributeName+":"+attributeValue);
output.UCScookie = attributeValue;
request.disconnect();
Reset Server Task:
Input:
Output:
The Code:
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(java.util.regex);
importPackage(com.cloupia.lib.util);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.cookie);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.auth);
importPackage(org.apache.commons.httpclient.protocol);
importClass(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory);
var httpRequest = function () {};
httpRequest.prototype.setup = function(serverIp, transport, username, password) {
this.serverIp = serverIp;
this.transport = transport;
this.username = username;
this.password = password;
this.httpClient = new HttpClient();
// Decide whether to create an HTTP or HTTPS connection based up 'transport'.
if( this.transport == "https" ) {
this.factory = new SecureProtocolSocketFactory() {
createSocket : function(socket, host, port, autoClose) {
this.fact = new com.cloupia.lib.util.easytrust.EasySSLProtocolSocketFactory();
this.sock = this.fact.createSocket(socket, host, port, autoClose);
this.sock.setEnabledCipherSuites([
"SSL_RSA_WITH_RC4_128_MD5",
"SSL_RSA_WITH_RC4_128_SHA",
"TLS_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
"SSL_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",
"SSL_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_DSS_WITH_DES_CBC_SHA",
"SSL_RSA_EXPORT_WITH_RC4_40_MD5",
"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"]);
return this.sock;
}
};
this.easyhttps = new Protocol(
"https",
this.factory,
443);
this.httpClient.getHostConfiguration().setHost(this.serverIp,443,this.easyhttps);
} else {
// Create new HTTP connection.
this.httpClient.getHostConfiguration().setHost(this.serverIp, 80, "http");
}
this.httpClient.getParams().setCookiePolicy("default");
// If username and password supplied, then use basicAuth.
if( this.username && this.password ) {
this.httpClient.getParams().setAuthenticationPreemptive(true);
this.defaultcreds = new UsernamePasswordCredentials(this.username, this.password);
this.httpClient.getState().setCredentials(new AuthScope(this.serverIp, -1, null), this.defaultcreds);
}
};
httpRequest.prototype.contentType = function(contentType) {
this.contentType = contentType;
this.contentTypes = [
["xml","application/xml"],
["json","application/json"]
];
for( this.i=0; this.i<this.contentTypes.length; this.i++)
if(this.contentTypes[this.i][0] == this.contentType)
this.httpMethod.addRequestHeader("Content-Type", this.contentTypes[this.i][1]);
};
httpRequest.prototype.addHeader = function(headerName,headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
this.httpMethod.addRequestHeader(this.headerName, this.headerValue);
};
httpRequest.prototype.execute = function() {
// Connection:close is hard coded here in order to ensure that the TCP connection
// gets torn down immediately after the request. Comment this line out if you wish to
// experiment with HTTP persistence.
this.httpMethod.addRequestHeader("Connection", "close");
this.httpClient.executeMethod(this.httpMethod);
// Retrieve status code.
this.statusCode = this.httpMethod.getStatusCode();
return this.statusCode;
}
httpRequest.prototype.getRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new GetMethod(this.uri);
};
httpRequest.prototype.postRequest = function(uri,bodytext) {
this.uri = uri;
this.bodytext = bodytext;
logger.addInfo("uri: "+uri);
logger.addInfo("bodytext: "+bodytext);
// POST Request.
this.httpMethod = new PostMethod(this.uri);
this.httpMethod.setRequestEntity(new StringRequestEntity(this.bodytext));
};
httpRequest.prototype.getResponse = function(asType) {
this.asType = asType;
if( this.asType == "asStream" )
return this.httpMethod.getResponseBodyAsStream();
else{
return this.httpMethod.getResponseBodyAsString();
}
};
httpRequest.prototype.deleteRequest = function(uri) {
this.uri = uri;
// Get request.
this.httpMethod = new DeleteMethod(this.uri);
};
httpRequest.prototype.disconnect = function() {
// Release connection.
this.httpMethod.releaseConnection();
};
function getAttributeValueOfTagFromXML(xmlResponse,tagName,attributeName){
//var ptn = "<"+tagName+".*?"+attributeName+"=('.*?')(.*?>)";
var ptn = "<"+tagName+".*?"+attributeName+"=(.*?)(\\s+[a-zA-Z])";
var pattern = Pattern.compile(ptn);
var matcher=pattern.matcher(xmlResponse);
logger.addInfo("matcher: "+matcher);
if(matcher.find()){
var str = matcher.group(1);
var attributeValue = str. substring(1,str.length()-1);
// var attributeValue = matcher.group(1);
logger.addInfo(attributeName+":"+attributeValue);
return attributeValue;
}
logger.addInfo("No Match Found"+matcher);
return;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// main();
//
//
// Workflow Inputs.
//
var ibUser = input.UCSUser;
var ibPassword = input.UCSPassword;
var ibIP = input.UCSIP;
var cookie = input.UCSCookie;
var msgXML = "configConfMos cookie="+cookie+" inHierarchical=\"no\">"
+"<inConfigs><pair key=\"org-root/ls-testLS1/power\">"
+"<lsPower dn=\"org-root/ls-marks\" state=\"hard-reset-immediate\" status=\"modified\"/>"
+"</pair></inConfigs></configConfMos>"
var request = new httpRequest();
request.setup(this.ibIP,"https",this.ibUser,this.ibPassword);
request.postRequest("/nuova/", msgXML);
request.contentType("xml");
var statusCode = request.execute();
if (statusCode != 200)
{
logger.addError("Request failed. HTTP response code: "+statusCode);
var response = request.getResponse("asString");
logger.addInfo("Response: " +response);
request.disconnect();
// Set this task as failed.
ctxt.setFailed("Request failed.");
}
var response = request.getResponse("asString");
logger.addInfo("Response1: " +response);
request.disconnect();
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: