CloudFORMS and Simple WebServices (RUBY)

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.

We will need a few things as follows;

  1. A sample method that we can call
  2. A ruby script to call our web services and execute our sample method.

Here is my sample method, nothing clever, just gonna write to the automate log the result.

</pre>
#
# Automate Method
#
$evm.log("info", "Automate Method Started")
#
# Method Code Goes here
$evm.log("info", " DEMO TEST INSTANCE")
#
#
#
$evm.log("info", "Automate Method Ended")
exit MIQ_OK
<pre>

Next is the ws_demo.rb, which you can find here. https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/CFME/WebServices/RUBY/ws_demo.rb

There are two sections we need to edit as follows;

  1. Webservice credentials
  2. Namespace, Class, Instance and Parameters you want to execute.

Here is the credentials sections, I have highlighted the attributes you need to set;

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

Here is the method section, put your  Namespace, Class, Instance and any Parameters for your sample method.

# 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')

To see the result, run the ws_demo.rb from any machine running Ruby. You will need some GEMs installed

  • ‘savon’
  • ‘httpi’

If all goes well you should see your result the automate log of the appliance you targeted.

So what else can you do with this???? Endless!

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…etc…

This is the complete code listing.

</pre>
###################################
# 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 "#{@method} - #{message}"
end

begin
 log(:info, "Method Started")

#################################
 # 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'] = "namespace=#{cfme_namespace}|class=#{cfme_class}|instance=#{cfme_instance}|message=create"
 body_hash['parameters'] = parms
 body_hash['requester'] = "auto_approve=true"
 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 = "https://#{servername}/vmdbws/wsdl"
 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, "Request Returned: #{request_hash.inspect}")
 log(:info, "Automation Request Id: #{request_hash[:create_automation_request_response][:return].inspect}")
 # Exit method
 log(:info, "Method Ended")
 exit

# Set Ruby rescue behavior
rescue => err
 log(:error, "[#{err}]n#{err.backtrace.join("n")}")
 exit
end
<pre>

Credits

Kevin Morey – What a great example of SOAP in Ruby for CFME, One I shall be using a lot!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s