ACI – API Subscriptions

Placeholder notes for Cisco ACI subscriptions using nodejs

ACI subscriptions require the following setup, in the following order:

  1. Login to APIC, obtain your token
  2. Create a Websocket using your token as an identifier
  3. Query the API and append subscription=yes, using your APIC token.
  4. Subscriptions expire every 60s, refresh faster than that

Sounds easy, the Login and query are very simple, however websockets are new to me.  I decided to use NodeJS for websockets.  It actually made it very simple.  In the below example i’m querying the APIC for all endpoints, and subscribing to that query.  When new endpoints come online the websocket will be notified.  In the future I’ll utilize this info to update our IPAM dynamically.  I just need to figure out Python Websockets…

Here’s the script

var Client = require('node-rest-client').Client;
var WebSocket = require('ws');

var options = {
	connection: {
		rejectUnauthorized: false
	}
};

var client = new Client(options);

var resp = function(data, response) {
    var token = data.imdata[0].aaaLogin.attributes.token;
	
    var ws = new WebSocket('https://APIC/socket'.concat(token), { rejectUnauthorized:false, strictSSL: false });
    ws.on('open', function(data) {
	console.log('WEBSOCKET-DATA: Socket Open');
	client.get('https://APIC/api/node/class/fvCEp.json?subscription=yes', { headers: {"Content-Type": "application/json", 'Cookie': 'APIC-COOKIE=' + token}}, function(data, response) {
	    console.log(data.imdata[0]);
	});
    });
    ws.on('message', function incoming(data) {
        console.log(data);
        #DO SOMETHING WITH THIS DATA
    });

    var testData = client.get('https://APIC/api/node/class/fvCEp.json?subscription=yes', { headers: {"Content-Type": "application/json", 'Cookie': 'APIC-COOKIE=' + token}}, function(data, response) {
        console.log(data.imdata[0]);
    });
};

var args = {
	data: { aaaUser: {attributes: { name: USERNAME, pwd: PWD}}},
	headers: { "Content-Type": "application/json" }
};

client.post('https://APIC/api/aaaLogin.json', args, resp);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s