API automate substitution of vm_templates

Hi Folks,
I’m trying to automate vm_src_id substitution process within Manageiq service templates in service catalog. I wrote few API calls and some parsing mechanisms to filter nicely catalog items which use specific vm_template. Everything works nice however I’m stuck with HTTP PATCH request to replace data within config_info: { vm_src_id : [ template_id, “template_name” ] } at manageiqurl/api/service_templates/service_id#. I’ve tried to send data using { “action”: “edit”, “path”: “config_info/src_vm_id”, “value”: [ 123456677990, “img_name” ] } as described in cloudforms blog https://cloudformsblog.redhat.com/2018/07/30/playing-with-rest-api/

Request body looks ok, however debug log says: MIQ(Api::ServiceTemplatesController,patch_resource) Can only patch attributes of the resource service_templates/service_id#

Thus I’m only able to update resources on top level like name of service template etc. Is there any other way to update those nested fields using API request?


Has anyone had a chance to look on this API case? Maybe you have different solution such as querying database directly instead?


You can always manipulate stuff in the database via rails console.
Why do you need this in the first place? Is this a export/import use-case?

Regarding the original question. If patching nested attributes does not work, I assume you already tried to patch the whole config_info hash?

Buc, thanks for your reply. This functionality would be useful in dynamic environments where you use ‘golden image / vm template’ for multiple services in catalog. Once vm template has been updated, you would like to replace it in all related catalog items. The more services use same vm template the more sense this functionality would have. Especially when vm template changes often.

You are right, I’ve tried to patch partialy as described previously. Then I’ve tried to edit whole config_info section using POST method however despite status code 200, values of config_info hash persisted untouched. I would prefer use API calls for this purpose as those are ‘safer’ rather than direct rails console operations.

ManageIQ has import/export scripts for Service Catalog items. They don’t work for us because they link the catalog item with a non-existing vm template, but you may be able to fix that if you do a deep dive on the code.

The ManageIQ guys are currently migrating the export scripts in the product proper, but it appears that they don’t have migrated catalog items yet
Old Implementation: https://github.com/rhtconsulting/cfme-rhconsulting-scripts/blob/master/rhconsulting_service_catalogs.rake
New implementation: https://github.com/ManageIQ/manageiq/tree/master/lib/task_helpers/imports

If that does not help, I guess you won’t have another chance other than replacing the VM template in the database. At least I am not aware of anything and it does not seem to be exposed on the AutomationEngine Service Models

In case you haven’t found the VM template yourself

catalog_item = $evm.vmdb(:service_template).where(...).first
request_template = catalog_item.service_resources.first.resource
database_object = request_template.instance_variable_get(:@object)
database_object.source = new_vm_template.id

This should be the pseudo code to do the thing you want (I haven’t tested it). basically you are just replacing the source_id that points to the VM template in the database.