public/Get-Usage.ps1
|
function Get-ZStackUsage { # .SYNOPSIS # Returns capacity and sampled host load from the selected cloud. # .DESCRIPTION # ZStack PowerCLI (ZStack.PowerCLI 1.1.4) uses CLI 1.1.4, commit dca13b1d898c80c0f7189b5adb4a5537685ad4a6. # Connection configuration namespace: zstack-powershell. # .LINK # https://github.com/terraform-zstack-modules/zstack-cli-go/blob/main/docs/powershell.md [CmdletBinding()] param( [ValidateSet('All', 'Capacity', 'Load')] [string] $Section = 'All', [ValidateRange(1, 3600)] [int] $TimeoutSeconds = 120, [object] $Server ) $connection = Get-CloudConnection $Server $null = Get-CloudCliPath 'usageV1' $result = Invoke-CloudConnectedCli $connection @('monitor', 'usage', '--section', $Section.ToLowerInvariant(), '-o', 'json') $TimeoutSeconds $report = Convert-CliResult $result 'Usage' -PreserveDates -Target $connection.Id $capacity = $null $load = $null if ($Section -ne 'Load') { if ($report['capacity'] -isnot [array]) { throw (New-CloudError -Operation Usage -Failure InvalidCapacity -Target $connection.Id) } $capacity = @(foreach ($row in $report['capacity']) { Convert-CloudCapacityRow $row $connection }) } if ($Section -ne 'Capacity') { $load = Convert-CloudUsageLoad $report['load'] $connection } [pscustomobject] @{ PSTypeName = 'Cloud.Automation.Usage' Server = $connection.Endpoint; ConnectionId = $connection.Id Capacity = $capacity; Load = $load } } |