Public/cloud-datastore.ps1

function Enable-CloudDatastore {
    <#
    .SYNOPSIS
        Enables a datastore on the Cloud Server.
     
    .DESCRIPTION
        Enables the specified datastore. Automatically handles token refresh.
            
    .PARAMETER ID
        Required. Filter by Datastore ID
     
    .EXAMPLE
        # Enable a specific datastore
        Enable-CloudDatastore -ID 5
    #>

    
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [string]$Name,
        
        [Parameter(Mandatory = $false)]
        [Nullable[int]]$ID
    )

        $enablejson = @{
            enable = $true
        }
    $uri = "$($script:CloudConnection.BaseUri)/manifold-api/v2/cloud/datastore/${ID}/enable"
    $response = Invoke-CloudApiRequest -Uri $Uri -Method PATCH -Body $enablejson
    return $response

}

function Get-CloudDatastore {
    <#
    .SYNOPSIS
        Gets Datastores from the Cloud Server.
     
    .DESCRIPTION
        Retrieves a list of Datastores. Automatically handles token refresh.
         
    .PARAMETER Name
        Optional. Filter by Datastore name
    
    .PARAMETER ID
        Optional. Filter by Datastore ID
     
    .PARAMETER Graph
        Optional. Display datastore capacity with visual percentage graphs
     
    .PARAMETER GB
        Display storage in Gigabytes (default)
     
    .PARAMETER TB
        Display storage in Terabytes
     
    .EXAMPLE
        # Get all datastores
        Get-CloudDatastore
         
    .EXAMPLE
        # Get a specific datastore
        Get-CloudDatastore -ID 5
         
    .EXAMPLE
        # Get the usage of all datastores and graph them
        Get-CloudDatastore -Graph
         
    .EXAMPLE
        # Get the usage of a datastore by name and graph it
        Get-CloudDatastore -Name "default" -Graph
         
    .EXAMPLE
        # Get the usage of all datastores in TB and graph them
        Get-CloudDatastore -Graph -TB
    #>

    
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [string]$Name,
        
        [Parameter(Mandatory = $false)]
        [int]$ID,
        
        [Parameter(Mandatory = $false)]
        [switch]$Graph,
        
        [Parameter(Mandatory = $false)]
        [switch]$GB,
        
        [Parameter(Mandatory = $false)]
        [switch]$TB
    )
    
    $uri = "$($script:CloudConnection.BaseUri)/manifold-api/v2/cloud/datastore"
    $response = Invoke-CloudApiRequest -Uri $uri -Method Get
    
    $Datastores = $response.datastore
    
    # Apply filters
    if ($PSBoundParameters.ContainsKey('ID')) {
        $Datastores = $Datastores | Where-Object {($_.id -eq $ID) -and ($null -ne $_.id)}
    }
    if ($Name) {
        $Datastores = $Datastores | Where-Object {($_.name -match $Name)}
    }
    
    # Handle graphing if requested
    if ($Graph) {
        # Determine target unit and conversion factor
        $targetUnit = "GB"  # Default to GB
        $conversionFactors = @{
            'GB' = @{ fromMB = 1/1024; label = 'GB'; decimals = 2; format = 'N2' }
            'TB' = @{ fromMB = 1/1048576; label = 'TB'; decimals = 4; format = 'N4' }
        }
        
        if ($TB) { $targetUnit = 'TB' }
        
        $unitConfig = $conversionFactors[$targetUnit]
        
        Write-Host ("=" * 70)
        
        foreach ($ds in $Datastores) {
            # Skip if no capacity info
            if (-not $ds.total_mb -or $ds.total_mb -eq 0) {
                continue
            }
            
            # Calculate percentage
            $percent = if ($ds.total_mb -gt 0) {
                [math]::Round(($ds.used_mb / $ds.total_mb) * 100)
            } else { 0 }
            
            # Convert values from MB to target unit
            $usedConverted = [math]::Round($ds.used_mb * $unitConfig.fromMB, $unitConfig.decimals)
            $totalConverted = [math]::Round($ds.total_mb * $unitConfig.fromMB, $unitConfig.decimals)
            $freeConverted = [math]::Round($ds.free_mb * $unitConfig.fromMB, $unitConfig.decimals)
            
            # Format the datastore info
            Write-Host "`nDatastore: $($ds.name) (ID: $($ds.id))" -ForegroundColor Cyan
            Write-Host " Type: $($ds.type) | State: $($ds.state)" -ForegroundColor Gray
            
            # Show capacity graph
            Write-Host " capacity " -NoNewline
            Show-PercentageGraph -percent $percent
            
            # Format numbers
            $formatString = $unitConfig.format
            $usedFormatted = "{0:$formatString}" -f $usedConverted
            $totalFormatted = "{0:$formatString}" -f $totalConverted
            $freeFormatted = "{0:$formatString}" -f $freeConverted
            
            Write-Host (" - {0} / {1} {2} ({3} free)" -f $usedFormatted, $totalFormatted, $unitConfig.label, $freeFormatted)
            
            # Show additional info
            if ($ds.images -and $ds.images.Count -gt 0) {
                Write-Host " Images: $($ds.images.Count)" -ForegroundColor Gray
            }
        }
        
        Write-Host ""
    }
    else {
        # Return normal object output
        return $Datastores
    }
}