VMware REST API标签关联



我正在尝试将VMware标签与vSphere 6.5的REST API相关联。该方法的API文档缺乏:https://vdc-repo.vmware.com/vmwb-repository/dcr-public/1cd28284-3b72-485-9885-9e31 c6 e31-d1c6d9e29e29e26686/71EF73304-A6C.933333B2-23B2-23B2-23B2-13B2-13B2-13B2-13B2-13B2-13B2-1AB2-1AB.AR/operations/com/vmware/cis/tagging/tag_association.attach-operation.html

特别是我努力寻找有关对象ID类型的任何信息,以及应通过两个ID(不同的来源给出不同的答案)的任何信息。有关这些字段格式和内容的任何信息将不胜感激。

这是我目前的帖子。在虚拟机创建过程中,ID正在从API中拉出。

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返回404,找不到错误标记对象,该对象类型为cis.tagging.objectnotfound.error。

将其解决

正确的帖子格式:

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"}}

我从powershell的VMware(带有PowerCli模块)获得了此工作代码:

$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)
}

最新更新