PSSubnetCarver.Core.psm1

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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<#
.SYNOPSIS
 Import a PSSubnetCarver context from a file on disk, or from Azure blob storage.
 
.DESCRIPTION
 Import a PSSubnetCarver context from a .json file either stored on disk, or stored in Azure blob storage. If in Azure blob storage, it will be downloaded via the REST API using a SAS token.
 
.PARAMETER Path
 The path of the .json file that contains the stored information about the context.
 
.PARAMETER Json
 The JSON of the object.
 
.PARAMETER StorageAccountName
 The name of the Azure storage account.
 
.PARAMETER ContainerName
 The name of the container in the Azure storage account.
 
.PARAMETER SASToken
 The SAS token to use when making the REST request.
 
.PARAMETER ContextName
 The name of the context to identify which .json file to download from Azure.
#>

function Import-SCContext {
    [CmdletBinding(DefaultParameterSetName = "FromFile")]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "FromFile")]
        [ValidateScript( {
                if (-not (Test-Path -Path $_ -PathType Leaf)) {
                    throw "File not found at $_"
                }

                return $true
            })]
        [string]$Path,

        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "FromJson")]
        [string]$Json,

        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "FromAzure")]
        [string]$StorageAccountName,

        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "FromAzure")]
        [string]$ContainerName,

        [Parameter(Mandatory = $true, Position = 2, ParameterSetName = "FromAzure")]
        [string]$SASToken,

        [Parameter(Mandatory = $false, Position = 3, ParameterSetName = "FromAzure")]
        [string]$ContextName = "default"
    )

    if ($PSCmdlet.ParameterSetName -eq "FromFile") {
        $model = Get-Content -Path $Path -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
    }
    elseif ($PSCmdlet.ParameterSetName -eq "FromJson") {
        $model = $Json | ConvertFrom-Json
    }
    elseif ($PSCmdlet.ParameterSetName -eq "FromAzure") {

        $fileName = "$($ContextName.ToLower()).json"

        $filePath = Join-Path -Path $env:TEMP -ChildPath $fileName -ErrorAction Stop

        $blobDownloadParams = @{
            URI         = "https://$($StorageAccountName).blob.core.windows.net/$($ContainerName)/$($fileName)?$($SASToken.TrimStart('?'))"
            Method      = "GET"
            Headers     = @{
                'x-ms-blob-type' = "BlockBlob"
                'x-ms-meta-m1'   = 'v1'
                'x-ms-meta-m2'   = 'v2'
            }
            OutFile     = $filePath
            ErrorAction = 'Stop'
        }

        $null = Invoke-RestMethod @blobDownloadParams

        try {
            $model = Get-Content -Path $filePath | ConvertFrom-Json -ErrorAction Stop
        }
        finally {
            Remove-Item -LiteralPath $filePath -Force -Confirm:$false -ErrorAction SilentlyContinue
        }

    }
    else {
        Write-Error -Message "Parameter set $($PSCmdlet.ParameterSetName) has not been properly implemented." -ErrorAction Stop
    }

    Set-SCContext -Name $model.Name -RootAddressSpace $model.RootIPAddressRange -ConsumedIPRanges $model.ConsumedRanges
}

<#
.SYNOPSIS
 Export a PSSubnetCarver context to disk, or to Azure blob storage.
 
.DESCRIPTION
 Export a PSSubnetCarver context to a .json file, and either store it on the local disk, or in Azure blob storage. If in Azure blob storage, it will be uploaded via the REST API using a SAS token.
 
.PARAMETER Path
 The path of the .json file that will contain the stored information about the context.
 
.PARAMETER StorageAccountName
 The name of the Azure storage account.
 
.PARAMETER ContainerName
 The name of the container in the Azure storage account.
 
.PARAMETER SASToken
 The SAS token to use when making the REST request.
 
.PARAMETER ContextName
 The name of the context to export.
 
#>

function Export-SCContext {
    [CmdletBinding(DefaultParameterSetName = "ToFile")]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "ToFile")]
        [string]$Path,

        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "ToAzure")]
        [string]$StorageAccountName,

        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "ToAzure")]
        [string]$ContainerName,

        [Parameter(Mandatory = $true, Position = 2, ParameterSetName = "ToAzure")]
        [string]$SASToken,

        [Parameter(Mandatory = $false, Position = 1, ParameterSetName = "ToFile")]
        [Parameter(Mandatory = $false, Position = 3, ParameterSetName = "ToAzure")]
        [string]$ContextName = "default",

        [Parameter(ParameterSetName = "ToFile")]
        [switch]$Force
    )

    $model = Get-SCContext -Name $ContextName -ErrorAction Stop

    $serialModel = [PSCustomObject]@{Name = $model.Name; RootIPAddressRange = $model.RootIPAddressRange.ToString(); ConsumedRanges = [System.Collections.ArrayList]@() }

    $model.ConsumedRanges | ForEach-Object -Process { $null = $serialModel.ConsumedRanges.Add($_.ToString()) }

    $json = $serialModel | ConvertTo-Json

    if ($PSCmdlet.ParameterSetName -eq "ToFile") {
        $json | Out-File -FilePath $Path -Force:$Force
    }
    elseif ($PSCmdlet.ParameterSetName -eq "ToAzure") {
        $fileName = "$($ContextName.ToLower()).json"

        $filePath = Join-Path -Path $env:TEMP -ChildPath $fileName -ErrorAction Stop

        $json | Out-File -FilePath $filePath -Force -ErrorAction Stop

        try {
            $blobUploadParams = @{
                URI         = "https://$($StorageAccountName).blob.core.windows.net/$($ContainerName)/$($fileName)?$($SASToken.TrimStart('?'))"
                Method      = "PUT"
                Headers     = @{
                    'x-ms-blob-type'                = "BlockBlob"
                    'x-ms-blob-content-disposition' = "attachment; filename=`"$($fileName)`""
                    'x-ms-meta-m1'                  = 'v1'
                    'x-ms-meta-m2'                  = 'v2'
                }
                InFile      = $filePath
                ErrorAction = 'Stop'
            }

            $null = Invoke-RestMethod @blobUploadParams
        }
        finally {
            Remove-Item -LiteralPath $filePath -Force -Confirm:$false -ErrorAction SilentlyContinue
        }
    }
    else {
        Write-Error -Message "Parameter set $($PSCmdlet.ParameterSetName) has not been properly implemented."
    }
}

<#
.SYNOPSIS
 Rename an in-memory context.
 
.DESCRIPTION
 Rename an in-memory context, without modifying it's contents.
 
.PARAMETER OldContextName
 The name of the context as it is in memory as the command is run. Case insensitive.
 
.PARAMETER NewContextName
 The new name of the context. Case insensitive.
#>

function Rename-SCContext {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$OldContextName,

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$NewContextName
    )

    if ([qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances.ContainsKey($OldContextName)) {
        $context = Get-SCContext -Name $OldContextName -ErrorAction Stop

        Set-SCContext -Name $NewContextName -RootAddressSpace $context.RootIPAddressRange -ConsumedIPRanges $context.ConsumedRanges -ErrorAction Stop

        $null = [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances.Remove($OldContextName)
    }
    else {
        Write-Error -Message "No context stored with name $OldContextName"
    }
}

function Test-ContextConfigured {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string] $Name
    )

    return [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances.ContainsKey($Name)

}

<#
.SYNOPSIS
 Clear the named context of it's stored networks, without deleting the context from memory.
 
.DESCRIPTION
 Clear the named context of it's stored networks, without deleting the context from memory.
 
.PARAMETER Name
 The name of the context stored in memory.
 
.EXAMPLE
 Clear-SCContext -Name default
 
 Clear the 'default' context of it's contents, but leave the empty store in memory.
#>


function Clear-SCContext {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $false, Position = 0)]
        [string] $Name = "default"
    )

    if (-not (Test-ContextConfigured -Name $Name -ErrorAction Stop)) {
        Write-Error -Message "No context configured named $Name." -Exception ([qIPAM.Core.Extensions.QIPSubnetCarverContextNotSetException]::new($Name)) -ErrorAction Stop
    }

    [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances.Clear()
}

<#
.SYNOPSIS
 Return an object that contains the root network, as well as all of the consumed networks in the named context.
 
.DESCRIPTION
 Return an object that contains the root network, as well as all of the consumed networks in the named context.
 
.PARAMETER Name
 The name of the context stored in memory.
 
.EXAMPLE
 Get-SCContext -Name default
 
 Return the contents of the default Context
 
.EXAMPLE
 Get-SCContext
 
 Return the contents of the all contexts stored in memory.
#>


function Get-SCContext {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $false, Position = 0)]
        [string[]] $Name
    )

    if (-not ($MyInvocation.BoundParameters.ContainsKey('Name'))) {

        [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances.Keys | ForEach-Object -Process {

            [qIPAM.Core.Models.QIPContextModel]::new($_, [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$_].RootCIDR, [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$_].ConsumedRanges)

        }

        return
    }

    $Name | ForEach-Object -Process {
        if (-not (Test-ContextConfigured -Name $_ -ErrorAction Stop)) {
            Write-Error -Message "No context configured named $_." -Exception ([qIPAM.Core.Extensions.QIPSubnetCarverContextNotSetException]::new($_)) -ErrorAction Stop
        }

        [qIPAM.Core.Models.QIPContextModel]::new($_, [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$_].RootCIDR, [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$_].ConsumedRanges)
    }
}

<#
.SYNOPSIS
 Reserve an IP address in a stored context.
 
.DESCRIPTION
 Reserve an IP address in a stored context. You can either reserve a specific IP, or the next available address space that meets your criteria.
 
.PARAMETER ContextName
 The name of the context stored in memory.
 
.PARAMETER ReserveCIDR
 The CIDR range(s) to reserve.
 
.PARAMETER ReserveCount
 The number of hosts to reserve.
 
.PARAMETER ReserveIPAddress
 The specific IP address range(s) to reserve.
 
.PARAMETER ReserveNextPointToPoint
 Reserve the next point-to-point network (/31) in the range.
 
.EXAMPLE
 Get-SCSubnet -ReserveCIDR 16 -ContextName default
 
 Reserves and returns the next available /16 in the default context.
 
.EXAMPLE
 Get-SCSubnet -ReserveNextPointToPoint -ContextName default
 
 Reserves and returns the next available point-to-point network (/31) in the default context.
 
.EXAMPLE
 Get-SCSubnet -ReserveIPAddress "10.0.1.0/24" -ContextName default
 
 Reserves and returns the 10.0.1.0/24 network from the default context, if it's available.
 
.EXAMPLE
 Get-SCSubnet -ReserveCount 250 -ContextName default
 
 Reserves the next available network from the default context with at least 250 usable hosts.
#>


function Get-SCSubnet {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName = "NEXT")]
        [ValidateRange(8, 31)]
        [int[]] $ReserveCIDR,

        [Parameter(Mandatory = $true, ParameterSetName = "NEXTP2P")]
        [Alias("P2P")]
        [switch] $ReserveNextPointToPoint,

        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName = "SPECIFIC")]
        [qIPAM.Core.Models.IPAddressRange[]] $ReserveIPAddress,

        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = "COUNT")]
        [ValidateRange(0, 4294967296)]
        [UInt64[]] $ReserveCount,

        [Parameter(Mandatory = $false, Position = 1, ParameterSetName = "NEXT")]
        [Parameter(Mandatory = $false, Position = 0, ParameterSetName = "NEXTP2P")]
        [Parameter(Mandatory = $false, Position = 1, ParameterSetName = "SPECIFIC")]
        [Parameter(Mandatory = $false, Position = 1, ParameterSetName = "COUNT")]
        [Alias("Name", "Context")]
        [string] $ContextName = "default"
    )
    Begin {
        if (-not (Test-ContextConfigured -Name $ContextName -ErrorAction Stop)) {
            Write-Error -Message "No context configured named $ContextName." -Exception ([qIPAM.Core.Extensions.QIPSubnetCarverContextNotSetException]::new($ContextName)) -ErrorAction Stop
        }
    }
    Process {

        if ($PSCmdlet.ParameterSetName -eq "NEXT") {

            foreach ($cidr in $ReserveCIDR) {
                [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$ContextName].Reserve($cidr)
            }

        }
        elseif ($PSCmdlet.ParameterSetName -eq "NEXTP2P") {
            [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$ContextName].Reserve(31)
        }
        elseif ($PSCmdlet.ParameterSetName -eq "SPECIFIC") {

            foreach ($ipAddress in $ReserveIPAddress) {
                [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$ContextName].Reserve($ipAddress)
            }

        }
        elseif ($PSCmdlet.ParameterSetName -eq "COUNT") {

            foreach ($count in $ReserveCount) {

                if ($count -le 2) {
                    $cidrNeeded = [qIPAM.Core.Extensions.QIPSubnetCarverContext]::GetCIDRFromCount($count)
                }
                else {
                    $cidrNeeded = [qIPAM.Core.Extensions.QIPSubnetCarverContext]::GetCIDRFromCount($count + 2)
                }

                [qIPAM.Core.Extensions.QIPSubnetCarverContext]::Instances[$ContextName].Reserve($cidrNeeded)
            }
        }
        else {
            Write-Error -Message "Parameter set $($PSCmdlet.ParameterSetName) has not been implemented" -ErrorAction Stop
        }

    }
}

<#
.SYNOPSIS
 Set or reset a root network in memory to be carved into subnets.
 
.DESCRIPTION
 Set or reset a root network in memory to be carved into subnets.
 
.PARAMETER Name
 The name of the context stored, or to be stored, in memory.
 
.PARAMETER ConsumedIPRanges
 Any consumed IP address range(s) that should be considered already consumed in the in-memory context.
 
.EXAMPLE
 Set-SCContext -RootAddressSpace "10.0.0.0/8" -Name default
 
 Create a context in memory named "default" with a root range of 10.0.0.0/8. This will be an empty network.
 
.EXAMPLE
 Set-SCContext -RootAddressSpace "10.0.0.0/8" -Name default -ConsumedIPRanges "10.0.1.0/16","10.0.2.0/16"
 
 Create a context in memory named "default" with a root range of 10.0.0.0/8, and the ranges 10.0.1.0/16 and 10.0.2.0/16 already occupied.
#>


function Set-SCContext {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [qIPAM.Core.Models.IPAddressRange] $RootAddressSpace,

        [Parameter(Mandatory = $false, Position = 1)]
        [Alias("Context", "ContextName")]
        [string] $Name = "default",

        [Parameter(Mandatory = $false, Position = 2)]
        [qIPAM.Core.Models.IPAddressRange[]] $ConsumedIPRanges = $null
    )

    [qIPAM.Core.Extensions.QIPSubnetCarverContext]::SetContext($Name, $RootAddressSpace, $ConsumedIPRanges)
}

<#
.SYNOPSIS
 Test if a network configuration is valid.
 
.DESCRIPTION
 Test if a network configuration is valid given a root network and a list of contained subnetworks.
 
.PARAMETER RootIPRange
 The root IP range of the virtual network to test.
 
.PARAMETER ConsumedIPRanges
 Consumed ranges within the virtual network being tested.
 
.EXAMPLE
 Test-SCNetworkIsValid -RootIPRange "10.0.0.0/8" -ConsumedIPRanges "10.1.0.0/16","10.2.0.0/16","10.1.0.0/24"
 
 Test if a list of consumed ranges can all fit into a given root. This example would fail, and indicate that the 10.1.0.0/24 range doesn't fit.
#>


function Test-SCNetworkIsValid {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [qIPAM.Core.Models.IPAddressRange] $RootIPRange,

        [Parameter(Mandatory = $false, Position = 1)]
        [qIPAM.Core.Models.IPAddressRange[]] $ConsumedIPRanges
    )

    [qIPAM.Core.Extensions.QIPSubnetCarverContext]::TestNetwork($RootIPRange, $ConsumedIPRanges)
}