Public/resource-icon.ps1

# Resource icons: the small images shown in the UI for resources such as
# instances, templates, networks, and zones.

function Get-CSResourceIcon {
    <#
    .SYNOPSIS
        Lists the icons set on one or more resources.

    .DESCRIPTION
        Wraps listResourceIcon. -ResourceType is the kind of resource (for example
        UserVm, Template, Iso, Network, Domain, Zone); -ResourceIds are the resources
        to read. Accepts objects with an id on the pipeline, so
        'Get-CSTemplate -Name web-golden | Get-CSResourceIcon -ResourceType Template'
        works.

    .PARAMETER ResourceIds
        The resource IDs to read icons for. Binds from a piped object's id.

    .PARAMETER ResourceType
        The resource type (for example UserVm, Template, Iso, Volume, Snapshot,
        Network, Domain, Zone)

    .EXAMPLE
        Get-CSResourceIcon -ResourceIds $vmId -ResourceType UserVm
        Gets the icon set on an instance.

    .EXAMPLE
        Get-CSTemplate -Name 'web-golden' | Get-CSResourceIcon -ResourceType Template
        Reads the icon of a template piped in by object.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id', 'ResourceId')]
        [string[]]$ResourceIds,

        [Parameter(Mandatory = $true)]
        [string]$ResourceType
    )

    process {
        $apiParams = @{ resourceids = ($ResourceIds -join ','); resourcetype = $ResourceType }
        ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listResourceIcon' -Parameters $apiParams) -Command 'listResourceIcon'
    }
}

function Set-CSResourceIcon {
    <#
    .SYNOPSIS
        Uploads an icon for one or more resources.

    .DESCRIPTION
        Wraps uploadResourceIcon, setting a base64-encoded image as the icon for the
        given resources (replacing any existing icon). Accepts objects with an id on
        the pipeline.

    .PARAMETER ResourceIds
        The resource IDs to set the icon on. Binds from a piped object's id.

    .PARAMETER ResourceType
        The resource type (for example UserVm, Template, Iso, Network, Domain, Zone)

    .PARAMETER Base64Image
        The icon as a base64-encoded image (a data URI or raw base64)

    .EXAMPLE
        $img = [Convert]::ToBase64String([IO.File]::ReadAllBytes('./icon.png'))
        Set-CSResourceIcon -ResourceIds $vmId -ResourceType UserVm -Base64Image $img
        Sets an instance's icon from a PNG file.

    .EXAMPLE
        Get-CSTemplate -Name 'web-golden' | Set-CSResourceIcon -ResourceType Template -Base64Image $img
        Sets the icon of a template piped in by object.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id', 'ResourceId')]
        [string[]]$ResourceIds,

        [Parameter(Mandatory = $true)]
        [string]$ResourceType,

        [Parameter(Mandatory = $true)]
        [string]$Base64Image
    )

    process {
        $apiParams = @{ resourceids = ($ResourceIds -join ','); resourcetype = $ResourceType; base64image = $Base64Image }
        if ($PSCmdlet.ShouldProcess("$ResourceType $($ResourceIds -join ',')", 'Set icon')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'uploadResourceIcon' -Parameters $apiParams) -Command 'uploadResourceIcon'
        }
    }
}

function Remove-CSResourceIcon {
    <#
    .SYNOPSIS
        Removes the icon from one or more resources.

    .DESCRIPTION
        Wraps deleteResourceIcon. Accepts objects with an id on the pipeline.

    .PARAMETER ResourceIds
        The resource IDs to clear the icon from. Binds from a piped object's id.

    .PARAMETER ResourceType
        The resource type (for example UserVm, Template, Iso, Network, Domain, Zone)

    .EXAMPLE
        Remove-CSResourceIcon -ResourceIds $vmId -ResourceType UserVm
        Removes an instance's icon.

    .EXAMPLE
        Get-CSTemplate -Name 'web-golden' | Remove-CSResourceIcon -ResourceType Template
        Clears the icon of a template piped in by object.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id', 'ResourceId')]
        [string[]]$ResourceIds,

        [Parameter(Mandatory = $true)]
        [string]$ResourceType
    )

    process {
        $apiParams = @{ resourceids = ($ResourceIds -join ','); resourcetype = $ResourceType }
        if ($PSCmdlet.ShouldProcess("$ResourceType $($ResourceIds -join ',')", 'Remove icon')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'deleteResourceIcon' -Parameters $apiParams) -Command 'deleteResourceIcon'
        }
    }
}