Hey everyone.
So I just finished my script for syncing Manageiq/cloudforms and vmware vcenter tags.
It’s kind of specific for the company where i’m doing my bachelor’s thesis, but it’s not that complicated and easy to adjust to your needs.
It’s made to only sync tags from specific categories i made in manageiq/cloudforms and vmware.
To use it you need to install “httparty” on your manageiq/cloudforms instance, i’ve tested it with httparty version 0.16.2.
It’s certainly not perfect but it works for my needs.
This is the code:
puts "Starting VMware TAG integration"
require 'httparty'
prov = $evm.root['miq_provision']
vmname = prov.get_option(:vm_target_name)
tags = prov.get_tags
puts "vm-tags: #{tags}"
tagcodename = tags[:cat_code]
tagbackupname = tags[:cat_backup]
tagreplicationname = tags[:cat_replication]
cat_id_code = $evm.object['cat_id_code']
cat_id_backup = $evm.object['cat_id_backup']
cat_id_replication = $evm.object['cat_id_replication']
base_url = $evm.object['base_url']
def getToken(base_url)
puts "VMware TAG integration Entering getToken"
auth = {:username => $evm.object['username'], :password => $evm.object.decrypt('password')}
url = "#{base_url}/com/vmware/cis/session"
response = HTTParty.post(url,
:basic_auth => auth, :verify => false)
puts "VMware Debug URL. Auth is #{auth} <>"
puts "VMware Debug URL. Response is #{response} <>"
token = response.parsed_response["value"]
puts "token = #{token}"
return token
end
def getTags(token, base_url, category)
puts "VMware TAG integration Entering getTags"
tags = Array.new
response = HTTParty.post("#{base_url}/com/vmware/cis/tagging/tag/id:#{category}?~action=list-tags-for-category",
:headers => { "vmware-api-session-id" => token },
:verify => false)
unless response.parsed_response["value"].nil?
tags = response.parsed_response["value"]
end
tags
end
#loop through each vmware tagname and see which one matches our name
def findTagMatch(token, base_url, tagname, tags)
puts "VMware TAG integration Entering findTagMatch"
tags.each do |tag|
response = HTTParty.get("#{base_url}/com/vmware/cis/tagging/tag/id:#{tag}",
:headers => { "vmware-api-session-id" => token },
:verify => false)
unless response.parsed_response["value"].nil?
puts "TagName: #{response.parsed_response["value"]["name"]}"
if response.parsed_response["value"]["name"] == tagname
puts "Tag found that matches #{34.chr}#{tagname}#{34.chr}"
return tag
end
end
end
end
def newTag(token, base_url, tagname, cat_id_code)
puts "VMware TAG integration Entering newTag"
if tagname =~ /[a-z][a-z][a-z]\d{4}/
category = cat_id_code
else abort "Defined tag name is not valid"
end
payload = {
create_spec: {
category_id: category,
description: "",
name: tagname
}
}.to_json
response = HTTParty.post("#{base_url}/com/vmware/cis/tagging/tag",
:headers => { "vmware-api-session-id" => token, "Content-Type" => "application/json" },
:verify => false, :body => payload)
unless response.parsed_response["value"].nil?
puts "Tag #{34.chr}#{tagname}#{34.chr} with id #{34.chr}#{response.parsed_response["value"]}#{34.chr} is made"
return response.parsed_response["value"]
end
end
def getVMID(token, base_url, vmname)
puts "VMware TAG integration Entering getVMID"
response = HTTParty.get("#{base_url}/vcenter/vm?filter.names=#{vmname}",
:headers => { "vmware-api-session-id" => token },
:verify => false)
unless response.parsed_response["value"].nil?
return response.parsed_response["value"][0]["vm"]
end
end
def attachTagObject(token, base_url, vmid, codetag, backuptag, replicationtag)
puts "VMware TAG integration Entering attachTagObject"
payload = {
object_id: {
id: vmid,
type: "VirtualMachine"
},
tag_ids: [
codetag,
backuptag,
replicationtag
]
}.to_json
response = HTTParty.post("#{base_url}/com/vmware/cis/tagging/tag-association?~action=attach-multiple-tags-to-object",
:headers => { "vmware-api-session-id" => token, "Content-Type" => "application/json" },
:verify => false, :body => payload)
unless response.parsed_response["value"].nil?
puts "Tag attached to #{vmid} #{response.parsed_response["value"]}"
end
end
#Get a token to use
mytoken = getToken(base_url)
#Get array of all codetags in vcenter
codetags = getTags(mytoken, base_url, cat_id_code)
puts "Code Tag-id's: #{codetags}"
#Get array of all backuptags in vcenter
backuptags = getTags(mytoken, base_url, cat_id_backup)
puts "Backup Tag-id's: #{backuptags}"
#Get array of all replicationtags in vcenter
replicationtags = getTags(mytoken, base_url, cat_id_replication)
puts "Replication Tag-id's: #{replicationtags}"
#Get the tagID of the vcenter codetag matching the manageiq codetag
codetag = findTagMatch(mytoken, base_url, tagcodename, codetags)
if codetag.kind_of?(Array)
codetag = "Tag not found"
end
puts "Code Tag-id: #{codetag}"
#if tag doesn't exist in vcenter, make a new one
if codetag == "Tag not found"
codetag = newTag(mytoken, base_url, tagcodename, cat_id_code)
end
#Get the tagID of the vcenter backuptag matching the manageiq backuptag
backuptag = findTagMatch(mytoken, base_url, tagbackupname, backuptags)
puts "Backup Tag-id: #{backuptag}"
#Get the tagID of the vcenter replicationtag matching the manageiq replicationtag
replicationtag = findTagMatch(mytoken, base_url, tagreplicationname, replicationtags)
puts "Replication Tag-id: #{replicationtag}"
#Get the id of the new VM in vcenter
vmid = getVMID(mytoken, base_url, vmname)
puts "VM-id: #{vmid}"
#Attach the tag to the vm
attachTagObject(mytoken, base_url, vmid, codetag, backuptag, replicationtag)
It needs to get called after the vm is provisioned so it can retrieve the vm-id from the vcenter appliance.
When it works your logs should look something like this:
I’ve also exported my code from my manageiq appliance so you can easily import it into yours 
Tagging.rar (7.3 KB)