From dccfde55ba53cd9ff37a381cc1dd3036a4aea11b Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 16:46:06 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8feature:=20adding=20Manufactures?= =?UTF-8?q?=20(GET/NEW)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Get-NetboxDCIMDManufacture.ps1 | 50 +++++++++++++++++++ .../Manufacture/New-NetboxDCIMManufacture.ps1 | 33 ++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Functions/DCIM/Manufacture/Get-NetboxDCIMDManufacture.ps1 create mode 100644 Functions/DCIM/Manufacture/New-NetboxDCIMManufacture.ps1 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 From 5afd24f53168f703ffa7193f51b4df6a3a65b0c6 Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 16:51:56 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8feature:=20add=20function=20to=20g?= =?UTF-8?q?enerate=20formatted=20slugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Functions/Helpers/GenerateSlug.ps1 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Functions/Helpers/GenerateSlug.ps1 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 From 471a9423099e2b6926d51358bad5a84c7f8e5958 Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 16:53:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=A8feature:=20add=20functions=20for?= =?UTF-8?q?=20NEW=20Device=20Role/Types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DCIM/Devices/New-NetboxDCIMDeviceRole.ps1 | 33 ++++++++++++ .../DCIM/Devices/New-NetboxDCIMDeviceType.ps1 | 51 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 Functions/DCIM/Devices/New-NetboxDCIMDeviceRole.ps1 create mode 100644 Functions/DCIM/Devices/New-NetboxDCIMDeviceType.ps1 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 From c736e0a1f70bd783422bb5967e5a0779ad09467c Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 16:57:07 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=94=B5=20other:=20run=20of=20.\deploy?= =?UTF-8?q?=20-skipversion=20for=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetboxPS.psd1 | 66 ++++++------- NetboxPS/NetboxPS.psd1 | 66 ++++++------- NetboxPS/NetboxPS.psm1 | 208 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+), 64 deletions(-) diff --git a/NetboxPS.psd1 b/NetboxPS.psd1 index b9c80d4..69a96c5 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-NetboxVersion', 'Get-NetboxVirtualizationCluster', + '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-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', @@ -107,16 +113,18 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress', 'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout', 'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine', - 'Set-NetboxVirtualMachineInterface', 'Test-NetboxAPIConnected' + '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..69a96c5 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-NetboxVersion', 'Get-NetboxVirtualizationCluster', + '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-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', @@ -107,16 +113,18 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress', 'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout', 'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine', - 'Set-NetboxVirtualMachineInterface', 'Test-NetboxAPIConnected' + '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..4b45566 100644 --- a/NetboxPS/NetboxPS.psm1 +++ b/NetboxPS/NetboxPS.psm1 @@ -655,6 +655,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 +1832,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 { @@ -3900,6 +3976,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 <# From 2e131d45d2bd8049b95cdace407023b5f0c46e31 Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 17:20:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9C=A8feature:=20Add=20ability=20to=20de?= =?UTF-8?q?fine=20a=20custom=20URLPath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Functions/Helpers/BuildNewURI.ps1 | 14 ++++-- Functions/Setup/Connect-NetboxAPI.ps1 | 19 ++++---- Functions/Setup/Get-NetboxURLPath.ps1 | 11 +++++ Functions/Setup/Set-NetboxURLPath.ps1 | 15 ++++++ NetboxPS.psd1 | 12 ++--- NetboxPS/NetboxPS.psd1 | 12 ++--- NetboxPS/NetboxPS.psm1 | 69 ++++++++++++++++++++++----- 7 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 Functions/Setup/Get-NetboxURLPath.ps1 create mode 100644 Functions/Setup/Set-NetboxURLPath.ps1 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/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 69a96c5..3de5e0f 100644 --- a/NetboxPS.psd1 +++ b/NetboxPS.psd1 @@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate', 'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix', 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag', - 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion', - 'Get-NetboxVirtualizationCluster', + 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath', + 'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster', 'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine', 'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest', 'New-NetboxCircuit', 'New-NetboxContact', @@ -112,10 +112,10 @@ 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', 'SetupNetboxConfigVariable', - 'Test-NetboxAPIConnected', 'ThrowNetboxRESTError', - 'VerifyAPIConnectivity' + '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 = @() diff --git a/NetboxPS/NetboxPS.psd1 b/NetboxPS/NetboxPS.psd1 index 69a96c5..3de5e0f 100644 --- a/NetboxPS/NetboxPS.psd1 +++ b/NetboxPS/NetboxPS.psd1 @@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface', 'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate', 'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix', 'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag', - 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion', - 'Get-NetboxVirtualizationCluster', + 'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath', + 'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster', 'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine', 'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest', 'New-NetboxCircuit', 'New-NetboxContact', @@ -112,10 +112,10 @@ 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', 'SetupNetboxConfigVariable', - 'Test-NetboxAPIConnected', 'ThrowNetboxRESTError', - 'VerifyAPIConnectivity' + '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 = @() diff --git a/NetboxPS/NetboxPS.psm1 b/NetboxPS/NetboxPS.psm1 index 4b45566..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" @@ -3069,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 @@ -5885,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