private async Task <HealthProbeResult> ProbeSubscriptionAsync(string query, string subscription) { try { await _azureResourceGraph.QueryAsync(query, new List <string> { subscription }); return(new HealthProbeResult(subscription, isSuccessful: true, "Successfully queried resources via Azure Resource Graph")); } catch (UnauthorizedException unauthorizedException) { return(HandleFailedHealthProbe(subscription, unauthorizedException, unauthorizedException.Message)); } catch (QueryContainsInvalidSubscriptionException queryContainsInvalidSubscriptionException) { return(HandleFailedHealthProbe(subscription, queryContainsInvalidSubscriptionException, queryContainsInvalidSubscriptionException.Message)); } catch (Exception exception) { var message = $"Unable to discover resources due to an unexpected exception: {exception.Message}"; return(HandleFailedHealthProbe(subscription, exception, message)); } }
/// <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="resourceCollectionName">Name of the resource collection</param> public async Task <List <Resource> > GetResourcesAsync(string resourceCollectionName) { var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue; var resourceCollectionDefinition = resourceDeclaration.ResourceCollections.SingleOrDefault(collection => collection.Name.Equals(resourceCollectionName, StringComparison.InvariantCultureIgnoreCase)); if (resourceCollectionDefinition == null) { // No collection found so nothing to return return(null); } var foundResources = await _azureResourceGraph.QueryAsync(resourceCollectionDefinition.Type, resourceCollectionDefinition.Criteria); return(foundResources); }
/// <summary> /// Get resources that are part of a given resource collection /// </summary> /// <param name="resourceCollectionName">Name of the resource collection</param> public async Task <List <Resource> > GetResourcesAsync(string resourceCollectionName) { var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue; var resourceCollectionDefinition = resourceDeclaration.ResourceCollections.SingleOrDefault(collection => collection.Name.Equals(resourceCollectionName, StringComparison.InvariantCultureIgnoreCase)); if (resourceCollectionDefinition == null) { // No collection found so nothing to return return(null); } var foundResources = await _azureResourceGraph.QueryAsync(resourceCollectionDefinition.Type, resourceCollectionDefinition.Criteria); var contextualInformation = new Dictionary <string, object> { { "ResourceType", resourceCollectionDefinition.Type }, { "CollectionName", resourceCollectionName } }; _logger.LogMetric("Discovered Resources", foundResources.Count, contextualInformation); return(foundResources); }