Skip to main content
DevOps Image

This post is from the XebiaLabs blog and has not been updated since the original publish date.

Last Updated Dec 09, 2014 — DevOps Expert

Defining XL Deploy Infrastructure with Dojo and JavaScript


XL Deploy's REST API can be easily called from Dojo/JavaScript scripts to define new infrastructure in the repository.  Earlier articles have already explained the basics using command-line curl examples, so let's look at how Dojo and JavaScript can be used to perform the same sorts of operations.

Here is the function that makes the call to XL Deploy's REST API:

function defineConfigItem(ciPath, postDataText) {
  // console.log("Starting defineConfigItem()");
    url: "http://localhost:4516/XL Deploy/repository/ci/" + ciPath,
    user: "admin",
    password: "admin",
    headers: {"Content-Type": "application/xml"},
    sync: true,
    postData: postDataText,
    //timeout: 10000,
    handleAs: "xml",
    load: function(result, ioargs) {
      dojo.publish("xldStatus", [{message: "DefineConfigItem HTTP status code " + ioargs.xhr.status,
      type: "message", duration: 2000}]);
    error: commonError
 // console.log("Exiting defineConfigItem()");
As you can see, we use a POST call with the path to the new configuration item at the end of the url.  As a reminder, XL Deploy's paths can be easily copied from the GUI by going into edit mode on the CI, right-clicking the id field and selecting Copy.  Besides passing in the path, we also pass the post data as a string in XML format, as in this example:
<overthere.SshHost id="Infrastructure/my-new-ci">
And of course we must code the rest of the parameters required for xhrPost.  The url, username, password and headers operate as in any HTTP call. You have a choice between sync (wait for the call to return) and timeout (execute asynchronously but error out after the specified number of milliseconds).  The handeAs parameter refers to what is returned, so it is superfluous in this case.  The last two properties are callbacks:, load defines a function called for a good result and error defines an "errback" function called otherwise. The remainder of the scripting serves to supply properly formatted data to defineConfigItem.  The xldDriver function receives a number of parameters derived from an input source, assembles the postDataText XML string, and makes the function call.  Any XML formatter can stand in here, but let's just concatenate tags and values the old-fashioned way for now:
function xldDriver(ciPath,ciType,deploymentGroup,os,connectionType,address,myPort,username,password,sudoUsername) {
  // console.log("0: Executing xldDriver");
  postDataText =
    "<" + ciType + " id='" + ciPath + "'>" +
    "<deploymentGroup>" + deploymentGroup + "</deploymentGroup>" +
    "<os>" + os + "</os>" +
    "<connectionType>" + connectionType + "</connectionType>" +
    "<address>" + address + "</address>" +
    "<port>" + myPort + "</port>" +
    "<username>" + username + "</username>" +
    "<password>" + password + "</password>" +
    "<sudoUsername>" + sudoUsername + "</sudoUsername>" +
    "</" + ciType +">";
 defineConfigItem(ciPath, postDataText);
And commonError provides a means of alerting the user to bad return codes from our HTTP calls:
function commonError(text, ioargs) {
 error = true;
 dojo.publish("xhrError", [{message: "HTTP status code " + ioargs.xhr.status, type: "error", duration: 2000}]);
 return text;
Note that we use dojo.publish to publish good and bad results to the dojo.Toaster objects xldStatus and xhrError respectively.  With these three functions, any provisioning tool that supports JavaScript calls can communicate its results directly to XL Deploy.

More from the Blog

View more
Mar 04, 2021

Getting key stakeholder buy-in for changes perceived as risky

Organizational leaders must recognize that change is vital for the sur ...
Read More
Mar 01, 2021

Discover the change management practices that are ripe for optimization

Change has become the most important part of modern digital product cr ...
Read More
Feb 22, 2021

Reckoning DevOps’ role in the enterprise value stream

If you’re a software or digital solutions company, you may use DevOps ...
Read More
Feb 10, 2021

Customer spotlight: Schneider avoiding bumps in the road with DevOps adoption

Everyone wants to deliver software faster and more reliably. Companies ...
Read More
Contact Us