Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active May 3, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/973f3c0fe69bd4e225726e7b01cb4d5b to your computer and use it in GitHub Desktop.
Save justinyoo/973f3c0fe69bd4e225726e7b01cb4d5b to your computer and use it in GitHub Desktop.
Know Your Cloud Resource Costs on Azure
public async Task<IEnumerable<ResourceGroupCost>> GetResourceGroupCostsAsync(
Subscription subscription,
DateTime dateStart,
DateTime dateEnd,
string authToken)
{
var costs = await GetResourceCostsAsync(subscription.SubscriptionId, dateStart, dateEnd, authToken).ConfigureAwait(false);
var grouped = costs.GroupBy(
p => new
{
DateStart = p.UsageValue.Properties.UsageStartTime,
DateEnd = p.UsageValue.Properties.UsageEndTime,
ResourceGroupName = ResourceGroupDateKey.GetResourceGroupKey(p)
})
.Select(
p => new ResourceGroupCost()
{
ResourceGroupName = p.Key.ResourceGroupName,
DateStart = DateTimeOffset.Parse(p.Key.DateStart),
DateEnd = DateTimeOffset.Parse(p.Key.DateEnd),
Cost = p.Sum(q => q.CalculatedCosts)
})
.OrderBy(p => p.ResourceGroupName)
.ThenBy(p => p.DateStart)
.ThenBy(p => p.DateEnd);
return grouped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment