-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWSCalc.psm1
More file actions
179 lines (150 loc) · 5.65 KB
/
AWSCalc.psm1
File metadata and controls
179 lines (150 loc) · 5.65 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
$script:awsRaw = @{}
function Get-AWSCalcRegion($AzureCalcData = $script:awsRaw) {
$AzureCalcData.Keys
}
function Get-AWSOfferData {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Path,
[switch]$Force,
[switch]$PassThru
)
process {
if (Test-Path $Path) {
if (! $Force.IsPresent) {
Throw "File exists, please use -Force to override"
}
}
$baseURL = 'https://pricing.us-east-1.amazonaws.com'
$awsIndex = Invoke-RestMethod -Method Get -URI "$baseURL/offers/v1.0/aws/index.json"
$awsRegionIndex = Invoke-RestMethod -Method Get -URI "$baseURL$($awsIndex.offers.AmazonEC2.currentRegionIndexUrl)"
#create runspace factory
$pool = [runspacefactory]::CreateRunspacePool(1, 10)
$pool.open()
#start tasks
$tasks =
($awsRegionIndex.regions | Get-Member -MemberType NoteProperty).Name |
ForEach-Object {
$region = $_
$regionUrl = $awsRegionIndex.regions.$_.currentVersionUrl -replace 'json', 'csv'
$p = [powershell]::Create().AddCommand('Invoke-WebRequest').Addparameter("Method", "Get").Addparameter("URI", ($baseurl + $regionUrl)).Addparameter("OutFile", (Join-Path $Path "$region.csv"))
$p.RunspacePool = $pool
$ia = $p.BeginInvoke()
@{p = $p; ia = $ia; path = (Join-Path $Path "$region.csv")}
#Invoke-WebRequest -Method Get -URI ($baseurl + $regionUrl) -OutFile (Join-Path $Path "$region.csv")
}
#wait for completion
foreach ($t in $tasks) {
$t.p.EndInvoke($t.ia)
$sourceFile = [System.IO.File]::ReadAllLines($t.path)
[System.IO.File]::WriteAllLines($t.path, ($sourceFile[5..($sourceFile.count)]))
$t.p.Dispose()
}
#return HT if needed
if ($PassThru.IsPresent) {get-item $Path}
}
}
function Import-AWSOfferDataFile {
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript( {Test-Path $_})]
[string]$Path,
[Parameter()]
[switch]$PassThru
)
process {
Write-Verbose 'importing raw data from $path'
$fileCount = (dir $Path -File).Count
$i = 0
foreach ($file in (Get-ChildItem $path -File)) {
Write-Progress -Activity "Importing $($file.fullname)" -PercentComplete ((++$i/$filecount)*100)
$region = $file.Name -replace '.csv'
$gpa = [GenericParsing.GenericParserAdapter]::new($file.fullname)
$gpa.FirstRowHasHeader = $true
$dt = $gpa.GetDataTable()
# for now i leave the DataRow type as is to speedup import and set datarow type as enabler in the format file
# foreach ($row in $dt) {
# $row.psobject.TypeNames.Insert(0, "AWSCalc.DataRow")
# }
#$dt.foreach({$_.psobject.TypeNames.Insert(0, "AWSCalc.DataRow")})
$script:awsRaw.$region = $dt
}
if ($PassThru.IsPresent) {$awsRaw}
}
}
function Get-AWSCalcPrice {
<#
.SYNOPSIS
This function is used to analyze Azure Calc data extracted by the REST call
.DESCRIPTION
This function is used to analyze Azure Calc data extracted by the REST call. It can then filter the data by CPU, RAM, OS Type, VM Size, Region
.EXAMPLE
Get-AWSOfferData -Path c:\temp\awsdata -Force -PassThru
Get-AWSCalcPrice -CPU 8 -RAM (8..32) -Region 'us-east-1'
Get-AWSCalcPrice -CPU 8 -RAM (8..32) -Region 'us-east-1','us-east-2' | sort PricePerUnit
#>
[cmdletbinding()]
param (
[Parameter()]
[int[]]$CPU,
[Parameter()]
[int[]]$RAM,
[Parameter()]
[string]$Type,
[Parameter()]
[string]$termType = 'ondemand',
[Parameter()]
[string]$Size,
[Parameter()]
[ValidateScript({if($script:awsRaw.count){$_ -in (Get-AWSCalcRegion)} else {throw "Please import AWS calc data"}})]
[string[]]$Region,
[Parameter()]
[string[]]$Tenancy = "Shared",
[Parameter()]
$CalcData = $script:rawAWSData
)
begin {
$cpuFilter = {param($objectSet) $objectSet | Where-Object {$_.vCPU -in @($CPU)} }
$ramFilter = {param($objectSet) $objectSet | Where-Object {(($_.Memory) -replace ' GiB') -in @($ram)} }
$typeFilter = {param($objectSet) $objectSet | Where-Object {$_.'Operating System' -eq $Type} }
$sizeFilter = {param($objectSet) $objectSet | Where-Object {$_.'Instance Type' -eq $Size} }
$tenancyFilter = {param($objectSet) $objectSet | Where-Object {$_.Tenancy -eq $Tenancy} }
$termTypeFilter = { param($objectSet) $objectSet | Where-Object {$_.TermType -eq $TermType} }
$regionFilter = {param($objectSet) $Region | ForEach-Object {$script:awsRaw.$_} }
}
process {
$filters = @()
if ($Region) {
$filters += $regionFilter
}
if ($termType) {
$filters += $termTypeFilter
}
if ($CPU) {
$filters += $cpuFilter
}
if ($RAM) {
$filters += $ramFilter
}
if ($Type) {
$filters += $typeFilter
}
if ($Size) {
$filters += $sizeFilter
}
if ($Tenancy) {
$filters += $tenancyFilter
}
$ret = $CalcData
foreach ($f in $filters) {
write-verbose "$f"
$ret = & $f $ret
write-verbose "$($ret.Count)"
}
$ret
}
}