Public/vm-group.ps1

function Get-CSVMGroup {
    <#
    .SYNOPSIS
        Lists VM groups (CloudStack instance groups).

    .DESCRIPTION
        Wraps listInstanceGroups. A VM's group is shown in its 'group' and
        'groupid' properties; put a VM in a group with
        Set-CSVMProperty -UpdateParams @{ group = '<group name>' } or
        New-CSVM -Group.

    .PARAMETER Name
        Filter by group name

    .PARAMETER Id
        Filter by group ID

    .PARAMETER Keyword
        Filter by keyword

    .PARAMETER Account
        Filter by account name. Must be used with -DomainId.

    .PARAMETER DomainId
        Filter by domain ID

    .PARAMETER ProjectId
        Filter by project ID (-1 for all projects)

    .PARAMETER IsRecursive
        With -DomainId, also include groups in subdomains

    .PARAMETER ListAll
        List every group the caller is allowed to see

    .PARAMETER Page
        Page number of results to return

    .PARAMETER PageSize
        Number of results per page

    .EXAMPLE
        Get-CSVMGroup
        Lists your VM groups.

    .EXAMPLE
        $group = Get-CSVMGroup -Name 'web-tier'
        Get-CSVM -ListAll | Where-Object groupid -eq $group.id
        Lists the VMs in a group.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Position = 0)]
        [string]$Name,

        [string]$Id,

        [string]$Keyword,

        [string]$Account,

        [string]$DomainId,

        [string]$ProjectId,

        [switch]$IsRecursive,

        [switch]$ListAll,

        [int]$Page,

        [int]$PageSize
    )

    if ($PSBoundParameters.ContainsKey('Account') -and -not $PSBoundParameters.ContainsKey('DomainId')) {
        throw 'DomainId is required when Account is specified.'
    }
    $apiParams = @{}
    Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
        Name = 'name'; Id = 'id'; Keyword = 'keyword'; Account = 'account'; DomainId = 'domainid'
        ProjectId = 'projectid'; IsRecursive = 'isrecursive'; ListAll = 'listall'; Page = 'page'; PageSize = 'pagesize'
    })
    ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listInstanceGroups' -Parameters $apiParams) -Command 'listInstanceGroups'
}

function New-CSVMGroup {
    <#
    .SYNOPSIS
        Creates a VM group (CloudStack instance group).

    .DESCRIPTION
        Wraps createInstanceGroup. Groups are a label for organising VMs; they do
        not affect placement (use affinity groups for that).

    .PARAMETER Name
        The name of the group

    .PARAMETER Account
        Account that will own the group. Must be used with -DomainId.

    .PARAMETER DomainId
        Domain of the owning account

    .PARAMETER ProjectId
        Project that will own the group

    .EXAMPLE
        New-CSVMGroup -Name 'web-tier'
        Creates a group.

    .EXAMPLE
        New-CSVMGroup -Name 'web-tier'
        Get-CSVM -Keyword 'web-' | Set-CSVMProperty -UpdateParams @{ group = 'web-tier' }
        Creates a group and puts every web VM in it.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Name,

        [string]$Account,

        [string]$DomainId,

        [string]$ProjectId
    )

    if ($PSBoundParameters.ContainsKey('Account') -and -not $PSBoundParameters.ContainsKey('DomainId')) {
        throw 'DomainId is required when Account is specified.'
    }
    $apiParams = @{ name = $Name }
    Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
        Account = 'account'; DomainId = 'domainid'; ProjectId = 'projectid'
    })
    if ($PSCmdlet.ShouldProcess("VM group $Name", 'Create')) {
        ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'createInstanceGroup' -Parameters $apiParams) -Command 'createInstanceGroup'
    }
}

function Set-CSVMGroup {
    <#
    .SYNOPSIS
        Renames a VM group (CloudStack instance group).

    .DESCRIPTION
        Wraps updateInstanceGroup. The VMs stay in the group. Accepts group objects
        from Get-CSVMGroup on the pipeline.

    .PARAMETER Id
        The ID of the group (binds from a piped group's id)

    .PARAMETER Name
        The new name. Never bound from the pipeline, because a piped group's own
        name is its current name.

    .EXAMPLE
        Set-CSVMGroup -Id group-uuid -Name 'web-tier-prod'
        Renames a group.

    .EXAMPLE
        Get-CSVMGroup -Name 'web-tier' | Set-CSVMGroup -Name 'web-tier-prod'
        Renames a group found by name.
    #>

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

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

    process {
        if ($PSCmdlet.ShouldProcess("VM group $Id", "Rename to $Name")) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'updateInstanceGroup' -Parameters @{ id = $Id; name = $Name }) -Command 'updateInstanceGroup'
        }
    }
}

function Remove-CSVMGroup {
    <#
    .SYNOPSIS
        Deletes a VM group (CloudStack instance group).

    .DESCRIPTION
        Wraps deleteInstanceGroup. Only the group is deleted; its VMs are left
        running, just without a group. Accepts group objects from Get-CSVMGroup on
        the pipeline.

    .PARAMETER Id
        The ID of the group to delete (binds from a piped group's id)

    .EXAMPLE
        Remove-CSVMGroup -Id group-uuid
        Deletes a group after prompting for confirmation.

    .EXAMPLE
        Get-CSVMGroup -Keyword 'test-' | Remove-CSVMGroup -Confirm:$false
        Deletes every group whose name contains 'test-'.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$Id
    )

    process {
        if ($PSCmdlet.ShouldProcess("VM group $Id", 'Delete')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'deleteInstanceGroup' -Parameters @{ id = $Id }) -Command 'deleteInstanceGroup'
        }
    }
}