This commit is contained in:
Ben Claussen 2018-05-21 15:36:04 -04:00
parent 107040f9cd
commit 644ddb95b6
7 changed files with 186 additions and 65 deletions

View file

@ -19,7 +19,7 @@
ModuleToProcess = 'NetboxPS.psm1' ModuleToProcess = 'NetboxPS.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '1.0.0.0' ModuleVersion = '1.0.1'
# ID used to uniquely identify this module # ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896' GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

View file

@ -6,5 +6,5 @@ $script:CommonParameterNames = New-Object System.Collections.ArrayList
SetupNetboxConfigVariable SetupNetboxConfigVariable
Export-ModuleMember -Function * #Export-ModuleMember -Function *
#Export-ModuleMember -Function *-* Export-ModuleMember -Function *-*

2
dist/NetboxPS.psd1 vendored
View file

@ -19,7 +19,7 @@
ModuleToProcess = 'NetboxPS.psm1' ModuleToProcess = 'NetboxPS.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '1.0.0.0' ModuleVersion = '1.0.1'
# ID used to uniquely identify this module # ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896' GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

146
dist/NetboxPS.psm1 vendored
View file

@ -2,7 +2,7 @@
.NOTES .NOTES
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152 Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Generated on: 5/18/2018 12:40 PM Generated on: 5/21/2018 3:34 PM
Generated by: Ben Claussen Generated by: Ben Claussen
Organization: NEOnet Organization: NEOnet
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -300,7 +300,7 @@
[switch]$Raw [switch]$Raw
) )
$creds = Get-NetboxCredentials $creds = Get-NetboxCredential
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password $Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
@ -460,7 +460,7 @@ public enum $EnumName
} }
function Set-NetboxHostName { function Set-NetboxHostName {
[CmdletBinding(ConfirmImpact = 'Medium', [CmdletBinding(ConfirmImpact = 'Low',
SupportsShouldProcess = $true)] SupportsShouldProcess = $true)]
[OutputType([string])] [OutputType([string])]
param param
@ -469,8 +469,10 @@ public enum $EnumName
[string]$Hostname [string]$Hostname
) )
$script:NetboxConfig.Hostname = $Hostname.Trim() if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) {
$script:NetboxConfig.Hostname $script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
}
} }
function Get-NetboxHostname { function Get-NetboxHostname {
@ -485,50 +487,49 @@ public enum $EnumName
$script:NetboxConfig.Hostname $script:NetboxConfig.Hostname
} }
function Set-NetboxCredentials { function Set-NetboxCredential {
[CmdletBinding(DefaultParameterSetName = 'CredsObject', [CmdletBinding(DefaultParameterSetName = 'CredsObject',
ConfirmImpact = 'Medium', ConfirmImpact = 'Low',
SupportsShouldProcess = $true)] SupportsShouldProcess = $true)]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
[OutputType([pscredential])] [OutputType([pscredential])]
param param
( (
[Parameter(ParameterSetName = 'CredsObject', [Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)] Mandatory = $true)]
[pscredential]$Credentials, [pscredential]$Credential,
[Parameter(ParameterSetName = 'UserPass', [Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)] Mandatory = $true)]
[string]$Token [securestring]$Token
) )
switch ($PsCmdlet.ParameterSetName) { if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) {
'CredsObject' { switch ($PsCmdlet.ParameterSetName) {
$script:NetboxConfig.Credentials = $Credentials 'CredsObject' {
break $script:NetboxConfig.Credential = $Credential
break
}
'UserPass' {
$script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token)
break
}
} }
'UserPass' { $script:NetboxConfig.Credential
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
} }
$script:NetboxConfig.Credentials
} }
function Get-NetboxCredentials { function Get-NetboxCredential {
[CmdletBinding()] [CmdletBinding()]
[OutputType([pscredential])] [OutputType([pscredential])]
param () param ()
if (-not $script:NetboxConfig.Credentials) { if (-not $script:NetboxConfig.Credential) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredentials" throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
} }
$script:NetboxConfig.Credentials $script:NetboxConfig.Credential
} }
function VerifyAPIConnectivity { function VerifyAPIConnectivity {
@ -545,7 +546,7 @@ public enum $EnumName
function Connect-NetboxAPI { function Connect-NetboxAPI {
<# <#
.SYNOPSIS .SYNOPSIS
Connects to the Netbox API and ensures credentials work properly Connects to the Netbox API and ensures Credential work properly
.DESCRIPTION .DESCRIPTION
A detailed description of the Connect-NetboxAPI function. A detailed description of the Connect-NetboxAPI function.
@ -553,13 +554,13 @@ public enum $EnumName
.PARAMETER Hostname .PARAMETER Hostname
A description of the Hostname parameter. A description of the Hostname parameter.
.PARAMETER Credentials .PARAMETER Credential
A description of the Credentials parameter. A description of the Credential parameter.
.EXAMPLE .EXAMPLE
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com" PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
This will prompt for credentials, then proceed to attempt a connection to Netbox This will prompt for Credential, then proceed to attempt a connection to Netbox
.NOTES .NOTES
Additional information about the function. Additional information about the function.
@ -572,22 +573,22 @@ public enum $EnumName
[string]$Hostname, [string]$Hostname,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[pscredential]$Credentials [pscredential]$Credential
) )
if (-not $Credentials) { if (-not $Credential) {
try { try {
$Credentials = Get-NetboxCredentials -ErrorAction Stop $Credential = Get-NetboxCredential -ErrorAction Stop
} catch { } catch {
# Credentials are not set... Try to obtain from the user # Credentials are not set... Try to obtain from the user
if (-not ($Credentials = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) { if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
throw "Token is necessary to connect to a Netbox API." throw "Token is necessary to connect to a Netbox API."
} }
} }
} }
$null = Set-NetboxHostName -Hostname $Hostname $null = Set-NetboxHostName -Hostname $Hostname
$null = Set-NetboxCredentials -Credentials $Credentials $null = Set-NetboxCredential -Credential $Credential
try { try {
Write-Verbose "Verifying API connectivity..." Write-Verbose "Verifying API connectivity..."
@ -777,7 +778,7 @@ public enum $EnumName
Virtualization object functions Virtualization object functions
#> #>
function VerifyVirtualizationChoices { function ValidateVirtualizationChoices {
<# <#
.SYNOPSIS .SYNOPSIS
Internal function to verify provided values for static choices Internal function to verify provided values for static choices
@ -985,7 +986,7 @@ public enum $EnumName
) )
if ($null -ne $Status) { if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus $PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
} }
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines')) $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines'))
@ -1225,9 +1226,9 @@ public enum $EnumName
#endregion GET commands #endregion GET commands
#region ADD commands #region ADD/NEW commands
function Add-NetboxVirtualMachine { function New-NetboxVirtualMachine {
[CmdletBinding()] [CmdletBinding()]
[OutputType([pscustomobject])] [OutputType([pscustomobject])]
param param
@ -1257,7 +1258,7 @@ public enum $EnumName
[string]$Comments [string]$Comments
) )
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus $PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines')) $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines'))
@ -1343,8 +1344,8 @@ public enum $EnumName
[switch]$Force [switch]$Force
) )
if ($Status) { if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus $PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
} }
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $Id)) $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $Id))
@ -1364,6 +1365,59 @@ public enum $EnumName
} }
} }
function Set-NetboxVirtualMachineInterface {
[CmdletBinding(ConfirmImpact = 'Medium',
SupportsShouldProcess = $true)]
[OutputType([pscustomobject])]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[string]$Name,
[string]$MAC_Address,
[uint16]$MTU,
[string]$Description,
[boolean]$Enabled,
[uint16]$Virtual_Machine,
[switch]$Force
)
begin {
}
process {
foreach ($VMI_ID in $Id) {
Write-Verbose "Obtaining VM Interface..."
$CurrentVMI = Get-NetboxVirtualMachineInterface -Id $VMI_ID -ErrorAction Stop
Write-Verbose "Finished obtaining VM Interface"
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces', $CurrentVMI.Id))
if ($Force -or $pscmdlet.ShouldProcess("Interface $($CurrentVMI.Id) on VM $($CurrentVMI.Virtual_Machine.Name)", "Set")) {
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force'
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
}
end {
}
}
#endregion SET commands#endregion #endregion SET commands#endregion
@ -1784,13 +1838,13 @@ public enum $EnumName
InvokeNetboxRequest -URI $uri -Raw:$Raw InvokeNetboxRequest -URI $uri -Raw:$Raw
} }
function Add-NetboxIPAMAddress { function New-NetboxIPAMAddress {
<# <#
.SYNOPSIS .SYNOPSIS
Add a new IP address to Netbox Create a new IP address to Netbox
.DESCRIPTION .DESCRIPTION
Adds a new IP address to Netbox with a status of Active by default. Create a new IP address to Netbox with a status of Active by default.
.PARAMETER Address .PARAMETER Address
IP address in CIDR notation: 192.168.1.1/24 IP address in CIDR notation: 192.168.1.1/24
@ -1823,7 +1877,7 @@ public enum $EnumName
Return raw results from API service Return raw results from API service
.EXAMPLE .EXAMPLE
PS C:\> Add-NetboxIPAMAddress PS C:\> Create-NetboxIPAMAddress
.NOTES .NOTES
Additional information about the function. Additional information about the function.

View file

@ -162,7 +162,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
} }
} }
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
} }

View file

@ -38,7 +38,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
} }
} }
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
} }
@ -352,9 +352,9 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
} }
} }
Context -Name "Add-NetboxIPAMAddress" -Fixture { Context -Name "New-NetboxIPAMAddress" -Fixture {
It "Should add a basic IP address" { It "Should create a basic IP address" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24' $Result = New-NetboxIPAMAddress -Address '10.0.0.1/24'
Assert-VerifiableMock Assert-VerifiableMock
@ -364,8 +364,8 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Body | Should -Be '{"status":1,"address":"10.0.0.1/24"}' $Result.Body | Should -Be '{"status":1,"address":"10.0.0.1/24"}'
} }
It "Should add an IP with a status and role names" { It "Should create an IP with a status and role names" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast' $Result = New-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast'
Assert-VerifiableMock Assert-VerifiableMock
@ -375,8 +375,8 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
$Result.Body | Should -Be '{"status":2,"address":"10.0.0.1/24","role":30}' $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" { It "Should create an IP with a status and role values" {
$Result = Add-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10' $Result = New-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10'
Assert-VerifiableMock Assert-VerifiableMock

View file

@ -37,7 +37,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
} }
} }
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
} }
@ -306,9 +306,9 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
} }
} }
Context -Name "Add-NetboxVirtualMachine" -Fixture { Context -Name "New-NetboxVirtualMachine" -Fixture {
It "Should add a basic VM" { It "Should create a basic VM" {
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1 $Result = New-NetboxVirtualMachine -Name 'testvm' -Cluster 1
Assert-VerifiableMock Assert-VerifiableMock
@ -318,8 +318,8 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}' $Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}'
} }
It "Should add a VM with CPUs, Memory, Disk, tenancy, and comments" { It "Should create a VM with CPUs, Memory, Disk, tenancy, and comments" {
$Result = Add-NetboxVirtualMachine -Name 'testvm' -Cluster 1 -Status Active -vCPUs 4 -Memory 4096 -Tenant 11 -Disk 50 -Comments "these are comments" $Result = New-NetboxVirtualMachine -Name 'testvm' -Cluster 1 -Status Active -vCPUs 4 -Memory 4096 -Tenant 11 -Disk 50 -Comments "these are comments"
Assert-VerifiableMock Assert-VerifiableMock
@ -328,6 +328,10 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Headers.Keys.Count | Should -BeExactly 1 $Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"tenant":11,"comments":"these are comments","disk":50,"memory":4096,"name":"testvm","cluster":1,"status":1,"vcpus":4}' $Result.Body | Should -Be '{"tenant":11,"comments":"these are comments","disk":50,"memory":4096,"name":"testvm","cluster":1,"status":1,"vcpus":4}'
} }
It "Should throw because of an invalid status" {
{ New-NetboxVirtualMachine -Name 'testvm' -Status 1123 -Cluster 1 } | Should -Throw
}
} }
Context -Name "Add-NetboxVirtualInterface" -Fixture { Context -Name "Add-NetboxVirtualInterface" -Fixture {
@ -390,6 +394,69 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
Assert-VerifiableMock Assert-VerifiableMock
} }
} }
Context -Name "Set-NetboxVirtualMachineInterface" -Fixture {
Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith {
return @{
'Id' = 1234
'Name' = 'TestVM'
}
}
It "Should set an interface to a new name" {
$Result = Set-NetboxVirtualMachineInterface -Id 1234 -Name 'newtestname' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set an interface to a new name, MTU, MAC address and description" {
$paramSetNetboxVirtualMachineInterface = @{
Id = 1234
Name = 'newtestname'
MAC_Address = '11:22:33:44:55:66'
MTU = 9000
Description = "Test description"
Force = $true
}
$Result = Set-NetboxVirtualMachineInterface @paramSetNetboxVirtualMachineInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"mac_address":"11:22:33:44:55:66","mtu":9000,"description":"Test description","name":"newtestname"}'
}
It "Should set multiple interfaces to a new name" {
Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith {
return @(
@{
'Id' = $Id
'Name' = 'TestVM'
}
)
}
$Result = Set-NetboxVirtualMachineInterface -Id 1234, 4321 -Name 'newtestname' -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 2 -Scope 'It' -Exactly
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/1234/', 'https://netbox.domain.com/api/virtualization/interfaces/4321/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}'
}
}
} }
} }