-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVM_Prices.bas
More file actions
358 lines (281 loc) · 10.8 KB
/
Copy pathVM_Prices.bas
File metadata and controls
358 lines (281 loc) · 10.8 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Attribute VB_Name = "VMPrice"
Option Explicit
Public sessionID As String
Dim responses(100) As String
Dim regionSplit() As String
Dim tempResponse As String
Dim xmlhttp As XMLHTTP60
Dim result As String
Dim Rows() As String
Dim Labels() As String
Dim cols() As String
Dim aahttp As XMLHTTP60
Function getversion()
getversion = "0.5.0.7"
End Function
Function GenGuid() As String
Dim sDate As Double, session As Double, dDate As String, test As String
dDate = Format(Now(), "ssmmddMMyy")
sDate = CDbl(dDate)
session = Round(CDbl((999 - 0 + 1) * Rnd + 0))
sDate = sDate * session
GenGuid = sDate
End Function
Function getSession()
If sessionID = "" Then
sessionID = GenGuid()
End If
getSession = sessionID
End Function
Public Function applicationInsights(region As String, CurrencyID, Optional ByVal isManagedDisk As Boolean, Optional ByVal indate As String, Optional ByVal Status As String = "")
Dim aahttp As New XMLHTTP60, resp As String
Dim sDate As String, body As String
If sessionID = "" Then
sessionID = GenGuid()
End If
sDate = Format(Now(), "yyyy-mm-ddThh:mm:ss.000Z")
body = "{""iKey"":"""",""time"":""" & sDate & """,""name"":""MetricData"",""tags"":{""ai.application.ver"":""" & getversion() & """,""ai.operation.name"":""request""},""data"":{""baseType"":""MetricData"",""baseData"":{""metrics"":[{""name"":""RequestMetric"",""value"":1,""count"":1}],""properties"":{""isManagedDisk"":""" & isManagedDisk & """,""region"":""" & LCase(region) & """,""currency"":""" & LCase(CurrencyID) & """,""inDate"":""" & indate & """,""sessionID"":""" & sessionID & """,""status"":""" & Status & """}}}}"
aahttp.Open "POST", "https://dc.services.visualstudio.com/v2/track", True
aahttp.Send body
'resp = aahttp.responseText
End Function
Public Function httpclient(region As String, CurrencyID, Optional ByVal isManagedDisk As Boolean, Optional ByVal indate As String)
Dim xmlhttp As New XMLHTTP60
'Dim xmlhttp As New MSXML2.xmlhttp
Dim myurl As String
Dim dateExtension As String
If (Len(indate) > 0) Then
dateExtension = "&date=" + indate
End If
If (isManagedDisk) Then
myurl = "https://vmsizeCDNv.azureedge.net/api/values/csv/mdisks?seed=2®ion=" & LCase(region) & "¤cy=" & LCase(CurrencyID) & LCase(dateExtension)
Else
myurl = "https://vmsizeCDNv.azureedge.net/api/values/csv?seed=2®ion=" & LCase(region) & "¤cy=" & LCase(CurrencyID) & LCase(dateExtension)
End If
xmlhttp.Open "GET", myurl, False
xmlhttp.setRequestHeader "User-Agent", "ExcelAzure" & getversion()
xmlhttp.setRequestHeader "Session-Id", getSession()
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Encoding", "gzip"
xmlhttp.setRequestHeader "Cache-Control", "max-age=259200"
xmlhttp.Send ""
httpclient = xmlhttp.responseText
applicationInsights region, CurrencyID, isManagedDisk, indate, xmlhttp.statusText
End Function
Private Function OnTimeOutMessage()
'MsgBox ("Server error: request time-out")
End Function
Function addResponse(response As String, region As String, CurrencyID As String, Optional ByVal managedDisk As Boolean = False, Optional indate As String = "")
Dim i As Integer
Dim searchString As String
If (managedDisk) Then
searchString = "MDISK" & LCase(region) & indate
Else
searchString = LCase(region)
End If
For i = LBound(responses) To UBound(responses)
'Find Empty response
If (i = UBound(responses)) Then
MsgBox ("Out of Memory. Too many requests")
Erase responses
Exit For
End If
If (responses(i) = "") Then
responses(i) = searchString & LCase(CurrencyID) & indate & "*" & response
Exit For
End If
Next i
End Function
Function findResponse(region As String, CurrencyID As String, Optional ByVal managedDisk As Boolean = False, Optional indate As String = "")
Dim i As Integer, ok
For i = LBound(responses) To UBound(responses)
'Find Empty response
If (responses(i) = "") Then
' No region match get region
tempResponse = httpclient(region, CurrencyID, managedDisk, indate)
ok = addResponse(tempResponse, region, CurrencyID, managedDisk, indate)
findResponse = tempResponse
Exit For
End If
regionSplit() = Split(responses(i), "*")
Dim searchString As String
If (managedDisk) Then
searchString = "MDISK" & LCase(region) & indate
Else
searchString = LCase(region)
End If
If (regionSplit(0) = searchString & LCase(CurrencyID) & indate) Then
' Found region
findResponse = regionSplit(1)
Exit For
End If
Next i
End Function
Function getVM(mincores As Integer, minram As Integer, reservedInstanceYears As Integer, region As String, CurrencyID As String, Optional ByVal excludeKeywords As String = "", Optional ByVal includeKeywords As String = "", Optional ByVal isCPU As String = "", Optional ByVal indate As String = "")
Dim i
result = findResponse(region, CurrencyID, False, indate)
Rows() = Split(result, vbCrLf)
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
If (cols(1) >= mincores And cols(2) >= minram And cols(4) <= reservedInstanceYears And searchKeywords(cols(0), excludeKeywords) = False And includeKeywords = "") Then
If (cols(12) = "True" Or InStr(LCase(cols(0)), "-sap")) Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getVM = cols(0)
Exit For
ElseIf (cols(1) >= mincores And cols(2) >= minram And cols(4) <= reservedInstanceYears And searchKeywords(cols(0), excludeKeywords) = False And includeKeywords <> "" And searchKeywords(cols(0), includeKeywords, True) = True) Then
If (cols(12) = "True" Or InStr(LCase(cols(0)), "-sap")) Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getVM = cols(0)
Exit For
End If
Next i
End Function
Function getManagedDisk(minSize As Integer, region As String, CurrencyID As String, Optional ByVal excludeKeywords As String = "", Optional ByVal includeKeywords As String = "", Optional indate As String = "")
result = findResponse(region, CurrencyID, True, indate)
Rows() = Split(result, vbCrLf)
Dim i
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
' nothing in includeKeywords
If (cols(1) >= minSize And searchKeywords(cols(0), excludeKeywords) = False And includeKeywords = "") Then
If (cols(12) = "True") Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getManagedDisk = cols(0)
Exit For
' something in includeKeywords
ElseIf (cols(1) >= minSize And searchKeywords(cols(0), excludeKeywords) = False And includeKeywords <> "" And searchKeywords(cols(0), includeKeywords, True) = True) Then
If (cols(12) = "True") Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getManagedDisk = cols(0)
Exit For
End If
Next i
End Function
Function getManagedDiskPriceMonth(name As String, region As String, CurrencyID As String, Optional indate As String = "")
Dim i As Integer, result As String
result = findResponse(region, CurrencyID, True, indate)
Rows() = Split(result, vbCrLf)
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
If (cols(0) = name) Then
If (cols(12) = "True") Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getManagedDiskPriceMonth = Val(cols(4))
Exit For
End If
Next i
End Function
Function getVMPriceHour(name As String, reservedInstanceYears As Integer, region As String, CurrencyID As String, Optional indate As String = "")
Dim i As Integer, result As String
result = findResponse(region, CurrencyID, False, indate)
Rows() = Split(result, vbCrLf)
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
If (cols(0) = name And cols(4) = reservedInstanceYears) Then
If (cols(12) = "True" Or InStr(LCase(cols(0)), "-sap")) Then
Application.Caller.Font.ColorIndex = 3
Else
Application.Caller.Font.ColorIndex = 1
End If
getVMPriceHour = Val(cols(6))
Exit For
End If
Next i
End Function
Function getVMData(name As String, region As String, CurrencyID As String, ParamName As String)
Dim e As Integer, i As Integer
result = findResponse(region, CurrencyID)
'Find the param
Rows() = Split(result, vbCrLf)
Labels() = Split(Rows(0), ";")
Do
For e = LBound(Labels) To UBound(Labels)
If (LCase(Labels(e)) = LCase(ParamName)) Then
' Search through the VM's
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
If (cols(0) = name) Then
getVMData = cols(e)
Exit Do
End If
Next i
End If
Next e
Loop While False
End Function
Function getMDiskData(name As String, region As String, CurrencyID As String, ParamName As String, Optional indate As String = "")
Dim e As Integer, i As Integer, result As String
result = findResponse(region, CurrencyID, True, indate)
'Find the param
Rows() = Split(result, vbCrLf)
Labels() = Split(Rows(0), ";")
Do
For e = LBound(Labels) To UBound(Labels)
If (LCase(Labels(e)) = LCase(ParamName)) Then
' Search through the VM's
For i = LBound(Rows) + 1 To UBound(Rows)
cols() = Split(Rows(i), ";")
If (cols(0) = name) Then
getMDiskData = cols(e)
Exit Do
End If
Next i
End If
Next e
Loop While False
End Function
Function searchKeywordsold(name As String, wordlist As String)
Dim words() As String
words() = Split(wordlist, ";")
For i = LBound(words) To UBound(words)
If (InStr(name, words(i)) > 0) Then
searchKeywords = True
Exit For
End If
If (i = UBound(words)) Then
searchKeywords = False
End If
Next i
End Function
Function searchKeywords(name As String, wordlist As String, Optional matchAll As Boolean = False)
Dim words() As String, i As Integer
Dim count As Integer
count = 0
words() = Split(wordlist, ";")
For i = LBound(words) To UBound(words)
If (InStr(name, words(i)) > 0) Then
If (matchAll = False) Then
searchKeywords = True
Exit For
Else
count = count + 1
End If
End If
If (i = UBound(words)) Then
If (matchAll = False) Then
searchKeywords = False
Else
If (count = UBound(words) + 1) Then
searchKeywords = True
Else
searchKeywords = False
End If
End If
End If
Next i
End Function