class OVIRT::Client

Attributes

api_entrypoint[R]
cluster_id[R]
credentials[R]
datacenter_id[R]

Public Class Methods

new(username, password, api_entrypoint, datacenter_id=nil, cluster_id=nil) click to toggle source
# File lib/rbovirt.rb, line 33
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

Public Instance Methods

add_interface(vm_id, opts={}) click to toggle source
# File lib/rbovirt.rb, line 104
def add_interface(vm_id, opts={})
  http_post("/vms/%s/nics" % vm_id, OVIRT::Interface.to_xml( opts))
end
add_volume(vm_id, opts={}) click to toggle source
# File lib/rbovirt.rb, line 98
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::Volume.to_xml(storage_domain_id, opts))
end
api_version() click to toggle source
# File lib/rbovirt.rb, line 78
def api_version
  xml = http_get("/")/'/api/product_info/version'
  (xml/'version').first[:major] +"."+ (xml/'version').first[:minor]
end
api_version?(major) click to toggle source
# File lib/rbovirt.rb, line 83
def api_version?(major)
  api_version.split('.')[0] == major
end
cluster(cluster_id) click to toggle source
# File lib/rbovirt.rb, line 145
def cluster(cluster_id)
  headers = {:accept => "application/xml; detail=datacenters"}
  cluster_xml = http_get("/clusters/%s" % cluster_id, headers)
  OVIRT::Cluster.new(self, cluster_xml.root)
end
cluster_version?(cluster_id, major) click to toggle source
# File lib/rbovirt.rb, line 87
def cluster_version?(cluster_id, major)
  c = cluster(cluster_id)
  c.version.split('.')[0] == major
end
clusters(opts={}) click to toggle source
# File lib/rbovirt.rb, line 137
def clusters(opts={})
  headers = {:accept => "application/xml; detail=datacenters"}
  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
create_template(opts) click to toggle source
# File lib/rbovirt.rb, line 108
def create_template(opts)
  template = http_post("/templates", Template.to_xml(opts))
  OVIRT::Template::new(self, template.root)
end
create_vm(opts) click to toggle source
# File lib/rbovirt.rb, line 92
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
current_cluster() click to toggle source
# File lib/rbovirt.rb, line 162
def current_cluster
  @current_cluster ||= self.cluster_id ? cluster(self.cluster_id) : clusters.first
end
current_datacenter() click to toggle source
# File lib/rbovirt.rb, line 158
def current_datacenter
  @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first
end
datacenter(datacenter_id) click to toggle source
# File lib/rbovirt.rb, line 166
def datacenter(datacenter_id)
  begin
    datacenter = http_get("/datacenters/%s" % datacenter_id)
    OVIRT::DataCenter::new(self, datacenter.root)
  rescue
    handle_fault $!
  end
end
datacenters(opts={}) click to toggle source
# File lib/rbovirt.rb, line 129
def datacenters(opts={})
  search = opts[:search] ||""
  datacenters = http_get("/datacenters?search=%s" % CGI.escape(search))
  datacenters.xpath('/data_centers/data_center').collect do |dc|
    OVIRT::DataCenter::new(self, dc)
  end
end
destroy_template(id) click to toggle source
# File lib/rbovirt.rb, line 113
def destroy_template(id)
  http_delete("/templates/%s" % id)
end
destroy_vm(id) click to toggle source
# File lib/rbovirt.rb, line 74
def destroy_vm(id)
  http_delete("/vms/%s" % id)
end
host(host_id, opts={}) click to toggle source
# File lib/rbovirt.rb, line 175
def host(host_id, opts={})
  xml_response = http_get("/hosts/%s" % host_id)
  OVIRT::Host::new(self, xml_response.root)
end
hosts(opts={}) click to toggle source
# File lib/rbovirt.rb, line 180
def hosts(opts={})
  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
interfaces(vm_id) click to toggle source
# File lib/rbovirt.rb, line 49
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
networks(opts) click to toggle source
# File lib/rbovirt.rb, line 151
def networks(opts)
  cluster_id = opts[:cluster_id] || current_cluster.id
  http_get("/clusters/%s/networks" % cluster_id, http_headers).xpath('/networks/network').collect do |cl|
    OVIRT::Network.new(self, cl)
  end
end
storagedomain(sd_id) click to toggle source
# File lib/rbovirt.rb, line 187
def storagedomain(sd_id)
  sd = http_get("/storagedomains/%s" % sd_id)
  OVIRT::StorageDomain::new(self, sd.root)
end
storagedomains(opts={}) click to toggle source
# File lib/rbovirt.rb, line 192
def storagedomains(opts={})
  search= opts[:search] ||''
  http_get("/storagedomains?search=%s" % CGI.escape(search)).xpath('/storage_domains/storage_domain').collect do |sd|
    OVIRT::StorageDomain::new(self, sd)
  end
end
template(template_id) click to toggle source
# File lib/rbovirt.rb, line 124
def template(template_id)
  template = http_get("/templates/%s" % template_id)
  OVIRT::Template::new(self, template.root)
end
templates(opts={}) click to toggle source
# File lib/rbovirt.rb, line 117
def templates(opts={})
  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
vm(vm_id, opts={}) click to toggle source
# File lib/rbovirt.rb, line 40
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)
  # 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
vm_action(id, action, opts={}) click to toggle source
# File lib/rbovirt.rb, line 69
def vm_action(id, action, opts={})
  xml_response = http_post("/vms/%s/%s" % [id, action],'<action/>', opts)
  return (xml_response/'action/status').first.text.strip.upcase=="COMPLETE"
end
vms(opts={}) click to toggle source
# File lib/rbovirt.rb, line 61
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)
  end
end
volumes(vm_id) click to toggle source
# File lib/rbovirt.rb, line 55
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