Public/Get-DuneSubnet.ps1

<#
.SYNOPSIS
Retrieve network subnets.

.DESCRIPTION
Gets subnets and supports filtering by name, id, collection, deployment, resource, ip address, resource provider, network address, external id, resource group, display name, IP version, and include-deleted flag. Returns `DuneSubnet` objects by default; use `-Raw` for raw API output.

.PARAMETER Name
Filter subnets by name (supports wildcards). Position 0 in the default parameter set.

.PARAMETER Id
The GUID of a subnet. Use the `Id` parameter set to retrieve a specific subnet.

.PARAMETER Collection
A `DuneCollection` object; returns subnets in the supplied collection (pipeline input supported).

.PARAMETER Deployment
A `DuneDeployment` object; returns subnets for the supplied deployment (pipeline input supported).

.PARAMETER Resource
A `DuneResource` object; returns the subnet corresponding to the supplied resource (pipeline input supported).

.PARAMETER IpAddress
A `DuneIpAddress` object; returns the subnet containing the provided IP address (pipeline input supported).

.PARAMETER ResourceProvider
A `DuneResourceProvider` object; filters subnets by resource provider (pipeline input supported).

.PARAMETER NetworkAddress
Filter by network address (supports wildcards).

.PARAMETER ExtId
Filter by external identifier.

.PARAMETER ResourceGroup
A `DuneResourceGroup` object; returns subnets in the supplied resource group (pipeline input supported).

.PARAMETER DisplayName
Filter by display name (supports wildcards).

.PARAMETER Version
IP version to filter by: `v4` or `v6`.

.PARAMETER Raw
If set, returns raw API objects instead of `DuneSubnet` objects.

.PARAMETER IncludeDeleted
Include deleted subnets in results.

.EXAMPLE
PS> Get-DuneSubnet
Returns all subnets.

.EXAMPLE
PS> Get-DuneSubnet -Id 3d8f6b5a-...
Returns the subnet with the specified `Id`.

.EXAMPLE
PS> Get-DuneResource -Name "subnet-resource" | Get-DuneSubnet
Pipeline example using the `Resource` parameter set.
#>

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
    }
}