Object
Construct a new ovirt client class. mandatory parameters
username, password, api_entrypoint - for example 'me@internal', 'secret', 'https://example.com/api'
optional parameters
datacenter_id, cluster_id and filtered_api can be sent in this order for backward compatibility, or as a hash in the 4th parameter. datacenter_id - setting the datacenter at initialization will add a default scope to any subsequent call to the client to the specified datacenter. cluster_id - setting the cluster at initialization will add a default scope to any subsequent call to the client to the specified cluster. filtered_api - when set to false (default) will use ovirt administrator api, else it will use the user api mode.
# File lib/rbovirt.rb, line 62 def initialize(username, password, api_entrypoint, options={}, backward_compatibility_cluster=nil, backward_compatibility_filtered=nil ) if !options.is_a?(Hash) # backward compatibility optional parameters options = {:datacenter_id => options, :cluster_id => backward_compatibility_cluster, :filtered_api => backward_compatibility_filtered} end @api_entrypoint = api_entrypoint @credentials = { :username => username, :password => password } @datacenter_id = options[:datacenter_id] @cluster_id = options[:cluster_id] @filtered_api = options[:filtered_api] @ca_cert_file = options[:ca_cert_file] @ca_cert_store = options[:ca_cert_store] @ca_no_verify = options[:ca_no_verify] end
# File lib/client/vm_api.rb, line 99 def activate_volume(vm_id, vol_id) http_post("/vms/%s/disks/%s/activate" % [vm_id, vol_id], '<action/>') unless volume_active?(vm_id, vol_id) end
# File lib/client/vm_api.rb, line 52 def add_interface(vm_id, opts={}) http_post("/vms/%s/nics" % vm_id, OVIRT::Interface.to_xml( opts)) end
# File lib/client/affinity_group_api.rb, line 33 def add_vm_to_affinity_group(affinity_group_id, vm_id, opts={}) cluster_id = opts[:cluster_id] || current_cluster.id http_post("/clusters/%s/affinitygroups/%s/vms" % [cluster_id, affinity_group_id], "<vm id='%s'/>" % vm_id) end
# File lib/client/vm_api.rb, line 73 def add_volume(vm_id, opts={}) search = opts[:search] || ("datacenter=%s" % current_datacenter.name) opts[:storage_domain_id] = opts[:storage_domain] || storagedomains(:role => 'data', :search => search).first.id # If no size is given, default to a volume size of 8GB opts[:size] = 8 * 1024 * 1024 * 1024 unless opts[:size] opts[:type] = 'data' unless opts[:type] opts[:bootable] = 'true' unless opts[:bootable] opts[:interface] = 'virtio' unless opts[:interface] opts[:format] = 'cow' unless opts[:format] opts[:sparse] = 'true' unless opts[:sparse] http_post("/vms/%s/disks" % vm_id, OVIRT::Volume.to_xml(opts)) end
# File lib/client/affinity_group_api.rb, line 3 def affinity_group(affinity_group_id, opts={}) cluster_id = opts[:cluster_id] || current_cluster.id ag_xml = http_get("/clusters/%s/affinitygroups/%s" % [cluster_id, affinity_group_id], http_headers) OVIRT::AffinityGroup.new(self, ag_xml.root) end
# File lib/client/affinity_group_api.rb, line 16 def affinity_group_vms(affinity_group_id, opts={}) cluster_id = opts[:cluster_id] || current_cluster.id http_get("/clusters/%s/affinitygroups/%s/vms" % [cluster_id, affinity_group_id], http_headers).xpath('/vms/vm').collect do |vm_ref| OVIRT::VM.new(self, http_get("/vms/%s" % vm_ref.attribute('id').value, http_headers).root) end end
# File lib/client/affinity_group_api.rb, line 9 def affinity_groups(opts={}) cluster_id = opts[:cluster_id] || current_cluster.id http_get("/clusters/%s/affinitygroups" % cluster_id, http_headers).xpath('/affinity_groups/affinity_group').collect do |ag| OVIRT::AffinityGroup.new(self, ag) end end
# File lib/rbovirt.rb, line 79 def api_version return @api_version unless @api_version.nil? xml = http_get("/")/'/api/product_info/version' @api_version = (xml/'version').first[:major] +"."+ (xml/'version').first[:minor] end
# File lib/rbovirt.rb, line 85 def api_version?(major, minor=nil) (api_version.split('.')[0] == major) && (minor.nil? ? true : api_version.split('.')[1] == minor) end
# File lib/client/host_api.rb, line 16 def approve_host(host_id, opts={}) http_post("/hosts/%s/approve" % host_id, "<action></action>") end
# File lib/client/vm_api.rb, line 107 def attach_volume(vm_id, vol_id, activate=true) http_post("/vms/%s/disks" % vm_id, "<disk id='%s'/>" % vol_id) activate_volume(vm_id, vol_id) if activate end
# File lib/client/cluster_api.rb, line 24 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
# File lib/client/cluster_api.rb, line 3 def cluster_version(cluster_id) c = cluster(cluster_id) return c.version.split('.')[0].to_i, c.version.split('.')[1].to_i end
# File lib/client/cluster_api.rb, line 8 def cluster_version?(cluster_id, major) c = cluster(cluster_id) c.version.split('.')[0] == major end
# File lib/client/cluster_api.rb, line 13 def clusters(opts={}) headers = {:accept => "application/xml; detail=datacenters"} path = "/clusters" path += search_url(opts) unless filtered_api http_get(path, headers).xpath('/clusters/cluster').collect do |cl| cluster = OVIRT::Cluster.new(self, cl) #the following line is needed as a work-around a bug in RHEV 3.0 rest-api cluster if filtered_api || (cluster.datacenter.id == current_datacenter.id) end.compact end
# File lib/client/affinity_group_api.rb, line 23 def create_affinity_group(opts={}) cluster_id = opts[:cluster_id] || current_cluster.id OVIRT::AffinityGroup.new(self, http_post("/clusters/%s/affinitygroups" % cluster_id, OVIRT::AffinityGroup.to_xml(opts)).root) end
# File lib/client/template_api.rb, line 17 def create_template(opts) template = http_post("/templates", Template.to_xml(opts)) OVIRT::Template::new(self, template.root) end
# File lib/client/vm_api.rb, line 17 def create_vm(opts) cluster_major_ver, cluster_minor_ver = cluster_version(self.cluster_id) if opts[:user_data] and not opts[:user_data].empty? if api_version?('3') and cluster_major_ver >= 3 if cluster_minor_ver >= 1 opts[:user_data_method] = :payload elsif floppy_hook? opts[:user_data_method] = :custom_property else raise "Required VDSM hook 'floppyinject' not supported by RHEV-M" end else raise BackendVersionUnsupportedException.new end end opts[:cluster_name] ||= clusters.first.name unless opts[:cluster] OVIRT::VM::new(self, http_post("/vms",OVIRT::VM.to_xml(opts)).root) end
# File lib/client/datacenter_api.rb, line 3 def datacenter(datacenter_id) begin datacenter = http_get("/datacenters/%s" % datacenter_id) OVIRT::DataCenter::new(self, datacenter.root) rescue handle_fault $! end end
# File lib/client/datacenter_api.rb, line 12 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
# File lib/client/vm_api.rb, line 103 def deactivate_volume(vm_id, vol_id) http_post("/vms/%s/disks/%s/deactivate" % [vm_id, vol_id], '<action/>') if volume_active?(vm_id, vol_id) end
# File lib/client/affinity_group_api.rb, line 38 def delete_vm_from_affinity_group(affinity_group_id, vm_id, opts={}) cluster_id = opts[:cluster_id] || current_cluster.id http_delete("/clusters/%s/affinitygroups/%s/vms/%s" % [cluster_id, affinity_group_id, vm_id]) end
# File lib/client/affinity_group_api.rb, line 28 def destroy_affinity_group(affinity_group_id, opts={}) cluster_id = opts[:cluster_id] || current_cluster.id http_delete("/clusters/%s/affinitygroups/%s" % [cluster_id, affinity_group_id]) end
# File lib/client/vm_api.rb, line 48 def destroy_interface(vm_id, interface_id) http_delete("/vms/%s/nics/%s" % [vm_id, interface_id]) end
# File lib/client/template_api.rb, line 22 def destroy_template(id) http_delete("/templates/%s" % id) end
# File lib/client/vm_api.rb, line 128 def destroy_vm(id) http_delete("/vms/%s" % id) end
# File lib/client/vm_api.rb, line 86 def destroy_volume(vm_id, vol_id) http_delete("/vms/%s/disks/%s" % [vm_id, vol_id]) end
# File lib/client/vm_api.rb, line 112 def detach_volume(vm_id, vol_id) deactivate_volume(vm_id, vol_id) http_delete("/vms/%s/disks/%s" % [vm_id, vol_id], '<action><detach>true</detach></action>') end
# File lib/client/disk_api.rb, line 10 def disk(disk_id) disk_xml = http_get("/disks/%s" % disk_id) OVIRT::Volume.new(self, disk_xml.root) end
# File lib/client/disk_api.rb, line 3 def disks(opts={}) path = "/disks" + search_url(opts) http_get(path).xpath('/disks/disk').collect do |d| OVIRT::Volume.new(self, d) end end
# File lib/rbovirt.rb, line 89 def floppy_hook? xml = http_get("/capabilities") !(xml/"version/custom_properties/custom_property[@name='floppyinject']").empty? end
# File lib/client/host_api.rb, line 3 def host(host_id, opts={}) xml_response = http_get("/hosts/%s" % host_id) OVIRT::Host::new(self, xml_response.root) end
# File lib/client/host_api.rb, line 8 def hosts(opts={}) path = "/hosts" path += search_url(opts) unless filtered_api http_get(path).xpath('/hosts/host').collect do |h| OVIRT::Host::new(self, h) end end
# File lib/client/cluster_api.rb, line 30 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
# File lib/client/quota_api.rb, line 3 def quota(quota_id, opts={}) q = http_get("/datacenters/%s/quotas/%s" % [current_datacenter.id, quota_id]) OVIRT::Quota::new(self, q.root) end
# File lib/client/quota_api.rb, line 8 def quotas(opts={}) http_get("/datacenters/%s/quotas" % CGI.escape(current_datacenter.id)).xpath('/quotas/quota').collect do |q| OVIRT::Quota::new(self, q) end.compact end
# File lib/client/host_api.rb, line 20 def reinstall_host(host_id, opts={}) http_post("/hosts/%s/install" % host_id, "<action><ssh> <authentication_method>PublicKey</authentication_method> </ssh></action>") end
# File lib/client/vm_api.rb, line 132 def set_ticket(vm_id, options={}) ticket = OVIRT::VM.ticket(options) xml_response = http_post("/vms/%s/ticket" % vm_id, ticket) (xml_response/'action/ticket/value').first.text end
# File lib/client/storage_domain_api.rb, line 3 def storagedomain(sd_id) sd = http_get("/storagedomains/%s" % sd_id) OVIRT::StorageDomain::new(self, sd.root) end
# File lib/client/storage_domain_api.rb, line 8 def storagedomains(opts={}) path = "/storagedomains" path += search_url(opts) unless filtered_api http_get(path).xpath('/storage_domains/storage_domain').collect do |sd| storage_domain = OVIRT::StorageDomain::new(self, sd) #filter by role is not supported by the search language. The work around is to list all, then filter. (opts[:role].nil? || storage_domain.role == opts[:role]) ? storage_domain : nil end.compact end
# File lib/client/template_api.rb, line 11 def template(template_id, opts={}) results = http_get("/templates/%s" % template_id) template = OVIRT::Template::new(self, results.root) template end
# File lib/client/template_api.rb, line 26 def template_interfaces template_id http_get("/templates/%s/nics" % template_id, http_headers).xpath('/nics/nic').collect do |nic| OVIRT::Interface::new(self, nic) end end
# File lib/client/template_api.rb, line 32 def template_volumes template_id http_get("/templates/%s/disks" % template_id, http_headers).xpath('/disks/disk').collect do |disk| OVIRT::Volume::new(self, disk) end end
# File lib/client/template_api.rb, line 3 def templates(opts={}) path = "/templates" path += search_url(opts) unless filtered_api http_get(path).xpath('/templates/template').collect do |t| OVIRT::Template::new(self, t) end.compact end
# File lib/client/vm_api.rb, line 56 def update_interface(vm_id, interface_id, opts={}) http_put("/vms/%s/nics/%s" % [vm_id, interface_id], OVIRT::Interface.to_xml( opts)) end
# File lib/client/vm_api.rb, line 138 def update_vm(opts) opts[:cluster_name] ||= clusters.first.name result_xml = http_put("/vms/%s" % opts[:id], OVIRT::VM.to_xml(opts)) OVIRT::VM::new(self, result_xml.root) end
# File lib/client/vm_api.rb, line 90 def update_volume(vm_id, vol_id, opts={}) http_put("/vms/%s/disks/%s" % [vm_id, vol_id], OVIRT::Volume.to_xml(opts)) end
# File lib/client/vm_api.rb, line 3 def vm(vm_id, opts={}) headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"} OVIRT::VM::new(self, http_get("/vms/%s" % vm_id, headers).root) end
# File lib/client/vm_api.rb, line 117 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
# File lib/client/vm_api.rb, line 37 def vm_interfaces vm_id begin http_get("/vms/%s/nics" % vm_id, http_headers).xpath('/nics/nic').collect do |nic| OVIRT::Interface::new(self, nic) end rescue => e # Catch case were vm_id is destroyed. raise e unless e.message =~ /Entity not found/ [] end end
# File lib/client/vm_api.rb, line 122 def vm_start_with_cloudinit(id, opts={}) xml = OVIRT::VM.cloudinit(opts) xml_response = http_post("/vms/%s/%s" % [id, 'start'], xml, {} ) return (xml_response/'action/status').first.text.strip.upcase=="COMPLETE" end
# File lib/client/vm_api.rb, line 60 def vm_volumes vm_id begin volumes = http_get("/vms/%s/disks" % vm_id, http_headers).xpath('/disks/disk').collect do |disk| OVIRT::Volume::new(self, disk) end rescue => e # Catch case were vm_id is destroyed. raise e unless e.message =~ /Entity not found/ volumes = [] end #this is a workaround to a bug that the list is not sorted by default. volumes.sort{ |l, r| l.name <=> r.name } end
# File lib/client/vm_api.rb, line 8 def vms(opts={}) headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"} path = "/vms" path += search_url(opts) unless filtered_api http_get(path, headers).xpath('/vms/vm').collect do |vm| OVIRT::VM::new(self, vm) end end
Generated with the Darkfish Rdoc Generator 2.