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'
# Version number of this module.
ModuleVersion = '1.0.0.0'
ModuleVersion = '1.0.1'
# ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

View file

@ -6,5 +6,5 @@ $script:CommonParameterNames = New-Object System.Collections.ArrayList
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'
# Version number of this module.
ModuleVersion = '1.0.0.0'
ModuleVersion = '1.0.1'
# ID used to uniquely identify this module
GUID = 'bba9b06c-49c8-47cf-8358-aca7c4e78896'

146
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/18/2018 12:40 PM
Generated on: 5/21/2018 3:34 PM
Generated by: Ben Claussen
Organization: NEOnet
--------------------------------------------------------------------------------
@ -300,7 +300,7 @@
[switch]$Raw
)
$creds = Get-NetboxCredentials
$creds = Get-NetboxCredential
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
@ -460,7 +460,7 @@ public enum $EnumName
}
function Set-NetboxHostName {
[CmdletBinding(ConfirmImpact = 'Medium',
[CmdletBinding(ConfirmImpact = 'Low',
SupportsShouldProcess = $true)]
[OutputType([string])]
param
@ -469,8 +469,10 @@ public enum $EnumName
[string]$Hostname
)
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) {
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
}
}
function Get-NetboxHostname {
@ -485,50 +487,49 @@ public enum $EnumName
$script:NetboxConfig.Hostname
}
function Set-NetboxCredentials {
function Set-NetboxCredential {
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
ConfirmImpact = 'Medium',
ConfirmImpact = 'Low',
SupportsShouldProcess = $true)]
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
[OutputType([pscredential], ParameterSetName = 'UserPass')]
[OutputType([pscredential])]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credentials,
[pscredential]$Credential,
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[string]$Token
[securestring]$Token
)
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credentials = $Credentials
break
if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) {
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credential = $Credential
break
}
'UserPass' {
$script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token)
break
}
}
'UserPass' {
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
break
}
$script:NetboxConfig.Credential
}
$script:NetboxConfig.Credentials
}
function Get-NetboxCredentials {
function Get-NetboxCredential {
[CmdletBinding()]
[OutputType([pscredential])]
param ()
if (-not $script:NetboxConfig.Credentials) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredentials"
if (-not $script:NetboxConfig.Credential) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
}
$script:NetboxConfig.Credentials
$script:NetboxConfig.Credential
}
function VerifyAPIConnectivity {
@ -545,7 +546,7 @@ public enum $EnumName
function Connect-NetboxAPI {
<#
.SYNOPSIS
Connects to the Netbox API and ensures credentials work properly
Connects to the Netbox API and ensures Credential work properly
.DESCRIPTION
A detailed description of the Connect-NetboxAPI function.
@ -553,13 +554,13 @@ public enum $EnumName
.PARAMETER Hostname
A description of the Hostname parameter.
.PARAMETER Credentials
A description of the Credentials parameter.
.PARAMETER Credential
A description of the Credential parameter.
.EXAMPLE
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
Additional information about the function.
@ -572,22 +573,22 @@ public enum $EnumName
[string]$Hostname,
[Parameter(Mandatory = $false)]
[pscredential]$Credentials
[pscredential]$Credential
)
if (-not $Credentials) {
if (-not $Credential) {
try {
$Credentials = Get-NetboxCredentials -ErrorAction Stop
$Credential = Get-NetboxCredential -ErrorAction Stop
} catch {
# 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."
}
}
}
$null = Set-NetboxHostName -Hostname $Hostname
$null = Set-NetboxCredentials -Credentials $Credentials
$null = Set-NetboxCredential -Credential $Credential
try {
Write-Verbose "Verifying API connectivity..."
@ -777,7 +778,7 @@ public enum $EnumName
Virtualization object functions
#>
function VerifyVirtualizationChoices {
function ValidateVirtualizationChoices {
<#
.SYNOPSIS
Internal function to verify provided values for static choices
@ -985,7 +986,7 @@ public enum $EnumName
)
if ($null -ne $Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
$PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines'))
@ -1225,9 +1226,9 @@ public enum $EnumName
#endregion GET commands
#region ADD commands
#region ADD/NEW commands
function Add-NetboxVirtualMachine {
function New-NetboxVirtualMachine {
[CmdletBinding()]
[OutputType([pscustomobject])]
param
@ -1257,7 +1258,7 @@ public enum $EnumName
[string]$Comments
)
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
$PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
$Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines'))
@ -1343,8 +1344,8 @@ public enum $EnumName
[switch]$Force
)
if ($Status) {
$PSBoundParameters.Status = VerifyVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
if ($null -ne $Status) {
$PSBoundParameters.Status = ValidateVirtualizationChoices -ProvidedValue $Status -VirtualMachineStatus
}
$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
@ -1784,13 +1838,13 @@ public enum $EnumName
InvokeNetboxRequest -URI $uri -Raw:$Raw
}
function Add-NetboxIPAMAddress {
function New-NetboxIPAMAddress {
<#
.SYNOPSIS
Add a new IP address to Netbox
Create a new IP address to Netbox
.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
IP address in CIDR notation: 192.168.1.1/24
@ -1823,7 +1877,7 @@ public enum $EnumName
Return raw results from API service
.EXAMPLE
PS C:\> Add-NetboxIPAMAddress
PS C:\> Create-NetboxIPAMAddress
.NOTES
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))
}

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))
}
@ -352,9 +352,9 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
}
}
Context -Name "Add-NetboxIPAMAddress" -Fixture {
It "Should add a basic IP address" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24'
Context -Name "New-NetboxIPAMAddress" -Fixture {
It "Should create a basic IP address" {
$Result = New-NetboxIPAMAddress -Address '10.0.0.1/24'
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"}'
}
It "Should add an IP with a status and role names" {
$Result = Add-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast'
It "Should create an IP with a status and role names" {
$Result = New-NetboxIPAMAddress -Address '10.0.0.1/24' -Status 'Reserved' -Role 'Anycast'
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}'
}
It "Should add an IP with a status and role values" {
$Result = Add-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10'
It "Should create an IP with a status and role values" {
$Result = New-NetboxIPAMAddress -Address '10.0.1.1/24' -Status '1' -Role '10'
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))
}
@ -306,9 +306,9 @@ 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
Context -Name "New-NetboxVirtualMachine" -Fixture {
It "Should create a basic VM" {
$Result = New-NetboxVirtualMachine -Name 'testvm' -Cluster 1
Assert-VerifiableMock
@ -318,8 +318,8 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$Result.Body | Should -Be '{"cluster":1,"name":"testvm","status":1}'
}
It "Should add 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"
It "Should create a VM with CPUs, Memory, Disk, tenancy, and comments" {
$Result = New-NetboxVirtualMachine -Name 'testvm' -Cluster 1 -Status Active -vCPUs 4 -Memory 4096 -Tenant 11 -Disk 50 -Comments "these are comments"
Assert-VerifiableMock
@ -328,6 +328,10 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
$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}'
}
It "Should throw because of an invalid status" {
{ New-NetboxVirtualMachine -Name 'testvm' -Status 1123 -Cluster 1 } | Should -Throw
}
}
Context -Name "Add-NetboxVirtualInterface" -Fixture {
@ -390,6 +394,69 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
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"}'
}
}
}
}