noob question. cli app utilizing node-rest-client

27 views
Skip to first unread message

Marc Phillips

unread,
Mar 13, 2015, 10:15:14 PM3/13/15
to nod...@googlegroups.com
I'm converting over some automation apps from perl to node.  I've run into what I'm sure is me making incorrect assumptions.

I have the following function.    I load up a json config file that defines a list of clusters and nodes (F5 ha pairs), then go out and query the restapi on each nodes in a cluster to detemine their state.
The problem I'm having is the rest call seems to only happen for the last item in the list.

Here's my rest config:

var client = require('node-rest-client').Client;
client = new client(options_auth);

function getActive() {
        var clusters = require('../etc/clusters.json')
        for (var cluster in clusters) {
          console.log(cluster+':')
          clusterlist += " "+cluster;
          for (var index in clusters[cluster]) {
            for (var node in clusters[cluster][index]) {
                client.get("https://"+clusters[cluster][index][node]+"/mgmt/tm/cm/device", function(data, response){
                        state = data["items"][1]["failoverState"];
                        console.log(state);
                });
                console.log("\t"+node.yellow+': '+clusters[cluster][index][node].magenta+" state: "+state);
            }
          }
        }
}

The client.get seems to happen asychronously, and after some debugging only occurs to the against the last node in the list (I presently have 4 nodes defined in the clusters.json).

I'm sure I'm doing this horribly wrong. 

Ryan Schmidt

unread,
Mar 14, 2015, 2:14:46 PM3/14/15
to nod...@googlegroups.com

On Mar 13, 2015, at 7:14 PM, Marc Phillips wrote:

> I'm converting over some automation apps from perl to node. I've run into what I'm sure is me making incorrect assumptions.
>
> I have the following function. I load up a json config file that defines a list of clusters and nodes (F5 ha pairs), then go out and query the restapi on each nodes in a cluster to detemine their state.
> The problem I'm having is the rest call seems to only happen for the last item in the list.
>
> Here's my rest config:
>
> var client = require('node-rest-client').Client;
> client = new client(options_auth);

This is confusing. You are reusing the same variable for two different purposes. For clarity, use two variables:

var Client = require('node-rest-client').Client;
var client = new Client(options_auth);

Now Client (uppercase C) is the class, and client (lowercase c) is the instance of that class, as per usual JavaScript conventions.


> function getActive() {
> var clusters = require('../etc/clusters.json')
> for (var cluster in clusters) {
> console.log(cluster+':')
> clusterlist += " "+cluster;
> for (var index in clusters[cluster]) {
> for (var node in clusters[cluster][index]) {
> client.get("https://"+clusters[cluster][index][node]+"/mgmt/tm/cm/device", function(data, response){
> state = data["items"][1]["failoverState"];

You haven't written "var state", so you've made "state" a global variable. Globals should be avoided.

> console.log(state);

Here you are logging the state after it has been assigned. This should be printing the correct value, for each run through the loop.

> });
> console.log("\t"+node.yellow+': '+clusters[cluster][index][node].magenta+" state: "+state);

Here you are logging the state before it has been assigned. (This console.log is outside of client.get's callback.) So you might get an undefined state, or you might get the state of the last run. Neither of these is likely to be what you want.

> }
> }
> }
> }
>
> The client.get seems to happen asychronously, and after some debugging only occurs to the against the last node in the list (I presently have 4 nodes defined in the clusters.json).
>
> I'm sure I'm doing this horribly wrong.

You have a function called getActive, and it gets data asynchronously (using client.get) but it doesn't take any parameters. I assume the function is supposed to get a list of active nodes, and then return that information to the caller somehow. The usual way to return such information would be to have a callback parameter, and for the function to call that callback once the answer has been retrieved or computed.


Reply all
Reply to author
Forward
0 new messages