Reputation: 21
I am trying to figure out how to update the associated tags with an Azure DevOps Environment Resource (virtual machine) through the Azure DevOps API.
I have figured out how to get the resources for a specific Environment - https://dev.azure.com/{organization}/{project}/_apis/distributedtask/environments/{environmentId}?api-version=6.0-preview.1
However, if I try to update and then post back the Environment JSON with the updated resource tags, it does not actually update the resources. I think it is only for updating the Environment Name and Description?
I have trying to figure out how to use the following API endpoint (I think this is the one I should be using?) - https://dev.azure.com/{organization}/{project}/_apis/distributedtask/environments/{environmentId}/providers/virtualmachinegroups/{resourceId}/virtualmachines?api-version=6.0-preview.1.
The API endpoint wants a "resourceId" but I am not able to get anything back besides a 404 error when I put in the Environment Resource Id's for my Environment. I am not sure if I am doing something wrong, or this is the wrong end point.
Does anyone have an experience with using the API to update resource tags?
Upvotes: 2
Views: 1065
Reputation: 261
Way too late here, but I have managed to figure this out with api-version=6.0-preview.1 at the very least.
Here's some PowerShell code that I have been able to use to update the tags on a machine for a particular environment.
$vmListUrl = "https://dev.azure.com/{organization}/{project}/_apis/pipelines/environments/{environmentId}/providers/virtualmachines?api-version=6.0-preview.1"
$vmList= $(Invoke-RestMethod -Uri $vmListUrl -Headers @{ Authorization = "Basic $B64PAT" }).value
$vm = $vmList | Where-Object { $_.name -like "*$ResourceName*" }
$vm.tags += "Extra Tag"
$body = $vm | ConvertTo-Json -Depth 5
$vmId = $vm.id
$updateUrl = "https://dev.azure.com/{organization}/{project}/_apis/pipelines/environments/{environmentId}/providers/virtualmachines/$vmID`?api-version=6.0-preview.1"
Invoke-RestMethod -Uri $updateUrl -Headers @{ Authorization = "Basic $B64PAT";"Content-Type" = "application/json" } -Method Patch -Body $body
Obviously you need to get a personal access token and encode it to pass into the calls, but after looking all over the place without any success for examples, I just messed around with it until I figured out that the input for the patch is asking for the output from one of the other calls, but you can modify the data before passing it back.
Upvotes: 2
Reputation: 30313
I can reproduce above issue. Currently, You have to manually edit the Environment Virtual machine Resource Tags.
Azure devops Environment Resource Rest API is not fully developed yet. Some features may be broken and unavailable currently.
You can report this issue to Microsoft Development Team. Hope they will fix it in the future sprint.
Upvotes: 1