Public/system-capacity.ps1

# System capacity: how much CPU, memory, storage, IPs, and so on the
# infrastructure has versus how much is already allocated.

function Get-CSCapacity {
    <#
    .SYNOPSIS
        Lists infrastructure capacity and how much of it is used.

    .DESCRIPTION
        Wraps listCapacity. Each row reports the total and used capacity of one
        resource type in one scope (zone, pod, cluster, or host), so it answers
        'how much room is left'. Filter by scope with -ZoneId, -PodId, -ClusterId,
        or -HostId, and by resource with -Type. Zone objects can be piped in.

    .PARAMETER Type
        Only this capacity type, as CloudStack's numeric capacity-type ID (for
        example 0 = memory, 1 = CPU, 2 = primary storage, 3 = storage allocated,
        6 = secondary storage; the exact set depends on the CloudStack version).
        Omit for all types.

    .PARAMETER ZoneId
        Only capacity in this zone

    .PARAMETER PodId
        Only capacity in this pod

    .PARAMETER ClusterId
        Only capacity in this cluster

    .PARAMETER HostId
        Only capacity on this host

    .PARAMETER SortBy
        Order the results by 'usage' (percent used) or 'reservedcapacity'

    .PARAMETER FetchLatest
        Recompute capacity now instead of returning the last cached figures

    .PARAMETER Keyword
        Filter by keyword

    .PARAMETER Page
        Page number of results to return

    .PARAMETER PageSize
        Number of results per page

    .EXAMPLE
        Get-CSCapacity
        Lists capacity across the whole cloud, by type and scope.

    .EXAMPLE
        Get-CSCapacity -ZoneId zone-uuid -SortBy usage
        Shows a zone's capacity, busiest resource first.

    .EXAMPLE
        Get-CSCapacity -Type 1 -FetchLatest | Select-Object zonename, capacitytotal, capacityused, percentused
        Gets freshly recomputed CPU capacity per zone.

    .EXAMPLE
        Get-CSZone | Get-CSCapacity -Type 0
        Reports memory capacity for every zone.
    #>

    [CmdletBinding()]
    param(
        [int]$Type,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias('Id')]
        [string]$ZoneId,

        [string]$PodId,

        [string]$ClusterId,

        [string]$HostId,

        [ValidateSet('usage', 'reservedcapacity')]
        [string]$SortBy,

        [switch]$FetchLatest,

        [string]$Keyword,

        [int]$Page,

        [int]$PageSize
    )

    process {
        $apiParams = @{}
        Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
            Type = 'type'; ZoneId = 'zoneid'; PodId = 'podid'; ClusterId = 'clusterid'; HostId = 'hostid'
            SortBy = 'sortby'; FetchLatest = 'fetchlatest'; Keyword = 'keyword'; Page = 'page'; PageSize = 'pagesize'
        })
        ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listCapacity' -Parameters $apiParams) -Command 'listCapacity'
    }
}