Forum Discussion
Azure DevOps REST API - tag DeploymentGroups' target
Hello everyone,
I am trying to setup a function in PowerShell to be able to set tags on specific targets of a deploymentgroup, and for that I am using this documentation page: https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/targets/update?view=azure-devops-rest-7.0&tabs=HTTP#request-body
I created the request body as described in the page like bellow:
{
"id": 541,
"tags": [
"tag1-backendWithDb",
"tag1-backendWithDb-active-node",
"tag2-backendWithDb-database",
"tag2-backendWithDb",
"tag2-backendWithDb-active-node",
"tag3-blazor",
"tag3-blazor-active-node",
"tag4-yarp",
"tag4-yarp-active-node"
]
}
Than I do the following command :
Invoke-RestMethod -Method Patch -Uri "$baseurl/distributedtask/deploymentgroups/$($DGid)/targets?api-version=6.0-preview.1" -Credential $cred -Body ($body | ConvertTo-Json) -ContentType 'Application/json'
But then I get an error like this :
Invoke-RestMethod:
{
"$id": "1",
"innerException": null,
"message": "Value cannot be null.\r\nParameter name: machinesToUpdate",
"typeName": "System.ArgumentNullException, mscorlib",
"typeKey": "ArgumentNullException",
"errorCode": 0,
"eventId": 0
}
The problem is that the document is not specifying any parameter named 'machinesToUpdate'. What is it that I am missing here?
I found the solution to the problem. In fact while using the ConvertTo-Json command onto the $body object, the formatting wasn't correct for the API. When I have a single target to update, the JSON doesn't show as an array so to enforce it I had to add the Switch parameter -AsArray to the convertTo-Json command. Doing so, the json payload is correctly formatted to PATCH the targets with the API.
So the solution is really to make sure the json payload starts as an Array containing Objects with the target ID and the list of tags as an array. Bellow the fixed command.
Invoke-RestMethod -Method Patch -Uri "$baseurl/distributedtask/deploymentgroups/$($DGid)/targets?api-version=6.0-preview.1" -Credential $cred -Body ($body | ConvertTo-Json -asArray) -ContentType 'Application/json'
And make sure the JSON is well in an Array like bellow :
[
{
"id": 541,
"tags": [
"tag1-backendWithDb",
"tag1-backendWithDb-active-node",
"tag2-backendWithDb-database",
"tag2-backendWithDb",
"tag2-backendWithDb-active-node",
"tag3-blazor",
"tag3-blazor-active-node",
"tag4-yarp",
"tag4-yarp-active-node"
]
}]
3 Replies
- AMDBauwensCopper Contributor
I found the solution to the problem. In fact while using the ConvertTo-Json command onto the $body object, the formatting wasn't correct for the API. When I have a single target to update, the JSON doesn't show as an array so to enforce it I had to add the Switch parameter -AsArray to the convertTo-Json command. Doing so, the json payload is correctly formatted to PATCH the targets with the API.
So the solution is really to make sure the json payload starts as an Array containing Objects with the target ID and the list of tags as an array. Bellow the fixed command.
Invoke-RestMethod -Method Patch -Uri "$baseurl/distributedtask/deploymentgroups/$($DGid)/targets?api-version=6.0-preview.1" -Credential $cred -Body ($body | ConvertTo-Json -asArray) -ContentType 'Application/json'
And make sure the JSON is well in an Array like bellow :
[
{
"id": 541,
"tags": [
"tag1-backendWithDb",
"tag1-backendWithDb-active-node",
"tag2-backendWithDb-database",
"tag2-backendWithDb",
"tag2-backendWithDb-active-node",
"tag3-blazor",
"tag3-blazor-active-node",
"tag4-yarp",
"tag4-yarp-active-node"
]
}]
Try below to include the target machine ID:
{ "machinesToUpdate": [ { "id": 541, "tags": [ "tag1-backendWithDb", "tag1-backendWithDb-active-node", "tag2-backendWithDb-database", "tag2-backendWithDb", "tag2-backendWithDb-active-node", "tag3-blazor", "tag3-blazor-active-node", "tag4-yarp", "tag4-yarp-active-node" ] } ] }
Please also check API version compatibility
- AMDBauwensCopper Contributor
Thank you for your answer. This is a solution I tried already but did not work and get the exact same error. The maximum API version I can use is the 6.0. I consulted the document page of this version but I cannot say judging by the information on the page that there would be an incompatibility using this version.