Synchronous response from Cloudforms


#1

Hi,

We have an application that is not capable of handling async responses from ManageIQ/Cloudforms. Is there a way to have ManageIQ responde synchronously? To make it more more clear, we’d want to have

url = "https://#{cfserver}/api/automation_requests"
resource = RestClient::Resource.new(url, user, password)
payload = {
“action” => “create”,
“resource” => {
“version” => “1.1”,
“uri_parts” => {
“namespace” => “System”,
“class” => “Request”,
“instance” => “someentry”,
“message” => “create”
},
“parameters” => {
“vm_name” => “vm1234”
},
“requester” => {
“auto_approve” => true
}
}
}

make the automation request , and the response object will be populated in the state machine and returned here.

response = resource.post(payload.to_json, “Content-Type” => “application/json”)
request_result = JSON.parse(response)


#2

@gmccullough can you review this question from @sirvi and forward to a SME if necessary.


#3

@mkanoor Can you help here?


#4

Or perhaps @abellotti ?


#5

If you are running the automation request from a script you can poll for the task till ‘request_state’ is finished.
e.g.
You would have to get the token and urls for your environment. The sample shows us sleeping 10 seconds before we check the request state
The automation_request.txt file has the data from the following URL

require 'rest_client'

token = "654195d9914302ceb2291946d165eb92" 
url = 'http://localhost:4000/api'
automation_url = "#{url}/automation_requests"

body = File.read('automation_request.txt')
response = RestClient::Request.new(:method => :post,
                                   :url => automation_url,
                                   :payload => body,
                                   :headers => {"X-Auth-Token" => token}).execute

parsed_data = JSON.parse(response.body)
request_id = parsed_data['results'][0]['id']
request_url = "#{automation_url}/#{request_id}"
state = ""
puts "Monitoring #{request_url}"
while state != 'finished' do
  sleep(10)
  automation_task = RestClient::Request.new(:method => :get,
                                            :url => request_url,
                                            :headers => {"X-Auth-Token" => token}).execute
  parsed_data = JSON.parse(automation_task.body)
  state = parsed_data['request_state']
  puts "Current State #{state}"
end

#6

If you are trying to do this from inside of Automate you would have to use a state machine and split it into 2 methods

  1. Method 1 Posts the request, stores the request id using $evm.set_state_var(:request_id, id_from_rest_call)

  2. Method 2 does the polling by fetching the id using $evm.get_state_var(:request_id)
    If the request_state is not finished it can set the $evm.root[‘ae_result’] = 'retry’
    If the request state is finished it can set the $evm.root[‘ae_result’] = ‘ok’