08-06-2015 02:27 PM - edited 03-01-2019 06:36 AM
Task Name | APIC REST API format |
Description |
|
Prerequisites |
|
Category | Workflow |
Components | vSphere 5.x |
User Inputs |
|
Output | Instructions for Regular Workflow Use: |
Thank you Mohamed Arif Hajamohideen
Potential issue with JDK1.8 result could be ‘Access denied’ exception.
This is the permission issue and Creating the https connection from custom task is blocked by JDK.
We are aware of this issue.
//ssl,apic
/*Example code for RestAPI execution on APIC*/
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(java.net);
importPackage(java.security);
importPackage(com.cloupia.lib.connector.account);
importPackage(com.cloupia.lib.connector.account.credential);
importPackage(com.cloupia.lib.util);
importClass(com.cloupia.lib.util.JSON);
importPackage(javax.net.ssl);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.methods);
importPackage(org.apache.commons.httpclient.protocol);
importPackage(com.cloupia.lib.util.easytrust);
importPackage(com.cloupia.lib.cIaaS.vcd.api);
//FUNCTIONS
function apicLogin(apicAccountName){
//Get the IP, user name, password, port and protocol from the APIC Device Identity object.
var apicAccount = AccountUtil.getAccountByName(apicAccountName);
var creds = apicAccount.getCredential();
logger.addInfo("creds :"+creds);
var username = JSON.getJsonElement(creds,"username").toString();
logger.addInfo("Username "+username);
var password = JSON.getJsonElement(creds,"password");
var ip = apicAccount.getServerAddress().toString();
logger.addInfo("IP "+ip);
var protocol = JSON.getJsonElement(creds,"protocol");
logger.addInfo("protocol "+protocol);
var port = JSON.getJsonElement(creds,"port").toString().trim();
port = Integer.parseInt(port);
logger.addInfo("port "+port);
var httpsClient = CustomEasySSLSocketFactory.getIgnoreSSLClient(ip,port);
httpsClient.getParams().setCookiePolicy("default");
var loginURL = "/api/aaaLogin.json";
/*
{
"aaaUser" : {
"attributes" : {
"name" : "georgewa",
"pwd" : "paSSword1"
}
}
}
*/
var buf = new StringBuffer();
buf.append("{");
buf.append("\"aaaUser\" : {");
buf.append("\"attributes\" : {");
buf.append("\"name\" : "+username+",");
buf.append("\"pwd\" : "+password+"");
buf.append("}");
buf.append("}");
buf.append("}");
var loginPayload = buf.toString();
logger.addInfo("loginPayload :"+loginPayload);
var httpMethod = new PostMethod(loginURL);
var requestEntity = new StringRequestEntity(loginPayload,"application/json","UTF-8");
httpMethod.setRequestEntity(requestEntity);
httpsClient.executeMethod(httpMethod);
var statuscode = httpMethod.getStatusCode();
logger.addInfo("statuscode :"+statuscode);
logger.addInfo("Status message : "+httpMethod.getStatusText());
logger.addInfo("Response body: "+httpMethod.getResponseBodyAsString());
}
var accountName = "apic185";//apic account name
apicLogin(accountName);
Coding suggestions by Russ...
var buf = new StringBuffer();
buf.append("{“);
buf.append("\"aaaUser\" : {“);
buf.append("\"attributes\" : {“);
buf.append("\"name\" : "+username+",”);
buf.append("\"pwd\" : "+password+"”);
buf.append("}”);
buf.append("}”);
buf.append("}”);
var loginPayload = buf.toString();
…can be much more elegantly written like this:
importPackage(com.cloupia.lib.util);
var username = "georgwa";
var password = "paSSword1";
var map_userDetails = new HashMap();
var map_attributes = new HashMap();
var map_aaaUser = new HashMap();
map_userDetails.put("name",username);
map_userDetails.put("pwd","password);
map_attributes.put("attributes",map_userDetails);
map_aaaUser.put("aaaUser",map_attributes);
var loginPayload = JSON.javaToJsonString(map_aaaUser, map_aaaUser.getClass());
( OutPut: {"aaaUser":{"attributes":{"pwd":"paSSword1","name":"georgewa"}}} )
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: