From 18852aa9905c21e37af0565600ce3a24c821737c Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Mon, 5 Oct 2020 10:43:22 -0400 Subject: [PATCH] Add parameter sets and logic for ID/Query searches --- .../IPAM/Prefix/Get-NetboxIPAMPrefix.ps1 | 83 ++++++++++++++----- Functions/IPAM/Role/Get-NetboxIPAMRole.ps1 | 44 ++++++++-- Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1 | 72 ++++++++++++---- Functions/Tenancy/Get-NetboxTenant.ps1 | 62 ++++++++++---- 4 files changed, 201 insertions(+), 60 deletions(-) diff --git a/Functions/IPAM/Prefix/Get-NetboxIPAMPrefix.ps1 b/Functions/IPAM/Prefix/Get-NetboxIPAMPrefix.ps1 index c6668d7..a554fb6 100644 --- a/Functions/IPAM/Prefix/Get-NetboxIPAMPrefix.ps1 +++ b/Functions/IPAM/Prefix/Get-NetboxIPAMPrefix.ps1 @@ -96,67 +96,110 @@ function Get-NetboxIPAMPrefix { [CmdletBinding(DefaultParameterSetName = 'Query')] param ( + [Parameter(ParameterSetName = 'Query', + Position = 0)] [string]$Prefix, + [Parameter(ParameterSetName = 'Query')] [string]$Query, - [uint16[]]$Id, + [Parameter(ParameterSetName = 'ByID')] + [uint32[]]$Id, + [Parameter(ParameterSetName = 'Query')] [object]$Family, + [Parameter(ParameterSetName = 'Query')] [boolean]$Is_Pool, + [Parameter(ParameterSetName = 'Query')] [string]$Within, + [Parameter(ParameterSetName = 'Query')] [string]$Within_Include, + [Parameter(ParameterSetName = 'Query')] [string]$Contains, + [Parameter(ParameterSetName = 'Query')] [ValidateRange(0, 127)] [byte]$Mask_Length, + [Parameter(ParameterSetName = 'Query')] [string]$VRF, - [uint16]$VRF_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$VRF_Id, + [Parameter(ParameterSetName = 'Query')] [string]$Tenant, - [uint16]$Tenant_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Tenant_Id, + [Parameter(ParameterSetName = 'Query')] [string]$Site, - [uint16]$Site_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Site_Id, + [Parameter(ParameterSetName = 'Query')] [string]$Vlan_VId, - [uint16]$Vlan_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Vlan_Id, + [Parameter(ParameterSetName = 'Query')] [object]$Status, + [Parameter(ParameterSetName = 'Query')] [string]$Role, - [uint16]$Role_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Role_Id, + [Parameter(ParameterSetName = 'Query')] [uint16]$Limit, + [Parameter(ParameterSetName = 'Query')] [uint16]$Offset, [switch]$Raw ) - if ($null -ne $Family) { - $PSBoundParameters.Family = ValidateIPAMChoice -ProvidedValue $Family -PrefixFamily + # if ($null -ne $Family) { + # $PSBoundParameters.Family = ValidateIPAMChoice -ProvidedValue $Family -PrefixFamily + # } + # + # if ($null -ne $Status) { + # $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus + # } + + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($Prefix_ID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes', $Prefix_ID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + } + + break + } + + default { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + + break + } } - - if ($null -ne $Status) { - $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus - } - - $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) - - $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters - - $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters - - InvokeNetboxRequest -URI $uri -Raw:$Raw } \ No newline at end of file diff --git a/Functions/IPAM/Role/Get-NetboxIPAMRole.ps1 b/Functions/IPAM/Role/Get-NetboxIPAMRole.ps1 index 1d4cc03..e7066c0 100644 --- a/Functions/IPAM/Role/Get-NetboxIPAMRole.ps1 +++ b/Functions/IPAM/Role/Get-NetboxIPAMRole.ps1 @@ -41,28 +41,56 @@ function Get-NetboxIPAMRole { [CmdletBinding()] param ( + [Parameter(ParameterSetName = 'Query', + Position = 0)] [string]$Name, + [Parameter(ParameterSetName = 'Query')] [string]$Query, - [uint16[]]$Id, + [Parameter(ParameterSetName = 'ByID')] + [uint32[]]$Id, + [Parameter(ParameterSetName = 'Query')] [string]$Slug, + [Parameter(ParameterSetName = 'Query')] [switch]$Brief, + [Parameter(ParameterSetName = 'Query')] [uint16]$Limit, + [Parameter(ParameterSetName = 'Query')] [uint16]$Offset, [switch]$Raw ) - $Segments = [System.Collections.ArrayList]::new(@('ipam', 'roles')) - - $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters - - $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters - - InvokeNetboxRequest -URI $uri -Raw:$Raw + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($Role_ID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'roles', $Role_ID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + } + + break + } + + default { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'roles')) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + + break + } + } } \ No newline at end of file diff --git a/Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1 b/Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1 index b85f545..050d5bc 100644 --- a/Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1 +++ b/Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1 @@ -16,56 +16,96 @@ function Get-NetboxIPAMVLAN { [CmdletBinding()] param ( + [Parameter(ParameterSetName = 'Query', + Position = 0)] + [ValidateRange(1, 4096)] [uint16]$VID, - [uint16[]]$Id, + [Parameter(ParameterSetName = 'ByID')] + [uint32[]]$Id, + [Parameter(ParameterSetName = 'Query')] [string]$Query, + [Parameter(ParameterSetName = 'Query')] [string]$Name, + [Parameter(ParameterSetName = 'Query')] [string]$Tenant, - [uint16]$Tenant_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Tenant_Id, + [Parameter(ParameterSetName = 'Query')] [string]$TenantGroup, - [uint16]$TenantGroup_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$TenantGroup_Id, + [Parameter(ParameterSetName = 'Query')] [object]$Status, + [Parameter(ParameterSetName = 'Query')] [string]$Region, + [Parameter(ParameterSetName = 'Query')] [string]$Site, - [uint16]$Site_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Site_Id, + [Parameter(ParameterSetName = 'Query')] [string]$Group, - [uint16]$Group_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Group_Id, + [Parameter(ParameterSetName = 'Query')] [string]$Role, - [uint16]$Role_Id, + [Parameter(ParameterSetName = 'Query')] + [uint32]$Role_Id, + [Parameter(ParameterSetName = 'Query')] [uint16]$Limit, + [Parameter(ParameterSetName = 'Query')] [uint16]$Offset, [switch]$Raw ) - if ($null -ne $Status) { - $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -VLANStatus + # if ($null -ne $Status) { + # $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -VLANStatus + # } + + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($VLAN_ID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans', $VLAN_ID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + } + + break + } + + default { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans')) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + + break + } } - - $Segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans')) - - $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters - - $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters - - InvokeNetboxRequest -URI $uri -Raw:$Raw } diff --git a/Functions/Tenancy/Get-NetboxTenant.ps1 b/Functions/Tenancy/Get-NetboxTenant.ps1 index 60d0373..85d110c 100644 --- a/Functions/Tenancy/Get-NetboxTenant.ps1 +++ b/Functions/Tenancy/Get-NetboxTenant.ps1 @@ -23,17 +23,17 @@ function Get-NetboxTenant { .PARAMETER Name The specific name of the tenant. Must match exactly as is defined in Netbox - .PARAMETER Slug - The specific slug of the tenant. Must match exactly as is defined in Netbox - .PARAMETER Id The database ID of the tenant .PARAMETER Query A standard search query that will match one or more tenants. + .PARAMETER Slug + The specific slug of the tenant. Must match exactly as is defined in Netbox + .PARAMETER Group - The specific group as defined in Netbox. + The specific group as defined in Netbox. .PARAMETER GroupID The database ID of the group in Netbox @@ -51,41 +51,71 @@ function Get-NetboxTenant { Return the unparsed data from the HTTP request .EXAMPLE - PS C:\> Get-NetboxTenant + PS C:\> Get-NetboxTenant .NOTES Additional information about the function. #> - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Query')] param ( + [Parameter(ParameterSetName = 'Query', + Position = 0)] [string]$Name, - [string]$Slug, - - [uint16[]]$Id, + [Parameter(ParameterSetName = 'ByID')] + [uint32[]]$Id, + [Parameter(ParameterSetName = 'Query')] [string]$Query, + [Parameter(ParameterSetName = 'Query')] + [string]$Slug, + + [Parameter(ParameterSetName = 'Query')] [string]$Group, + [Parameter(ParameterSetName = 'Query')] [uint16]$GroupID, + [Parameter(ParameterSetName = 'Query')] [hashtable]$CustomFields, + [Parameter(ParameterSetName = 'Query')] [uint16]$Limit, + [Parameter(ParameterSetName = 'Query')] [uint16]$Offset, [switch]$Raw ) - $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants')) - - $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters - - $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters - - InvokeNetboxRequest -URI $uri -Raw:$Raw + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($Tenant_ID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants', $Tenant_ID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + } + + break + } + + default { + $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants')) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + + break + } + } } \ No newline at end of file