hooklift/node-libvirt

list by domain name?

Closed this issue · 6 comments

Is it possible to list all domains/running domains by name rather than ID? so rather than .listActiveDomains() we could have .listActiveDomainsByName() If it exists, I'm just not seeing it :/

Thanks in advance

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.

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

@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.

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); });

@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

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