<?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[CloudFORMS and Simple WebServices&nbsp;(RUBY)]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>This post will solve anything! Quite simply anything you can script, code or model in CloudFORMS you can call externally too using simple web services.</p>
<p>We will need a few things as follows;</p>
<ol>
<li>A sample method that we can call</li>
<li>A ruby script to call our web services and execute our sample method.</li>
</ol>
<p><!--more--></p>
<p>Here is my sample method, nothing clever, just gonna write to the automate log the result.</p>
<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
$evm.log(&quot;info&quot;, &quot; DEMO TEST INSTANCE&quot;)
#
#
#
$evm.log(&quot;info&quot;, &quot;Automate Method Ended&quot;)
exit MIQ_OK
&lt;pre&gt;
</pre>
<p>Next is the ws_demo.rb, which you can find here. <a href="https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/CFME/WebServices/RUBY/ws_demo.rb">https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/CFME/WebServices/RUBY/ws_demo.rb</a></p>
<p>There are two sections we need to edit as follows;</p>
<ol>
<li>Webservice credentials</li>
<li>Namespace, Class, Instance and Parameters you want to execute.</li>
</ol>
<p>Here is the credentials sections, I have highlighted the attributes you need to set;</p>
<pre class="brush: ruby; title: ; notranslate" title="">#
Set username, password, CFME Server below
username, password, servername = 'ws', 'smartvm', '192.168.201.20'
</pre>
<p>Here is the method section, put your  Namespace, Class, Instance and any Parameters for your sample method.</p>
<pre class="brush: ruby; title: ; notranslate" title=""># Build a hash with the path to the instance you want to run
body_hash = build_body_hash('Sample', 'Methods', 'demo', 'foo=bar|hello=world')
</pre>
<p>To see the result, run the ws_demo.rb from any machine running Ruby. You will need some GEMs installed</p>
<ul>
<li>&#8216;savon&#8217;</li>
<li>&#8216;httpi&#8217;</li>
</ul>
<p>If all goes well you should see your result the automate log of the appliance you targeted.</p>
<p>So what else can you do with this???? Endless!</p>
<p><em>Delete a VM, migrate a host, migrate a vm, stop a vm, start a vm, configure a load balancer, deploy some applications, tag some objects, etc&#8230;etc&#8230;</em></p>
<p>This is the complete code listing.</p>
<pre class="brush: ruby; title: ; notranslate" title="">&lt;/pre&gt;
###################################
# EVM Automate Method: call_CFME.rb
# Notes: This method is used to call CFME via SOAP to run an automate method
#
###################################
# Method for logging
def log(level, message)
 @method = 'call_CFME'
 puts &quot;#{@method} - #{message}&quot;
end

begin
 log(:info, &quot;Method Started&quot;)

#################################
 # Method: buildBody
 # Notes: Build a hash of values for the automation request
 #################################
 def build_body_hash(cfme_namespace, cfme_class, cfme_instance, parms)
 body_hash = {}
 body_hash['version'] = '1.1'
 body_hash['uri_parts'] = &quot;namespace=#{cfme_namespace}|class=#{cfme_class}|instance=#{cfme_instance}|message=create&quot;
 body_hash['parameters'] = parms
 body_hash['requester'] = &quot;auto_approve=true&quot;
 return body_hash
 end

#################################
 # Method: createAutomationRequest
 # Notes: Create a SOAP call to EVM and submit an Automation Request
 #################################
 def createAutomationRequest(body_hash, username, password, servername)
 gem 'savon', '0.8.5'

require 'rubygems'
 require 'savon'
 require 'httpi'

HTTPI.log_level = :debug # changing the log level
 HTTPI.log = false
 HTTPI.adapter = :net_http # [:httpclient, :curb, :net_http]

# Setup Savon Configuration
 Savon.configure do |config|
 config.log = false
 config.log_level = :debug # changing the log level
 end
puts username
puts password
puts servername
puts body_hash

 # Set up Savon client
 client = Savon::Client.new do |wsdl, http|
 wsdl.document = &quot;https://#{servername}/vmdbws/wsdl&quot;
 http.auth.basic username, password
 http.auth.ssl.verify_mode = :none
 end

response = client.request :create_automation_request do
 soap.body = body_hash
 end
 return response.to_hash
 end

# Set username, password, CFME Server below
 username, password, servername = 'ws', 'smartvm', '192.168.201.20'

 # Build a hash with the path to the instance you want to run
 body_hash = build_body_hash('Sample', 'Methods', 'demo', 'foo=bar|hello=world')

 # Call CFME via SOAP
 request_hash = createAutomationRequest(body_hash, username, password, servername)

log(:info, &quot;Request Returned: #{request_hash.inspect}&quot;)
 log(:info, &quot;Automation Request Id: #{request_hash[:create_automation_request_response][:return].inspect}&quot;)
 # Exit method
 log(:info, &quot;Method Ended&quot;)
 exit

# Set Ruby rescue behavior
rescue =&gt; err
 log(:error, &quot;[#{err}]n#{err.backtrace.join(&quot;n&quot;)}&quot;)
 exit
end
&lt;pre&gt;
</pre>
<h3>Credits</h3>
<p><em>Kevin Morey</em> &#8211; What a great example of SOAP in Ruby for CFME, One I shall be using a lot!</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>