VMWARE rest api tag关联

时间:2017-09-12 15:11:44

标签: rest vmware vsphere

我试图将vmware标记与vsphere 6.5的其余api上的虚拟机关联起来。缺少此方法的api文档:https://vdc-repo.vmware.com/vmwb-repository/dcr-public/1cd28284-3b72-4885-9e31-d1c6d9e26686/71ef7304-a6c9-43b3-a3cd-868b2c236c81/doc/operations/com/vmware/cis/tagging/tag_association.attach-operation.html

特别是我努力寻找关于对象id类型的任何信息以及应该作为两个id传递什么(不同的源给出不同的答案)。任何有关这些领域的格式和内容的信息都将受到赞赏。

这是我目前的帖子。在虚拟机创建过程中会从api中提取ID。

POST /rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:<hex number>:GLOBAL?~action=attach

Json编码的身体:

{"object_id":{"id":"Elstree_vm-<4 digit number>","type":"VirtualMachine"}}

api返回404s并找不到错误标记对象,类型为cis.tagging.objectNotFound.error。

2 个答案:

答案 0 :(得分:1)

完成了工作

正确的帖子格式:

POST /rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:<hex number>:GLOBAL?~action=attach

Json编码的身体

{"object_id":{"id":"vm-<4 digit number>","type":"VirtualMachine"}}

答案 1 :(得分:0)

我在PowerShell(使用powercli模块)中从VMware获得了这个工作代码:

$creds = Get-Credential
$vCenter = 'vcenter.fqdn'
$tagName = "WebServer"
$vmName = "web01"

Connect-CisServer -Server $vCenter -Credential $creds
Connect-ViServer -Server $vCenter -Credential $creds

$tagSvc = Get-CisService -Name com.vmware.cis.tagging.tag
$tagList = $tagSvc.list()
$tags = @()
foreach ($t in $tagList) {
    $tags += $tagSvc.Get($t)
}
$tag = $tags | where {$_.Name -eq $tagName}
$vm = Get-VM -Name $vmName
$tagAssoc = Get-CisService -Name com.vmware.cis.tagging.tag_association
$objId = $tagAssoc.Help.attach.object_id.Create()
$objId.type = $vm.ExtensionData.MoRef.Type
$objId.id = $vm.ExtensionData.MoRef.Value
$tagAssoc.attach($tag.id.Value, $objId)

$attachedTags = $tagAssoc.list_attached_tags($objId)
foreach ($at in $attachedTags) {
    $tagSvc.Get($at)
}