mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-12 17:32:29 +00:00
Merge branch 'dev' of https://github.com/benclaussen/NetboxPS into dev
This commit is contained in:
commit
f289854ddf
5 changed files with 158 additions and 0 deletions
40
README.md
40
README.md
|
|
@ -14,6 +14,46 @@ This module is a wrapper for the [Netbox](https://github.com/netbox-community/ne
|
|||
2. Import module
|
||||
3. Connect to an API endpoint by using `Connect-NetboxAPI -Hostname netbox.example.com`
|
||||
|
||||
## Basic Commands
|
||||
|
||||
```powershell
|
||||
#Just adding a new IP
|
||||
New-NetboxIPAMAddress -Address 10.0.0.1/24 -Dns_name this.is.thedns.fqdn -Custom_Fields @{CustomFieldID="CustomFieldContent"} -Tenant 1 -Description "Description"
|
||||
|
||||
#Creating a new VM, add an interface and assign Interface IP
|
||||
function New-NBVirtualMachine
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
[string]$Name,
|
||||
[string]$Cluster,
|
||||
[string]$IP,
|
||||
[string]$tenant,
|
||||
[string]$VMNICName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$NBCluster = Get-NetboxVirtualizationCluster -name $Cluster
|
||||
$NBTenant = Get-NetboxTenant -Name $tenant
|
||||
}
|
||||
Process
|
||||
{
|
||||
$vm = New-NetboxVirtualMachine -Name $Name -Cluster $NBCluster.id -Tenant $NBtenant.id
|
||||
$interface = Add-NetboxVirtualMachineInterface -Name $VMNICName -Virtual_Machine $vm.id
|
||||
|
||||
|
||||
$NBip = New-NetboxIPAMAddress -Address $IP -Tenant $NBtenant.id
|
||||
Set-NetboxIPAMAddress -Assigned_Object_Type virtualization.vminterface -Assigned_Object_Id $interface.id -id $NBip.id
|
||||
Set-NetboxVirtualMachine -Primary_IP4 $NBip.id -Id $vm.id
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
# Notes
|
||||
I started this project years ago with Powershell Studio using the built in deployment methods, learning Git, and learning PS best practices. So please forgive any "obvious" mistakes 😅
|
||||
Over time I have had to adjust my methods for deployment... change the design of functions, and refactor code as I learn new and better things.
|
||||
|
|
|
|||
1
Tests/.gitignore
vendored
Normal file
1
Tests/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
credential.ps1
|
||||
19
Tests/common.ps1
Normal file
19
Tests/common.ps1
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#
|
||||
# Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
|
||||
Param()
|
||||
|
||||
$script:pester_site1 = "pester_site1"
|
||||
|
||||
$Credential = New-Object System.Management.Automation.PSCredential("username", (ConvertTo-SecureString $token -AsPlainText -Force))
|
||||
$script:invokeParams = @{
|
||||
hostname = $hostname;
|
||||
Credential = $Credential;
|
||||
SkipCertificateCheck = $true;
|
||||
}
|
||||
|
||||
. ../credential.ps1
|
||||
#TODO: Add check if no ipaddress/token info...
|
||||
13
Tests/credential.example.ps1
Normal file
13
Tests/credential.example.ps1
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#
|
||||
# Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
# Copy this file to credential.ps1 (on Tests folder) and change connection settings..
|
||||
|
||||
$script:hostname = "10.44.23.213"
|
||||
$script:token = "aaaaaaaaaaaaaaaaaa"
|
||||
|
||||
#Uncomment if you want to personnalize some setting
|
||||
#script:pester_site1 = "pester_site1"
|
||||
85
Tests/integration/DCIM.Site.Tests.ps1
Normal file
85
Tests/integration/DCIM.Site.Tests.ps1
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#
|
||||
# Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
. ../common.ps1
|
||||
|
||||
BeforeAll {
|
||||
Connect-NetboxAPI @invokeParams
|
||||
}
|
||||
|
||||
Describe "Get (DCIM) Site" {
|
||||
|
||||
BeforeAll {
|
||||
New-NetboxDCIMSite -name $pester_site1
|
||||
}
|
||||
|
||||
It "Get Site Does not throw an error" {
|
||||
{
|
||||
Get-NetboxDCIMSite
|
||||
} | Should -Not -Throw
|
||||
}
|
||||
|
||||
It "Get ALL Site" {
|
||||
$site = Get-NetboxDCIMSite
|
||||
$site.count | Should -Not -Be $NULL
|
||||
}
|
||||
|
||||
It "Get Site ($pester_site1)" {
|
||||
$site = Get-NetboxDCIMSite | Where-Object { $_.name -eq $pester_site1 }
|
||||
$site.id | Should -Not -BeNullOrEmpty
|
||||
$site.name | Should -Be $pester_site1
|
||||
$site.status.value | Should -Be "active"
|
||||
}
|
||||
|
||||
It "Search Site by name ($pester_site1)" {
|
||||
$site = Get-NetboxDCIMSite -name $pester_site1
|
||||
@($site).count | Should -Be 1
|
||||
$site.id | Should -Not -BeNullOrEmpty
|
||||
$site.name | Should -Be $pester_site1
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Get-NetboxDCIMSite -name $pester_site1 | Remove-NetboxDCIMSite -confirm:$false
|
||||
}
|
||||
}
|
||||
|
||||
Describe "New (DCIM) Site" {
|
||||
|
||||
It "New Site with no option" {
|
||||
New-NetboxDCIMSite -name $pester_site1
|
||||
$site = Get-NetboxDCIMSite -name $pester_site1
|
||||
$site.id | Should -Not -BeNullOrEmpty
|
||||
$site.name | Should -Be $pester_site1
|
||||
$site.slug | Should -Be $pester_site1
|
||||
}
|
||||
|
||||
It "New Site with different slug" {
|
||||
New-NetboxDCIMSite -name $pester_site1 -slug pester_slug
|
||||
$site = Get-NetboxDCIMSite -name $pester_site1
|
||||
$site.id | Should -Not -BeNullOrEmpty
|
||||
$site.name | Should -Be $pester_site1
|
||||
$site.slug | Should -Be "pester_slug"
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
Get-NetboxDCIMSite -name $pester_site1 | Remove-NetboxDCIMSite -confirm:$false
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Remove Site" {
|
||||
|
||||
BeforeEach {
|
||||
New-NetboxDCIMSite -name $pester_site1
|
||||
}
|
||||
|
||||
It "Remove Site" {
|
||||
$site = Get-NetboxDCIMSite -name $pester_site1
|
||||
Remove-NetboxDCIMSite -id $site.id -confirm:$false
|
||||
$site = Get-NetboxDCIMSite -name $pester_site1
|
||||
$site | Should -BeNullOrEmpty
|
||||
@($site).count | Should -Be 0
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue