Call of custom automation method with specific object attributes


#1

Hi,

Working with the automate model, I’m currently looking for a way to run specific methods either through API or custom button.

When the method is called through button call, I’m able to retrieve vm information using $evm.root[‘vm’] according the context of the call.

But, calling the same method through API with ‘api/automation_requests’ doesn’t work, indeed.

One solution is to test the object $evm.root[‘vm’] and use another parameter to look for the vm’s information in the vmdb. This generate few lines of useless code…

Is there a proper way to run API call reproducing the simulation feature that provide a way to specify the object affected by the called method ?

Currently, I found a trick that do the job, but I’m not satisfied with it :

URL : https://x.x.x.x/api/automation_requests
Method : Post
Auth : Basic
json :
{
"version" : "1.1",
"uri_parts" : {
"namespace" : "SelfDeploy/Testing/CustomCall",
"class" : "Methods",
"instance" : "CustomMethod?vmdb_object_type=vm&VmOrTemplate%3A%3Avm=<vm_id>",
"message" : "create"
},
"parameters" : {
"dialog_TestDropDown1" : "42"
},
"requester" : {
"user_name" : "admin",
"auto_approve" : true
}
}

Thanks for your feedback.


#2

We use something like this to identify the vm:

“parameters” => {
“guid” => guid,
“request_id” => request_id
},

In your json, where guid is set to the VMDB guid for the vm that we want to work with.

Then in the code:

vm= $evm.vmdb(‘vm’).find_by_guid($evm.root[‘guid’])

Hope that helps.


#3

This is one option, as I said, to use additional parameters. Seems like it’s currently the supported one.

But I would prefer having the option to specify the related object specifying its type and id directly from the call, just like it’s possible in the simulator.

Thank you for your reply.


#4

Ah. Take a look at ./lib/tasks/evm_automate.rake,

It demonstrates how to use simulate from the rails console. The code something like this:

automate_attrs = {:guid => ‘deb87904-527f-1111-XXXX-fa163ef4df7f’ }
uri = MiqAeEngine.create_automation_object(‘some_request’, automate_attrs, :fqclass => “System/Request”)
results = MiqAeEngine.resolve_automation_object(uri)


#5

Hi,

I notice a difference between the way those calls are handled.

Simulation create an automation object which is immediately call while an API request go through the request/approval process and generate a queued Task.

Also, when I try to give my attribute through the parameters hash like this :

URL : https://x.x.x.x/api/automation_requests
Method : Post
Auth : Basic
json :
{
"version" : "1.1",
"uri_parts" : {
"namespace" : "SelfDeploy/Testing/CustomCall",
"class" : "Methods",
"instance" : "CustomMethod",
"message" : "create"
},
"parameters" : {
"vmdb_object_type" : "vm",
"VmOrTemplate%3A%3Avm" : "101",
"dialog_TestDropDown1" : "42"
},
"requester" : {
"user_name" : "admin",
"auto_approve" : true
}
}

This doesn’t allow me to access the vm object’s property in my method while the generated URI seems correct :

Instantiating [/SelfDeploy/Testing/CustomCall/Methods/CustomMethod?AutomationTask%3A%3Aautomation_task=36&MiqServer%3A%3Amiq_server=1&User%3A%3Auser=1&dialog_testdropdown1=42&object_name=CustomMethod&userid=admin&vmdb_object_type=vm&vmdb_object_type=automation_task&vmortemplate%253a%253avm=101]

When passing the vmdb_object_type through the “instance” parameter (“instance” : “CustomMethod?vmdb_object_type=vm&VmOrTemplate%3A%3Avm=<vm_id>”), this generate this URI :

Instantiating [/SelfDeploy/Testing/CustomCall/Methods/CustomMethod?vmdb_object_type=vm&VmOrTemplate%3A%3Avm=101?AutomationTask%3A%3Aautomation_task=37&MiqServer%3A%3Amiq_server=1&User%3A%3Auser=1&dialog_testdropdown1=42&object_name=CustomMethod%3Fvmdb_object_type%3Dvm%26VmOrTemplate%253A%253Avm%3D101&userid=admin&vmdb_object_type=automation_task]

Which allow me to retrieve vm (id 101) attributes in my method.

I don’t know if this is the expected behavior.

Regards.


#6

This might help: https://pemcg.gitbooks.io/introduction-to-cloudforms-automation/content/chapter21/calling_automation_from_api.html

It contains an example of a generic call_via_api script, and shows how a called automation method can return results to the external caller.

Regards,
pemcg


#7

Hi,

Thanks, useful link, but I already read it and this comfort me in the fact that maybe passing the following parameters should work, but doesn’t :

"parameters" : {
"vmdb_object_type" : "vm",
"VmOrTemplate%3A%3Avm" : "101",
"dialog_TestDropDown1" : "42"
}

Has anyone already tried such API call ?

Regards.


#8

Have you checked what the receiving method reads these as? Try calling object_walker with the same parameters and see what it dumps.

pemcg


#9

(the latest object_walker is here: https://github.com/pemcg/object_walker)