Lab 3.3 - Interacting with 3rd Party Systems

iControl LX can be used to communicate with external 3rd party systems/APIs via REST.

To achieve this, one must understand how to create REST API calls within an iControl LX Extension.

There are two communication methods supported by the iControl LX Framework:

  1. restOperation()
  2. Native Node.js HTTP requests (recommended by F5)

Task 1 - Using restOperation()

While you can use restOperation() to create REST transactions (using setURI(), setBody(), etc.) to 3rd party systems, F5 recommends that you use the built-in Node.js functionality.

Task 2 - Creating a REST call using Native Node.js HTTP Requests

In this example we will modify the HelloWorld extension to retrieve its operating state from a JSON file in GitHub:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* A simple iControl LX Extension that handles only HTTP GET
*/
function HelloWorld() {}

HelloWorld.prototype.WORKER_URI_PATH = "ilxe_lab/hello_world";
HelloWorld.prototype.isPublic = true;

/**
* Perform worker start functions
*/

HelloWorld.prototype.onStart = function(success, error) {

  logger.info("HelloWorld onStart()");

  var options = {
    "method": "GET",
    "hostname": "raw.githubusercontent.com",
    "port": null,
    "path": "/n8labs/super-netops/master/worker_state.json",
    "headers": {
      "cache-control": "no-cache"
    }
  };

  var req = http.request(options, function (res) {

    var chunks = [];

    res.on("data", function (chunk) {
      chunks.push(chunk);
    });

    res.on("end", function () {
      var body = Buffer.concat(chunks);
      this.state = JSON.parse(body);
    });
  });

  req.end();

  success();
};

/**
* handle onGet HTTP request
*/
HelloWorld.prototype.onGet = function(restOperation) {
  restOperation.setBody(this.state);
  this.completeRestOperation(restOperation);
};

/**
* handle /example HTTP request
*/
HelloWorld.prototype.getExampleState = function () {
  return {
    "value": "your_string"
  };
};

module.exports = HelloWorld;

With these modifications, anytime an HTTP GET is sent to /mgmt/ilxe_lab/hello_world it will reply with the JSON blob that was retrieved from GitHub when the worker was initially loaded.

Also, note the change in onGet() to restOperation.setBody()

Note

If you already have the call to another system working in the Postman REST client, you can click the Generate Code button to obtain the Node.js code.