diff --git a/README.md b/README.md index 5763fc1..a29827e 100644 --- a/README.md +++ b/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. diff --git a/Tests/.gitignore b/Tests/.gitignore new file mode 100644 index 0000000..c6e1f89 --- /dev/null +++ b/Tests/.gitignore @@ -0,0 +1 @@ +credential.ps1 \ No newline at end of file diff --git a/Tests/common.ps1 b/Tests/common.ps1 new file mode 100644 index 0000000..116dc24 --- /dev/null +++ b/Tests/common.ps1 @@ -0,0 +1,19 @@ +# +# Copyright 2021, Alexis La Goutte +# +# 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... \ No newline at end of file diff --git a/Tests/credential.example.ps1 b/Tests/credential.example.ps1 new file mode 100644 index 0000000..81c7adc --- /dev/null +++ b/Tests/credential.example.ps1 @@ -0,0 +1,13 @@ +# +# Copyright 2021, Alexis La Goutte +# +# 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" diff --git a/Tests/integration/DCIM.Site.Tests.ps1 b/Tests/integration/DCIM.Site.Tests.ps1 new file mode 100644 index 0000000..295188d --- /dev/null +++ b/Tests/integration/DCIM.Site.Tests.ps1 @@ -0,0 +1,85 @@ +# +# Copyright 2021, Alexis La Goutte +# +# 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 + } + +} \ No newline at end of file