0
votes

What specific changes need to be made to the below code in order to successfully filter the list of Agent Pools by the properties name and isHosted using the Python requests module and the Azure DevOps Services REST API endpoint documented at this link?

Only 1 agent pool should be returned when the list is filtered by {name=Default,isHosted=false} as shown below, but the problem is that the code below is returning ALL agent pools including those with different names and those that are Hosted.

import requests
import os
import base64
import json 

personal_access_token = ":"+os.environ["AZ_PERSONAL_ACCESS_TOKEN"]
headers = {}
headers['Content-type'] = "application/json"
headers['Authorization'] = b'Basic ' + base64.b64encode(personal_access_token.encode('utf-8'))

#Get a list of agent pools.
instance = "dev.azure.com/MyOrganization"
propVals = "{name=Default,isHosted=false}"
api_version = "5.1"
url = ("https://%s/_apis/distributedtask/pools?properties=%s?api-version=%s" % (instance, propVals, api_version))

r = requests.get(url, headers=headers)  

print("r.status_code is: ", r.status_code)
print("r.json() is: ", r.json())

Note there seems to be very little documentation of how to use Python to integrate with the Azure DevOps Services API. Also, note we are using Python 3.7.6.

1

1 Answers

1
votes

According to the docs you provided if you want to get only 1 pool you can use the following URL parameter (instead of properties):

poolName={poolName}

And not just name= like in your code: propVals = "{name=Default,isHosted=false}".

In addition, I don't see any parameter of isHosted and I don't think you need it, because if you use poolName you will get only one pool anyway.

So, update your code to: propVals = "poolName=Default" because your final URL should be:

https://dev.azure.com/{organization}/_apis/distributedtask/pools?poolName={poolName}&api-version=5.1

Note: &api-version=5.1 and not ?api-version=5.1