【问题标题】:VMWARE rest api tag associationVMWARE REST API 标签关联
【发布时间】:2017-09-12 15:11:43
【问题描述】:

我正在尝试通过 vsphere 6.5 的其余 api 将 vmware 标签与虚拟机相关联。缺少此方法的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 传递的信息(不同的来源给出不同的答案)。任何有关这些字段的格式和内容的信息将不胜感激。

这是我当前的帖子。 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,错误Tagging object not found,类型为cis.tagging.objectNotFound.error。

【问题讨论】:

  • 你在哪里找到了获取VirtualMachine的类型列表?我一直在寻找几个小时试图找到这个,你的片段帮助了我......我一直在使用string,就像vdc-repo.vmware.com/vmwb-repository/dcr-public/… 的 API 文档示例一样——谢谢!!!

标签: rest vmware vsphere


【解决方案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"}}

【讨论】:

  • com.vmware.vapi.rest.unsupportedMediaType
【解决方案2】:

我在 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多