Created
May 3, 2017 14:40
-
-
Save justinyoo/d67953f3e0d02db0fb7234a8e60c2ced to your computer and use it in GitHub Desktop.
Know Your Cloud Resource Costs on Azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task SaveResourceGroupCostsResultsAsync(Subscription subscription, IEnumerable<ResourceGroupCostResult> results) | |
{ | |
var now = DateTimeOffset.UtcNow; | |
foreach (var result in results) | |
{ | |
var totalSpendLimit = Convert.ToDecimal(result.TotalSpendLimit); | |
var dailySpendLimit = Convert.ToDecimal(result.DailySpendLimit); | |
var overspendAction = result.OverspendAction; | |
var record = await this._dbContext.ResourceGroupCostHistories | |
.SingleOrDefaultAsync(p => p.Subscription.Equals(subscription.DisplayName, StringComparison.CurrentCultureIgnoreCase) && | |
p.ResourceGroupName.Equals(result.ResourceGroupName, StringComparison.CurrentCultureIgnoreCase) && | |
p.Owners.Equals(result.OwnerEmails, StringComparison.CurrentCultureIgnoreCase) && | |
p.DateStart == result.DateStart && | |
p.DateEnd == result.DateEnd) | |
.ConfigureAwait(false); | |
if (record == null) | |
{ | |
record = new ResourceGroupCostHistory() | |
{ | |
ResourceGroupCostHistoryId = Guid.NewGuid(), | |
Subscription = subscription.DisplayName, | |
SubscriptionId = Guid.Parse(subscription.SubscriptionId), | |
ResourceGroupName = result.ResourceGroupName, | |
Owners = result.OwnerEmails, | |
DateStart = result.DateStart, | |
DateEnd = result.DateEnd, | |
DateCreated = now | |
}; | |
} | |
record.Cost = Convert.ToDecimal(result.Cost); | |
record.TotalSpendLimit = totalSpendLimit; | |
record.DailySpendLimit = dailySpendLimit; | |
record.OverspendAction = overspendAction; | |
record.DateUpdated = now; | |
this._dbContext.ResourceGroupCostHistories.AddOrUpdate(record); | |
} | |
await this._dbContext.SaveChangesAsync().ConfigureAwait(false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment