diff --git a/Functions/DCIM/Devices/New-NetboxDCIMDeviceRole.ps1 b/Functions/DCIM/Devices/New-NetboxDCIMDeviceRole.ps1 new file mode 100644 index 0000000..d34d897 --- /dev/null +++ b/Functions/DCIM/Devices/New-NetboxDCIMDeviceRole.ps1 @@ -0,0 +1,33 @@ + +function New-NetboxDCIMDeviceRole { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + param + ( + [string]$Name, + + [string]$Color, + + [bool]$VM_Role, + + [string]$Description, + + [hashtable]$Custom_Fields + ) + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $Name)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Device Role')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} \ No newline at end of file diff --git a/Functions/DCIM/Devices/New-NetboxDCIMDeviceType.ps1 b/Functions/DCIM/Devices/New-NetboxDCIMDeviceType.ps1 new file mode 100644 index 0000000..dc3ff09 --- /dev/null +++ b/Functions/DCIM/Devices/New-NetboxDCIMDeviceType.ps1 @@ -0,0 +1,51 @@ + +function New-NetboxDCIMDeviceType { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + #region Parameters + param + ( + [Parameter(Mandatory = $true)] + [string]$Manufacturer, + + [Parameter(Mandatory = $true)] + [string]$Model, + + [string]$Part_Number, + + [uint16]$U_Height, + + [bool]$Is_Full_Depth, + + [string]$Subdevice_Role, + + [string]$Airflow, + + [uint16]$Weight, + + [string]$Weight_Unit, + + [string]$Description, + + [string]$Comments, + + [hashtable]$Custom_Fields + ) + #endregion Parameters + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-types')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $Model)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Device Types')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} \ No newline at end of file diff --git a/Functions/DCIM/Manufacture/Get-NetboxDCIMDManufacture.ps1 b/Functions/DCIM/Manufacture/Get-NetboxDCIMDManufacture.ps1 new file mode 100644 index 0000000..57384a3 --- /dev/null +++ b/Functions/DCIM/Manufacture/Get-NetboxDCIMDManufacture.ps1 @@ -0,0 +1,50 @@ + +function Get-NetboxDCIMManufacture { + [CmdletBinding()] + #region Parameters + param + ( + [uint16]$Offset, + + [uint16]$Limit, + + [Parameter(ParameterSetName = 'ById')] + [uint16[]]$Id, + + [string]$Name, + + [string]$Slug, + + [switch]$Raw + ) + #endregion Parameters + + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($ManuID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers', $ManuID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Raw' + + $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $URI -Raw:$Raw + } + + break + } + + default { + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers')) + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' + + $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $URI -Raw:$Raw + + } + } + +} \ No newline at end of file diff --git a/Functions/DCIM/Manufacture/New-NetboxDCIMManufacture.ps1 b/Functions/DCIM/Manufacture/New-NetboxDCIMManufacture.ps1 new file mode 100644 index 0000000..e792ba4 --- /dev/null +++ b/Functions/DCIM/Manufacture/New-NetboxDCIMManufacture.ps1 @@ -0,0 +1,33 @@ + +function New-NetboxDCIMManufacture { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + #region Parameters + param + ( + [Parameter(Mandatory = $true)] + [string]$Name, + + [string]$Description, + + [hashtable]$Custom_Fields + + ) + #endregion Parameters + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $name)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Manufacture')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} \ No newline at end of file diff --git a/Functions/Helpers/BuildNewURI.ps1 b/Functions/Helpers/BuildNewURI.ps1 index b7a734b..213a3cf 100644 --- a/Functions/Helpers/BuildNewURI.ps1 +++ b/Functions/Helpers/BuildNewURI.ps1 @@ -1,6 +1,6 @@  function BuildNewURI { -<# + <# .SYNOPSIS Create a new URI for Netbox @@ -52,11 +52,17 @@ function BuildNewURI { $null = CheckNetboxIsConnected } - # Begin a URI builder with HTTP/HTTPS and the provided hostname - $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort) + # Begin a URI builder with HTTP/HTTPS and the provided hostname, and url path if required + if (-not $script:NetboxConfig.URLPath) { + throw "Netbox Credentials not set! You may set with Set-NetboxCredential" + $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort) + } else { + $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort, "/$($script:NetboxConfig.URLPath.trim('/'))") + } + # Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together - $uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({ + $uriBuilder.Path += "/api/{0}/" -f ($Segments.ForEach({ $_.trim('/').trim() }) -join '/') diff --git a/Functions/Helpers/GenerateSlug.ps1 b/Functions/Helpers/GenerateSlug.ps1 new file mode 100644 index 0000000..5d8f675 --- /dev/null +++ b/Functions/Helpers/GenerateSlug.ps1 @@ -0,0 +1,16 @@ +function GenerateSlug { + [CmdletBinding()] + [OutputType([string])] + param ( + [Parameter(Mandatory = $true)] + [string]$Slug + ) + + Write-Verbose "Generating slug" + + $Slug = $Slug.Replace("-", "").Replace(" ", "-").ToLower() + + Write-Verbose " Completed building URIBuilder" + # Return the entire UriBuilder object + $Slug +} \ No newline at end of file diff --git a/Functions/Setup/Connect-NetboxAPI.ps1 b/Functions/Setup/Connect-NetboxAPI.ps1 index 89e900c..5675ca9 100644 --- a/Functions/Setup/Connect-NetboxAPI.ps1 +++ b/Functions/Setup/Connect-NetboxAPI.ps1 @@ -1,5 +1,5 @@ function Connect-NetboxAPI { -<# + <# .SYNOPSIS Connects to the Netbox API and ensures Credential work properly @@ -40,7 +40,7 @@ param ( [Parameter(ParameterSetName = 'Manual', - Mandatory = $true)] + Mandatory = $true)] [string]$Hostname, [Parameter(Mandatory = $false)] @@ -54,7 +54,7 @@ [uint16]$Port = 443, [Parameter(ParameterSetName = 'URI', - Mandatory = $true)] + Mandatory = $true)] [string]$URI, [Parameter(Mandatory = $false)] @@ -112,6 +112,7 @@ $null = Set-NetboxCredential -Credential $Credential $null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme $null = Set-NetboxHostPort -Port $uriBuilder.Port + $null = Set-NetboxURLPath -Path $uriBuilder.Path $null = Set-NetboxInvokeParams -invokeParams $invokeParams $null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds @@ -128,12 +129,12 @@ } } -# Write-Verbose "Caching API definition" -# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition -# -# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) { -# $Script:NetboxConfig.Connected = $false -# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)" + # Write-Verbose "Caching API definition" + # $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition + # + # if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) { + # $Script:NetboxConfig.Connected = $false + # throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)" # } Write-Verbose "Checking Netbox version compatibility" diff --git a/Functions/Setup/Get-NetboxURLPath.ps1 b/Functions/Setup/Get-NetboxURLPath.ps1 new file mode 100644 index 0000000..aeb9cee --- /dev/null +++ b/Functions/Setup/Get-NetboxURLPath.ps1 @@ -0,0 +1,11 @@ +function Get-NetboxURLPath { + [CmdletBinding()] + param () + + Write-Verbose "Getting Netbox URL Path" + if ($null -eq $script:NetboxConfig.URLPath) { + throw "Netbox URL Path is not set! You may set it with Set-NetboxURLPath -Path 'netbox/'" + } + + $script:NetboxConfig.URLPath +} \ No newline at end of file diff --git a/Functions/Setup/Set-NetboxURLPath.ps1 b/Functions/Setup/Set-NetboxURLPath.ps1 new file mode 100644 index 0000000..42cabb8 --- /dev/null +++ b/Functions/Setup/Set-NetboxURLPath.ps1 @@ -0,0 +1,15 @@ +function Set-NetboxURLPath { + [CmdletBinding(ConfirmImpact = 'Low', + SupportsShouldProcess = $true)] + [OutputType([string])] + param + ( + [Parameter(Mandatory = $true)] + [string]$Path + ) + + if ($PSCmdlet.ShouldProcess('Netbox URL Path', 'Set')) { + $script:NetboxConfig.URLPath = $Path.Trim() + $script:NetboxConfig.URLPath + } +} \ No newline at end of file diff --git a/NetboxPS.psd1 b/NetboxPS.psd1 index b9c80d4..3de5e0f 100644 --- a/NetboxPS.psd1 +++ b/NetboxPS.psd1 @@ -1,9 +1,9 @@ # -# Module manifest for module 'NetboxPS' +# Module manifest for module 'PSGet_NetboxPS' # # Generated by: Ben Claussen # -# Generated on: 2023-03-17 +# Generated on: 22/03/2023 # @{ @@ -66,35 +66,41 @@ CLRVersion = '2.0.50727' # FormatsToProcess = @() # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess -NestedModules = @() +# NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Add-NetboxDCIMInterfaceConnection', 'Add-NetboxDCIMRearPort', - 'Add-NetboxVirtualMachineInterface', 'Clear-NetboxCredential', - 'Connect-NetboxAPI', 'Get-ModelDefinition', 'Get-NetboxAPIDefinition', - 'Get-NetboxCircuit', 'Get-NetboxCircuitProvider', - 'Get-NetboxCircuitTermination', 'Get-NetboxCircuitType', + 'Add-NetboxVirtualMachineInterface', 'BuildNewURI', + 'BuildURIComponents', 'CheckNetboxIsConnected', + 'Clear-NetboxCredential', 'Connect-NetboxAPI', 'CreateEnum', + 'GenerateSlug', 'Get-ModelDefinition', 'Get-NetboxAPIDefinition', + 'GetNetboxAPIErrorBody', 'Get-NetboxCircuit', + 'Get-NetboxCircuitProvider', 'Get-NetboxCircuitTermination', + 'Get-NetboxCircuitType', 'GetNetboxConfigVariable', 'Get-NetboxContact', 'Get-NetboxContactAssignment', 'Get-NetboxContactRole', 'Get-NetboxContentType', 'Get-NetboxCredential', 'Get-NetboxDCIMCable', 'Get-NetboxDCIMCableTermination', 'Get-NetboxDCIMDevice', 'Get-NetboxDCIMDeviceRole', 'Get-NetboxDCIMDeviceType', - 'Get-NetboxDCIMFrontPort', 'Get-NetboxDCIMInterface', - 'Get-NetboxDCIMInterfaceConnection', 'Get-NetboxDCIMPlatform', - 'Get-NetboxDCIMRearPort', 'Get-NetboxDCIMSite', 'Get-NetboxHostname', - 'Get-NetboxHostPort', 'Get-NetboxHostScheme', - 'Get-NetboxInvokeParams', 'Get-NetboxIPAMAddress', - 'Get-NetboxIPAMAggregate', 'Get-NetboxIPAMAvailableIP', - 'Get-NetboxIPAMPrefix', 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', - 'Get-NetboxTag', 'Get-NetboxTenant', 'Get-NetboxTimeout', + 'Get-NetboxDCIMDManufacture', 'Get-NetboxDCIMFrontPort', + 'Get-NetboxDCIMInterface', 'Get-NetboxDCIMInterfaceConnection', + 'Get-NetboxDCIMPlatform', 'Get-NetboxDCIMRearPort', + 'Get-NetboxDCIMSite', 'Get-NetboxHostname', 'Get-NetboxHostPort', + 'Get-NetboxHostScheme', 'Get-NetboxInvokeParams', + 'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate', + 'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix', + 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag', + 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath', 'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster', 'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine', - 'Get-NetboxVirtualMachineInterface', 'New-NetboxCircuit', - 'New-NetboxContact', 'New-NetboxContactAssignment', - 'New-NetboxContactRole', 'New-NetboxDCIMDevice', 'New-NetboxDCIMSite', - 'New-NetboxIPAMAddress', 'New-NetboxIPAMPrefix', 'New-NetboxIPAMVLAN', - 'New-NetboxTenant', 'New-NetboxVirtualMachine', + 'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest', + 'New-NetboxCircuit', 'New-NetboxContact', + 'New-NetboxContactAssignment', 'New-NetboxContactRole', + 'New-NetboxDCIMDevice', 'New-NetboxDCIMDeviceRole', + 'New-NetboxDCIMDeviceType', 'New-NetboxDCIMManufacture', + 'New-NetboxDCIMSite', 'New-NetboxIPAMAddress', 'New-NetboxIPAMPrefix', + 'New-NetboxIPAMVLAN', 'New-NetboxTenant', 'New-NetboxVirtualMachine', 'Remove-NetboxDCIMDevice', 'Remove-NetboxDCIMFrontPort', 'Remove-NetboxDCIMInterface', 'Remove-NetboxDCIMInterfaceConnection', 'Remove-NetboxDCIMRearPort', @@ -106,17 +112,19 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme', 'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress', 'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout', - 'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine', - 'Set-NetboxVirtualMachineInterface', 'Test-NetboxAPIConnected' + 'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath', + 'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface', + 'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected', + 'ThrowNetboxRESTError', 'VerifyAPIConnectivity' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = '*' +CmdletsToExport = @() # Variables to export from this module -VariablesToExport = '*' +# VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = '*' +AliasesToExport = @() # DSC resources to export from this module # DscResourcesToExport = @() @@ -147,14 +155,8 @@ PrivateData = @{ # ReleaseNotes of this module # ReleaseNotes = '' - # Prerelease string of this module - # Prerelease = '' - - # Flag to indicate whether the module requires explicit user acceptance for install/update/save - # RequireLicenseAcceptance = $false - # External dependent modules of this module - # ExternalModuleDependencies = @() + # ExternalModuleDependencies = '' } # End of PSData hashtable diff --git a/NetboxPS/NetboxPS.psd1 b/NetboxPS/NetboxPS.psd1 index b9c80d4..3de5e0f 100644 --- a/NetboxPS/NetboxPS.psd1 +++ b/NetboxPS/NetboxPS.psd1 @@ -1,9 +1,9 @@ # -# Module manifest for module 'NetboxPS' +# Module manifest for module 'PSGet_NetboxPS' # # Generated by: Ben Claussen # -# Generated on: 2023-03-17 +# Generated on: 22/03/2023 # @{ @@ -66,35 +66,41 @@ CLRVersion = '2.0.50727' # FormatsToProcess = @() # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess -NestedModules = @() +# NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Add-NetboxDCIMInterfaceConnection', 'Add-NetboxDCIMRearPort', - 'Add-NetboxVirtualMachineInterface', 'Clear-NetboxCredential', - 'Connect-NetboxAPI', 'Get-ModelDefinition', 'Get-NetboxAPIDefinition', - 'Get-NetboxCircuit', 'Get-NetboxCircuitProvider', - 'Get-NetboxCircuitTermination', 'Get-NetboxCircuitType', + 'Add-NetboxVirtualMachineInterface', 'BuildNewURI', + 'BuildURIComponents', 'CheckNetboxIsConnected', + 'Clear-NetboxCredential', 'Connect-NetboxAPI', 'CreateEnum', + 'GenerateSlug', 'Get-ModelDefinition', 'Get-NetboxAPIDefinition', + 'GetNetboxAPIErrorBody', 'Get-NetboxCircuit', + 'Get-NetboxCircuitProvider', 'Get-NetboxCircuitTermination', + 'Get-NetboxCircuitType', 'GetNetboxConfigVariable', 'Get-NetboxContact', 'Get-NetboxContactAssignment', 'Get-NetboxContactRole', 'Get-NetboxContentType', 'Get-NetboxCredential', 'Get-NetboxDCIMCable', 'Get-NetboxDCIMCableTermination', 'Get-NetboxDCIMDevice', 'Get-NetboxDCIMDeviceRole', 'Get-NetboxDCIMDeviceType', - 'Get-NetboxDCIMFrontPort', 'Get-NetboxDCIMInterface', - 'Get-NetboxDCIMInterfaceConnection', 'Get-NetboxDCIMPlatform', - 'Get-NetboxDCIMRearPort', 'Get-NetboxDCIMSite', 'Get-NetboxHostname', - 'Get-NetboxHostPort', 'Get-NetboxHostScheme', - 'Get-NetboxInvokeParams', 'Get-NetboxIPAMAddress', - 'Get-NetboxIPAMAggregate', 'Get-NetboxIPAMAvailableIP', - 'Get-NetboxIPAMPrefix', 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', - 'Get-NetboxTag', 'Get-NetboxTenant', 'Get-NetboxTimeout', + 'Get-NetboxDCIMDManufacture', 'Get-NetboxDCIMFrontPort', + 'Get-NetboxDCIMInterface', 'Get-NetboxDCIMInterfaceConnection', + 'Get-NetboxDCIMPlatform', 'Get-NetboxDCIMRearPort', + 'Get-NetboxDCIMSite', 'Get-NetboxHostname', 'Get-NetboxHostPort', + 'Get-NetboxHostScheme', 'Get-NetboxInvokeParams', + 'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate', + 'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix', + 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag', + 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath', 'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster', 'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine', - 'Get-NetboxVirtualMachineInterface', 'New-NetboxCircuit', - 'New-NetboxContact', 'New-NetboxContactAssignment', - 'New-NetboxContactRole', 'New-NetboxDCIMDevice', 'New-NetboxDCIMSite', - 'New-NetboxIPAMAddress', 'New-NetboxIPAMPrefix', 'New-NetboxIPAMVLAN', - 'New-NetboxTenant', 'New-NetboxVirtualMachine', + 'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest', + 'New-NetboxCircuit', 'New-NetboxContact', + 'New-NetboxContactAssignment', 'New-NetboxContactRole', + 'New-NetboxDCIMDevice', 'New-NetboxDCIMDeviceRole', + 'New-NetboxDCIMDeviceType', 'New-NetboxDCIMManufacture', + 'New-NetboxDCIMSite', 'New-NetboxIPAMAddress', 'New-NetboxIPAMPrefix', + 'New-NetboxIPAMVLAN', 'New-NetboxTenant', 'New-NetboxVirtualMachine', 'Remove-NetboxDCIMDevice', 'Remove-NetboxDCIMFrontPort', 'Remove-NetboxDCIMInterface', 'Remove-NetboxDCIMInterfaceConnection', 'Remove-NetboxDCIMRearPort', @@ -106,17 +112,19 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme', 'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress', 'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout', - 'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine', - 'Set-NetboxVirtualMachineInterface', 'Test-NetboxAPIConnected' + 'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath', + 'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface', + 'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected', + 'ThrowNetboxRESTError', 'VerifyAPIConnectivity' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = '*' +CmdletsToExport = @() # Variables to export from this module -VariablesToExport = '*' +# VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = '*' +AliasesToExport = @() # DSC resources to export from this module # DscResourcesToExport = @() @@ -147,14 +155,8 @@ PrivateData = @{ # ReleaseNotes of this module # ReleaseNotes = '' - # Prerelease string of this module - # Prerelease = '' - - # Flag to indicate whether the module requires explicit user acceptance for install/update/save - # RequireLicenseAcceptance = $false - # External dependent modules of this module - # ExternalModuleDependencies = @() + # ExternalModuleDependencies = '' } # End of PSData hashtable diff --git a/NetboxPS/NetboxPS.psm1 b/NetboxPS/NetboxPS.psm1 index 87e5c9f..a212c86 100644 --- a/NetboxPS/NetboxPS.psm1 +++ b/NetboxPS/NetboxPS.psm1 @@ -271,7 +271,7 @@ function Add-NetboxVirtualMachineInterface { function BuildNewURI { -<# + <# .SYNOPSIS Create a new URI for Netbox @@ -323,11 +323,17 @@ function BuildNewURI { $null = CheckNetboxIsConnected } - # Begin a URI builder with HTTP/HTTPS and the provided hostname - $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort) + # Begin a URI builder with HTTP/HTTPS and the provided hostname, and url path if required + if (-not $script:NetboxConfig.URLPath) { + throw "Netbox Credentials not set! You may set with Set-NetboxCredential" + $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort) + } else { + $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort, "/$($script:NetboxConfig.URLPath.trim('/'))") + } + # Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together - $uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({ + $uriBuilder.Path += "/api/{0}/" -f ($Segments.ForEach({ $_.trim('/').trim() }) -join '/') @@ -466,7 +472,7 @@ function Clear-NetboxCredential { #region File Connect-NetboxAPI.ps1 function Connect-NetboxAPI { -<# + <# .SYNOPSIS Connects to the Netbox API and ensures Credential work properly @@ -507,7 +513,7 @@ function Connect-NetboxAPI { param ( [Parameter(ParameterSetName = 'Manual', - Mandatory = $true)] + Mandatory = $true)] [string]$Hostname, [Parameter(Mandatory = $false)] @@ -521,7 +527,7 @@ function Connect-NetboxAPI { [uint16]$Port = 443, [Parameter(ParameterSetName = 'URI', - Mandatory = $true)] + Mandatory = $true)] [string]$URI, [Parameter(Mandatory = $false)] @@ -579,6 +585,7 @@ function Connect-NetboxAPI { $null = Set-NetboxCredential -Credential $Credential $null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme $null = Set-NetboxHostPort -Port $uriBuilder.Port + $null = Set-NetboxURLPath -Path $uriBuilder.Path $null = Set-NetboxInvokeParams -invokeParams $invokeParams $null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds @@ -595,12 +602,12 @@ function Connect-NetboxAPI { } } -# Write-Verbose "Caching API definition" -# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition -# -# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) { -# $Script:NetboxConfig.Connected = $false -# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)" + # Write-Verbose "Caching API definition" + # $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition + # + # if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) { + # $Script:NetboxConfig.Connected = $false + # throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)" # } Write-Verbose "Checking Netbox version compatibility" @@ -655,6 +662,27 @@ public enum $EnumName #endregion +#region File GenerateSlug.ps1 + +function GenerateSlug { + [CmdletBinding()] + [OutputType([string])] + param ( + [Parameter(Mandatory = $true)] + [string]$Slug + ) + + Write-Verbose "Generating slug" + + $Slug = $Slug.Replace("-", "").Replace(" ", "-").ToLower() + + Write-Verbose " Completed building URIBuilder" + # Return the entire UriBuilder object + $Slug +} + +#endregion + #region File Get-ModelDefinition.ps1 @@ -1811,6 +1839,61 @@ function Get-NetboxDCIMDeviceType { #endregion +#region File Get-NetboxDCIMDManufacture.ps1 + + +function Get-NetboxDCIMManufacture { + [CmdletBinding()] + #region Parameters + param + ( + [uint16]$Offset, + + [uint16]$Limit, + + [Parameter(ParameterSetName = 'ById')] + [uint16[]]$Id, + + [string]$Name, + + [string]$Slug, + + [switch]$Raw + ) + #endregion Parameters + + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($ManuID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers', $ManuID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Raw' + + $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $URI -Raw:$Raw + } + + break + } + + default { + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers')) + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' + + $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $URI -Raw:$Raw + + } + } + +} + +#endregion + #region File Get-NetboxDCIMFrontPort.ps1 function Get-NetboxDCIMFrontPort { @@ -2993,6 +3076,22 @@ function Get-NetboxTimeout { #endregion +#region File Get-NetboxURLPath.ps1 + +function Get-NetboxURLPath { + [CmdletBinding()] + param () + + Write-Verbose "Getting Netbox URL Path" + if ($null -eq $script:NetboxConfig.URLPath) { + throw "Netbox URL Path is not set! You may set it with Set-NetboxURLPath -Path 'netbox/'" + } + + $script:NetboxConfig.URLPath +} + +#endregion + #region File Get-NetboxVersion.ps1 @@ -3900,6 +3999,138 @@ function New-NetboxDCIMDevice { #endregion +#region File New-NetboxDCIMDeviceRole.ps1 + + +function New-NetboxDCIMDeviceRole { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + param + ( + [string]$Name, + + [string]$Color, + + [bool]$VM_Role, + + [string]$Description, + + [hashtable]$Custom_Fields + ) + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $Name)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Device Role')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} + +#endregion + +#region File New-NetboxDCIMDeviceType.ps1 + + +function New-NetboxDCIMDeviceType { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + #region Parameters + param + ( + [Parameter(Mandatory = $true)] + [string]$Manufacturer, + + [Parameter(Mandatory = $true)] + [string]$Model, + + [string]$Part_Number, + + [uint16]$U_Height, + + [bool]$Is_Full_Depth, + + [string]$Subdevice_Role, + + [string]$Airflow, + + [uint16]$Weight, + + [string]$Weight_Unit, + + [string]$Description, + + [string]$Comments, + + [hashtable]$Custom_Fields + ) + #endregion Parameters + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-types')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $Model)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Device Types')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} + +#endregion + +#region File New-NetboxDCIMManufacture.ps1 + + +function New-NetboxDCIMManufacture { + [CmdletBinding(ConfirmImpact = 'low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + #region Parameters + param + ( + [Parameter(Mandatory = $true)] + [string]$Name, + + [string]$Description, + + [hashtable]$Custom_Fields + + ) + #endregion Parameters + + $Segments = [System.Collections.ArrayList]::new(@('dcim', 'manufacturers')) + $Method = 'POST' + + if (-not $PSBoundParameters.ContainsKey('slug')) { + $PSBoundParameters.Add('slug', (GenerateSlug -Slug $name)) + } + + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Name, 'Create new Manufacture')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters + } +} + +#endregion + #region File New-NetboxDCIMSite.ps1 <# @@ -5677,6 +5908,26 @@ Function Set-NetboxUntrustedSSL { #endregion +#region File Set-NetboxURLPath.ps1 + +function Set-NetboxURLPath { + [CmdletBinding(ConfirmImpact = 'Low', + SupportsShouldProcess = $true)] + [OutputType([string])] + param + ( + [Parameter(Mandatory = $true)] + [string]$Path + ) + + if ($PSCmdlet.ShouldProcess('Netbox URL Path', 'Set')) { + $script:NetboxConfig.URLPath = $Path.Trim() + $script:NetboxConfig.URLPath + } +} + +#endregion + #region File Set-NetboxVirtualMachine.ps1