• This topic has 8 replies, 3 voices, and was last updated April 3, 2019 by Justin N.

Change VPG/VM/Volume datastore via API

  • <p style=”padding-left: 30px;”>Due to a storage change at our Recovery site, we are migrating all recovery and journal volumes to new datastores (VMware). I have a working script that will change the VPG recovery and journal datastore and the recovery and journal datastore for each VM in the VPG. I’m now stuck trying to update the Volume datastore for each volume on each VM.  I found how to GET the data I need, but when I try to PUT the changes (as I do for VPG and VM settings), it returns an error 405 Method Not Allowed.  Here is the code snippet I’m stuck on…

    $VolumeList = Invoke-RestMethod -Uri $VMSettingsURL"/volumes" -Method GET -TimeoutSec 100 -Headers $ZertosessionHeader -ContentType $TypeJSON

    foreach ($volume in $VolumeList){
    $ChangeVolumeLocationBody = '{"Datastore":{"DatastoreIdentifier":"'+ $newVMDS.DatastoreIdentifier + '"}}'
    $ChangeVolumeLocationRequest = Invoke-RestMethod -Uri $VMSettingsURL -Method PUT -TimeoutSec 100 -Headers $ZertosessionHeader -ContentType $TypeJSON -Body $ChangeVolumeLocationBody
    } #Close Volume Loop</p>
    I know this must be possible because we can do it through the GUI.  I’m fairly new to using APIs with PowerShell, so I hope this is something easy I’m overlooking.  Thanks!

    Hi Jason – Currently the volumes API only supports GET methods. Having said that, a PUT to the VPGSettings API can be used to update a recovery volume or journal locations. We also have a specific section of the API docs that covers how to work with the VPGSettings API here. The section for updating a vpg should be the most helpful. — Justin

    Thanks for the reply Justin. That is very discouraging to hear and seems contrary to what the documentation says.  Looking at the “Zerto Virtual Replication RESTful APIs” document for version 6.5 (which is what we run), see pages 205 and 228 which directly mentions editing volume settings with the PUT method (also available here).
    http://s3.amazonaws.com/zertodownload_docs/Latest/Zerto%20Virtual%20Replication%20Zerto%20Virtual%20Manager%20%28ZVM%29%20-%20vSphere%20Online%20Help/index.html#page/RestfulAPIs%2FStatusAPIs.5.115.html%23

    Let me know if you have any other suggestions, or if I must update these manually using the GUI.  Thanks!

    Hi Jason,

    Just looking at the code snippit you have provided, it seems that you are attempting to only PUT the volume information for one volume at a time. I believe you need to be updating your URI to something along the lines of:

    $ChangeVolumeLocationRequest = Invoke-RestMethod -Uri $VMSettingsURL/volumes/{volumeId} -Method PUT -TimeoutSec 100 -Headers $ZertosessionHeader -ContentType $TypeJSON -Body $ChangeVolumeLocationBody

    I have not tested this in my lab, but according to the documentation it should work this way.

    Thanks,
    Wes

    Thinking about this a bit more, I think I would approach the loop as I have outlined here in this Gist. I’ve included the linked code below, but believe the gist will have better readability.

    foreach ($volume in $VolumeList){
    $volumeUri = "$VMSettingsUrl/volumes/$($volume.volumeIdentifier)"
    $currentVolume = Invoke-RestMethod -Uri $volumeUri -Method GET -TimeoutSec 100 -Headers $ZertosessionHeader -ContentType $TypeJSON
    $currentVolume.Datastore.DatastoreIdentifier = $newVMDS.DatastoreIdentifier
    $body = $currentVolume | Convertto-Json -depth 10
    $ChangeVolumeLocationRequest = Invoke-RestMethod -Uri $volumeUri -Method PUT -TimeoutSec 100 -Headers $ZertosessionHeader -ContentType $TypeJSON -Body $body
    } #Close Volume Loop
    

    You, sir, are awesome!  I would have sworn I had tried that already but I obviously had something wrong.  I went back this morning with your suggestion and it worked perfectly.  Thank you!!!

    I think where I got tripped up was using different URIs to PUT each volume change and VM change.  Using a different URI for each PUT statement, then a final POST Commit worked exactly as I hoped it would.

You must be logged in to create new topics. Click here to login