This commit is contained in:
Ben Claussen 2018-05-18 12:14:27 -04:00
parent 18dfa3a07a
commit 97a88f5cf2
20 changed files with 2067 additions and 501 deletions

View file

@ -25,8 +25,9 @@ function Get-NetboxCircuitsChoices {
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('circuits', '_choices'))

View file

@ -12,7 +12,8 @@
#>
function Get-NetboxExtrasChoices {
[CmdletBinding()]
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('extras', '_choices'))

View file

@ -145,7 +145,7 @@ function BuildURIComponents {
Write-Verbose "Building URI components"
$URIParameters = [System.Collections.Hashtable]::new()
$URIParameters = @{}
foreach ($CmdletParameterName in $ParametersDictionary.Keys) {
if ($CmdletParameterName -in $CommonParameterNames) {
@ -163,7 +163,7 @@ function BuildURIComponents {
# Check if there is one or more values for Id and build a URI or query as appropriate
if (@($ParametersDictionary[$CmdletParameterName]).Count -gt 1) {
Write-Verbose " Joining IDs for parameter"
$URIParameters['id__in'] = $Id -join ','
$URIParameters['id__in'] = $ParametersDictionary[$CmdletParameterName] -join ','
} else {
Write-Verbose " Adding ID to segments"
[void]$uriSegments.Add($ParametersDictionary[$CmdletParameterName])
@ -185,6 +185,7 @@ function BuildURIComponents {
function GetChoiceValidValues {
[CmdletBinding()]
[OutputType([System.Collections.ArrayList])]
param
(
[Parameter(Mandatory = $true)]
@ -220,13 +221,16 @@ function ValidateChoice {
[string]$MajorObject,
[Parameter(Mandatory = $true)]
[string]$ChoiceName
[string]$ChoiceName,
[Parameter(Mandatory = $true)]
[object]$ProvidedValue
)
$ValidValues = GetChoiceValidValues -MajorObject $MajorObject -Choice $ChoiceName
Write-Verbose "Validating $ChoiceName"
Write-Verbose "Checking '$ProvidedValue' against $($ValidValues -join ', ')"
Write-Verbose "Checking '$ProvidedValue' against [$($ValidValues -join ', ')]"
if ($ValidValues -inotcontains $ProvidedValue) {
throw "Invalid value '$ProvidedValue' for '$ChoiceName'. Must be one of: $($ValidValues -join ', ')"
@ -263,7 +267,7 @@ function GetNetboxAPIErrorBody {
}
function InvokeNetboxRequest {
[CmdletBinding(SupportsShouldProcess = $true)]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]

View file

@ -13,6 +13,7 @@
function Get-NetboxIPAMChoices {
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('ipam', '_choices'))
@ -115,12 +116,13 @@ function VerifyIPAMChoices {
[switch]$ServiceProtocol
)
ValidateChoice -MajorObject 'IPAM' -ChoiceName $PSCmdlet.ParameterSetName
ValidateChoice -MajorObject 'IPAM' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
}
function Get-NetboxIPAMAggregate {
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param
(
[uint16]$Limit,
@ -142,7 +144,7 @@ function Get-NetboxIPAMAggregate {
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -AggregateFamily
}
@ -198,15 +200,15 @@ function Get-NetboxIPAMAddress {
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -IPAddressFamily
}
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
}
if ($Role -ne $null) {
if ($null -ne $Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole
}
@ -395,11 +397,11 @@ function Get-NetboxIPAMPrefix {
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -PrefixFamily
}
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -PrefixStatus
}
@ -485,7 +487,7 @@ function Add-NetboxIPAMAddress {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
if ($Role -ne $null) {
if ($null -ne $Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole
}
@ -558,7 +560,7 @@ function Set-NetboxIPAMAddress {
[string]$Address,
[object]$Status = 'Active',
[object]$Status,
[uint16]$Tenant,
@ -577,7 +579,9 @@ function Set-NetboxIPAMAddress {
[switch]$Force
)
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
if ($Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
}
if ($Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole

View file

@ -36,15 +36,17 @@ function GetNetboxConfigVariable {
}
function Set-NetboxHostName {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname
)
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
[CmdletBinding(ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname
)
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
}
function Get-NetboxHostname {
@ -52,7 +54,7 @@ function Get-NetboxHostname {
param ()
Write-Verbose "Getting Netbox hostname"
if ($script:NetboxConfig.Hostname -eq $null) {
if ($null -eq $script:NetboxConfig.Hostname) {
throw "Netbox Hostname is not set! You may set it with Set-NetboxHostname -Hostname 'hostname.domain.tld'"
}
@ -60,34 +62,37 @@ function Get-NetboxHostname {
}
function Set-NetboxCredentials {
[CmdletBinding(DefaultParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[string]$Token
)
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credentials = $Credentials
break
}
'UserPass' {
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
}
$script:NetboxConfig.Credentials
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
[OutputType([pscredential])]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[string]$Token
)
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credentials = $Credentials
break
}
'UserPass' {
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
}
$script:NetboxConfig.Credentials
}
function Get-NetboxCredentials {
@ -162,7 +167,7 @@ function Connect-NetboxAPI {
try {
Write-Verbose "Verifying API connectivity..."
$APIInfo = VerifyAPIConnectivity
$null = VerifyAPIConnectivity
$script:NetboxConfig.Connected = $true
Write-Verbose "Successfully connected!"
} catch {

View file

@ -72,13 +72,14 @@ function VerifyVirtualizationChoices {
[switch]$VirtualMachineStatus
)
ValidateChoice -MajorObject 'Virtualization' -ChoiceName $PSCmdlet.ParameterSetName
ValidateChoice -MajorObject 'Virtualization' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
}
#region GET commands
function Get-NetboxVirtualizationChoices {
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('virtualization', '_choices'))
@ -217,7 +218,7 @@ function Get-NetboxVirtualMachine {
[switch]$Raw
)
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
@ -533,8 +534,69 @@ function Add-NetboxVirtualInterface {
InvokeNetboxRequest -URI $uri -Method POST -Body $URIComponents.Parameters
}
#endregion ADD commands
#region SET commands
function Set-NetboxVirtualMachine {
[CmdletBinding(ConfirmImpact = 'High',
SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16]$Id,
[string]$Name,
[uint16]$Role,
[uint16]$Cluster,
[object]$Status,
[uint16]$Platform,
[uint16]$Primary_IPv4,
[uint16]$Primary_IPv6,
[byte]$VCPUs,
[uint16]$Memory,
[uint16]$Disk,
[uint16]$Tenant,
[string]$Comments,
[hashtable]$Custom_Fields,
[switch]$Force
)
if ($Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $Id))
Write-Verbose "Obtaining VM from ID $Id"
$CurrentVM = Get-NetboxVirtualMachine -Id $Id -ErrorAction Stop
Write-Verbose "Finished obtaining VM"
if ($Force -or $pscmdlet.ShouldProcess($CurrentVM.Name, "Set")) {
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force'
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
#endregion SET commands

View file

@ -26,7 +26,8 @@
<File Build="2" Shared="True" ReferenceFunction="Invoke-Virtualization_Tests_ps1">Tests\Virtualization.Tests.ps1</File>
<File Build="2" Shared="True" ReferenceFunction="Invoke-IPAM_Tests_ps1">Tests\IPAM.Tests.ps1</File>
<File Build="0" Shared="True" ReferenceFunction="Invoke-IPAM_ps1">Functions\IPAM\IPAM.ps1</File>
<File Build="2">Tests\IPAMChoices.json.txt</File>
<File Build="2">Tests\IPAMChoices.json</File>
<File Build="2">Tests\VirtualizationChoices.json</File>
</Files>
<StartupScript>R:\Netbox\NetboxPS\Test-Module.ps1</StartupScript>
</Project>

File diff suppressed because it is too large Load diff

View file

@ -31,8 +31,8 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
return $true
} -ModuleName 'NetboxPS'
Context "Building URI tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URIBuilder" -Fixture {
It "Should give a basic URI object" {
BuildNewURI -HostName 'netbox.domain.com' | Should -BeOfType [System.UriBuilder]
}
@ -85,10 +85,70 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'https://netbox.domain.com/api/seg1/seg2/?param1=paramval1&param2=paramval2'
}
}
}
Context "Invoking request tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URI components" -Fixture {
It "Should give a basic hashtable" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
}
It "Should add a single ID parameter to the segments" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = 123}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2", '123')
$URIComponents.Parameters.Count | Should -BeExactly 0
$URIComponents.Parameters | Should -BeOfType [hashtable]
}
It "Should add multiple IDs to the parameters id__in" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = "123", "456"}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['id__in'] | Should -Be '123,456'
}
It "Should skip a particular parameter name" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1; 'param2' = 2} -SkipParameterByName 'param2'
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
$URIComponents.Parameters['param2'] | Should -BeNullOrEmpty
}
It "Should add a query (q) parameter" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'query' = 'mytestquery'}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['q'] | Should -Be 'mytestquery'
}
}
Context -Name "Invoking request tests" -Fixture {
Mock -CommandName 'Invoke-RestMethod' -Verifiable -MockWith {
# Return an object of the items we would normally pass to Invoke-RestMethod
return [pscustomobject]@{
@ -123,7 +183,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result = InvokeNetboxRequest -URI $URIBuilder -Raw
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be $URIBuilder.Uri.AbsoluteUri
$Result.Headers | Should -BeOfType [System.Collections.HashTable]
@ -136,7 +196,9 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
It "Should generate a POST request with body" {
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{'bodyparam1' = 'val1'} -Raw
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{
'bodyparam1' = 'val1'
} -Raw
Assert-VerifiableMock
@ -161,6 +223,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Body | Should -Be '{"bodyparam1":"val1"}'
$Result.Headers.Count | Should -BeExactly 2
$Result.Headers.Authorization | Should -Be "Token faketoken"
$Result.Headers.Connection | Should -Be "keep-alive"
}
@ -179,6 +242,235 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
} | Should -Throw
}
}
Context -Name "Validating choices" -Fixture {
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Virtualization choices" -Fixture {
$MajorObject = 'Virtualization'
It "Should return a valid integer for status when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer for status when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 0
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 0
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Fake'
} | Should -Throw
}
}
Context -Name "IPAM choices" -Fixture {
$MajorObject = 'IPAM'
Context -Name "aggregate:family" -Fixture {
$ChoiceName = 'aggregate:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:family" {
$ChoiceName = 'prefix:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:status" {
$ChoiceName = 'prefix:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:family" {
$ChoiceName = 'ip-address:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:status" {
$ChoiceName = 'ip-address:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:role" {
$ChoiceName = 'ip-address:role'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Anycast'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 30
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
} | Should -Throw
}
}
Context -Name "vlan:status" {
$ChoiceName = 'vlan:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "service:protocol" {
$ChoiceName = 'service:protocol'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'TCP'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 6
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
}
}
}
}

View file

@ -47,6 +47,12 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "VerifyIPAMChoices" -Fixture {
#It "Should return a valid integer"
}
Context -Name "Get-NetboxIPAMAggregate" -Fixture {
It "Should request the default number of aggregates" {
$Result = Get-NetboxIPAMAggregate
@ -182,31 +188,25 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Headers.Authorization | Should -Be "Token faketoken"
}
#region TODO: Figure out how to mock/test Verification appropriately...
<#
It "Should request with a family number" {
Mock -CommandName 'Get-NetboxIPAMChoices' -ModuleName 'NetboxPS' -MockWith {
return @"
{"aggregate:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:status":[{"label":"Container","value":0},{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"ip-address:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"ip-address:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3},{"label":"DHCP","value":5}],"ip-address:role":[{"label":"Loopback","value":10},{"label":"Secondary","value":20},{"label":"Anycast","value":30},{"label":"VIP","value":40},{"label":"VRRP","value":41},{"label":"HSRP","value":42},{"label":"GLBP","value":43},{"label":"CARP","value":44}],"vlan:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"service:protocol":[{"label":"TCP","value":6},{"label":"UDP","value":17}]}
"@ | ConvertFrom-Json
}
Mock -CommandName 'Connect-NetboxAPI' -ModuleName 'NetboxPS' -MockWith {
$script:NetboxConfig.Connected = $true
$script:NetboxConfig.Choices.IPAM = Get-NetboxIPAMChoices
}
Connect-NetboxAPI
$Result = Get-NetboxIPAMAddress -Role 4
$Result = Get-NetboxIPAMAddress -Family 4
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMChoices"
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?role=4'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?family=4'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a family name" {
$Result = Get-NetboxIPAMAddress -Family 'IPv4'
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/?family=4'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
#>
#endregion
}
Context -Name "Get-NetboxIPAMAvailableIP" -Fixture {
@ -233,7 +233,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
}
}
Context -Name "Get-NetboxIPAMPrefix" {
Context -Name "Get-NetboxIPAMPrefix" -Fixture {
It "Should request the default number of prefixes" {
$Result = Get-NetboxIPAMPrefix
@ -311,11 +311,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$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
@ -325,7 +321,24 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$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
@ -337,11 +350,118 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Headers.Authorization | Should -Be "Token faketoken"
}
}
Context -Name "Add-NetboxIPAMAddress" -Fixture {
It "Should add a basic IP address" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24'
Assert-VerifiableMock
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":1,"address":"10.0.0.1/24"}'
}
It "Should throw because the mask length is too large" {
{
Get-NetboxIPAMPrefix -Mask_length 128
} | Should -Throw
It "Should add an IP with a status and role names" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast'
Assert-VerifiableMock
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":2,"address":"10.0.0.1/24","role":30}'
}
It "Should add an IP with a status and role values" {
$Result = Add-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10'
Assert-VerifiableMock
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":1,"address":"10.0.1.1/24","role":10}'
}
}
Context -Name "Remove-NetboxIPAMAddress" -Fixture {
It "Should remove a single IP" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = '10.1.1.1/24'
'id' = 4109
}
}
$Result = Remove-NetboxIPAMAddress -Id '4109' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
$Result.Method | Should -Be 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be $null
}
It "Should remove multiple IPs" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @(
@{
'address' = '10.1.1.1/24'
'id' = 4109
},
@{
'address' = '10.1.1.2/24'
'id' = 4110
}
)
}
$Result = Remove-NetboxIPAMAddress -Id 4109, 4110 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4110/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
Context -Name "Set-NetboxIPAMAddress" -Fixture {
It "Should set an IP with a new status" {
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
return @{
'address' = '10.1.1.1/24'
'id' = 4109
}
}
$Result = Set-NetboxIPAMAddress -Id '4109' -Status 2 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":2}'
}
It "Should set an IP with VRF, Tenant, and Description" {
$Result = Set-NetboxIPAMAddress -Id 4109 -VRF 10 -Tenant 14 -Description 'Test description' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
}
}
}

View file

@ -46,6 +46,8 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Get-NetboxVirtualMachine" -Fixture {
It "Should request the default number of VMs" {
$Result = Get-NetboxVirtualMachine
@ -128,7 +130,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
}
Context -Name "Get-VirtualMachineInterface" -Fixture {
Context -Name "Get-NetboxVirtualMachineInterface" -Fixture {
It "Should request the default number of interfaces" {
$Result = Get-NetboxVirtualMachineInterface
@ -190,7 +192,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
}
Context -Name "Get-VirtualMachineCluster" -Fixture {
Context -Name "Get-NetboxVirtualMachineCluster" -Fixture {
It "Should request the default number of clusters" {
$Result = Get-NetboxVirtualizationCluster
@ -262,7 +264,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
}
Context -Name "Get-VirtualMachineClusterGroup" -Fixture {
Context -Name "Get-NetboxVirtualMachineClusterGroup" -Fixture {
It "Should request the default number of cluster groups" {
$Result = Get-NetboxVirtualizationClusterGroup
@ -351,6 +353,43 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Body | Should -Be '{"mtu":1500,"description":"Test description","enabled":true,"virtual_machine":10,"name":"Ethernet0","mac_address":"11:22:33:44:55:66"}'
}
}
Context -Name "Set-NetboxVirtualMachine" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
}
It "Should set a VM to a new name" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set a VM with a new name, cluster, platform, and status" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"cluster":10,"platform":15,"name":"newtestname","status":0}'
}
It "Should throw because of an invalid status" {
{ Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw
Assert-VerifiableMock
}
}
}
}

View file

@ -31,8 +31,8 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
return $true
} -ModuleName 'NetboxPS'
Context "Building URI tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URIBuilder" -Fixture {
It "Should give a basic URI object" {
BuildNewURI -HostName 'netbox.domain.com' | Should -BeOfType [System.UriBuilder]
}
@ -85,10 +85,70 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'https://netbox.domain.com/api/seg1/seg2/?param1=paramval1&param2=paramval2'
}
}
}
Context "Invoking request tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URI components" -Fixture {
It "Should give a basic hashtable" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
}
It "Should add a single ID parameter to the segments" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = 123}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2", '123')
$URIComponents.Parameters.Count | Should -BeExactly 0
$URIComponents.Parameters | Should -BeOfType [hashtable]
}
It "Should add multiple IDs to the parameters id__in" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = "123", "456"}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['id__in'] | Should -Be '123,456'
}
It "Should skip a particular parameter name" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1; 'param2' = 2} -SkipParameterByName 'param2'
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
$URIComponents.Parameters['param2'] | Should -BeNullOrEmpty
}
It "Should add a query (q) parameter" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'query' = 'mytestquery'}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['q'] | Should -Be 'mytestquery'
}
}
Context -Name "Invoking request tests" -Fixture {
Mock -CommandName 'Invoke-RestMethod' -Verifiable -MockWith {
# Return an object of the items we would normally pass to Invoke-RestMethod
return [pscustomobject]@{
@ -123,7 +183,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result = InvokeNetboxRequest -URI $URIBuilder -Raw
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be $URIBuilder.Uri.AbsoluteUri
$Result.Headers | Should -BeOfType [System.Collections.HashTable]
@ -136,7 +196,9 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
It "Should generate a POST request with body" {
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{'bodyparam1' = 'val1'} -Raw
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{
'bodyparam1' = 'val1'
} -Raw
Assert-VerifiableMock
@ -161,6 +223,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Body | Should -Be '{"bodyparam1":"val1"}'
$Result.Headers.Count | Should -BeExactly 2
$Result.Headers.Authorization | Should -Be "Token faketoken"
$Result.Headers.Connection | Should -Be "keep-alive"
}
@ -179,6 +242,235 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
} | Should -Throw
}
}
Context -Name "Validating choices" -Fixture {
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Virtualization choices" -Fixture {
$MajorObject = 'Virtualization'
It "Should return a valid integer for status when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer for status when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 0
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 0
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Fake'
} | Should -Throw
}
}
Context -Name "IPAM choices" -Fixture {
$MajorObject = 'IPAM'
Context -Name "aggregate:family" -Fixture {
$ChoiceName = 'aggregate:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:family" {
$ChoiceName = 'prefix:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:status" {
$ChoiceName = 'prefix:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:family" {
$ChoiceName = 'ip-address:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:status" {
$ChoiceName = 'ip-address:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:role" {
$ChoiceName = 'ip-address:role'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Anycast'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 30
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
} | Should -Throw
}
}
Context -Name "vlan:status" {
$ChoiceName = 'vlan:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "service:protocol" {
$ChoiceName = 'service:protocol'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'TCP'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 6
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
}
}
}
}

View file

@ -49,6 +49,10 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "VerifyIPAMChoices" -Fixture {
#It "Should return a valid integer"
}
Context -Name "Get-NetboxIPAMAggregate" -Fixture {
It "Should request the default number of aggregates" {
$Result = Get-NetboxIPAMAggregate
@ -368,7 +372,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":2,"role":30,"address":"10.0.0.1/24"}'
$Result.Body | Should -Be '{"status":2,"address":"10.0.0.1/24","role":30}'
}
It "Should add an IP with a status and role values" {
@ -379,7 +383,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":1,"role":10,"address":"10.0.1.1/24"}'
$Result.Body | Should -Be '{"status":1,"address":"10.0.1.1/24","role":10}'
}
}
@ -457,7 +461,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"description":"Test description","status":1,"tenant":14,"vrf":10}'
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
}
}
}

View file

@ -307,7 +307,6 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
Context -Name "Add-NetboxVirtualMachine" -Fixture {
It "Should add a basic VM" {
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1
@ -316,7 +315,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"testvm","cluster":1,"status":1}'
$Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}'
}
It "Should add a VM with CPUs, Memory, Disk, tenancy, and comments" {
@ -327,7 +326,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"tenant":11,"name":"testvm","comments":"these are comments","cluster":1,"status":1,"memory":4096,"vcpus":4,"disk":50}'
$Result.Body | Should -Be '{"tenant":11,"comments":"these are comments","disk":50,"memory":4096,"name":"testvm","cluster":1,"status":1,"vcpus":4}'
}
}
@ -340,7 +339,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"virtual_machine":10,"enabled":true,"name":"Ethernet0"}'
$Result.Body | Should -Be '{"virtual_machine":10,"name":"Ethernet0","enabled":true}'
}
It "Should add an interface with a MAC, MTU, and Description" {
@ -351,7 +350,44 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"description":"Test description","virtual_machine":10,"enabled":true,"name":"Ethernet0","mtu":1500,"mac_address":"11:22:33:44:55:66"}'
$Result.Body | Should -Be '{"mtu":1500,"description":"Test description","enabled":true,"virtual_machine":10,"name":"Ethernet0","mac_address":"11:22:33:44:55:66"}'
}
}
Context -Name "Set-NetboxVirtualMachine" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
}
It "Should set a VM to a new name" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set a VM with a new name, cluster, platform, and status" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"cluster":10,"platform":15,"name":"newtestname","status":0}'
}
It "Should throw because of an invalid status" {
{ Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw
Assert-VerifiableMock
}
}
}

201
dist/NetboxPS.psm1 vendored
View file

@ -2,7 +2,7 @@
.NOTES
--------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Generated on: 5/15/2018 4:52 PM
Generated on: 5/18/2018 11:21 AM
Generated by: Ben Claussen
Organization: NEOnet
--------------------------------------------------------------------------------
@ -159,7 +159,7 @@
Write-Verbose "Building URI components"
$URIParameters = [System.Collections.Hashtable]::new()
$URIParameters = @{}
foreach ($CmdletParameterName in $ParametersDictionary.Keys) {
if ($CmdletParameterName -in $CommonParameterNames) {
@ -177,7 +177,7 @@
# Check if there is one or more values for Id and build a URI or query as appropriate
if (@($ParametersDictionary[$CmdletParameterName]).Count -gt 1) {
Write-Verbose " Joining IDs for parameter"
$URIParameters['id__in'] = $Id -join ','
$URIParameters['id__in'] = $ParametersDictionary[$CmdletParameterName] -join ','
} else {
Write-Verbose " Adding ID to segments"
[void]$uriSegments.Add($ParametersDictionary[$CmdletParameterName])
@ -199,6 +199,7 @@
function GetChoiceValidValues {
[CmdletBinding()]
[OutputType([System.Collections.ArrayList])]
param
(
[Parameter(Mandatory = $true)]
@ -234,13 +235,16 @@
[string]$MajorObject,
[Parameter(Mandatory = $true)]
[string]$ChoiceName
[string]$ChoiceName,
[Parameter(Mandatory = $true)]
[object]$ProvidedValue
)
$ValidValues = GetChoiceValidValues -MajorObject $MajorObject -Choice $ChoiceName
Write-Verbose "Validating $ChoiceName"
Write-Verbose "Checking '$ProvidedValue' against $($ValidValues -join ', ')"
Write-Verbose "Checking '$ProvidedValue' against [$($ValidValues -join ', ')]"
if ($ValidValues -inotcontains $ProvidedValue) {
throw "Invalid value '$ProvidedValue' for '$ChoiceName'. Must be one of: $($ValidValues -join ', ')"
@ -277,7 +281,7 @@
}
function InvokeNetboxRequest {
[CmdletBinding(SupportsShouldProcess = $true)]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
@ -456,15 +460,17 @@ public enum $EnumName
}
function Set-NetboxHostName {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname
)
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
[CmdletBinding(ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname
)
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
}
function Get-NetboxHostname {
@ -472,7 +478,7 @@ public enum $EnumName
param ()
Write-Verbose "Getting Netbox hostname"
if ($script:NetboxConfig.Hostname -eq $null) {
if ($null -eq $script:NetboxConfig.Hostname) {
throw "Netbox Hostname is not set! You may set it with Set-NetboxHostname -Hostname 'hostname.domain.tld'"
}
@ -480,34 +486,37 @@ public enum $EnumName
}
function Set-NetboxCredentials {
[CmdletBinding(DefaultParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[string]$Token
)
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credentials = $Credentials
break
}
'UserPass' {
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
}
$script:NetboxConfig.Credentials
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
[OutputType([pscredential])]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[string]$Token
)
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credentials = $Credentials
break
}
'UserPass' {
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
}
$script:NetboxConfig.Credentials
}
function Get-NetboxCredentials {
@ -582,7 +591,7 @@ public enum $EnumName
try {
Write-Verbose "Verifying API connectivity..."
$APIInfo = VerifyAPIConnectivity
$null = VerifyAPIConnectivity
$script:NetboxConfig.Connected = $true
Write-Verbose "Successfully connected!"
} catch {
@ -632,7 +641,8 @@ public enum $EnumName
#>
function Get-NetboxExtrasChoices {
[CmdletBinding()]
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('extras', '_choices'))
@ -671,8 +681,9 @@ public enum $EnumName
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('circuits', '_choices'))
@ -827,13 +838,14 @@ public enum $EnumName
[switch]$VirtualMachineStatus
)
ValidateChoice -MajorObject 'Virtualization' -ChoiceName $PSCmdlet.ParameterSetName
ValidateChoice -MajorObject 'Virtualization' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
}
#region GET commands
function Get-NetboxVirtualizationChoices {
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param ()
$uriSegments = [System.Collections.ArrayList]::new(@('virtualization', '_choices'))
@ -972,7 +984,7 @@ public enum $EnumName
[switch]$Raw
)
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
@ -1288,12 +1300,72 @@ public enum $EnumName
InvokeNetboxRequest -URI $uri -Method POST -Body $URIComponents.Parameters
}
#endregion ADD commands
#endregion
#region SET commands
function Set-NetboxVirtualMachine {
[CmdletBinding(ConfirmImpact = 'High',
SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16]$Id,
[string]$Name,
[uint16]$Role,
[uint16]$Cluster,
[object]$Status,
[uint16]$Platform,
[uint16]$Primary_IPv4,
[uint16]$Primary_IPv6,
[byte]$VCPUs,
[uint16]$Memory,
[uint16]$Disk,
[uint16]$Tenant,
[string]$Comments,
[hashtable]$Custom_Fields,
[switch]$Force
)
if ($Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $Id))
Write-Verbose "Obtaining VM from ID $Id"
$CurrentVM = Get-NetboxVirtualMachine -Id $Id -ErrorAction Stop
Write-Verbose "Finished obtaining VM"
if ($Force -or $pscmdlet.ShouldProcess($CurrentVM.Name, "Set")) {
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force'
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
#endregion SET commands#endregion
#region Invoke-IPAM_ps1
<#
@ -1413,12 +1485,13 @@ public enum $EnumName
[switch]$ServiceProtocol
)
ValidateChoice -MajorObject 'IPAM' -ChoiceName $PSCmdlet.ParameterSetName
ValidateChoice -MajorObject 'IPAM' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
}
function Get-NetboxIPAMAggregate {
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
param
(
[uint16]$Limit,
@ -1440,7 +1513,7 @@ public enum $EnumName
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -AggregateFamily
}
@ -1496,15 +1569,15 @@ public enum $EnumName
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -IPAddressFamily
}
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
}
if ($Role -ne $null) {
if ($null -ne $Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole
}
@ -1693,11 +1766,11 @@ public enum $EnumName
[switch]$Raw
)
if ($Family -ne $null) {
if ($null -ne $Family) {
$PSBoundParameters.Family = VerifyIPAMChoices -ProvidedValue $Family -PrefixFamily
}
if ($Status -ne $null) {
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -PrefixStatus
}
@ -1783,7 +1856,7 @@ public enum $EnumName
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
if ($Role -ne $null) {
if ($null -ne $Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole
}
@ -1856,7 +1929,7 @@ public enum $EnumName
[string]$Address,
[object]$Status = 'Active',
[object]$Status,
[uint16]$Tenant,
@ -1875,7 +1948,9 @@ public enum $EnumName
[switch]$Force
)
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
if ($Status) {
$PSBoundParameters.Status = VerifyIPAMChoices -ProvidedValue $Status -IPAddressStatus
}
if ($Role) {
$PSBoundParameters.Role = VerifyIPAMChoices -ProvidedValue $Role -IPAddressRole

View file

@ -31,8 +31,8 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
return $true
} -ModuleName 'NetboxPS'
Context "Building URI tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URIBuilder" -Fixture {
It "Should give a basic URI object" {
BuildNewURI -HostName 'netbox.domain.com' | Should -BeOfType [System.UriBuilder]
}
@ -85,10 +85,70 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$URIBuilder.URI.AbsoluteURI | Should -BeExactly 'https://netbox.domain.com/api/seg1/seg2/?param1=paramval1&param2=paramval2'
}
}
}
Context "Invoking request tests" {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
Context -Name "Building URI components" -Fixture {
It "Should give a basic hashtable" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
}
It "Should add a single ID parameter to the segments" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = 123}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2", '123')
$URIComponents.Parameters.Count | Should -BeExactly 0
$URIComponents.Parameters | Should -BeOfType [hashtable]
}
It "Should add multiple IDs to the parameters id__in" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'id' = "123", "456"}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['id__in'] | Should -Be '123,456'
}
It "Should skip a particular parameter name" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'param1' = 1; 'param2' = 2} -SkipParameterByName 'param2'
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['param1'] | Should -Be 1
$URIComponents.Parameters['param2'] | Should -BeNullOrEmpty
}
It "Should add a query (q) parameter" {
$URIComponents = BuildURIComponents -URISegments @('segment1', 'segment2') -ParametersDictionary @{'query' = 'mytestquery'}
$URIComponents | Should -BeOfType [hashtable]
$URIComponents.Keys.Count | Should -BeExactly 2
$URIComponents.Keys | Should -Be @("Segments", "Parameters")
$URIComponents.Segments | Should -Be @("segment1", "segment2")
$URIComponents.Parameters.Count | Should -BeExactly 1
$URIComponents.Parameters | Should -BeOfType [hashtable]
$URIComponents.Parameters['q'] | Should -Be 'mytestquery'
}
}
Context -Name "Invoking request tests" -Fixture {
Mock -CommandName 'Invoke-RestMethod' -Verifiable -MockWith {
# Return an object of the items we would normally pass to Invoke-RestMethod
return [pscustomobject]@{
@ -123,7 +183,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result = InvokeNetboxRequest -URI $URIBuilder -Raw
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be $URIBuilder.Uri.AbsoluteUri
$Result.Headers | Should -BeOfType [System.Collections.HashTable]
@ -136,7 +196,9 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
It "Should generate a POST request with body" {
$URIBuilder = BuildNewURI -Hostname "netbox.domain.com" -Segments 'seg1', 'seg2'
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{'bodyparam1' = 'val1'} -Raw
$Result = InvokeNetboxRequest -URI $URIBuilder -Method POST -Body @{
'bodyparam1' = 'val1'
} -Raw
Assert-VerifiableMock
@ -161,6 +223,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Body | Should -Be '{"bodyparam1":"val1"}'
$Result.Headers.Count | Should -BeExactly 2
$Result.Headers.Authorization | Should -Be "Token faketoken"
$Result.Headers.Connection | Should -Be "keep-alive"
}
@ -179,6 +242,235 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
} | Should -Throw
}
}
Context -Name "Validating choices" -Fixture {
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "Virtualization choices" -Fixture {
$MajorObject = 'Virtualization'
It "Should return a valid integer for status when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer for status when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 0
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 0
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName 'virtual-machine:status' -ProvidedValue 'Fake'
} | Should -Throw
}
}
Context -Name "IPAM choices" -Fixture {
$MajorObject = 'IPAM'
Context -Name "aggregate:family" -Fixture {
$ChoiceName = 'aggregate:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:family" {
$ChoiceName = 'prefix:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "prefix:status" {
$ChoiceName = 'prefix:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:family" {
$ChoiceName = 'ip-address:family'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'IPv4'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 4
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 4
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:status" {
$ChoiceName = 'ip-address:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 10
} | Should -Throw
}
}
Context -Name "ip-address:role" {
$ChoiceName = 'ip-address:role'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Anycast'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 30
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 30
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
} | Should -Throw
}
}
Context -Name "vlan:status" {
$ChoiceName = 'vlan:status'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 1
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
Context -Name "service:protocol" {
$ChoiceName = 'service:protocol'
It "Should return a valid integer when provided a name" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'TCP'
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should return a valid integer when provided an integer" {
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 6
$Result | Should -BeOfType [uint16]
$Result | Should -BeExactly 6
}
It "Should throw because of an invalid choice" {
{
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
} | Should -Throw
}
}
}
}
}
}

View file

@ -49,6 +49,10 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
Context -Name "VerifyIPAMChoices" -Fixture {
#It "Should return a valid integer"
}
Context -Name "Get-NetboxIPAMAggregate" -Fixture {
It "Should request the default number of aggregates" {
$Result = Get-NetboxIPAMAggregate
@ -368,7 +372,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":2,"role":30,"address":"10.0.0.1/24"}'
$Result.Body | Should -Be '{"status":2,"address":"10.0.0.1/24","role":30}'
}
It "Should add an IP with a status and role values" {
@ -379,7 +383,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"status":1,"role":10,"address":"10.0.1.1/24"}'
$Result.Body | Should -Be '{"status":1,"address":"10.0.1.1/24","role":10}'
}
}
@ -457,7 +461,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"description":"Test description","status":1,"tenant":14,"vrf":10}'
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
}
}
}

1
dist/Tests/IPAMChoices.json vendored Normal file
View file

@ -0,0 +1 @@
{"aggregate:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:status":[{"label":"Container","value":0},{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"ip-address:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"ip-address:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3},{"label":"DHCP","value":5}],"ip-address:role":[{"label":"Loopback","value":10},{"label":"Secondary","value":20},{"label":"Anycast","value":30},{"label":"VIP","value":40},{"label":"VRRP","value":41},{"label":"HSRP","value":42},{"label":"GLBP","value":43},{"label":"CARP","value":44}],"vlan:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"service:protocol":[{"label":"TCP","value":6},{"label":"UDP","value":17}]}

View file

@ -307,7 +307,6 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
}
Context -Name "Add-NetboxVirtualMachine" -Fixture {
It "Should add a basic VM" {
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1
@ -316,7 +315,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"testvm","cluster":1,"status":1}'
$Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}'
}
It "Should add a VM with CPUs, Memory, Disk, tenancy, and comments" {
@ -327,7 +326,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"tenant":11,"name":"testvm","comments":"these are comments","cluster":1,"status":1,"memory":4096,"vcpus":4,"disk":50}'
$Result.Body | Should -Be '{"tenant":11,"comments":"these are comments","disk":50,"memory":4096,"name":"testvm","cluster":1,"status":1,"vcpus":4}'
}
}
@ -340,7 +339,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"virtual_machine":10,"enabled":true,"name":"Ethernet0"}'
$Result.Body | Should -Be '{"virtual_machine":10,"name":"Ethernet0","enabled":true}'
}
It "Should add an interface with a MAC, MTU, and Description" {
@ -351,7 +350,44 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"description":"Test description","virtual_machine":10,"enabled":true,"name":"Ethernet0","mtu":1500,"mac_address":"11:22:33:44:55:66"}'
$Result.Body | Should -Be '{"mtu":1500,"description":"Test description","enabled":true,"virtual_machine":10,"name":"Ethernet0","mac_address":"11:22:33:44:55:66"}'
}
}
Context -Name "Set-NetboxVirtualMachine" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
}
It "Should set a VM to a new name" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set a VM with a new name, cluster, platform, and status" {
$Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force
Assert-VerifiableMock
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"cluster":10,"platform":15,"name":"newtestname","status":0}'
}
It "Should throw because of an invalid status" {
{ Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw
Assert-VerifiableMock
}
}
}

1
dist/Tests/VirtualizationChoices.json vendored Normal file
View file

@ -0,0 +1 @@
{"virtual-machine:status":[{"label":"Active","value":1},{"label":"Offline","value":0},{"label":"Staged","value":3}]}