Replace (Not Add) Category On VM Via RestAPI | Nutanix Community
Skip to main content
Solved

Replace (Not Add) Category On VM Via RestAPI

  • November 8, 2024
  • 5 replies
  • 54 views

Greetings everyone,

I have been successful at creating a rest api call to ADD a category to the VM.  The issue is we only want 1 category per VM, so if the VM already has a category assigned we want it to REPLACE the value and not ADD it.  With my current code (ie: use GET command of VM, remove Status, replace categories and categories_mapping then use PUT command) the JSON object looks good before uploading but afterwards I see the new category and the old one.  I tried using a PATCH command but AHV doesn’t like that (and it isn’t in the documentation either).  

 

New Category = NEW_CAT

VM Original State:
    "categories": {

      "OLD_CAT": "OLD_CAT",
    }

 

VM Post My Update State:
    "categories": {

      "OLD_CAT": "OLD_CAT",
      "NEW_CAT": "NEW_CAT"
    }

 

VM Desired State:
    "categories": {

      "NEW_CAT": "NEW_CAT"
    }


Any help would be greatly appreciated.

Best answer by Smmartin330

Oh, my bad, the links ARE wrong.
Here is the API call to disassociate a category from a VM, hopefully this link takes you directly to the API call. https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/disassociateCategories

View original
Did this topic help you find an answer to your question?
This topic has been closed for comments

5 replies

  • Voyager
  • 2 replies
  • November 14, 2024

there is no “replace” action, so the way I would do this is to write a script iterates through each VM and for each VM:
1. get the current categories - https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/getVmById
2. removes the current categories - https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/getVmById
3. adds the desired category https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/getVmById


  • Author
  • Adventurer
  • 3 replies
  • November 14, 2024

Thank you for the reply,

All the links you provided are actually the same link.  Your approach makes sense I just need to understand how to remove categories since the objects don’t function like other data I have been able to alter.


  • Voyager
  • 2 replies
  • Answer
  • November 14, 2024

Oh, my bad, the links ARE wrong.
Here is the API call to disassociate a category from a VM, hopefully this link takes you directly to the API call. https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/disassociateCategories


  • Author
  • Adventurer
  • 3 replies
  • November 14, 2024
Smmartin330 wrote:

Oh, my bad, the links ARE wrong.
Here is the API call to disassociate a category from a VM, hopefully this link takes you directly to the API call. https://developers.nutanix.com/api-reference?namespace=vmm&version=v4.0.b1#tag/Vm/operation/disassociateCategories

Thank you.  I will give it a try.  Cheers.


  • Author
  • Adventurer
  • 3 replies
  • November 15, 2024

It seems like I have to wait until our systems are running V4 of the API (still on V3) as this isn’t listed in the current documentation.