05-13-2017 07:39 AM - edited 03-01-2019 06:46 AM
Workflow Name: | ACI/APIC - Create Export Contract |
Description: |
Following parameter are required
|
Compatible UCS Director Versions: | UCSD 6.x.x.x |
Category: | ACI, APIC
|
Instructions for Use:
The Import of the workflow (Name of workflow and custom task):
The workflow:
Workflow input:
Workflow variables:
The custom Script:
importPackage(java.util);
importPackage(java.lang);
importPackage(java.io);
importPackage(com.cloupia.feature.apic);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(com.cloupia.lib.connector.account);
importPackage(com.cloupia.lib.util);
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);
importPackage(com.cloupia.lib.cIaaS.vcd.api);
/****************************************************************************************/
/** **/
/** !!! IMPORTANT NOTE !!! **/
/** **/
/** **/
/** THIS SCRIPT REQUIRES A MINIMUM UCS DIRECTOR VERSION OF 5.4.0.0 **/
/** **/
/****************************************************************************************/
//----------------------------------------------------------------------------------------
// ### FUNCTIONS ###
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//
// Author: Russ Whitear (rwhitear@cisco.com)
//
// Function Name: httpRequest()
//
// Version: 3.0
//
// Modifications: Added HTTP header Connection:close to execute method to overcome the
// CLOSE_WAIT issue caused with releaseConnection().
//
// Modified SSL socket factory code to work with UCS Director 5.4.0.0.
//
// Description: HTTP Request function - httpRequest.
//
// I have made the httpClient functionality more object like in order to
// make cloupia scripts more readable when making many/multiple HTTP/HTTPS
// requests within a single script.
//
// Usage: 1. var request = new httpRequest(); // Create new object.
//
// 2. request.setup("192.168.10.10","https","admin","cisco123"); // SSL.
// or: request.setup("192.168.10.10","http","admin","cisco123"); // HTTP.
// or: request.setup("192.168.10.10","https"); // SSL, no basicAuth.
// or: request.setup("192.168.10.10","http"); // HTTP, no basicAuth.
//
// 3. request.getRequest("/"); // HTTP GET (URI).
// or: request.postRequest("/","some body text"); // HTTP POST (URI,BodyText).
// or: request.deleteRequest("/"); // HTTP DELETE (URI).
//
// (optional) 4. request.contentType("json"); // Add Content-Type HTTP header.
// or: request.contentType("xml");
//
// (optional) 5. request.addHeader("X-Cloupia-Request-Key","1234567890"); // Any Header.
//
// 6. var statusCode = request.execute(); // Send request.
//
// 7. var response = request.getResponse("asString"); // Response as string.
// or: var response = request.getResponse("asStream"); // Response as stream.
//
// 8. request.disconnect(); // Release connection.
//
//
// Note: Be sure to add these lines to the top of your script:
//
// importPackage(java.util);
// importPackage(com.cloupia.lib.util);
// 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);
// importPackage(com.cloupia.lib.cIaaS.vcd.api);
//
//----------------------------------------------------------------------------------------
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 on "transport".
if( this.transport == "https" ) {
this.httpClient = CustomEasySSLSocketFactory.getIgnoreSSLClient(this.serverIp, 443);
this.httpClient.getParams().setCookiePolicy("default");
} 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;
// 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();
};
// main()
//var apic_account_name = "ACIAPIC";
var apic_account_name = String(input.aciApicAccountName);
var store = CredentialStore.getStore(new ApicInfraAccountConfig().getClass());
var apic_account = store.getCredential(apic_account_name);
var apicIP = apic_account.getServer();
var apicLogin = apic_account.getUsername();
var apicPassword = apic_account.getPassword();
//output.APIC_ACCOUNT = apic_account_name;
// Place individual variables here...
//var New = input.New;
//
var requestURI = "/api/mo/.json";
// Create JSON login data.
var aaaUser = new TreeMap();
aaaUser.put("aaaUser",new TreeMap());
aaaUser.get("aaaUser").put("attributes",new TreeMap());
aaaUser.get("aaaUser").put("children",new ArrayList());
aaaUser.get("aaaUser").get("attributes").put("name",apicLogin);
aaaUser.get("aaaUser").get("attributes").put("pwd",apicPassword);
var loginUri = "/api/mo/aaaLogin.json";
var loginData = JSON.javaToJsonString(aaaUser, aaaUser.getClass());
var newTenantName = String(input.newTenantName);
var res1 = newTenantName.split("@");
var newTenantNameX = res1[1];
var newContractName = String(input.newContractName);
var str = String(input.Contract);
var res = str.split("@");
var originalTenantName = "uni/tn-"+res[1]+"/brc-"+res[2];
var originalContractName = res[2];
var res2 = "uni/tn-"+res1[1]+"/cif-"+newContractName;
// PASTE APIC JSON REQUEST DATA HERE...
var javaMap = new TreeMap();
javaMap.put("vzCPIf",new TreeMap());
javaMap.get("vzCPIf").put("attributes",new TreeMap());
javaMap.get("vzCPIf").put("children",new ArrayList());
javaMap.get("vzCPIf").get("attributes").put("dn",res2);
javaMap.get("vzCPIf").get("attributes").put("name", newContractName);
javaMap.get("vzCPIf").get("attributes").put("status","created");
javaMap.get("vzCPIf").get("children").add(new TreeMap());
javaMap.get("vzCPIf").get("children").get("0").put("vzRsIf",new TreeMap());
javaMap.get("vzCPIf").get("children").get("0").get("vzRsIf").put("attributes",new TreeMap());
javaMap.get("vzCPIf").get("children").get("0").get("vzRsIf").put("children",new ArrayList());
javaMap.get("vzCPIf").get("children").get("0").get("vzRsIf").get("attributes").put("tDn",originalTenantName);
javaMap.get("vzCPIf").get("children").get("0").get("vzRsIf").get("attributes").put("status","created");
var requestData = JSON.javaToJsonString(javaMap, javaMap.getClass());
//
requestData = requestData+""; // Convert requestData from object to string.
requestData = requestData.replace(/\\/g,""); // Overcome some APIC ugliness.
logger.addInfo("Original Contract Name: " + originalContractName);
logger.addInfo("Original Tenant Name: " + res[1]);
logger.addInfo("New Tenant Name: " + newTenantNameX);
logger.addInfo("New Contract Name: " + newContractName);
logger.addInfo("requestData: " +requestData);
var request = new httpRequest();
request.setup(apicIP,"https");
request.postRequest(loginUri,loginData);
request.contentType("json");
var statusCode = request.execute();
var response = request.getResponse("asString");
logger.addInfo("Status code: " +statusCode );
logger.addInfo("Response: " +response );
request.postRequest(requestURI,requestData);
var statusCode = request.execute();
var response = request.getResponse("asString");
logger.addInfo("Status code: " +statusCode );
var responseData = JSON.getJsonElement(response,null);
if( responseData.has("imdata") && responseData.get("imdata").size() ) {
for( var i = 0; i < responseData.get("imdata").size(); i++ ) {
if( responseData.get("imdata").get(i).has("error") ) {
if( responseData.get("imdata").get(i).get("error").has("attributes") ) {
if( responseData.get("imdata").get(i).get("error").get("attributes").has("code") ) {
logger.addError("APIC Error Code: " + responseData.get("imdata").get(i).get("error").get("attributes").get("code") );
}
if( responseData.get("imdata").get(i).get("error").get("attributes").has("text") ) {
logger.addError("APIC Error Text: " + responseData.get("imdata").get(i).get("error").get("attributes").get("text") );
}
}
// Set failed.
ctxt.setFailed("Request failed.");
}
}
}
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: