def create_vm(template_id, opts={})
opts ||= {}
templ = template(template_id)
raise RHEVMBackendException::new("Requested VM not found in datacenter #{self.current_datacenter.id}") unless templ
builder = Nokogiri::XML::Builder.new do
vm {
name opts[:name] || "i-#{Time.now.to_i}"
template_(:id => template_id)
cluster_(:id => opts[:realm_id].nil? ? templ.cluster.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