forked from fullscale180/asm2arm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm2armNetwork.ps1
381 lines (313 loc) · 12.1 KB
/
asm2armNetwork.ps1
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<#
© 2015 Microsoft Corporation. All rights reserved. This sample code is not supported under any Microsoft standard support program or service.
This sample code is provided AS IS without warranty of any kind. Microsoft disclaims all implied warranties including, without limitation,
any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance
of the sample code and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business
profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the
sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#>
function New-VirtualNetworkResource
{
Param
(
$Name,
$Location,
[string[]]
$AddressSpacePrefixes,
$Subnets
)
$addressSpace = @{'addressPrefixes' = $AddressSpacePrefixes;}
$createProperties = @{"addressSpace" = $addressSpace; 'subnets'= $subnets;}
$resource = New-ResourceTemplate -Type "Microsoft.Network/virtualNetworks" -Name $Name `
-Location $Location -ApiVersion $Global:apiVersion -Properties $createProperties
return $resource
}
function New-VirtualNetworkSubnet
{
Param
(
$Name,
$AddressPrefix,
$NetworkSecurityGroup
)
$properties = @{'addressPrefix' = $addressPrefix}
if ($networkSecurityGroup)
{
$properties.Add('networkSecurityGroup', @{'id' = $networkSecurityGroup})
}
$subnet = @{'name' = $Name; 'properties' = $properties}
return $subnet
}
function New-NetworkSecurityGroupResource
{
Param
(
$Name,
$Location,
[Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMRoleContext]
$VM
)
# Report all load balanced endpoints to the user so that they know that we do not currently handle these endpoints
$VM | Get-AzureEndpoint | Where-Object {$_.LBSetName -ne $null} | ForEach-Object { Write-Warning $("Endpoint {0} is skipped as load balanced endpoints are NOT currently supported by this cmdlet. You can still manually recreate this endpoint in ARM using the following details: {1}" -f $_.Name, $(ConvertTo-Json $_)) }
# Walk through all endpoints and filter those that are assigned to a load balancer set (we do not currently handle these endpoints)
$endpoints = $VM | Get-AzureEndpoint | Where-Object {$_.LBSetName -eq $null}
# Start translating endpoints to NSG security rules
$securityRules = @()
$priority = 1000
foreach ($endpoint in $endpoints)
{
$securityRules += @{'name' = $endpoint.Name;
'properties' = @{ 'priority'= $priority;
'sourceAddressPrefix' = '*';
'protocol' = $endpoint.Protocol;
'destinationPortRange' = $endpoint.LocalPort;
'access' = 'Allow';
'direction' = 'inbound';
'sourcePortRange' = '*';
'destinationAddressPrefix' = '*'
}}
$priority += 500
}
$createProperties = @{'securityRules' = $securityRules}
$resource = New-ResourceTemplate -Type "Microsoft.Network/networkSecurityGroups" -Name $Name `
-Location $Location -ApiVersion '2015-06-15' -Properties $createProperties
return $resource
}
function New-NetworkInterfaceResource
{
Param
(
$Name,
$Location,
[string]
$SubnetReference,
[string]
$NsgReference,
[string]
$PrivateIpAddress,
[string]
$PublicIpAddressName,
[string[]]
$Dependencies
)
$subnet = @{'id' = $SubnetReference;}
$ipConfigurations = @{ 'subnet' = $subnet;}
if($PublicIpAddressName)
{
$publicIPAddress = @{'id' = '[resourceId(''Microsoft.Network/publicIPAddresses'',''{0}'')]' -f $PublicIpAddressName;}
$ipConfigurations.Add('publicIPAddress', $publicIPAddress);
}
# Static and dynamic IPs will result in different property sets
if($PrivateIpAddress)
{
$ipConfigurations.Add('privateIPAllocationMethod', 'Static');
$ipConfigurations.Add('privateIPAddress', $PrivateIpAddress);
}
else
{
$ipConfigurations.Add('privateIPAllocationMethod', 'Dynamic');
}
$ipConfigName = "{0}_config1" -f $Name
$createProperties = @{'ipConfigurations' = @(@{'name' = $ipConfigName; 'properties' = $ipConfigurations;});
'networkSecurityGroup' = @{'id' = $NsgReference}}
$resource = New-ResourceTemplate -Type "Microsoft.Network/networkInterfaces" -Name $Name `
-Location $Location -ApiVersion $Global:apiVersion -Properties $createProperties -DependsOn $Dependencies
return $resource
}
function New-PublicIpAddressResource
{
Param
(
$Name,
$Location,
[string]
$AllocationMethod,
[string]
$DnsName
)
$dnsSettings = @{'domainNameLabel' = $DnsName}
$createProperties = @{'publicIPAllocationMethod' = $AllocationMethod; 'dnsSettings' = $dnsSettings}
$resource = New-ResourceTemplate -Type "Microsoft.Network/publicIPAddresses" -Name $Name `
-Location $Location -ApiVersion $Global:apiVersion -Properties $createProperties
return $resource
}
function Get-AvailableAddressSpace
{
[OutputType([string])]
Param
(
[string[]]
$addressSpaces
)
if ($addressSpaces -eq $null -or $addressSpaces.Length -eq 0)
{
# Return default, 10.0.0.1/16 network
return "10.0.0.0/16"
}
$hostRanges = @()
$addressSpaces | ForEach-Object {Get-HostRange $_} | Sort-Object -Property 'NetworkInt' | ForEach-Object {$hostRanges += $_}
$minRange = [uint32]::MinValue
$firstRangeNetwork = $hostRanges[0].Network.Split('.')
if ($firstRangeNetwork[0] -eq 10)
{
$minRange = [uint32]([uint32]10 -shl 24)
} elseif ($firstRangeNetwork[0] -eq 172 -and $firstRangeNetwork[1] -eq 16)
{
$minRange = [uint32]([uint32]172 -shl 24) + [uint32]([uint32]16 -shl 16)
} elseif ($firstRangeNetwork[0] -eq 192 -and $firstRangeNetwork[1] -eq 168)
{
$minRange = [uint32]([uint32]192 -shl 24) + [uint32]([uint32]168 -shl 16)
} else
{
throw "Invalid IP range. Must conform rfc1918"
}
$networkRanges = @()
$networkRanges += Get-FirstAvailableRange -Start $minRange -End ($hostRanges[0].NetworkInt - 1)
for ($i = 0; $i -lt $hostRanges.Length - 1; $i++)
{
$networkRanges += Get-FirstAvailableRange -Start ($hostRanges[$i].BroadcastInt + 1) -End ($hostRanges[$i + 1].NetworkInt - 1)
}
$maxRange = [uint32]::MinValue
$lastRangeNetwork = $hostRanges[$hostRanges.Length - 1].Network.Split('.')
if ($lastRangeNetwork[0] -eq 10)
{
$maxRange = [uint32](([uint32]10 -shl 24) + ([uint32]255 -shl 16) + ([uint32]255 -shl 8) + [uint32]255) + 1
} elseif ($lastRangeNetwork[0] -eq 172 -and $lastRangeNetwork[1] -eq 16)
{
$maxRange = [uint32](([uint32]172 -shl 24) + ([uint32]31 -shl 16) + ([uint32]255 -shl 8) + [uint32]255) + 1
} elseif ($lastRangeNetwork[0] -eq 192 -and $lastRangeNetwork[1] -eq 168)
{
$maxRange = [uint32](([uint32]192 -shl 24) + ([uint32]168 -shl 16) + ([uint32]255 -shl 8) + [uint32]255) + 1
} else
{
throw "Invalid IP range. Must conform rfc1918"
}
$networkRanges += Get-FirstAvailableRange -Start ($hostRanges[$hostRanges.Length - 1].BroadcastInt + 1) -End $maxRange
if (-not $networkRanges -or $networkRanges.Length -le 0)
{
return ""
}
$firstRange = $networkRanges[0]
return "{0}/{1}" -f $firstRange.Network, $(Get-PrefixForNetwork $firstRange.Hosts)
}
function Get-HostRange
{
[OutputType([PSCustomObject])]
Param
(
[string]
$cidrBlock
)
$network, [int]$cidrPrefix = $cidrBlock.Split('/')
if ($cidrPrefix -eq 0)
{
throw "No network prefix is found"
}
[uint32] $uintNetwork = Get-IntIp $network
$networkMask = (-bnot [uint32]0) -shl (32 - $cidrPrefix)
$broadcast = $uintNetwork -bor ((-bnot $networkMask) -band [uint32]::MaxValue)
$networkRange = @{'Network' = Get-DecimalIp $uintNetwork; 'Broadcast' = Get-DecimalIp $broadcast; `
'Hosts' = ($broadcast - $uintNetwork - 1); 'StartHost' = Get-DecimalIp ($uintNetwork + 1); 'EndHost' = Get-DecimalIp($broadcast - 1); `
'BroadcastInt' = $broadcast; 'NetworkInt' = $uintNetwork}
return [PSCustomObject] $networkRange
}
function Get-DecimalIp
{
[OutputType([string])]
Param
(
[uint32]
$uintIp
)
return "{0}.{1}.{2}.{3}" -f [int]($uintIp -shr 24), [int](($uintIp -shr 16) -band 255), [int](($uintIp -shr 8) -band 255), [int]($uintIp -band 255)
}
function Get-IntIp
{
[OutputType([uint32])]
Param(
[string]
$ipString
)
$dottedDecimals = $ipString.Split('.')
return [uint32]([uint32]$dottedDecimals[0] -shl 24) + [uint32]([uint32]$dottedDecimals[1] -shl 16) + [uint32]([uint32]$dottedDecimals[2] -shl 8) + [uint32]$dottedDecimals[3]
}
function Get-FirstAvailableRange
{
[OutputType([PSCustomObject])]
Param
(
[uint32]
$Start,
[uint32]
$End
)
if ($Start -ge $End)
{
return @()
}
$blockSize = 512
$rangesCount = [math]::Floor(($End - $Start) / $blockSize)
$ranges = @()
if ($rangesCount -gt 0)
{
#for ($i = 0; $i -lt $rangesCount; $i++)
# Just grab the first range, but leave the above for reference. The potential number of ranges can
# be quite large, so go for this optimization for the small block sizes.
for ($i = 0; $i -lt 1; $i++)
{
$uintNetwork = ($start + ($i * $blockSize))
$broadcast = ($start + ($i + 1) * $blockSize -1)
$networkRange = @{'Network' = Get-DecimalIp $uintNetwork; 'Broadcast' = Get-DecimalIp $broadcast; `
'Hosts' = ($broadcast - $uintNetwork - 1); 'StartHost' = Get-DecimalIp ($uintNetwork + 1); 'EndHost' = Get-DecimalIp($broadcast - 1); `
'BroadcastInt' = $broadcast; 'NetworkInt' = $uintNetwork}
$ranges += $networkRange
}
}
$remainingRange = ($End - $Start) % $blockSize
if ($remainingRange -gt 0) {
$uintNetwork = $start
$broadcast = $start + $remainingRange
$networkRange = @{'Network' = Get-DecimalIp $uintNetwork; 'Broadcast' = Get-DecimalIp $broadcast; `
'Hosts' = ($broadcast - $uintNetwork - 1); 'StartHost' = Get-DecimalIp ($uintNetwork + 1); 'EndHost' = Get-DecimalIp($broadcast - 1); `
'BroadcastInt' = $broadcast; 'NetworkInt' = $uintNetwork}
$ranges += $networkRange
}
if($ranges.Count -gt 0)
{
return $ranges[0]
}
}
function Get-PrefixForNetwork
{
[OutputType([int])]
Param
(
[uint32]
$NetworkSize
)
$NetworkSize++
$netPrefix = 0
do
{
$NetworkSize = $NetworkSize -shr 1
$netPrefix++
}
until ($NetworkSize -eq 0)
return (32 - $netPrefix)
}
function Test-SubnetInAddressSpace
{
[OutputType([boolean])]
Param(
$SubnetPrefix,
$AddressSpace
)
$subnetIp, [int]$subnetBits = $SubnetPrefix.Split('/')
$AddressSpaceIp, [int]$addressSpaceBits = $AddressSpace.Split('/')
$subnetMask = (-bnot [uint32]0) -shl (32 - $subnetBits)
$addressSpaceMask = (-bnot [uint32]0) -shl (32 - $addressSpaceBits)
$intSubnet = Get-IntIp $subnetIp
$intAddressSpace = Get-IntIp $AddressSpaceIp
return (($intSubnet -band $addressSpaceMask) -eq $intAddressSpace)
}