Example #1
0
        /// <summary>
        ///     Get resources that are part of a given resource collection
        /// </summary>
        /// <param name="resourceDiscoveryGroupName">Name of the resource collection</param>
        public virtual 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(resourceDiscoveryGroupName, 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);
        }
        public async Task <JObject> QueryAsync(string queryName, string query, bool skipCache = false)
        {
            if (skipCache == false && _memoryCache.TryGetValue(queryName, out JObject cachedQueryResult))
            {
                return(cachedQueryResult);
            }

            var queryResult = await _azureResourceGraph.QueryAsync(queryName, query);

            AddCacheEntry(queryName, queryResult);

            return(queryResult);
        }