static async Task ProcessResourceGroups(IEnumerable <string> requiredTagsList, List <InvalidTagResource> invalidTypes, string subscriptionId, AuditStats stats) { var resourceGroups = await _resourceManager.GetResourceGroups(subscriptionId); stats.ResourceGroupsTotal = resourceGroups.Count; foreach (var rg in resourceGroups) { _log.Info("*** Resource Group: " + rg.Name); var tagsToSync = TagService.GetRequiredTags((Dictionary <string, string>)rg.Tags, requiredTagsList); if (tagsToSync.Count < 1) { _log.Warning("Resource group: " + rg.Name + " does not have required tags."); stats.ResourceGroupsSkipped += 1; } else { List <ResourceItem> resources = await _resourceManager.GetResources(rg.Name, subscriptionId, invalidTypes.Select(t => t.Type).ToList()); stats.ResourceItemsTotal = resources.Count(); foreach (var resource in resources) { var result = TagService.GetTagUpdates(resource.Tags.ToDictionary(x => x.Key, x => x.Value), tagsToSync); if (result.Count > 0) { stats.ResourceItemsWithUpdates += 1; resource.Tags = result; string messageText = JsonConvert.SerializeObject(resource); _log.Info("Requesting tags for: " + resource.Id); _outQueue.Add(messageText); } else { stats.ResourceItemsSkipped += 1; } } } } }
static async Task <List <ResourceItem> > ProcessResourceGroups(IEnumerable <string> requiredTagsList, string subscriptionId, AuditStats stats) { List <ResourceItem> updateList = new List <ResourceItem>(); List <string> invalidResourceTypes = _resourceTypes.Where(t => !String.IsNullOrEmpty(t.ErrorMessage)).Select(t => t.Type).ToList(); var resourceGroups = await _resourceManager.GetResourceGroups(subscriptionId); stats.ResourceGroupsTotal = resourceGroups.Count; foreach (var rg in resourceGroups) { _log.Info("Resource group: " + rg.Name); if (rg.Tags == null) { _log.Warning("Resource group: " + rg.Name + " does not have tags."); continue; } var tagsToSync = TagService.GetRequiredTags((Dictionary <string, string>)rg.Tags, requiredTagsList); if (tagsToSync.Count < 1) { _log.Warning("Resource group: " + rg.Name + " does not have required tags."); stats.ResourceGroupsSkipped += 1; } else { List <ResourceItem> resources = await _resourceManager.GetResources(rg.Name, subscriptionId, invalidResourceTypes); stats.ResourceItemsTotal = resources.Count(); foreach (var resource in resources) { var result = TagService.GetTagUpdates(resource.Tags.ToDictionary(x => x.Key, x => x.Value), tagsToSync); if (result.Count > 0) { try { stats.ResourceItemsWithUpdates += 1; resource.Tags = result; resource.ApiVersion = await GetApiVersion(resource); updateList.Add(resource); } catch (Exception ex) { _log.Error("Failure processing resource: " + resource.Id); _log.Error(ex.Message); } } else { stats.ResourceItemsSkipped += 1; } } } } return(updateList); }