UCSD and UCSM HTTP integration Example

Version 2
    Task NameUCSD and UCSM HTTP integration Example
    Description
    1. Obtain UCSM boot order
    Prerequisites
    1. Tested on 5.3
    CategoryWorkflow
    ComponentsvSphere 5.x
    User Inputs

    Instructions for Regular Workflow Use:

    1. Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
    2. Unzip the file on your computer. Should end up with a .WFD file.
    3. Log in to UCS Director as a user that has "system-admin" privileges.
    4. Navigate to "Policies-->Orchestration" and click on "Import".
    5. Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
    6. Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
    7. Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
    8. Click "Submit".
    9. A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment.

     

     

     

    Custom Workflow Task : GetUCSM_ServiceProfileBootOrder

     

    This task has  4 inputs : uri, username, passwd, serviceprofilename

    This task has  4 outputs : vHBA0_P, vHBA0_S, vHBA1_P, vHBA1_S

    This task has a Cloupia Script .

     

    The Script does the following

     

    —>Login to UCS-XML-API  using xml over http

    —>get session cookie

    —>Use the cookie to fetch other details

    —>Parse Xml response , get the required output data(wwn)

     

    TEST using Sample Workflow : Get UCSM boot Order

    This w/f has 4 inputs associated with admin input ( done for ease of testing … However these inputs can be mapped to the output from prior tasks in any other workflow)

     

     

    Code Example:

     

    importPackage(java.io);

    importPackage(javax.xml.xpath);

    importPackage(org.apache.commons.httpclient);

    importPackage(org.apache.commons.httpclient.methods);

    importPackage(org.xml.sax);

    importPackage(com.cloupia.model.cIM);

    importPackage(com.cloupia.service.cIM.inframgr);

     

    var UCSM_URI = "UCSM_URI";

    var UCSM_Username = "UCSM_Username";

    var UCSM_Password = "UCSM_Password";

    var UCSM_ServiceProfile = "UCSM_ServiceProfile";

     

    // var url = "http://" + ctxt.getInput(UCSM_URI) + ":80/nuova";

    // var username = ctxt.getInput(UCSM_Username);

    // var password = ctxt.getInput(UCSM_Password);

    // var serviceProfile = ctxt.getInput(UCSM_ServiceProfile);

     

    var url = "http://" + input.UCSM_URI + ":80/nuova";

    var username = input.UCSM_Username;

    var password = input.UCSM_Password;

    var serviceProfile = input.UCSM_ServiceProfile;

     

    // =====================================================

     

    var cookie;

    var quesryResp;

     

    logger.addInfo("=================BEGIN=================");

    logger.addInfo("========LOGIN to UCS XML API========");

    doLogin();

     

    logger.addInfo("========UCS Query set context========");

    doQueryDN("org-root/ls-" + serviceProfile, false);

     

    logger.addInfo("========UCS Query Fetch Data========");

    var responseXML = doQueryDN("org-root/boot-policy-SAN_Boot/storage", true);

     

    logger.addInfo("========LOGOUT frm UCS XML API========");

    doLogout();

     

    logger.addInfo("========Parse Response and get data========");

    getStorageBootOrder(responseXML);

    logger.addInfo("=================END=================");

     

    // =====================================================

     

    function doLogin() {

    var loginxml = "<aaaLogin inName='" + username + "' inPassword= '" + password + "'></aaaLogin>";

    logger.addInfo("loginXML:" + loginxml);

    var xml = doPostXML(loginxml);

    logger.addInfo("tokenXML:" + xml);

    cookie = getXpathData("//aaaLogin/@outCookie", xml);

    logger.addInfo("token:" + cookie);

    }

     

    function doLogout() {

    var logoutxml = "<aaaLogout inCookie='" + cookie + "'></aaaLogout>";

    logger.addInfo("logoutxml:" + logoutxml);

    var xml = doPostXML(logoutxml);

    logger.addInfo("logoutRespxml:" + xml);

    }

     

    function doQueryDN(queryPath, inHierarchical) {

    var queryxml = "<configResolveDn dn='" + queryPath + "' cookie='" + cookie + "' inHierarchical='" + inHierarchical + "'/>";

    var xml = doPostXML(queryxml);

    logger.addInfo("Result:" + queryPath + ": " + xml);

    return xml;

    }

     

    function doQueryChildren(queryPath) {

    var queryxml = "<configResolveChildren inDn='" + queryPath + "' cookie='" + cookie + "' inHierarchical='" + inHierarchical + "'/>";

    var xml = doPostXML(queryxml);

    logger.addInfo("Result:" + xml);

    }

     

    function getStorageBootOrder(xml) {

    var vHBA0_P = getXpathData("/configResolveDn/outConfig/lsbootStorage[@dn='org-root/boot-policy-SAN_Boot/storage']/lsbootSanImage[@type='primary']/lsbootSanImagePath[@type='primary']/@wwn", xml);

    var vHBA0_S = getXpathData("/configResolveDn/outConfig/lsbootStorage[@dn='org-root/boot-policy-SAN_Boot/storage']/lsbootSanImage[@type='primary']/lsbootSanImagePath[@type='secondary']/@wwn", xml);

    var vHBA1_P = getXpathData("/configResolveDn/outConfig/lsbootStorage[@dn='org-root/boot-policy-SAN_Boot/storage']/lsbootSanImage[@type='secondary']/lsbootSanImagePath[@type='primary']/@wwn", xml);

    var vHBA1_S = getXpathData("/configResolveDn/outConfig/lsbootStorage[@dn='org-root/boot-policy-SAN_Boot/storage']/lsbootSanImage[@type='secondary']/lsbootSanImagePath[@type='secondary']/@wwn", xml);

     

    logger.addInfo("vHBA0_P:" + vHBA0_P);

    logger.addInfo("vHBA0_S:" + vHBA0_S);

    logger.addInfo("vHBA1_P:" + vHBA1_P);

    logger.addInfo("vHBA1_S:" + vHBA1_S);

     

    output.vHBA0_P = vHBA0_P;

    output.vHBA0_S = vHBA0_S;

    output.vHBA1_P = vHBA1_P;

    output.vHBA1_S = vHBA1_S;

     

    // ctxt.setOutputValue("vHBA0_P",vHBA0_P)

     

    }

     

    function getXpathData(xpathString, xml) {

    var xpathFactory = XPathFactory.newInstance();

    var xpath = xpathFactory.newXPath();

    var source = new InputSource(new StringReader(xml));

    try {

    var msg = xpath.evaluate(xpathString, source);

    return msg;

    } catch (e) {

    logger.addInfo("ERROR :" + e);

    }

    return "";

    }

     

    function doPostXML(data) {

    var br = null;

    var xml = "";

     

    var httpClient = new HttpClient();

    httpClient.getParams().setParameter("http.useragent", "UCSD Script Client");

     

    var httpMethod = new PostMethod(url);

    httpMethod.setRequestEntity(new StringRequestEntity(data));

    httpMethod.addRequestHeader("Content-Type", "application/xml");

     

    try {

    var statuscode = httpClient.executeMethod(httpMethod);

    if (statuscode != 200) {

    // logger.addError("Error in invocation "+statuscode);

    } else {

    br = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream()));

    var readLine = "";

    var response = "";

    while (((readLine = br.readLine()) != null)) {

    response = response + readLine;

    }

    xml = response.toString();

    }

    } catch (e) {

    logger.addInfo("ERROR :" + e);

    } finally {

    httpMethod.releaseConnection();

    if (br != null)

    try {

    br.close();

    } catch (fe) {}

    }

    return xml;

    }