Как настроить теги виртуальной машины через vsphere rest api

#tags #virtual-machine #vsphere

#Теги #виртуальная машина #vsphere

Вопрос:

Я пишу это, чтобы привести примеры того, как создать тег виртуальной машины с помощью vsphere api.Мне было трудно получить рабочий процесс.Надеюсь, это облегчит чью-то жизнь. Используемый api explorer или центр разработчиков: https:///ui/app/devcenter/api-explorer

Ответ №1:

Получить все теги :

 curl -X GET 'https://10.195.233.120/rest/com/vmware/cis/tagging/tag' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800'
   Response:

    enter code here

{
    "value": [
        "urn:vmomi:InventoryServiceTag:c8249785-ca13-4efa-add6-f0818d1fb7a2:GLOBAL"
    ]
}

Get  a particular tag

Curl -X GET 'https://10.195.233.120/rest/com/vmware/cis/tagging/tag/id:urn:vmomi:InventoryServiceTag:c8249785-ca13-4efa-add6-f0818d1fb7a2:GLOBAL' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800'
Response:
{
    "value": {
        "category_id": "urn:vmomi:InventoryServiceCategory:b0bdec18-7714-4ad5-941a-7c111ab57f54:GLOBAL",
        "name": "vm1",
        "description": "",
        "id": "urn:vmomi:InventoryServiceTag:c8249785-ca13-4efa-add6-f0818d1fb7a2:GLOBAL",
        "used_by": []
    }
}

Get tag category :
curl -X GET 'https://10.195.233.120/rest/com/vmware/cis/tagging/category' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800'

Response:
{
    "value": [
        "urn:vmomi:InventoryServiceCategory:b0bdec18-7714-4ad5-941a-7c111ab57f54:GLOBAL"
 ]}


Get information on a  tag category :

curl -X GET 'https://10.195.233.120/rest/com/vmware/cis/tagging/category/id:urn:vmomi:InventoryServiceCategory:b0bdec18-7714-4ad5-941a-7c111ab57f54:GLOBAL' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800

Response:
{
    "value": {
        "associable_types": [
            "VirtualMachine"
        ],
        "name": "cat1",
        "description": "",
        "id": "urn:vmomi:InventoryServiceCategory:b0bdec18-7714-4ad5-941a-7c111ab57f54:GLOBAL",
        "used_by": [],
        "cardinality": "SINGLE"
    }
}

Create a Category : 

curl -X POST 'https://10.195.233.120/rest/com/vmware/cis/tagging/category' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800' -H 'Content-type: application/json' -d '{ "create_spec": { "associable_types": [ "VirtualMachine" ], "cardinality": "SINGLE", "description": "desc 2", "name": "CAT2" } }'

Response : with created category ID 
{
    "value": "urn:vmomi:InventoryServiceCategory:f148c399-9edd-4ae9-a743-276a4cdec39b:GLOBAL"
}

Create a TAG


curl -X POST 'https://10.195.233.120/rest/com/vmware/cis/tagging/tag' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800' -H 'Content-type: application/json' -d '{ "create_spec": { "category_id": "urn:vmomi:InventoryServiceCategory:f148c399-9edd-4ae9-a743-276a4cdec39b:GLOBAL", "description": "value for category CAT2", "name": "tagValue2", "tag_id": "" } }'

Response with tag ID

{
    "value": [
        "urn:vmomi:InventoryServiceTag:f19eb54d-fa0c-4dac-9a4c-f5b89aa3a19e:GLOBAL"
    ]
}



Associate Tag to a VM :[ Use the tag ID here]

curl -X POST 'https://10.195.233.120/rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:f19eb54d-fa0c-4dac-9a4c-f5b89aa3a19e:GLOBAL?~action=attach' -H 'vmware-api-session-id: db8a50b84d02eeae1cfd0c83c697b800' -H 'Content-type: application/json' -d '{ "object_id": { "id": "vm-14", "type": "VirtualMachine" } }'

Response : Status: 200, OK
 

Вот как это сделать.