Sync Vmware and ManageIQ tags (VM tags)

fine
providers
ui

#1

Hi all,

Please help me to find out how to solve Tags synchronization ManageIQ and Vmware ,
Does anybody has sync with ManageIQ and Vmware Tags ?

I have read AWS Tag Synchronization
which is 3 years ago published . so many things has been changed and maybe someone can point me to good documentation how to sync Tags ?

Maybe I can ask @carbonin to point me to right person ?

Tested in 2 different ways . 1. Created Vm in ManageIQ and assigned Tags ( which was not assigned in Vmware) . 2. Create Tag in Vmware and assigned to VM ( this vm in ManageIQ has no tags assigned to it )

So somewhere I miss sync configuration between ManageIQ -> Vmware

Regards
Kalinux


#2

I’m not sure about this. @agrare would know if we pull in tag information from VMware inventory. If we don’t do any tag sync, maybe that’s a thing you could do through automate? @gmccullough should be able to point you in the right direction there.


#3

Thank you @carbonin ! I appreciate your fast and awesome support .


#4

Hey @kalinux, we talked about this and there is an RFE open [https://bugzilla.redhat.com/show_bug.cgi?id=1382488] but the client libraries from VMware to retrieve the tags from vSphere aren’t in a state where we can readily use them yet.


#5

Hi @agrare,

Thanks for info . Then we have to postpone this discussion until Vmware build API logic for Tags.
Very satisfied how fast you guys reacting . Thanks a lot .!

Regards
Kalinux


#6

Hey @kalinux,

I’m just getting into manageiq and i wanted to do the same thing as you.
After doing some research i’ve discovered that vsphere 6.5 has a rest api that includes methods to get all tags, attach tags to vm’s, create a new tag,…
Here’s the link to the documentation
Using this you can write some ruby scripts to automate it. I’m probably going to try it myself, and i can share my results if u want.


#7

Hi @Brackaboy,

That`'s awesome! please share results here , maybe not necessary me but other users will need this script/solution. We solved it by using only ManageIQ tags and not Vcenter.


#8

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 :slight_smile:

Tagging.rar (7.3 KB)