Code Monkey home page Code Monkey logo

Comments (6)

atoy40 avatar atoy40 commented on May 31, 2024

This is a feature of the libvirt api, not about the node wrapper : virConnectListDomains() returns a list of ID
note : I think we have to add support for the new libvirt function virConnectListAllDomains() (even if it's not directly link to your question) it fixes race problems we can have by calling two different functions to get all domains (virConnectListDomains followed by virConnectListDefinedDomains) :
http://libvirt.org/html/libvirt-libvirt-domain.html#virConnectListAllDomains

For your problem, you can just get the list of ID as a promise, then map() ids to names.

Anthony.

from node-libvirt.

austinfrey avatar austinfrey commented on May 31, 2024

@atoy40 would I need to keep track of the names myself or is there a way of accessing them via the node wrapper?

from node-libvirt.

mbroadst avatar mbroadst commented on May 31, 2024

@aafrey the node wrapper tracking them would be exactly like you tracking them in this case. The libvirt api does not provide a method for looking up names of all domains - you would have to get all the ids, lookup each by id (to get the actual Domain object), and call getName() on it, this is what @atoy40 was suggesting.

Even with the added support for virConnectListAllDomains, we still end up with Domain objects, which then need to have getName() called on them, it saves you one step from the suggestion above. Currently node-libvirt binding is a 1-1 mapping from C to node.js, and I don't foresee us getting creative beyond that, however, there is precedent for extending the prototype of the existing modules, for instance: https://github.com/hooklift/node-libvirt/blob/master/lib/index.js#L55.

from node-libvirt.

austinfrey avatar austinfrey commented on May 31, 2024

I got this working, thanks for the advice! If you see a better way though let me know!

var libvirt = require('libvirt');
var hyper = new libvirt.Hypervisor('qemu:///system');

var domainName = (val) => {
  for (var i = 0; i < val.length; i++) {
    hyper.lookupDomainById( val[i], (err, domain) => {
      domain.getName( (err, name) => {
        console.log(name);
      });
    });
  }
}
var domainIds = new Promise( (resolve, reject) => {
  hyper.listActiveDomains( (err, domains) => {
    resolve(domains);
  });
});

domainIds.then( val => { domainName(val); });

from node-libvirt.

mbroadst avatar mbroadst commented on May 31, 2024

@aafrey how about:

const libvirt = require('libvirt'),
          hv = new libvirt.Hypervisor('qemu:///system');

hv.connectAsync()
  .then(() => hv.listActiveDomainsAsync())
  .map(id => hv.lookupDomainByIdAsync(id))
  .map(domain => domain.getNameAsync())
  .then(result => console.log(result));

caveat: I haven't actually tried running this, it might need minor modifications

from node-libvirt.

austinfrey avatar austinfrey commented on May 31, 2024

@mbroadst I'll give it a try, since it looks much much cleaner :)

from node-libvirt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.