diff --git a/Functions/IPAM/IPAM.ps1 b/Functions/IPAM/IPAM.ps1 index 34c354a..4a79fe0 100644 --- a/Functions/IPAM/IPAM.ps1 +++ b/Functions/IPAM/IPAM.ps1 @@ -113,6 +113,10 @@ function VerifyIPAMChoices { $ValidValues = New-Object System.Collections.ArrayList + if (-not $script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName)) { + throw "Missing choices for $($PSCmdlet.ParameterSetName)" + } + [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).value) [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).label) @@ -274,7 +278,7 @@ function Get-NetboxIPAMAddress { InvokeNetboxRequest -URI $uri -Raw:$Raw } -function Get-NetboxIPAMAvaiableIP { +function Get-NetboxIPAMAvailableIP { <# .SYNOPSIS A convenience method for returning available IP addresses within a prefix @@ -289,6 +293,9 @@ function Get-NetboxIPAMAvaiableIP { .PARAMETER NumberOfIPs A description of the NumberOfIPs parameter. + .PARAMETER Raw + A description of the Raw parameter. + .EXAMPLE PS C:\> Get-NetboxIPAMAvaiableIP -Prefix_ID $value1 @@ -302,6 +309,7 @@ function Get-NetboxIPAMAvaiableIP { [Parameter(Mandatory = $true)] [uint16]$Prefix_ID, + [Alias('Limit')] [uint16]$NumberOfIPs, [switch]$Raw @@ -321,6 +329,86 @@ function Get-NetboxIPAMAvaiableIP { } function Get-NetboxIPAMPrefix { +<# + .SYNOPSIS + A brief description of the Get-NetboxIPAMPrefix function. + + .DESCRIPTION + A detailed description of the Get-NetboxIPAMPrefix function. + + .PARAMETER Limit + A description of the Limit parameter. + + .PARAMETER Offset + A description of the Offset parameter. + + .PARAMETER Family + A description of the Family parameter. + + .PARAMETER Is_Pool + A description of the Is_Pool parameter. + + .PARAMETER Id + A description of the Id parameter. + + .PARAMETER Query + A description of the Query parameter. + + .PARAMETER Within + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Within_Include + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Contains + A description of the Contains parameter. + + .PARAMETER Mask_Length + CIDR mask length value + + .PARAMETER VRF + A description of the VRF parameter. + + .PARAMETER VRF_Id + A description of the VRF_Id parameter. + + .PARAMETER Tenant + A description of the Tenant parameter. + + .PARAMETER Tenant_Id + A description of the Tenant_Id parameter. + + .PARAMETER Site + A description of the Site parameter. + + .PARAMETER Site_Id + A description of the Site_Id parameter. + + .PARAMETER Vlan_VId + A description of the Vlan_VId parameter. + + .PARAMETER Vlan_Id + A description of the Vlan_Id parameter. + + .PARAMETER Status + A description of the Status parameter. + + .PARAMETER Role + A description of the Role parameter. + + .PARAMETER Role_Id + A description of the Role_Id parameter. + + .PARAMETER Raw + A description of the Raw parameter. + + .EXAMPLE + PS C:\> Get-NetboxIPAMPrefix + + .NOTES + Additional information about the function. +#> + [CmdletBinding()] param ( @@ -330,16 +418,19 @@ function Get-NetboxIPAMPrefix { [object]$Family, + [boolean]$Is_Pool, + [uint16[]]$Id, [string]$Query, - #[string]$Within, - - #[string]$Within_Include, - + [string]$Within, + + [string]$Within_Include, + [string]$Contains, + [ValidateRange(0, 127)] [byte]$Mask_Length, [string]$VRF, @@ -377,7 +468,8 @@ function Get-NetboxIPAMPrefix { $uriSegments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) - $URIParameters = @{} + $URIParameters = @{ + } foreach ($CmdletParameterName in $PSBoundParameters.Keys) { if ($CmdletParameterName -in $CommonParameterNames) { diff --git a/Staging/NetboxPS.psm1 b/Staging/NetboxPS.psm1 index 2787b1c..4e5c1d3 100644 --- a/Staging/NetboxPS.psm1 +++ b/Staging/NetboxPS.psm1 @@ -2,7 +2,7 @@ .NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150 - Generated on: 5/11/2018 3:48 PM + Generated on: 5/11/2018 4:30 PM Generated by: Ben Claussen Organization: NEOnet -------------------------------------------------------------------------------- @@ -13,16 +13,16 @@ #region Invoke-Helpers_ps1 <# - .NOTES - =========================================================================== + .NOTES + =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.148 - Created on: 2/28/2018 3:33 PM - Created by: Ben Claussen - Organization: NEOnet - Filename: Helpers.ps1 - =========================================================================== - .DESCRIPTION - These function are internal functions and generally are not + Created on: 2/28/2018 3:33 PM + Created by: Ben Claussen + Organization: NEOnet + Filename: Helpers.ps1 + =========================================================================== + .DESCRIPTION + These function are internal functions and generally are not exposed to the end user #> @@ -885,8 +885,7 @@ $uriSegments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces')) - $URIParameters = @{ - } + $URIParameters = @{} foreach ($CmdletParameterName in $PSBoundParameters.Keys) { if ($CmdletParameterName -in $CommonParameterNames) { @@ -1284,6 +1283,10 @@ $ValidValues = New-Object System.Collections.ArrayList + if (-not $script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName)) { + throw "Missing choices for $($PSCmdlet.ParameterSetName)" + } + [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).value) [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).label) @@ -1445,7 +1448,7 @@ InvokeNetboxRequest -URI $uri -Raw:$Raw } - function Get-NetboxIPAMAvaiableIP { + function Get-NetboxIPAMAvailableIP { <# .SYNOPSIS A convenience method for returning available IP addresses within a prefix @@ -1460,6 +1463,9 @@ .PARAMETER NumberOfIPs A description of the NumberOfIPs parameter. + .PARAMETER Raw + A description of the Raw parameter. + .EXAMPLE PS C:\> Get-NetboxIPAMAvaiableIP -Prefix_ID $value1 @@ -1473,6 +1479,7 @@ [Parameter(Mandatory = $true)] [uint16]$Prefix_ID, + [Alias('Limit')] [uint16]$NumberOfIPs, [switch]$Raw @@ -1492,6 +1499,86 @@ } function Get-NetboxIPAMPrefix { + <# + .SYNOPSIS + A brief description of the Get-NetboxIPAMPrefix function. + + .DESCRIPTION + A detailed description of the Get-NetboxIPAMPrefix function. + + .PARAMETER Limit + A description of the Limit parameter. + + .PARAMETER Offset + A description of the Offset parameter. + + .PARAMETER Family + A description of the Family parameter. + + .PARAMETER Is_Pool + A description of the Is_Pool parameter. + + .PARAMETER Id + A description of the Id parameter. + + .PARAMETER Query + A description of the Query parameter. + + .PARAMETER Within + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Within_Include + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Contains + A description of the Contains parameter. + + .PARAMETER Mask_Length + CIDR mask length value + + .PARAMETER VRF + A description of the VRF parameter. + + .PARAMETER VRF_Id + A description of the VRF_Id parameter. + + .PARAMETER Tenant + A description of the Tenant parameter. + + .PARAMETER Tenant_Id + A description of the Tenant_Id parameter. + + .PARAMETER Site + A description of the Site parameter. + + .PARAMETER Site_Id + A description of the Site_Id parameter. + + .PARAMETER Vlan_VId + A description of the Vlan_VId parameter. + + .PARAMETER Vlan_Id + A description of the Vlan_Id parameter. + + .PARAMETER Status + A description of the Status parameter. + + .PARAMETER Role + A description of the Role parameter. + + .PARAMETER Role_Id + A description of the Role_Id parameter. + + .PARAMETER Raw + A description of the Raw parameter. + + .EXAMPLE + PS C:\> Get-NetboxIPAMPrefix + + .NOTES + Additional information about the function. + #> + [CmdletBinding()] param ( @@ -1501,16 +1588,19 @@ [object]$Family, + [boolean]$Is_Pool, + [uint16[]]$Id, [string]$Query, - #[string]$Within, - - #[string]$Within_Include, - + [string]$Within, + + [string]$Within_Include, + [string]$Contains, + [ValidateRange(0, 127)] [byte]$Mask_Length, [string]$VRF, @@ -1548,7 +1638,8 @@ $uriSegments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) - $URIParameters = @{} + $URIParameters = @{ + } foreach ($CmdletParameterName in $PSBoundParameters.Keys) { if ($CmdletParameterName -in $CommonParameterNames) { diff --git a/Staging/Tests/IPAM.Tests.ps1 b/Staging/Tests/IPAM.Tests.ps1 index ddcf174..ef9b994 100644 --- a/Staging/Tests/IPAM.Tests.ps1 +++ b/Staging/Tests/IPAM.Tests.ps1 @@ -56,6 +56,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -66,6 +67,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -76,6 +78,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=10.10.0.0' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -86,6 +89,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=my+aggregate' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -96,6 +100,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -106,6 +111,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } } @@ -118,6 +124,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -128,6 +135,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -138,6 +146,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=10.10.10.10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -148,6 +157,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=my+ip+address' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -158,6 +168,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -168,6 +179,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } #region TODO: Figure out how to mock/test Verification appropriately... @@ -197,8 +209,140 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { #endregion } - Context -Name "Get-NetboxIPAMPrefix" { + Context -Name "Get-NetboxIPAMAvailableIP" -Fixture { + It "Should request the default number of available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/available-ips/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + It "Should request 10 available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 1504 -NumberOfIPs 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/1504/available-ips/?limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + } + + Context -Name "Get-NetboxIPAMPrefix" { + It "Should request the default number of prefixes" { + $Result = Get-NetboxIPAMPrefix + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with limit and offset" { + $Result = Get-NetboxIPAMPrefix -Limit 10 -Offset 12 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?offset=12&limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a query" { + $Result = Get-NetboxIPAMPrefix -Query '10.10.10.10' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=10.10.10.10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with an escaped query" { + $Result = Get-NetboxIPAMPrefix -Query 'my ip address' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=my+ip+address' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a single ID" { + $Result = Get-NetboxIPAMPrefix -Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with multiple IDs" { + $Result = Get-NetboxIPAMPrefix -Id 10, 12, 15 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?id__in=10,12,15' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with VLAN vID" { + $Result = Get-NetboxIPAMPrefix -VLAN_VID 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?vlan_vid=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + <# + It "Should request with family of 4" { + Mock -CommandName "VerifyIPAMChoices" -ModuleName 'NetboxPS' -MockWith { + return 4 + } -Verifiable + $Result = Get-NetboxIPAMPrefix -Family 4 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?family=4' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + #> + + It "Should request with mask length 24" { + $Result = Get-NetboxIPAMPrefix -Mask_length 24 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?mask_length=24' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should throw because the mask length is too large" { + { + Get-NetboxIPAMPrefix -Mask_length 128 + } | Should -Throw + } } } } diff --git a/Tests/IPAM.Tests.ps1 b/Tests/IPAM.Tests.ps1 index ddcf174..8ec8aa6 100644 --- a/Tests/IPAM.Tests.ps1 +++ b/Tests/IPAM.Tests.ps1 @@ -56,6 +56,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -66,6 +67,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -76,6 +78,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=10.10.0.0' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -86,6 +89,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=my+aggregate' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -96,6 +100,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -106,6 +111,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } } @@ -118,6 +124,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -128,6 +135,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -138,6 +146,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=10.10.10.10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -148,6 +157,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=my+ip+address' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -158,6 +168,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -168,6 +179,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } #region TODO: Figure out how to mock/test Verification appropriately... @@ -197,8 +209,152 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { #endregion } - Context -Name "Get-NetboxIPAMPrefix" { + Context -Name "Get-NetboxIPAMAvailableIP" -Fixture { + It "Should request the default number of available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/available-ips/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + It "Should request 10 available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 1504 -NumberOfIPs 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/1504/available-ips/?limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + } + + Context -Name "Get-NetboxIPAMPrefix" { + It "Should request the default number of prefixes" { + $Result = Get-NetboxIPAMPrefix + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with limit and offset" { + $Result = Get-NetboxIPAMPrefix -Limit 10 -Offset 12 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?offset=12&limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a query" { + $Result = Get-NetboxIPAMPrefix -Query '10.10.10.10' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=10.10.10.10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with an escaped query" { + $Result = Get-NetboxIPAMPrefix -Query 'my ip address' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=my+ip+address' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a single ID" { + $Result = Get-NetboxIPAMPrefix -Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with multiple IDs" { + $Result = Get-NetboxIPAMPrefix -Id 10, 12, 15 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?id__in=10,12,15' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with VLAN vID" { + $Result = Get-NetboxIPAMPrefix -VLAN_VID 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?vlan_vid=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + <# + It "Should request with family of 4" { + Mock -CommandName "VerifyIPAMChoices" -ModuleName 'NetboxPS' -MockWith { + return 4 + } -Verifiable + $Result = Get-NetboxIPAMPrefix -Family 4 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?family=4' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + #> + + It "Should throw because the mask length is too large" { + { + Get-NetboxIPAMPrefix -Mask_length 128 + } | Should -Throw + } + + It "Should throw because the mask length is too small" { + { + Get-NetboxIPAMPrefix -Mask_length -1 + } | Should -Throw + } + + It "Should not throw because the mask length is just right" { + { + Get-NetboxIPAMPrefix -Mask_length 24 + } | Should -Not -Throw + } + + It "Should request with mask length 24" { + $Result = Get-NetboxIPAMPrefix -Mask_length 24 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?mask_length=24' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } } } } diff --git a/dist/NetboxPS.psm1 b/dist/NetboxPS.psm1 index 2787b1c..4e5c1d3 100644 --- a/dist/NetboxPS.psm1 +++ b/dist/NetboxPS.psm1 @@ -2,7 +2,7 @@ .NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150 - Generated on: 5/11/2018 3:48 PM + Generated on: 5/11/2018 4:30 PM Generated by: Ben Claussen Organization: NEOnet -------------------------------------------------------------------------------- @@ -13,16 +13,16 @@ #region Invoke-Helpers_ps1 <# - .NOTES - =========================================================================== + .NOTES + =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.148 - Created on: 2/28/2018 3:33 PM - Created by: Ben Claussen - Organization: NEOnet - Filename: Helpers.ps1 - =========================================================================== - .DESCRIPTION - These function are internal functions and generally are not + Created on: 2/28/2018 3:33 PM + Created by: Ben Claussen + Organization: NEOnet + Filename: Helpers.ps1 + =========================================================================== + .DESCRIPTION + These function are internal functions and generally are not exposed to the end user #> @@ -885,8 +885,7 @@ $uriSegments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces')) - $URIParameters = @{ - } + $URIParameters = @{} foreach ($CmdletParameterName in $PSBoundParameters.Keys) { if ($CmdletParameterName -in $CommonParameterNames) { @@ -1284,6 +1283,10 @@ $ValidValues = New-Object System.Collections.ArrayList + if (-not $script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName)) { + throw "Missing choices for $($PSCmdlet.ParameterSetName)" + } + [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).value) [void]$ValidValues.AddRange($script:NetboxConfig.Choices.IPAM.$($PSCmdlet.ParameterSetName).label) @@ -1445,7 +1448,7 @@ InvokeNetboxRequest -URI $uri -Raw:$Raw } - function Get-NetboxIPAMAvaiableIP { + function Get-NetboxIPAMAvailableIP { <# .SYNOPSIS A convenience method for returning available IP addresses within a prefix @@ -1460,6 +1463,9 @@ .PARAMETER NumberOfIPs A description of the NumberOfIPs parameter. + .PARAMETER Raw + A description of the Raw parameter. + .EXAMPLE PS C:\> Get-NetboxIPAMAvaiableIP -Prefix_ID $value1 @@ -1473,6 +1479,7 @@ [Parameter(Mandatory = $true)] [uint16]$Prefix_ID, + [Alias('Limit')] [uint16]$NumberOfIPs, [switch]$Raw @@ -1492,6 +1499,86 @@ } function Get-NetboxIPAMPrefix { + <# + .SYNOPSIS + A brief description of the Get-NetboxIPAMPrefix function. + + .DESCRIPTION + A detailed description of the Get-NetboxIPAMPrefix function. + + .PARAMETER Limit + A description of the Limit parameter. + + .PARAMETER Offset + A description of the Offset parameter. + + .PARAMETER Family + A description of the Family parameter. + + .PARAMETER Is_Pool + A description of the Is_Pool parameter. + + .PARAMETER Id + A description of the Id parameter. + + .PARAMETER Query + A description of the Query parameter. + + .PARAMETER Within + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Within_Include + Should be a CIDR notation prefix such as '10.0.0.0/16' + + .PARAMETER Contains + A description of the Contains parameter. + + .PARAMETER Mask_Length + CIDR mask length value + + .PARAMETER VRF + A description of the VRF parameter. + + .PARAMETER VRF_Id + A description of the VRF_Id parameter. + + .PARAMETER Tenant + A description of the Tenant parameter. + + .PARAMETER Tenant_Id + A description of the Tenant_Id parameter. + + .PARAMETER Site + A description of the Site parameter. + + .PARAMETER Site_Id + A description of the Site_Id parameter. + + .PARAMETER Vlan_VId + A description of the Vlan_VId parameter. + + .PARAMETER Vlan_Id + A description of the Vlan_Id parameter. + + .PARAMETER Status + A description of the Status parameter. + + .PARAMETER Role + A description of the Role parameter. + + .PARAMETER Role_Id + A description of the Role_Id parameter. + + .PARAMETER Raw + A description of the Raw parameter. + + .EXAMPLE + PS C:\> Get-NetboxIPAMPrefix + + .NOTES + Additional information about the function. + #> + [CmdletBinding()] param ( @@ -1501,16 +1588,19 @@ [object]$Family, + [boolean]$Is_Pool, + [uint16[]]$Id, [string]$Query, - #[string]$Within, - - #[string]$Within_Include, - + [string]$Within, + + [string]$Within_Include, + [string]$Contains, + [ValidateRange(0, 127)] [byte]$Mask_Length, [string]$VRF, @@ -1548,7 +1638,8 @@ $uriSegments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) - $URIParameters = @{} + $URIParameters = @{ + } foreach ($CmdletParameterName in $PSBoundParameters.Keys) { if ($CmdletParameterName -in $CommonParameterNames) { diff --git a/dist/Tests/IPAM.Tests.ps1 b/dist/Tests/IPAM.Tests.ps1 index ddcf174..ef9b994 100644 --- a/dist/Tests/IPAM.Tests.ps1 +++ b/dist/Tests/IPAM.Tests.ps1 @@ -56,6 +56,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -66,6 +67,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -76,6 +78,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=10.10.0.0' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -86,6 +89,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?q=my+aggregate' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -96,6 +100,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -106,6 +111,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/aggregates/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } } @@ -118,6 +124,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with limit and offset" { @@ -128,6 +135,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?offset=12&limit=10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a query" { @@ -138,6 +146,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=10.10.10.10' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with an escaped query" { @@ -148,6 +157,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?q=my+ip+address' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with a single ID" { @@ -158,6 +168,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/10/' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } It "Should request with multiple IDs" { @@ -168,6 +179,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Method | Should -Be 'GET' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?id__in=10,12,15' $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" } #region TODO: Figure out how to mock/test Verification appropriately... @@ -197,8 +209,140 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { #endregion } - Context -Name "Get-NetboxIPAMPrefix" { + Context -Name "Get-NetboxIPAMAvailableIP" -Fixture { + It "Should request the default number of available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/available-ips/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + It "Should request 10 available IPs" { + $Result = Get-NetboxIPAMAvailableIP -Prefix_Id 1504 -NumberOfIPs 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/1504/available-ips/?limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + } + + Context -Name "Get-NetboxIPAMPrefix" { + It "Should request the default number of prefixes" { + $Result = Get-NetboxIPAMPrefix + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with limit and offset" { + $Result = Get-NetboxIPAMPrefix -Limit 10 -Offset 12 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?offset=12&limit=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a query" { + $Result = Get-NetboxIPAMPrefix -Query '10.10.10.10' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=10.10.10.10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with an escaped query" { + $Result = Get-NetboxIPAMPrefix -Query 'my ip address' + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?q=my+ip+address' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with a single ID" { + $Result = Get-NetboxIPAMPrefix -Id 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/10/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with multiple IDs" { + $Result = Get-NetboxIPAMPrefix -Id 10, 12, 15 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?id__in=10,12,15' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should request with VLAN vID" { + $Result = Get-NetboxIPAMPrefix -VLAN_VID 10 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?vlan_vid=10' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + <# + It "Should request with family of 4" { + Mock -CommandName "VerifyIPAMChoices" -ModuleName 'NetboxPS' -MockWith { + return 4 + } -Verifiable + $Result = Get-NetboxIPAMPrefix -Family 4 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?family=4' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + #> + + It "Should request with mask length 24" { + $Result = Get-NetboxIPAMPrefix -Mask_length 24 + + Assert-VerifiableMock + + $Result.Method | Should -Be 'GET' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/prefixes/?mask_length=24' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Headers.Authorization | Should -Be "Token faketoken" + } + + It "Should throw because the mask length is too large" { + { + Get-NetboxIPAMPrefix -Mask_length 128 + } | Should -Throw + } } } }