简体   繁体   中英

Getting Lists from callback in nodejs

I am trying to get list of "servers" for a particular "stack" in Rackspace. I have to do some processing of the Stack resource to get the server list as some of the servers are in a resource group(nested). I get each server through asynchronous call one by one. How to bundle them together and send at one go to the client? Below is the code:

   app.js

   function setClient(credentials)
   {
     var client = pkgcloud.orchestration.createClient({
       provider: 'rackspace',
       username: credentials.uname,
       apiKey: credentials.key,
       region: 'HKG'
     });
     return client;
   }

   app.io.route('ready', function(req) {
     var client = setClient(req.session.credentials);

     client.getStacks(function (err, stacks) {
        if (err) {
          req.io.emit("error", { error: err.result.error.message });
          return;
        }

        if (stacks.length > 0) {
          var stack = stacks[0].name;

          getSer(stack, client, req, 'init');
        }
        req.io.emit("showStacks", { stacks: stacks });
     });
   });

   app.io.route('create_stack', function(req) {
      var stack = req.data.stack_name;
      var tmpl = req.data.content;
      var client = setClient(req.session.credentials);

      client.createStack({
        name: stack,
        timeout: 60,
        template: tmpl // Heat template
       }, handle(req));
   });

   function handle(req) {
      return function handle(err, stack)
      {
         var client = setClient(req.session.credentials);

         if (err) {
            req.io.emit("error", { error: err.result.error.message });
            console.dir(err.result.error.message);
            return;
         }

         client.getStacks(function (err, stacks) {
            req.io.emit("showStacks", { stacks: stacks });
         });

         req.io.emit("status", {status: 'Stack created'});

         // Wait for status: CREATE_COMPLETE on our stack, and then callback
         stack.setWait({ status: 'CREATE_COMPLETE' }, 5000, function (err) {
            if (err) {
              req.io.emit("error", { error: err.result.error.message });
              console.dir(err);
              return;
            }

            client.getStacks(function (err, stacks) {
              req.io.emit("showStacks", { stacks: stacks });
            });

            client.getStack(stack, function (err, stack) {
               if (err) {
                  req.io.emit("error", { error: err.result.error.message             });
                  console.dir(err.result.error.message);
                  return;
                }
                req.io.emit("showStack", { stack: stack });
            });

            getSer(stack, client, req, 'create');
         });
      }
   }

   function getSer(stack, client, req, mode)
   {
      client.getResources(stack, function(err, resources) {
          if (err) {
             console.dir(err);
             req.io.emit("error", { error: err.result.error.message });
             return;
           }

           resources.forEach(function (resource) {
              var nestedlink = '';
              var nested = false;
              if (resource.links != undefined) {
                resource.links.forEach(function (link) {
                   if (link.rel == "nested") {
                      nestedlink = link.href;
                      nested = true;
                   }
                });
              }
              if (nested) {
                 var stack_name = nestedlink.substr(nestedlink.indexOf('stacks/')+7, nestedlink.lastIndexOf('/'));
                 getSer(stack_name, client, req, mode);
              } else {
                 var serId = resource.physicalResourceId;
                 var ser = serClient(req.session.credentials);
                 ser.getServer(serId, function (err, server) {
                   if (mode == 'init')
                     req.io.emit("showServer", { server: server});
                   else if (mode == 'create')
                     req.io.emit("stackCreate", { server: server});
                 });
              }
           });
      });
   }

Actually, the problem is pkgcloud has a method 'getResources' which given the stack name returns the resources. From which I get 'physical_resource_id' (server_id) of the server which is not in a group but for the servers which were created through 'OS::Heat::ResourceGroup' one's to do some extra work because the resource list has only a 'nested' link for it. So I take the stack_name from the nested 'link' and get its server-id var stack_name = nestedlink.substr(nestedlink.indexOf('stacks/')+7, nestedlink.lastIndexOf('/')); . Life would've been easy if pkgcloud had something like given a stack-name it'd return the servers list in it.

The issue I mentioned in the comments ( https://github.com/pkgcloud/pkgcloud/issues/410 ) has been fixed now. With that fix you should be able to request a list of all resources for a stack, including nested resources . Just do this in your code:

  client.getResources(stack, { nestedDepth: XXX }, function(err, resources) {

where XXX is a sufficient nesting depth, depending on your stack.

instead of:

  client.getResources(stack, function(err, resources) {

The fix to pkgcloud will be included in the next official release. That might not become available for a couple of weeks or months so if you need to download this fix immediately, you can do so with the following command:

  npm install https://github.com/pkgcloud/pkgcloud/tarball/master

Alternatively, you could modify the pkgcloud dependency in your project's package.json like so:

"pkgcloud": "git://github.com/pkgcloud/pkgcloud.git"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM