Public/host.ps1

function Get-CSHost {
    <#
    .SYNOPSIS
        Lists hosts in CloudStack.
    
    .DESCRIPTION
        Retrieves a list of hosts with optional filtering by ID, name,
        zone, state, or other parameters.
    
    .PARAMETER Id
        Filter by host ID
    
    .PARAMETER Name
        Filter by host name (exact match)
    
    .PARAMETER Keyword
        Filter by keyword (partial match on name)
    
    .PARAMETER ZoneId
        Filter by zone ID
    
    .PARAMETER State
        Filter by host state (Running, Stopped, etc.)

    .PARAMETER type
        Filter by host type. Defaults to 'routing' (hypervisor hosts).

    .PARAMETER DomainId
        Filter by domain ID

    .PARAMETER ListAll
        List all hosts (requires appropriate permissions)
    
    .EXAMPLE
        Get-CSHost
        Lists all hosts in your account
    
    .EXAMPLE
        Get-CSHost -Name "web-server-01"
        Gets a specific host by name
    
    .EXAMPLE
        Get-CSHost -State Running
        Lists all running hosts
    
    .EXAMPLE
        Get-CSHost -Keyword "web"
        Lists all hosts with "web" in their name
    #>

    [CmdletBinding(DefaultParameterSetName='Default')]
    param(
        [Parameter(ParameterSetName='ById')]
        [string]$Id,
        
        [Parameter(ParameterSetName='ByName')]
        [string]$Name,
        
        [Parameter(ParameterSetName='Default')]
        [string]$Keyword,
        
        [Parameter(ParameterSetName='Default')]
        [string]$ZoneId,
        
        [Parameter(ParameterSetName='Default')]
        [ValidateSet('Running', 'Stopped', 'Starting', 'Stopping', 'Present', 'Destroyed', 'Expunging', 'Migrating', 'Error', 'Unknown', 'Shutdowned')]
        [string]$State,
        
        [Parameter(ParameterSetName='Default')]
        [string]$type = "routing",
        
        [Parameter(ParameterSetName='Default')]
        [string]$DomainId,
        
        [Parameter(ParameterSetName='Default')]
        [switch]$ListAll
    )
    
    # Build parameters
    $apiParams = @{}
    
    if ($PSBoundParameters.ContainsKey('Id')) {
        $apiParams['id'] = $Id
    }
    
    if ($PSBoundParameters.ContainsKey('Name')) {
        $apiParams['name'] = $Name
    }
    
    if ($PSBoundParameters.ContainsKey('Keyword')) {
        $apiParams['keyword'] = $Keyword
    }
    
    if ($PSBoundParameters.ContainsKey('ZoneId')) {
        $apiParams['zoneid'] = $ZoneId
    }
    
    if ($PSBoundParameters.ContainsKey('State')) {
        $apiParams['state'] = $State
    }
    
    if ($PSBoundParameters.ContainsKey('Type')) {
        $apiParams['type'] = $Type
    }
     
    if ($ListAll) {
        $apiParams['listall'] = 'true'
    }
    
    # Make the API call
    $response = Invoke-CSApiRequest -Command 'listHosts' -Parameters $apiParams
    write-verbose $apiParams
    # Return the host list
    if ($response.listHostsresponse.Host) {
        return $response.listHostsresponse.Host
    }
    else {
        Write-Verbose "No hosts found matching the criteria."
        return $null
    }
}

function New-CSHost {
    <#
    .SYNOPSIS
        Adds a host to CloudStack.

    .DESCRIPTION
        Wraps CloudStack's addHost API. A pod, zone, hypervisor type, and host
        URL are required. For hypervisors other than VMware, CloudStack also
        requires the host credentials. Target the cluster with either -ClusterId
        or -ClusterName, not both.

    .PARAMETER Hypervisor
        The host's hypervisor type (for example KVM, VMware, XenServer)

    .PARAMETER PodId
        The pod to add the host to

    .PARAMETER Url
        The host connection URL (for example http://kvm-01.example.com)

    .PARAMETER ZoneId
        The zone to add the host to

    .PARAMETER AllocationState
        Initial allocation state: Enable or Disable

    .PARAMETER ClusterId
        The cluster to add the host to. Mutually exclusive with -ClusterName.

    .PARAMETER ClusterName
        The cluster to add the host to, by name. Mutually exclusive with -ClusterId.

    .PARAMETER HostTags
        Host tags to assign, as an array (used by offerings to pin placement)

    .PARAMETER Password
        The host's connection password. Supply from a secure source, not a literal.

    .PARAMETER UserName
        The host's connection username

    .EXAMPLE
        New-CSHost -Hypervisor KVM -ZoneId 'zone-uuid' -PodId 'pod-uuid' -ClusterId 'cluster-uuid' -Url 'http://kvm-01.example.com' -UserName root -Password 'use-a-secret-store'

        Adds a KVM host to an existing cluster.

    .EXAMPLE
        New-CSHost -Hypervisor VMware -ZoneId $zoneId -PodId $podId -ClusterName 'vmware-cluster-1' -Url 'http://vcenter.example.com/dc/cluster'

        Adds a VMware host by cluster name (no host credentials needed for VMware).
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$Hypervisor,
        [Parameter(Mandatory=$true)][string]$PodId,
        [Parameter(Mandatory=$true)][string]$Url,
        [Parameter(Mandatory=$true)][string]$ZoneId,
        [ValidateSet('Enable', 'Disable')][string]$AllocationState,
        [string]$ClusterId,
        [string]$ClusterName,
        [string[]]$HostTags,
        [string]$Password,
        [string]$UserName
    )
    if ($PSBoundParameters.ContainsKey('ClusterId') -and $PSBoundParameters.ContainsKey('ClusterName')) { throw 'Specify either ClusterId or ClusterName, not both.' }
    $apiParams = @{ hypervisor = $Hypervisor; podid = $PodId; url = $Url; zoneid = $ZoneId }
    $parameterMap = @{ AllocationState = 'allocationstate'; ClusterId = 'clusterid'; ClusterName = 'clustername'; Password = 'password'; UserName = 'username' }
    foreach ($parameter in $parameterMap.Keys) { if ($PSBoundParameters.ContainsKey($parameter)) { $apiParams[$parameterMap[$parameter]] = (Get-Variable -Name $parameter -ValueOnly) } }
    if ($PSBoundParameters.ContainsKey('HostTags')) { $apiParams['hosttags'] = $HostTags -join ',' }
    Invoke-CSApiRequest -Command 'addHost' -Parameters $apiParams
}