-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexcelAzureHelpers.cs
More file actions
139 lines (114 loc) · 4.67 KB
/
Copy pathexcelAzureHelpers.cs
File metadata and controls
139 lines (114 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace excelAzureBackend
{
public class excelAzureHelpers
{
public excelAzureHelpers(IConfiguration iConfig)
{
configuration = iConfig;
}
private IConfiguration configuration;
public string GetVMURL(int bindingPeriod, string currency, DateTime? date)
{
if (date.HasValue && !(date.Value < (DateTime.Now.AddDays(-7))))
{
date = null;
}
if (!date.HasValue)
{
switch (bindingPeriod)
{
case 0:
// 0 years no ahub
return "https://azure.microsoft.com/api/v2/pricing/virtual-machines-base/calculator/?culture=en-us&discount=mosp¤cy=" + currency;
case 1:
return "https://azure.microsoft.com/api/v2/pricing/virtual-machines-base-one-year/calculator/?culture=en-us&discount=mosp¤cy=" + currency;
case 3:
return "https://azure.microsoft.com/api/v2/pricing/virtual-machines-base-three-year/calculator/?culture=en-us&discount=mosp¤cy=" + currency;
default:
throw new KeyNotFoundException();
}
}
else
{
string blobUri = configuration.GetValue<string>("BlobStorageSettings:blobUri");
string containerName = configuration.GetValue<string>("BlobStorageSettings:containerName");
return blobUri + getBlobPath(date.Value, currency, "vm", bindingPeriod);
}
}
public string GetMdiskURL(string currency, DateTime? date = null) {
if (date.HasValue && !(date.Value < (DateTime.Now.AddDays(-7))))
{
date = null;
}
if (date.HasValue)
{
string blobUri = configuration.GetValue<string>("BlobStorageSettings:blobUri");
return blobUri + getBlobPath(date.Value, currency, "mdisk");
}
else
{
return configuration.GetValue<string>("CalculatorUrls:mdisk") + currency;
}
}
private static int GetIso8601WeekOfYear(DateTime time)
{
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
public string getBlobPath(DateTime date, string currency, string type = "vm", int? ri = null, bool container = true)
{
string containerName = configuration.GetValue<string>("BlobStorageSettings:containerName");
string week = GetIso8601WeekOfYear(date).ToString();
// year + "/" + week + "/" + currency + "/" + filename
var filename = "";
if (type.Equals("vm"))
{
filename = "vm_ri" + ri.ToString();
}
else if (type.Equals("mdisk"))
{
filename = "mdisk";
}
if (container)
{
return ("/" + containerName + "/" + date.Year.ToString() + "/" + week + "/" + currency + "/" + filename).ToLower();
}
else
{
return ("" + date.Year.ToString() + "/" + week + "/" + currency + "/" + filename).ToLower();
}
}
public static DateTime? parseDTString(string input) {
try
{
return DateTime.ParseExact(input, "yyyyMMdd", CultureInfo.InvariantCulture);
}
catch (Exception)
{
return null;
}
}
public static Boolean validateCurrency(string currency) {
var currencies = new string[] { "USD", "EUR", "CHF", "ARS", "AUD", "DKK", "CAD", "IDR", "JPY", "KRW", "NZD", "NOK", "RUB", "SAR", "ZAR", "SEK", "TWD", "TRY", "GBP", "MXN", "MYR", "INR", "HKD", "BRL" };
bool has = currencies.Contains(currency.ToUpper());
if (has != true)
{
throw new ArgumentException("Currency not supported");
}
return has;
}
}
}