Parent

Class/Module Index [+]

Quicksearch

RHEVM::Client

Attributes

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

Public Class Methods

new(username, password, api_entrypoint, datacenter_id=nil) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 49
def initialize(username, password, api_entrypoint, datacenter_id=nil)
  @credentials = { :username => username, :password => password }
  @datacenter_id = datacenter_id
  @api_entrypoint = api_entrypoint
end
parse_response(response) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 295
def self.parse_response(response)
  Nokogiri::XML(response)
end

Public Instance Methods

api_version?(major) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 98
def api_version?(major)
  headers = {
    :content_type => 'application/xml',
    :accept => 'application/xml'
  }
  headers.merge!(auth_header)
  result_xml = Nokogiri::XML(RHEVM::client(@api_entrypoint)["/"].get(headers))
  if (result_xml/'api/system_version').empty?
    (result_xml/'/api/product_info/version').first[:major].strip == major
  else
    (result_xml/'/api/system_version').first[:major].strip == major
  end
end
auth_header() click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 284
def auth_header
  # As RDOC says this is the function for strict_encode64:
  encoded_credentials = ["#{@credentials[:username]}:#{@credentials[:password]}"].pack("m0").gsub(/\n/,'')
  { :authorization => "Basic " + encoded_credentials }
end
base_url() click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 290
def base_url
  url = URI.parse(@api_entrypoint)
  "#{url.scheme}://#{url.host}:#{url.port}"
end
cluster(cluster_id) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 237
def cluster(cluster_id)
  current_datacenter.cluster(cluster_id)
end
cluster_version?(cluster_id, major) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 112
def cluster_version?(cluster_id, major)
  headers = {
    :content_type => 'application/xml',
    :accept => 'application/xml'
  }
  headers.merge!(auth_header)
  result_xml = Nokogiri::XML(RHEVM::client(@api_entrypoint)["/clusters/%s" % cluster_id].get(headers))
  (result_xml/'/cluster/version').first[:major].strip == major
end
clusters() click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 233
def clusters
  current_datacenter.clusters
end
create_template(vm_id, opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 170
def create_template(vm_id, opts={})
  opts ||= {}
  builder = Nokogiri::XML::Builder.new do
    template_ {
      name opts[:name]
      description opts[:description]
      vm(:id => vm_id)
    }
  end
  headers = opts[:headers] || {}
  headers.merge!({
    :content_type => 'application/xml',
    :accept => 'application/xml',
  })
  headers.merge!(auth_header)
  template = RHEVM::client(@api_entrypoint)["/templates"].post(Nokogiri::XML(builder.to_xml).root.to_s, headers)
  RHEVM::Template::new(self, Nokogiri::XML(template).root)
end
create_vm(template_id, opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 122
def create_vm(template_id, opts={})
  opts ||= {}
  raise RHEVMBackendException::new("Requested VM not found in datacenter #{self.current_datacenter.id}") unless template(template_id)
  builder = Nokogiri::XML::Builder.new do
    vm {
      name opts[:name] || "i-#{Time.now.to_i}"
      template_(:id => template_id)
      cluster_(:id => opts[:realm_id].empty? ? clusters.first.id : opts[:realm_id])
      type_ opts[:hwp_id] || 'desktop'
      memory opts[:hwp_memory] ? (opts[:hwp_memory].to_i*1024*1024).to_s : (512*1024*1024).to_s
      cpu {
        topology( :cores => (opts[:hwp_cpu] || '1'), :sockets => '1' )
      }
      if opts[:user_data] and not opts[:user_data].empty?
        if api_version?('3') and cluster_version?((opts[:realm_id] || clusters.first.id), '3')
          custom_properties {
            custom_property({
              :name => "floppyinject",
              :value => "#{RHEVM::FILEINJECT_PATH}:#{opts[:user_data]}",
              :regexp => "^([^:]+):(.*)$"})
          }
        else
          raise BackendVersionUnsupportedException.new
        end
      end
    }
  end
  headers = opts[:headers] || {}
  headers.merge!({
    :content_type => 'application/xml',
    :accept => 'application/xml',
  })
  templates = templates(:id => template_id)
  raise RHEVMBackendException::new("Requested VM not found in datacenter #{self.current_datacenter.id}") if templates.empty?
  headers.merge!(auth_header)
  begin
    vm = RHEVM::client(@api_entrypoint)["/vms"].post(Nokogiri::XML(builder.to_xml).root.to_s, headers)
  rescue
    if $!.respond_to?(:http_body)
      fault = (Nokogiri::XML($!.http_body)/'/fault/detail').first
      fault = fault.text.gsub(/\[|\]/, '') if fault
    end
    fault ||= $!.message
    raise RHEVMBackendException::new(fault)
  end
  RHEVM::VM::new(self, Nokogiri::XML(vm).root)
end
current_datacenter() click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 241
def current_datacenter
  @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first
end
datacenter(datacenter_id) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 245
def datacenter(datacenter_id)
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  rhevm_datacenter = RHEVM::client(@api_entrypoint)["/datacenters/%s" % datacenter_id].get(headers)
  RHEVM::DataCenter::new(self, Client::parse_response(rhevm_datacenter).root)
end
datacenters(opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 222
def datacenters(opts={})
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  rhevm_datacenters = RHEVM::client(@api_entrypoint)["/datacenters"].get(headers)
  Client::parse_response(rhevm_datacenters).xpath('/data_centers/data_center').collect do |dc|
    RHEVM::DataCenter::new(self, dc)
  end
end
destroy_template(id, headers={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 189
def destroy_template(id, headers={})
  headers.merge!({
    :content_type => 'application/xml',
    :accept => 'application/xml',
  })
  tmpl = template(id)
  raise RHEVMBackendException::new("Requested VM not found in datacenter #{self.current_datacenter.id}") unless tmpl
  headers.merge!(auth_header)
  RHEVM::client(@api_entrypoint)["/templates/%s" % id].delete(headers)
  return true
end
has_datacenter?(vm) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 299
def has_datacenter?(vm)
  value=!(vm/'data_center').empty?
  value
end
hosts(opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 254
def hosts(opts={})
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  if opts[:id]
    vm = Client::parse_response(RHEVM::client(@api_entrypoint)["/hosts/%s" % opts[:id]].get(headers)).root
    [ RHEVM::Host::new(self, vm)]
  else
    Client::parse_response(RHEVM::client(@api_entrypoint)["/hosts"].get(headers)).xpath('/hosts/host').collect do |vm|
      RHEVM::Host::new(self, vm)
    end
  end
end
storagedomains(opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 269
def storagedomains(opts={})
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  if opts[:id]
    vm = Client::parse_response(RHEVM::client(@api_entrypoint)["/storagedomains/%s" % opts[:id]].get(headers)).root
    [ RHEVM::StorageDomain::new(self, vm)]
  else
    Client::parse_response(RHEVM::client(@api_entrypoint)["/storagedomains"].get(headers)).xpath('/storage_domains/storage_domain').collect do |vm|
      RHEVM::StorageDomain::new(self, vm)
    end
  end
end
template(template_id) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 213
def template(template_id)
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  rhevm_template = RHEVM::client(@api_entrypoint)["/templates/%s" % template_id].get(headers)
  RHEVM::Template::new(self, Client::parse_response(rhevm_template).root)
end
templates(opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 201
def templates(opts={})
  headers = {
    :accept => "application/xml"
  }
  headers.merge!(auth_header)
  rhevm_templates = RHEVM::client(@api_entrypoint)["/templates"].get(headers)
  Client::parse_response(rhevm_templates).xpath('/templates/template').collect do |t|
    next unless current_datacenter.cluster_ids.include?((t/'cluster').first[:id])
    RHEVM::Template::new(self, t)
  end.compact
end
vm_action(id, action, headers={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 72
def vm_action(id, action, headers={})
  headers.merge!(auth_header)
  headers.merge!({:accept => 'application/xml'})
  vm = vms(:id => id)
  raise RHEVMBackendException::new("Requested VM not found in datacenter #{self.current_datacenter.id}") if vm.empty?
  if action==:delete
    RHEVM::client(@api_entrypoint)["/vms/%s" % id].delete(headers)
  else
    headers.merge!({ :content_type => 'application/xml' })
    begin
      client_response = RHEVM::client(@api_entrypoint)["/vms/%s/%s" % [id, action]].post('<action/>', headers)
    rescue
      if $!.is_a?(RestClient::BadRequest)
        fault = (Nokogiri::XML($!.http_body)/'//fault/detail')
        fault = fault.text.gsub(/\[|\]/, '') if fault
      end
      fault ||= $!.message
      raise RHEVMBackendException::new(fault)
    end
    xml_response = Client::parse_response(client_response)

    return false if (xml_response/'action/status').first.text.strip.upcase!="COMPLETE"
  end
  return true
end
vms(opts={}) click to toggle source
# File lib/deltacloud/drivers/rhevm/rhevm_client.rb, line 55
def vms(opts={})
  headers = {
    :accept => "application/xml; detail=disks; detail=nics; detail=hosts"
  }
  headers.merge!(auth_header)
  if opts[:id]
    vm = Client::parse_response(RHEVM::client(@api_entrypoint)["/vms/%s" % opts[:id]].get(headers)).root
    return [] unless current_datacenter.cluster_ids.include?((vm/'cluster').first[:id])
    [ RHEVM::VM::new(self, vm)]
  else
    Client::parse_response(RHEVM::client(@api_entrypoint)["/vms"].get(headers)).xpath('/vms/vm').collect do |vm|
      next unless current_datacenter.cluster_ids.include?((vm/'cluster').first[:id])
      RHEVM::VM::new(self, vm)
    end.compact
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.