Let's continue using the example of retrieving fabric endpoints by accomplishing the same functionality in JavaScript. This will provide a better interface to work with, show how to display the data, and use libraries to provide a way to manipulate it.

We had talked previously about the classQuery when we did the first request call in Python. The ACI system works the same across languages so you will be calling the same classQuery from javascript. The following diagram provides a visual cue on to the difference between a class query and a distinguished name.

If you recall from the previous discussion, each object in the ACI Management Information Tree belongs to a specific class. So LLDP neighbors has an object to define what an LLDP neighbor looks like. Endpoints, access ports, trunk ports, OSPF neighbors all have a class definition also. For this reason when you do a query towards the class, the fabric will return all the objects that are of that class in the fabric. For this reason class queries can also be done with filters to handle specific lookups ( for example, all the endpoints that belong to a particular Tenant)

Step 1 - Write an ACI classQuery() Function

The class fvCEp is used to find learned endpoints in the fabric. In the request section a URL was built for the specific class, request_url = '/node/class/fvCEp.json'.

Since class queries always use the same base URL structure /api/node/class/, a function can be built to execute the request for given a specific class and return the data for processing. Don't forget the classQuery() function must utilize the tokens to authenticate.


var creds = {
  url: 'http://10.0.226.41',
  name: 'aciproglab04',
  pwd: 'cisco.123',
  token: '',
  urlToken: ''
}

function classQuery(classname) {
  return $.ajax({
    url: creds.url + '/api/node/class/' + classname + '.json',
    headers: {
      'DevCookie': creds.token,
      'APIC-challenge': creds.urlToken,
      'Content-Type': 'application/json'
    }
  });
};


function login() {
  return $.ajax({
    type: 'POST',
    url: creds.url + '/api/aaaLogin.json?gui-token-request=yes',
    data: JSON.stringify({
      aaaUser: {
        attributes: {
          name: creds.name,
          pwd: creds.pwd
        }
      }
    }),
  });
};

$( document ).ready(function() {
  login().then(function (data) {
    var attrs = data['imdata'][0]['aaaLogin']['attributes'];
    creds.token = attrs['token'];
    creds.urlToken = attrs['urlToken'];
  });
});

Step 2 - Calling classQuery() from document.ready()

Call the classQuery() function in document.ready(). Specifically, within the callback function passed into login().then().

Then, create the endpoints variable outside of the document.ready() invocation. This will play a role in showing you around Chrome's Developer Tools.


var creds = {
  url: 'http://10.0.226.41',
  name: 'aciproglab04',
  pwd: 'cisco.123',
  token: '',
  urlToken: ''
}

var endpoints = {};

function classQuery(classname) {
  return $.ajax({
    url: creds.url + '/api/node/class/' + classname + '.json',
    headers: {
      'DevCookie': creds.token,
      'APIC-challenge': creds.urlToken,
      'Content-Type': 'application/json'
    }
  });
};


function login() {
  return $.ajax({
    type: 'POST',
    url: creds.url + '/api/aaaLogin.json?gui-token-request=yes',
    data: JSON.stringify({
      aaaUser: {
        attributes: {
          name: creds.name,
          pwd: creds.pwd
        }
      }
    }),
  });
};

$( document ).ready(function() {
  login().then(function (data) {
    var attrs = data['imdata'][0]['aaaLogin']['attributes'];
    creds.token = attrs['token'];
    creds.urlToken = attrs['urlToken'];

    classQuery('fvCEp').then(function (data) {
      endpoints = data['imdata'];    // endpoints is an array of objects
      console.log('Total # of endpoints: ' + endpoints.length); 
    });

  });
});

Step 3 - Using Chrome's Developer Tools to inspect objects

Since JavaScript is a client side programing language, the debugger is located on the browser which interfaces with the client. Previously, you were shown how to use the console.log() JavaScript function to print log output. While this is useful, a more useful tool is available in the browser console.

To activate the console right click on the page and select Inspect from the Menu or use the keyboard shortcut Ctrl+Shift+i. You should see something similar to the following.

In the console, there should be a cursor for input under the Total # of endpoints: line. Type creds and press enter

Elements  Console  Sources  Network

creds

Object {url: "http://10.0.226.xx", name: "aciproglabxx", pwd: "cisco.123", token: "JkSaPW
    name:"aciproglab01"
    pwd:"cisco.123"
    token:"JkSaPW
    url:"http://10.0.226.41"
    urlToken:"00049b1765ec25f49dfb3b43a22613fd15358586f4afff79ecc620db1fca35f1"
    __proto__:Object
            

As you can see, the credential object is displayed. If you were to repeat the command for the endpoint array that was built from the response from the fabric then output should contain all the endpoint data.

If you type endpoints in the chrome console then you would get to see the whole object that is now stored in the browser window. You can expand the object and even manipulate it.

Now that you have the data, let's take things up a notch and make them available easily to the end user.