microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
522 stars 519 forks source link

BuildHttpClient.GetQueuesAsync() missing in API version 15 #225

Open HeinerMichael opened 5 years ago

HeinerMichael commented 5 years ago

Hi, in order to create build definitions and to queue builds programmatically, I need to specify a build definition member "Queue" of type "AgentPoolQueue". In Version 14 of VSTS API there was a function called "GetQueuesAsync" that returned a list of AgentPoolQueue objects. In Version 15 this function is missing. Is there an alternate way to retrieve the agent pool queues? Thanks in advance Michael

vbtrek commented 4 years ago

I'm also looking for something similar to this, I want to get a list of Pools linked to a project, and subsequently a count of agents within each pool.

HeinerMichael commented 4 years ago

Hi vbtrek, by now I didn't find any methode like 'GetQueuesAsync()'. So I decided to use a REST call. In tfs team projects agent pools are represented by agent queues. I used following function to retrieve a specific agent queue:

    public AgentPoolQueue GetBuildQueue(string project, string queueName)
    {
        AgentPoolQueue apQueue = null;

        HttpClientHandler authtHandler = new HttpClientHandler()
        {
            Credentials = CredentialCache.DefaultNetworkCredentials
        };

        using (HttpClient client = new HttpClient(authtHandler))
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string restString = $"{CollectionUrl}/{project}/_apis/distributedtask/queues?actionFilter=16";

            using (HttpResponseMessage response = client.GetAsync(restString).Result)
            {
                response.EnsureSuccessStatusCode();
                var jResult = response.Content.ReadAsStringAsync().Result;
                JObject jObject = JObject.Parse(jResult);
                List<AgentPoolQueue> queues = JsonConvert.DeserializeObject<List<AgentPoolQueue>>(jObject["value"].ToString());

                if (queues != null)
                {
                    foreach (var queue in queues)
                    {
                        if (queue.Name.Equals(queueName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            apQueue = queue;
                            break;
                        }
                    }
                }
            }
        }

        return apQueue;
    }

If you need direct access to the pools, you should use the 'TaskAgentHttpClient' like this:

TaskClient = Connection.GetClient(); public List GetAgentPools() { return TaskClient.GetAgentPoolsAsync().Result; }

I hope that helps! Michael

smaglio81 commented 4 years ago

I would like to have the functionality as well.

quintessential5 commented 4 years ago

Looks like they moved it to TaskAgentHttpClient

https://docs.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.distributedtask.webapi.taskagenthttpclientbase.getagentqueuesasync?view=azure-devops-dotnet