Beispiel #1
0
        /// <summary>
        ///     Get resources that are part of a given resource collection
        /// </summary>
        /// <param name="resourceDiscoveryGroupName">Name of the resource collection</param>
        public async Task <List <AzureResourceDefinition> > GetResourcesAsync(string resourceDiscoveryGroupName)
        {
            var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue;
            var resourceDiscoveryGroupDefinition = resourceDeclaration.ResourceDiscoveryGroups.SingleOrDefault(collection => collection.Name.Equals(resourceDiscoveryGroupName, StringComparison.InvariantCultureIgnoreCase));

            if (resourceDiscoveryGroupDefinition == null)
            {
                // No collection found so nothing to return
                return(null);
            }

            var resourceDiscovery = ResourceDiscoveryFactory.UseResourceDiscoveryFor(resourceDiscoveryGroupDefinition.Type);

            // 1. Create query per type
            var query = resourceDiscovery.DefineQuery(resourceDiscoveryGroupDefinition.Criteria).Build();

            // 2. Run Query
            var unparsedResults = await _azureResourceGraph.QueryAsync(query);

            // 3. Parse query results into resource
            var foundResources = resourceDiscovery.ParseQueryResults(unparsedResults);

            var contextualInformation = new Dictionary <string, object>
            {
                { "ResourceType", resourceDiscoveryGroupDefinition.Type },
                { "CollectionName", resourceDiscoveryGroupName }
            };

            _logger.LogMetric("Discovered Resources", foundResources.Count, contextualInformation);

            return(foundResources);
        }
        /// <summary>
        ///     Get resources that are part of a given resource collection
        /// </summary>
        /// <param name="resourceDiscoveryGroupName">Name of the resource collection</param>
        /// <param name="pageSize">The amount of results that are allowed per page</param>
        /// <param name="currentPage">Current page that is being queried</param>
        public virtual async Task <PagedPayload <AzureResourceDefinition> > GetResourcesAsync(string resourceDiscoveryGroupName, int pageSize, int currentPage)
        {
            var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue;
            var resourceDiscoveryGroupDefinition = resourceDeclaration.ResourceDiscoveryGroups.SingleOrDefault(collection => collection.Name.Equals(resourceDiscoveryGroupName, StringComparison.InvariantCultureIgnoreCase));

            if (resourceDiscoveryGroupDefinition == null)
            {
                // No collection found so nothing to return
                return(null);
            }

            var resourceDiscovery = ResourceDiscoveryFactory.UseResourceDiscoveryFor(resourceDiscoveryGroupDefinition.Type);

            // 1. Create query per type
            var query = resourceDiscovery.DefineQuery(resourceDiscoveryGroupDefinition.Criteria).Build();

            // 2. Run Query
            var unparsedResults = await _azureResourceGraph.QueryTargetSubscriptionsAsync(resourceDiscoveryGroupName, query, pageSize, currentPage);

            // 3. Parse query results into resource
            var foundResources = resourceDiscovery.ParseQueryResults(unparsedResults.Result);

            var contextualInformation = new Dictionary <string, object>
            {
                { "ResourceType", resourceDiscoveryGroupDefinition.Type },
                { "CollectionName", resourceDiscoveryGroupName }
            };

            // Log a metric with amount of discovered resources
            // But only if the current page is 1, or we'll emit it too much
            if (currentPage == 1)
            {
                _logger.LogMetric("Discovered Resources", unparsedResults.TotalRecords, contextualInformation);
            }

            return(new PagedPayload <AzureResourceDefinition>(foundResources, unparsedResults.TotalRecords, unparsedResults.CurrentPage, unparsedResults.PageSize));
        }