Ejemplo n.º 1
0
        /// <summary>Get the virtual machines by resource group and availability set ids.</summary>
        /// <param name="resourceGroupName"></param>
        /// <param name="availabilitySetIds"></param>
        /// <param name="azureClient"></param>
        /// <returns></returns>
        private static IList <IGrouping <string, IVirtualMachine> > GetVirtualMachineListByResourceGroup(
            string resourceGroupName,
            List <string> availabilitySetIds,
            AzureClient azureClient)
        {
            // Get the virtual machines by resource group
            var virtualMachinesList = azureClient.AzureInstance.VirtualMachines.ListByResourceGroup(resourceGroupName).ToList();

            if (!virtualMachinesList.Any())
            {
                return(null);
            }
            var loadBalancerVmTask = VirtualMachineHelper.GetVirtualMachinesFromLoadBalancers(resourceGroupName, azureClient);
            var loadbalancerVms    = loadBalancerVmTask.Result;

            if (loadbalancerVms != null && loadbalancerVms.Any())
            {
                virtualMachinesList = virtualMachinesList.Where(x => !loadbalancerVms.Contains(x.Id))?.ToList();
            }

            // Group the the virtual machine based on the availability set id
            var virtualMachinesByAvailabilitySetId = virtualMachinesList?.Where(x => availabilitySetIds
                                                                                .Contains(x.AvailabilitySetId, StringComparer.OrdinalIgnoreCase))
                                                     .GroupBy(x => x.AvailabilitySetId, x => x).ToList();

            return(virtualMachinesByAvailabilitySetId);
        }
        /// <summary>1. Get the List of virtual machines for the resource group.
        /// 2. Get all the virtual machines from the load balancers.</summary>
        /// <param name="resourceGroup">From which resource group needs to get the virtual machines.</param>
        /// <param name="log">Trace writer instance</param>
        /// <returns>List of virtual machines which excludes the load balancer virtual machines and availability set virtual machines.</returns>
        private static async Task <IEnumerable <IVirtualMachine> > GetVirtualMachinesByResourceGroup(
            IResourceGroup resourceGroup, TraceWriter log)
        {
            try
            {
                var azureClient = new AzureClient();
                var loadBalancersVirtualMachines = VirtualMachineHelper.GetVirtualMachinesFromLoadBalancers(resourceGroup.Name, azureClient);
                var pagedCollection =
                    azureClient.AzureInstance.VirtualMachines.ListByResourceGroupAsync(resourceGroup.Name);
                var tasks = new List <Task>
                {
                    loadBalancersVirtualMachines,
                    pagedCollection
                };

                await Task.WhenAll(tasks);

                if (pagedCollection.Result == null || !pagedCollection.Result.Any())
                {
                    log.Info(
                        $"timercrawlerforvirtualmachines: no virtual machines for the resource group: {resourceGroup.Name}");
                    return(null);
                }

                var loadBalancerIds  = loadBalancersVirtualMachines.Result;
                var virtuallMachines = pagedCollection.Result;
                return(virtuallMachines?.Select(x => x).Where(x => string.IsNullOrWhiteSpace(x.AvailabilitySetId) &&
                                                              !loadBalancerIds.Contains(x.Id,
                                                                                        StringComparer.OrdinalIgnoreCase)));
            }
            catch (Exception e)
            {
                log.Error("Error occured on GetVirtualMachinesByResourceGroup", e);
                return(null);
            }
        }