lib/rbovirt.rb in rbovirt-0.0.3 vs lib/rbovirt.rb in rbovirt-0.0.4

- old
+ new

@@ -3,12 +3,12 @@ require "ovirt/datacenter" require "ovirt/host" require "ovirt/storage_domain" require "ovirt/template" require "ovirt/vm" -require "ovirt/disk" -require "ovirt/nic" +require "ovirt/volume" +require "ovirt/interface" require "nokogiri" require "rest_client" module OVIRT @@ -25,30 +25,40 @@ end end class Client - attr_reader :credentials, :api_entrypoint, :datacenter_id + attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id - def initialize(username, password, api_entrypoint, datacenter_id=nil) + def initialize(username, password, api_entrypoint, datacenter_id=nil, cluster_id=nil) @credentials = { :username => username, :password => password } @datacenter_id = datacenter_id + @cluster_id = cluster_id @api_entrypoint = api_entrypoint end def vm(vm_id, opts={}) headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"} vm = OVIRT::VM::new(self, http_get("/vms/%s" % vm_id, headers).root) - vm.nics = http_get("/vms/%s/nics" % vm_id, headers).xpath('/nics/nic').collect do |nic| - OVIRT::Nic::new(self, nic) - end if opts[:nics] - vm.disks = http_get("/vms/%s/disks" % vm_id, headers).xpath('/disks/disk').collect do |disk| - OVIRT::Disk::new(self, disk) - end if opts[:disks] + # optional eager loading + vm.interfaces = interfaces(vm_id) if opts[:include] && opts[:include].include?(:interfaces) + vm.volumes = volumes(vm_id) if opts[:include] && opts[:include].include?(:volumes) vm end + def interfaces vm_id + http_get("/vms/%s/nics" % vm_id, http_headers).xpath('/nics/nic').collect do |nic| + OVIRT::Interface::new(self, nic) + end + end + + def volumes vm_id + http_get("/vms/%s/disks" % vm_id, http_headers).xpath('/disks/disk').collect do |disk| + OVIRT::Volume::new(self, disk) + end + end + def vms(opts={}) headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"} search= opts[:search] || ("datacenter=%s" % current_datacenter.name) http_get("/vms?search=%s" % CGI.escape(search), headers).xpath('/vms/vm').collect do |vm| OVIRT::VM::new(self, vm) @@ -63,50 +73,50 @@ def destroy_vm(id) http_delete("/vms/%s" % id) end def api_version - result_xml = http_get("/") - (result_xml/'/api/product_info/version') + xml = http_get("/")/'/api/product_info/version' + (xml/'version').first[:major] +"."+ (xml/'version').first[:minor] end def api_version?(major) - api_version.first[:major].strip == major + api_version.split('.')[0] == major end def cluster_version?(cluster_id, major) - result_xml = http_get("/clusters/%s" % cluster_id) - (result_xml/'/cluster/version').first[:major].strip == major + c = cluster(cluster_id) + c.version.split('.')[0] == major end - def create_vm(template_name, opts) - cluster_name = opts[:cluster_name] || clusters.first.name - result_xml = http_post("/vms",OVIRT::VM.to_xml(template_name, cluster_name, opts)) + def create_vm(opts) + opts[:cluster_name] ||= clusters.first.name + result_xml = http_post("/vms",OVIRT::VM.to_xml(opts)) OVIRT::VM::new(self, result_xml.root) end - def add_disk(vm_id, opts={}) + def add_volume(vm_id, opts={}) storage_domain_id = opts[:storage_domain] || storagedomains.first.id - result_xml = http_post("/vms/%s/disks" % vm_id, OVIRT::Disk.to_xml(storage_domain_id, opts)) + result_xml = http_post("/vms/%s/disks" % vm_id, OVIRT::Volume.to_xml(storage_domain_id, opts)) end - def add_nic(vm_id, opts={}) - http_post("/vms/%s/nics" % vm_id, OVIRT::Nic.to_xml( opts)) + def add_interface(vm_id, opts={}) + http_post("/vms/%s/nics" % vm_id, OVIRT::Interface.to_xml( opts)) end - def create_template(vm_id, opts) - template = http_post("/templates", Template.to_xml(vm_id, opts)) + def create_template(opts) + template = http_post("/templates", Template.to_xml(opts)) OVIRT::Template::new(self, template.root) end def destroy_template(id) http_delete("/templates/%s" % id) end def templates(opts={}) - search= opts[:search] || ("datacenter=$s" % current_datacenter.name) + search= opts[:search] || ("datacenter=%s" % current_datacenter.name) http_get("/templates?search=%s" % CGI.escape(search)).xpath('/templates/template').collect do |t| OVIRT::Template::new(self, t) end.compact end @@ -123,11 +133,11 @@ end end def clusters(opts={}) headers = {:accept => "application/xml; detail=datacenters"} - search= opts[:search] || ("datacenter=$s" % current_datacenter.name) + search= opts[:search] || ("datacenter=%s" % current_datacenter.name) http_get("/clusters?search=%s" % CGI.escape(search), headers).xpath('/clusters/cluster').collect do |cl| OVIRT::Cluster.new(self, cl) end end @@ -139,10 +149,14 @@ def current_datacenter @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first end + def current_cluster + @current_cluster ||= self.cluster_id ? cluster(self.cluster_id) : clusters.first + end + def datacenter(datacenter_id) begin datacenter = http_get("/datacenters/%s" % datacenter_id) OVIRT::DataCenter::new(self, datacenter.root) rescue @@ -154,10 +168,10 @@ xml_response = http_get("/hosts/%s" % host_id) OVIRT::Host::new(self, xml_response.root) end def hosts(opts={}) - search= opts[:search] || ("datacenter=$s" % current_datacenter.name) + search= opts[:search] || ("datacenter=%s" % current_datacenter.name) http_get("/hosts?search=%s" % CGI.escape(search)).xpath('/hosts/host').collect do |h| OVIRT::Host::new(self, h) end end \ No newline at end of file