Assigning tags to service & VM using automate method upon provisioning


#1

Hi,

I’ve got a service catalog item and I’m ordering it using REST API. Upon provisioning, I wanted to tag the service & VM being provisioned using automate method.

Method

tag = $evm.root['dialog_dept_tag']
prov = $evm.root['miq_provision']
prov.add_tag(:department, tag) 

Error

ERROR -- : Q-task_id([service_template_provision_task_252]) <AEMethod tag_service>   NoMethodError: undefined method `add_tag' for nil:NilClass

Tried the following but no luck.

  • mapped tag_service method with pre1/post1 step
  • used service_template_provision_request instead of miq_provision

All I’m looking is to get a tag assigned to the Service object & its VMs being provisioned. Thanks for any help to sort this out.


#2

Hi jayyvis,

$evm.root['miq_provision'] is returning nil object. What root objects are available in the method you are trying to use?

You can use a method like this, take from the InspectMe method, to log it:

def dump_root
  $evm.log("info", "Root:<$evm.root> Attributes - Begin")
  $evm.root.attributes.sort.each { |k, v| $evm.log("info", "  Attribute - #{k}: #{v}") }
  $evm.log("info", "Root:<$evm.root> Attributes - End")
  $evm.log("info", "")
end

The add_tag method you are calling is exposed on request and task objects. If you have a handle to the service or VM object you would want to use the tag_assign method.


#3

@gmccullough Thanks for helping me out

Dumped root and log shows that method have got access to service_template_provision_task object

So tried the following code to add tag (ServiceProvision_Template->pre1). Execution went without any errors but the tag didn’t get assigned neither on Service nor VM.

req = $evm.root[‘service_template_provision_task’]
req.add_tag(:department, “accounting”)

Root dumps

  Attribute - ae_result: ok
  Attribute - ae_state: pre1
  Attribute - ae_state_retries: 0
  Attribute - ae_state_started: 2015-03-16 15:38:16 UTC
  Attribute - ae_status_state: on_entry
  Attribute - dialog_agree: t
  Attribute - miq_server: #<MiqAeMethodService::MiqAeServiceMiqServer:0x0000000e55c2e8>
  Attribute - miq_server_id: 1
  Attribute - object_name: test_customization
  Attribute - request: clone_to_service
  Attribute - service_template_provision_task: #<MiqAeMethodService::MiqAeServiceServiceTemplateProvisionTask:0x0000000df4f990>
  Attribute - service_template_provision_task_id: 273
  Attribute - user: #<MiqAeMethodService::MiqAeServiceUser:0x0000000e567738>
  Attribute - user_id: 1
  Attribute - vmdb_object_type: service_template_provision_task


INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service> Root:<$evm.root> Attributes - Begin
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - ae_result: ok
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - ae_state: pre1
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - ae_state_retries: 0
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - ae_state_started: 2015-03-16 15:38:16 UTC
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - ae_status_state: on_entry
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - dialog_agree: t
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - miq_server: #<MiqAeMethodService::MiqAeServiceMiqServer:0x0000000e55c2e8>
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - miq_server_id: 1
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - object_name: test_customization
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - request: clone_to_service
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - service_template_provision_task: #<MiqAeMethodService::MiqAeServiceServiceTemplateProvisionTask:0x0000000df4f990>
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - service_template_provision_task_id: 273
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - user: #<MiqAeMethodService::MiqAeServiceUser:0x0000000e567738>
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - user_id: 1
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service>   Attribute - vmdb_object_type: service_template_provision_task
INFO -- : Q-task_id([service_template_provision_task_273]) <AEMethod tag_service> Root:<$evm.root> Attributes - End

#4

Hello,

I am exactly in the same phase. I would like to assign a Tag to a vm during provisioning. Pls. did you find a way how to set it up?

Thanks,
Karel


#5

Hey,

we figured out this code, and it work’s for us:

prov         = $evm.root['miq_provision'] || $evm.root['miq_provision_request'] || $evm.root['miq_provision_request_template']
vm           = prov.vm

vm.tag_assign("customer/#{customer.downcase}")

the tags has to be present before assigning them, but there is a method for that also, if you’re intressted in, let me know, i have to look for this…

thanks


#6

I’m filtering based on department tags and this code copies the tags of owner group to the vm:

$evm.vmdb(:miq_group).find_by_description(prov.options[:owner_group]).tags(:department).each do |entry| 
  prov.vm.tag_assign("department/#{entry}")
end