[Solved] REST API call to get Tags for a named VM

I have been tasked to create a Ruby script that a Puppet Master can use to call the CloudForms REST API. This is the basic flow I am trying to work out:

  1. VM Provisioned via CloudForms and tagged with puppet-understood tags (e.g.: pp_tagcatagory=tag)
  2. VM comes online and phones home to the Puppet Master (PM) providing its hostname (e.g.: testserver1)
  3. PM calls CF API and asks for all tags for “testserver1"
  4. PM parses tags to get only the puppet related tags and does it’s puppet magic

I have seen in John Hardy’s RESTapi lab where he was doing something similar. But it appears that I have to make 2 calls to get the tags. One to get the server ID and then get the tags. And I am having issues with the filter[]=“name=‘testserver1’” call.

Has anyone done something similar to this? And can you provide me any code, advice or direct me to some additional documentation (I have the latest API Doc).


@abellotti @gtanzillo Please respond

You can query the tags of an object, by specifically asking for that subcollection while querying the details of the object via the expand parameter.


GET /api/vms/:id?expand=tags

so you get both vm details and tags. If there are specific attributes to look for in the vm, or additional virtual attributes, those can also be asked for with the attributes parameter.


GET /api/vms/:id?expand=tags&attributes=ipaddresses,…

1 Like

Note that if you querying vm’s by filter and not a specific :id, you would need to expand resources otherwise you just get the href,

GET /api/vms?filter[]=“name=‘test%’”&expand=resources,tags&attributes=ipaddresses,…

query info @ https://github.com/ManageIQ/manageiq_docs/blob/master/api/query.adoc

Thank you for the information. It helped me create a simple ruby script to get the tags. I’ll include it here in the event someone has a similar need. Obviously we’ll modify this script to dynamically get the MIQ URL and target server name. And I’ll add a check to ensure the server was found prior to trying to get tags from a nil object.

require 'rest-client'
require 'json'

#Hack to disable SSL verification!
require 'openssl'

@user_id = "admin"
@password = "smartvm"
server_url = "www.work.com"
server_name = "server01"

##            'https://admin:smartvm@www.work.com/api/vms?expand=resources&attributes=name,id&filter[]=name=server01'
initial_url = "https://#{@user_id}:#{@password}@#{server_url}/api/vms?expand=resources&attributes=name,id&filter[]=name=#{server_name}"
puts "Inspecting initial_url:  #{initial_url.inspect}"
result=RestClient.get "#{initial_url}"

server = items['resources'].first
#puts "Inspect Server object:  #{server.inspect}"
server_url = server['id']
server_path = server_url[8..-1]
server_name = server['name']
puts "Server Name: #{server_name}  URL:  #{server_url}  Directory: #{server_path}"

tags_url = "https://#{@user_id}:#{@password}@#{server_path}/tags?expand=resources&attributes=name"
#puts "Tags Full URL:  #{tags_url}"
result=RestClient.get "#{tags_url}"

tags_raw['resources'].each do |tag_raw|
  tag = tag_raw['name'].split(/\//)
  tag_catagory = tag[2]
  tag_value = tag[3]
  puts "Catagory=>Tag: #{tag_catagory}=>#{tag_value}"
1 Like