<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[CloudForms Now]]></provider_name><provider_url><![CDATA[http://cloudformsblog.redhat.com]]></provider_url><author_name><![CDATA[johnhardy36]]></author_name><author_url><![CDATA[https://cloudformsblog.redhat.com/author/johnhardy36/]]></author_url><title><![CDATA[Create Template from VM&nbsp;(RHEV)]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>CloudFORMS lacks currently the ability to create a template from an existing VM, here is the method to enable this function from a custom button.</p>
<p>First you need a dialog to collect from the new user the name of the template, so create a dialog of your liking, including at least;</p>
<p><strong>Textbox &#8211; Name = dialog_name</strong></p>
<p><!--more--></p>
<p>&nbsp;</p>
<p>Next is to create the custom button, so create a new group to take the button, and in it a button that calls your new dialog you created in the previous step, along with a request instance called create_template. Don&#8217;t forget you are creating the button in the VM group.</p>
<p>&nbsp;</p>
<p>Now for the automate bits,</p>
<ol>
<li>Create an instance in <strong>/system/request</strong> called <strong>create_template</strong>.</li>
<li>Edit your new instance and wire it to <strong>/Sample/Methods/create_template</strong></li>
<li>Create an instance in <strong>/Sample/Methods</strong> called <strong>create_template</strong>.</li>
<li>Create an method in <strong>/Sample/Methods</strong> called <strong>create_template</strong>.</li>
<li>add the following code to your new method;</li>
</ol>
<pre class="brush: ruby; title: ; notranslate" title="">&lt;/pre&gt;
#

#            Automate Method

#

$evm.log(&quot;info&quot;, &quot;Automate Method Started&quot;)

#

#            Method Code Goes here

#

&amp;nbsp;

#!/usr/bin/ruby

require 'rubygems'

require 'rest_client'

vm = $evm.root['vm']

ext_management_system = vm.ext_management_system

$evm.log(&quot;info&quot;, &quot;VM Name - #{vm.name}&quot;)

$evm.log(&quot;info&quot;, &quot;VM UID - #{vm.uid_ems}&quot;)

$evm.log(&quot;info&quot;, &quot;RHEVM Name - #{ext_management_system.name}&quot;)

$dialog_name = ' '

$evm.root.attributes.sort.each { |k, v|

$evm.log(&quot;info&quot;,&quot;#{k}---#{v}&quot;)

if &quot;#{k}&quot; == &quot;dialog_name&quot;

$dialog_name = &quot;#{v}&quot;

$evm.log(&quot;info&quot;, &quot;Found #{$dialog_name}&quot;)

end

}

rhevm = &quot;https://#{ext_management_system.ipaddress}/api/templates&quot;

rhevadmin = 'admin@internal'

rhevadminpass = 'monster'

resource = RestClient::Resource.new(rhevm, :user =&gt; rhevadmin, :password =&gt; rhevadminpass)

createTemplate = resource.post &quot;&lt;template&gt;&lt;name&gt;#{$dialog_name}&lt;/name&gt;&lt;vm id=&quot;#{vm.uid_ems}&quot;&gt;&lt;/vm&gt;&lt;/template&gt;&quot;, :content_type =&gt; 'application/xml', :accept =&gt; 'application/xml'

$evm.log(&quot;info&quot;, &quot;Result - #{createTemplate}&quot;)

#

#

#

$evm.log(&quot;info&quot;, &quot;Automate Method Ended&quot;)

exit MIQ_OK

</pre>
<p>So to test, select a VM and execute your button, in the log you should see something like;</p>
<pre class="brush: plain; title: ; notranslate" title="">&lt;/pre&gt;
[----] I, [2013-05-08T11:00:27.509399 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; Automate Method Started
[----] I, [2013-05-08T11:00:27.623669 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; VM Name - temtest1
[----] I, [2013-05-08T11:00:27.625774 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; VM UID - 42916780-a571-44a6-aae4-a4ae3c07ee20
[----] I, [2013-05-08T11:00:27.628009 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; RHEVM Name - RHEV-M (192.168.201.51)
[----] I, [2013-05-08T11:00:27.639567 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; dialog_name---Template1
[----] I, [2013-05-08T11:00:27.640451 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; Found Template1
[----] I, [2013-05-08T11:00:27.642538 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; miq_server---#&lt;MiqAeMethodService::MiqAeServiceMiqServer:0x0000000c4f3510&gt;
[----] I, [2013-05-08T11:00:27.643877 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; miq_server_id---1
[----] I, [2013-05-08T11:00:27.645208 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; object_name---Request
[----] I, [2013-05-08T11:00:27.646342 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; request---createTemplate
[----] I, [2013-05-08T11:00:27.648066 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; user---#&lt;MiqAeMethodService::MiqAeServiceUser:0x0000000c511718&gt;
[----] I, [2013-05-08T11:00:27.650808 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; user_id---1
[----] I, [2013-05-08T11:00:27.654278 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; vm---temtest1
[----] I, [2013-05-08T11:00:27.655532 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; vm_id---40
[----] I, [2013-05-08T11:00:27.656746 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; vmdb_object_type---vm
[----] I, [2013-05-08T11:00:28.135843 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; Result - &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;template href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679&quot; id=&quot;56527a40-9fbf-4f46-a652-e68ee0521679&quot;&gt;
 &lt;actions&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/export&quot; rel=&quot;export&quot;/&gt;
 &lt;/actions&gt;
 &lt;name&gt;Template1&lt;/name&gt;
 &lt;creation_status&gt;
 &lt;state&gt;pending&lt;/state&gt;
 &lt;/creation_status&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/disks&quot; rel=&quot;disks&quot;/&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/nics&quot; rel=&quot;nics&quot;/&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/cdroms&quot; rel=&quot;cdroms&quot;/&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/permissions&quot; rel=&quot;permissions&quot;/&gt;
 &lt;link href=&quot;/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/creation_status/c036ecce-facf-479a-9b02-2cb7da154252&quot; rel=&quot;creation_status&quot;/&gt;
 &lt;type&gt;server&lt;/type&gt;
 &lt;status&gt;
 &lt;state&gt;locked&lt;/state&gt;
 &lt;/status&gt;
 &lt;memory&gt;4294967296&lt;/memory&gt;
 &lt;cpu&gt;
 &lt;topology sockets=&quot;1&quot; cores=&quot;1&quot;/&gt;
 &lt;/cpu&gt;
 &lt;os type=&quot;rhel_6x64&quot;&gt;
 &lt;boot dev=&quot;hd&quot;/&gt;
 &lt;kernel&gt;&lt;/kernel&gt;
 &lt;initrd&gt;&lt;/initrd&gt;
 &lt;cmdline&gt;&lt;/cmdline&gt;
 &lt;/os&gt;
 &lt;cluster href=&quot;/api/clusters/af2f210e-6ab2-11e2-8023-000c292b2e63&quot; id=&quot;af2f210e-6ab2-11e2-8023-000c292b2e63&quot;/&gt;
 &lt;creation_time&gt;2013-05-08T12:00:27.780+01:00&lt;/creation_time&gt;
 &lt;origin&gt;rhev&lt;/origin&gt;
 &lt;high_availability&gt;
 &lt;enabled&gt;false&lt;/enabled&gt;
 &lt;priority&gt;1&lt;/priority&gt;
 &lt;/high_availability&gt;
 &lt;display&gt;
 &lt;type&gt;spice&lt;/type&gt;
 &lt;monitors&gt;1&lt;/monitors&gt;
 &lt;allow_override&gt;false&lt;/allow_override&gt;
 &lt;/display&gt;
 &lt;stateless&gt;false&lt;/stateless&gt;
 &lt;usb&gt;
 &lt;enabled&gt;false&lt;/enabled&gt;
 &lt;/usb&gt;
&lt;/template&gt;

[----] I, [2013-05-08T11:00:28.136449 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) &lt;User-Defined Method&gt; Automate Method Ended
[----] I, [2013-05-08T11:00:28.144107 #4118:28ef7f4] INFO -- : Q-task_id([resource_action_3]) Method exited with rc=MIQ_OK

</pre>
<p>and  in RHEV-M you should see your new template&#8230;</p>
<p>&nbsp;</p>
<p>Method code available here&#8230;<a href="https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/RHEV/Templates/create_template.rb">https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/RHEV/Templates/create_template.rb</a></p>
<h3>Credit</h3>
<p>Matteo Bernacchi &#8211; How nice are these RHEV RestAPI&#8217;s !</p>
]]></html><thumbnail_url><![CDATA[https://i1.wp.com/s2.wp.com/wp-content/themes/vip/rh-parent/img/og_shadowman.png?ssl=1&fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[200]]></thumbnail_width><thumbnail_height><![CDATA[200]]></thumbnail_height></oembed>