Public/Get-DuneSubnet.ps1
|
function Get-DuneSubnet { [CmdletBinding(DefaultParameterSetName = "Default")] param ( [Parameter(Position = 0)] [string]$Name, [Parameter(ParameterSetName = "Id")] [guid]$Id, [Parameter(ParameterSetName = "Collection", ValueFromPipeline)] [DuneCollection]$Collection, [Parameter(ParameterSetName = "Deployment", ValueFromPipeline)] [DuneDeployment]$Deployment, [Parameter(ParameterSetName = "Resource", ValueFromPipeline)] [DuneResource]$Resource, [Parameter(ParameterSetName = "IpAddress", ValueFromPipeline)] [DuneIpAddress]$IpAddress, [Parameter(ParameterSetName = "ResourceProvider", ValueFromPipeline)] [DuneResourceProvider]$ResourceProvider, [Parameter()] [string]$NetworkAddress, [Parameter(ParameterSetName = "ExtId")] [string]$ExtId, [Parameter(ParameterSetName = "ResourceGroup", ValueFromPipeline)] [DuneResourceGroup]$ResourceGroup, [Parameter()] [string]$DisplayName, [Parameter()][ValidateSet("v4", "v6")] [string]$Version, [Parameter()] [switch]$Raw, [Parameter()] [switch]$IncludeDeleted ) begin { Write-Debug "$($MyInvocation.MyCommand)|begin" $ReturnObjects = @() $ProcessedUrls = @() $BaseUri = "subnets" $Method = "GET" } process { Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)" # Build Uri $Uri = switch ($PSCmdlet.ParameterSetName) { 'Id' { '{0}/{1}' -f $BaseUri, $Id } 'ResourceProvider' { '{0}?ResourceProviderId={1}' -f $BaseUri, $ResourceProvider.Id } 'Collection' { '{0}?CollectionId={1}' -f $BaseUri, $Collection.Id } 'Deployment' { '{0}?DeploymentId={1}' -f $BaseUri, $Deployment.Id } 'ResourceGroup' { '{0}?ParentId={1}' -f $BaseUri, $ResourceGroup.Id } # 'NetworkAddress' { '{0}?NetworkAddress={1}' -f $BaseUri, $NetworkAddress } 'ExtId' { '{0}?ExtIdIEquals={1}' -f $BaseUri, $ExtId } 'IpAddress' { '{0}/{1}' -f $BaseUri, $IpAddress.SubnetId } 'Resource' { if ($Resource.ObjectType -eq 'Subnet') { '{0}/{1}' -f $BaseUri, $Resource.Id } else { Write-Warning "Wrong ObjectType $($Resource.ObjectType) for $($Resource.Name) (id: $($Resource.Id))" return } } Default { $BaseUri } } if ($Name) { $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards } if ($DisplayName) { $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards } if ($NetworkAddress) { $Uri = $Uri | Add-UriQueryParam "NetworkAddressLike=$NetworkAddress" -ConvertWildcards } if ($Version) { $Uri = $Uri | Add-UriQueryParam "Version=$Version" } if ($IncludeDeleted) { $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1" } $Uri = $Uri | Add-UriQueryParam "IncludeReferencedObjects=1" # ApiCall Cache if ($ProcessedUrls -notcontains $Uri) { try { # ApiCall and Object conversion $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method $ProcessedUrls += $Uri $Results = if ($Response.Content) { $Response.Content | ConvertFrom-Json } if ($PSCmdlet.ParameterSetName -notin 'Id', 'IpAddress', 'Resource') { $Results = $Results.Items } # list endpoint returns content in items property $ReturnObjects += $Results | ForEach-Object { if ($Raw) { $_ } else { ConvertTo-DuneClassObject -Class DuneSubnet -InputObject $_ #-AbstractClass "Deployment" } } } catch { throw $_ } } else { Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked" } } end { Write-Debug "$($MyInvocation.MyCommand)|end" return $ReturnObjects | Sort-Object -Unique Id | Sort-Object NetworkAddress } } |