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

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.

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


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.


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


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.


Reply