Public/zone-management.ps1

function Remove-CSZone {
    <#
    .SYNOPSIS
        Deletes a zone.

    .DESCRIPTION
        Wraps the deleteZone API. The zone must be empty (no pods, clusters, or hosts)
        before it can be removed. Accepts zone objects on the pipeline by their id.

    .PARAMETER Id
        The zone to delete. Binds from a piped zone's id.

    .EXAMPLE
        Remove-CSZone -Id zone-uuid
        Deletes a zone after prompting for confirmation.

    .EXAMPLE
        Get-CSZone -Name 'us-east-1' | Remove-CSZone
        Deletes a zone located by name.
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id
    )
    process {
        if($PSCmdlet.ShouldProcess("zone $Id",'Delete')){Invoke-CSApiRequest -Command deleteZone -Parameters @{id=$Id}}
    }
}

function Set-CSZone {
    <#
    .SYNOPSIS
        Updates a zone.

    .DESCRIPTION
        Wraps the updateZone API. Only the attributes you supply are changed. Accepts
        zone objects on the pipeline by their id.

    .PARAMETER Id
        The zone to update. Binds from a piped zone's id.

    .PARAMETER AllocationState
        New allocation state (Enabled or Disabled).

    .PARAMETER Dns1
        Primary public DNS server.

    .PARAMETER Dns2
        Secondary public DNS server.

    .PARAMETER InternalDns1
        Primary internal DNS server.

    .PARAMETER InternalDns2
        Secondary internal DNS server.

    .PARAMETER Name
        New name for the zone.

    .PARAMETER GuestCidrAddress
        Guest CIDR for the zone.

    .PARAMETER IsEdge
        Mark the zone as an edge zone.

    .PARAMETER LocalStorageEnabled
        Enable local storage for the zone.

    .EXAMPLE
        Set-CSZone -Id zone-uuid -AllocationState Enabled
        Enables a zone for allocation.

    .EXAMPLE
        Get-CSZone -Name 'us-east-1' | Set-CSZone -Dns1 '1.1.1.1' -Dns2 '8.8.8.8'
        Updates the public DNS servers on a zone located by name.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id,
        [string]$AllocationState,
        [string]$Dns1,
        [string]$Dns2,
        [string]$InternalDns1,
        [string]$InternalDns2,
        [string]$Name,
        [string]$GuestCidrAddress,
        [switch]$IsEdge,
        [switch]$LocalStorageEnabled
    )
    process {
        $p=@{id=$Id}
        foreach($k in @('AllocationState','Dns1','Dns2','InternalDns1','InternalDns2','Name','GuestCidrAddress')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
        if($IsEdge){$p.isedge='true'}
        if($LocalStorageEnabled){$p.localstorageenabled='true'}
        Invoke-CSApiRequest -Command updateZone -Parameters $p
    }
}

function Set-CSZoneDedicated {
    <#
    .SYNOPSIS
        Dedicates a zone to a domain or account.

    .DESCRIPTION
        Wraps the dedicateZone API, reserving a whole zone so only the given domain
        (or -Account within it) can run instances in it. Release it again with
        Clear-CSZoneDedicated. Accepts zone objects on the pipeline by their id.

    .PARAMETER ZoneId
        The zone to dedicate. Binds from a piped zone's id.

    .PARAMETER DomainId
        The domain to dedicate the zone to.

    .PARAMETER Account
        Dedicate the zone to this account within -DomainId.

    .PARAMETER ProjectId
        Dedicate the zone to this project.

    .EXAMPLE
        Set-CSZoneDedicated -ZoneId zone-uuid -DomainId domain-uuid
        Dedicates a zone to a domain.

    .EXAMPLE
        Get-CSZone -Name 'us-east-1' | Set-CSZoneDedicated -DomainId $domainId -Account 'engineering'
        Dedicates a zone piped in by object to one account.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][Alias('Id')][string]$ZoneId,
        [Parameter(Mandatory=$true)][string]$DomainId,
        [string]$Account,
        [string]$ProjectId
    )
    process {
        $p=@{zoneid=$ZoneId;domainid=$DomainId}
        foreach($k in @('Account','ProjectId')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
        Invoke-CSApiRequest -Command dedicateZone -Parameters $p
    }
}

function Clear-CSZoneDedicated {
    <#
    .SYNOPSIS
        Releases a zone's dedication back to the system.

    .DESCRIPTION
        Wraps the releaseDedicatedZone API, undoing a dedication made with
        Set-CSZoneDedicated so any account can run instances in the zone again.
        Accepts zone objects on the pipeline by their id.

    .PARAMETER ZoneId
        The zone to release. Binds from a piped zone's id.

    .EXAMPLE
        Clear-CSZoneDedicated -ZoneId zone-uuid
        Releases a zone's dedication.

    .EXAMPLE
        Get-CSZoneDedicated -DomainId $domainId | Clear-CSZoneDedicated
        Releases every zone dedicated to a domain.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][Alias('Id')][string]$ZoneId
    )
    process {
        Invoke-CSApiRequest -Command releaseDedicatedZone -Parameters @{zoneid=$ZoneId}
    }
}

function Get-CSZoneDedicated {
    <#
    .SYNOPSIS
        Lists zones dedicated to domains or accounts.

    .DESCRIPTION
        Wraps the listDedicatedZones API, showing which zones have been reserved for
        which accounts or domains.

    .PARAMETER ZoneId
        Filter by zone ID.

    .PARAMETER Name
        Filter by name.

    .PARAMETER Account
        Filter by account name.

    .PARAMETER DomainId
        Filter by domain ID.

    .PARAMETER Page
        Page number of results to return.

    .PARAMETER PageSize
        Number of results per page.

    .EXAMPLE
        Get-CSZoneDedicated
        Lists all dedicated zones.

    .EXAMPLE
        Get-CSZoneDedicated -DomainId domain-uuid
        Lists the zones dedicated to a domain.
    #>

    [CmdletBinding()]
    param(
        [string]$ZoneId,
        [string]$Name,
        [string]$Account,
        [string]$DomainId,
        [int]$Page,
        [int]$PageSize
    )
    $p=@{}
    foreach($k in @('ZoneId','Name','Account','DomainId','Page','PageSize')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
    $r=Invoke-CSApiRequest -Command listDedicatedZones -Parameters $p
    if($r.listdedicatedzonesresponse.zone){$r.listdedicatedzonesresponse.zone}
}

function Enable-CSZoneHA {
    <#
    .SYNOPSIS
        Enables host high availability across a zone.

    .DESCRIPTION
        Wraps the enableHAForZone API, turning on HA for every host in the zone.
        Accepts zone objects on the pipeline by their id.

    .PARAMETER ZoneId
        The zone to enable HA on. Binds from a piped zone's id.

    .EXAMPLE
        Enable-CSZoneHA -ZoneId zone-uuid
        Enables HA for every host in a zone.

    .EXAMPLE
        Get-CSZone -Name 'us-east-1' | Enable-CSZoneHA
        Enables HA for a zone located by name.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][Alias('Id')][string]$ZoneId
    )
    process {
        Invoke-CSApiRequest -Command enableHAForZone -Parameters @{zoneid=$ZoneId}
    }
}

function Disable-CSZoneHA {
    <#
    .SYNOPSIS
        Disables host high availability across a zone.

    .DESCRIPTION
        Wraps the disableHAForZone API, turning off HA for every host in the zone.
        Accepts zone objects on the pipeline by their id.

    .PARAMETER ZoneId
        The zone to disable HA on. Binds from a piped zone's id.

    .EXAMPLE
        Disable-CSZoneHA -ZoneId zone-uuid
        Disables HA for every host in a zone.

    .EXAMPLE
        Get-CSZone -Name 'us-east-1' | Disable-CSZoneHA
        Disables HA for a zone located by name.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][Alias('Id')][string]$ZoneId
    )
    process {
        Invoke-CSApiRequest -Command disableHAForZone -Parameters @{zoneid=$ZoneId}
    }
}

function New-CSIpv4SubnetForZone {
    <#
    .SYNOPSIS
        Creates an IPv4 subnet for a zone.

    .DESCRIPTION
        Wraps the createIpv4SubnetForZone API, adding an IPv4 subnet the zone can
        allocate guest addresses from. Optionally tie it to a network with -NetworkId.

    .PARAMETER ZoneId
        The zone to add the subnet to.

    .PARAMETER Subnet
        The IPv4 subnet in CIDR notation.

    .PARAMETER NetworkId
        Associate the subnet with this network.

    .EXAMPLE
        New-CSIpv4SubnetForZone -ZoneId zone-uuid -Subnet 192.0.2.0/24
        Adds an IPv4 subnet to a zone.

    .EXAMPLE
        New-CSIpv4SubnetForZone -ZoneId $zoneId -Subnet 198.51.100.0/24 -NetworkId $networkId
        Adds a subnet tied to a specific network.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$ZoneId,
        [Parameter(Mandatory=$true)][string]$Subnet,
        [string]$NetworkId
    )
    $p=@{zoneid=$ZoneId;subnet=$Subnet}
    if($PSBoundParameters.ContainsKey('NetworkId')){$p.networkid=$NetworkId}
    Invoke-CSApiRequest -Command createIpv4SubnetForZone -Parameters $p
}

function Remove-CSIpv4SubnetForZone {
    <#
    .SYNOPSIS
        Deletes an IPv4 subnet from a zone.

    .DESCRIPTION
        Wraps the deleteIpv4SubnetForZone API, removing a zone IPv4 subnet by its ID.
        Accepts subnet objects on the pipeline by their id.

    .PARAMETER Id
        The IPv4 subnet to delete. Binds from a piped subnet's id.

    .EXAMPLE
        Remove-CSIpv4SubnetForZone -Id subnet-uuid
        Deletes a zone IPv4 subnet after prompting for confirmation.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -ZoneId $zoneId | Remove-CSIpv4SubnetForZone
        Deletes the IPv4 subnets in a zone.
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id
    )
    process {
        if($PSCmdlet.ShouldProcess("IPv4 subnet $Id",'Delete')){Invoke-CSApiRequest -Command deleteIpv4SubnetForZone -Parameters @{id=$Id}}
    }
}

function Get-CSIpv4SubnetForZone {
    <#
    .SYNOPSIS
        Lists IPv4 subnets for a zone.

    .DESCRIPTION
        Wraps the listIpv4SubnetsForZone API, returning the IPv4 subnets defined in a
        zone.

    .PARAMETER Id
        Filter by subnet ID.

    .PARAMETER ZoneId
        Filter by zone ID.

    .PARAMETER Subnet
        Filter by subnet CIDR.

    .PARAMETER Page
        Page number of results to return.

    .PARAMETER PageSize
        Number of results per page.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -ZoneId zone-uuid
        Lists the IPv4 subnets in a zone.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -Id subnet-uuid
        Gets a single IPv4 subnet by ID.
    #>

    [CmdletBinding()]
    param(
        [string]$Id,
        [string]$ZoneId,
        [string]$Subnet,
        [int]$Page,
        [int]$PageSize
    )
    $p=@{}
    foreach($k in @('Id','ZoneId','Subnet','Page','PageSize')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
    $r=Invoke-CSApiRequest -Command listIpv4SubnetsForZone -Parameters $p
    if($r.listipv4subnetsforzoneresponse.ipv4subnet){$r.listipv4subnetsforzoneresponse.ipv4subnet}
}

function Set-CSIpv4SubnetForZone {
    <#
    .SYNOPSIS
        Updates a zone IPv4 subnet.

    .DESCRIPTION
        Wraps the updateIpv4SubnetForZone API, changing a zone IPv4 subnet's CIDR.
        Accepts subnet objects on the pipeline by their id.

    .PARAMETER Id
        The IPv4 subnet to update. Binds from a piped subnet's id.

    .PARAMETER Subnet
        The new subnet in CIDR notation.

    .EXAMPLE
        Set-CSIpv4SubnetForZone -Id subnet-uuid -Subnet 192.0.2.0/25
        Changes a zone IPv4 subnet's CIDR.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -ZoneId $zoneId | Select-Object -First 1 | Set-CSIpv4SubnetForZone -Subnet 192.0.2.0/25
        Updates a subnet piped in by object.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id,
        [string]$Subnet
    )
    process {
        $p=@{id=$Id}
        if($PSBoundParameters.ContainsKey('Subnet')){$p.subnet=$Subnet}
        Invoke-CSApiRequest -Command updateIpv4SubnetForZone -Parameters $p
    }
}

function Set-CSIpv4SubnetForZoneDedicated {
    <#
    .SYNOPSIS
        Dedicates a zone IPv4 subnet to a domain or account.

    .DESCRIPTION
        Wraps the dedicateIpv4SubnetForZone API, reserving a zone IPv4 subnet for the
        given domain (or -Account within it). Release it again with
        Clear-CSIpv4SubnetForZoneDedicated. Accepts subnet objects on the pipeline by
        their id.

    .PARAMETER Id
        The IPv4 subnet to dedicate. Binds from a piped subnet's id.

    .PARAMETER DomainId
        The domain to dedicate the subnet to.

    .PARAMETER Account
        Dedicate the subnet to this account within -DomainId.

    .PARAMETER ProjectId
        Dedicate the subnet to this project.

    .EXAMPLE
        Set-CSIpv4SubnetForZoneDedicated -Id subnet-uuid -DomainId domain-uuid
        Dedicates a zone IPv4 subnet to a domain.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -ZoneId $zoneId | Select-Object -First 1 | Set-CSIpv4SubnetForZoneDedicated -DomainId $domainId -Account 'engineering'
        Dedicates a subnet piped in by object to one account.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id,
        [Parameter(Mandatory=$true)][string]$DomainId,
        [string]$Account,
        [string]$ProjectId
    )
    process {
        $p=@{id=$Id;domainid=$DomainId}
        foreach($k in @('Account','ProjectId')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
        Invoke-CSApiRequest -Command dedicateIpv4SubnetForZone -Parameters $p
    }
}

function Clear-CSIpv4SubnetForZoneDedicated {
    <#
    .SYNOPSIS
        Releases a dedicated zone IPv4 subnet.

    .DESCRIPTION
        Wraps the releaseIpv4SubnetForZone API, undoing a dedication made with
        Set-CSIpv4SubnetForZoneDedicated so the subnet is available again. Accepts
        subnet objects on the pipeline by their id.

    .PARAMETER Id
        The IPv4 subnet to release. Binds from a piped subnet's id.

    .EXAMPLE
        Clear-CSIpv4SubnetForZoneDedicated -Id subnet-uuid
        Releases a dedicated zone IPv4 subnet.

    .EXAMPLE
        Get-CSIpv4SubnetForZone -ZoneId $zoneId | Clear-CSIpv4SubnetForZoneDedicated
        Releases the dedications on a zone's IPv4 subnets.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id
    )
    process {
        Invoke-CSApiRequest -Command releaseIpv4SubnetForZone -Parameters @{id=$Id}
    }
}

function New-CSVmwareDatacenter {
    <#
    .SYNOPSIS
        Registers a VMware datacenter.

    .DESCRIPTION
        Wraps the addVmwareDc API, associating an entire vCenter datacenter with a
        zone so its resources can be managed by CloudStack.

    .PARAMETER Name
        The vCenter datacenter name.

    .PARAMETER Vcenter
        The vCenter server hostname or address.

    .PARAMETER ZoneId
        The zone to associate the datacenter with.

    .PARAMETER Username
        vCenter username.

    .PARAMETER Password
        vCenter password.

    .EXAMPLE
        New-CSVmwareDatacenter -Name 'DC1' -Vcenter 'vcenter.example.com' -ZoneId zone-uuid
        Registers a VMware datacenter with a zone.

    .EXAMPLE
        New-CSVmwareDatacenter -Name 'DC1' -Vcenter 'vcenter.example.com' -ZoneId $zoneId -Username 'administrator@vsphere.local' -Password $pw
        Registers a datacenter with explicit vCenter credentials.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$Name,
        [Parameter(Mandatory=$true)][string]$Vcenter,
        [Parameter(Mandatory=$true)][string]$ZoneId,
        [string]$Username,
        [string]$Password
    )
    $p=@{name=$Name;vcenter=$Vcenter;zoneid=$ZoneId}
    foreach($k in @('Username','Password')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
    Invoke-CSApiRequest -Command addVmwareDc -Parameters $p
}

function Remove-CSVmwareDatacenter {
    <#
    .SYNOPSIS
        Removes a registered VMware datacenter.

    .DESCRIPTION
        Wraps the removeVmwareDc API, disassociating a vCenter datacenter from its
        zone. Accepts datacenter objects on the pipeline by their id.

    .PARAMETER Id
        The VMware datacenter to remove. Binds from a piped datacenter's id.

    .EXAMPLE
        Remove-CSVmwareDatacenter -Id dc-uuid
        Removes a VMware datacenter after prompting for confirmation.

    .EXAMPLE
        Get-CSVmwareDatacenter -Name 'DC1' | Remove-CSVmwareDatacenter
        Removes a datacenter located by name.
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id
    )
    process {
        if($PSCmdlet.ShouldProcess("VMware datacenter $Id",'Remove')){Invoke-CSApiRequest -Command removeVmwareDc -Parameters @{id=$Id}}
    }
}

function Set-CSVmwareDatacenter {
    <#
    .SYNOPSIS
        Updates a registered VMware datacenter.

    .DESCRIPTION
        Wraps the updateVmwareDc API. Only the attributes you supply are changed.
        Accepts datacenter objects on the pipeline by their id.

    .PARAMETER Id
        The VMware datacenter to update. Binds from a piped datacenter's id.

    .PARAMETER Name
        New datacenter name.

    .PARAMETER Url
        New vCenter URL.

    .PARAMETER Username
        New vCenter username.

    .PARAMETER Password
        New vCenter password.

    .EXAMPLE
        Set-CSVmwareDatacenter -Id dc-uuid -Username 'administrator@vsphere.local' -Password $pw
        Updates the vCenter credentials for a datacenter.

    .EXAMPLE
        Get-CSVmwareDatacenter -Name 'DC1' | Set-CSVmwareDatacenter -Name 'DC1-primary'
        Renames a datacenter located by name.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string]$Id,
        [string]$Name,
        [string]$Url,
        [string]$Username,
        [string]$Password
    )
    process {
        $p=@{id=$Id}
        foreach($k in @('Name','Url','Username','Password')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
        Invoke-CSApiRequest -Command updateVmwareDc -Parameters $p
    }
}

function Get-CSVmwareDatacenter {
    <#
    .SYNOPSIS
        Lists registered VMware datacenters.

    .DESCRIPTION
        Wraps the listVmwareDcs API, returning the vCenter datacenters associated with
        zones.

    .PARAMETER Id
        Filter by datacenter ID.

    .PARAMETER Name
        Filter by name.

    .PARAMETER ZoneId
        Filter by zone ID.

    .PARAMETER Page
        Page number of results to return.

    .PARAMETER PageSize
        Number of results per page.

    .EXAMPLE
        Get-CSVmwareDatacenter -ZoneId zone-uuid
        Lists the VMware datacenters associated with a zone.

    .EXAMPLE
        Get-CSVmwareDatacenter -Name 'DC1'
        Gets a single datacenter by name.
    #>

    [CmdletBinding()]
    param(
        [string]$Id,
        [string]$Name,
        [string]$ZoneId,
        [int]$Page,
        [int]$PageSize
    )
    $p=@{}
    foreach($k in @('Id','Name','ZoneId','Page','PageSize')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
    $r=Invoke-CSApiRequest -Command listVmwareDcs -Parameters $p
    if($r.listvmwaredcsresponse.vmwaredc){$r.listvmwaredcsresponse.vmwaredc}
}

function Get-CSVmwareDatacenterVirtualMachine {
    <#
    .SYNOPSIS
        Lists the virtual machines in a registered VMware datacenter.

    .DESCRIPTION
        Wraps the listVmwareDcVms API, returning the VMs that exist in a registered
        vCenter datacenter (for example as candidates for import).

    .PARAMETER VmwareDcId
        The VMware datacenter to list VMs from.

    .PARAMETER Name
        Filter by VM name.

    .PARAMETER Page
        Page number of results to return.

    .PARAMETER PageSize
        Number of results per page.

    .EXAMPLE
        Get-CSVmwareDatacenterVirtualMachine -VmwareDcId dc-uuid
        Lists the VMs in a registered VMware datacenter.

    .EXAMPLE
        Get-CSVmwareDatacenterVirtualMachine -VmwareDcId $dcId -Name 'legacy-app'
        Finds a VM by name in a datacenter.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$VmwareDcId,
        [string]$Name,
        [int]$Page,
        [int]$PageSize
    )
    $p=@{vmwaredcid=$VmwareDcId}
    foreach($k in @('Name','Page','PageSize')){if($PSBoundParameters.ContainsKey($k)){$p[$k.ToLowerInvariant()]=(Get-Variable $k -ValueOnly)}}
    $r=Invoke-CSApiRequest -Command listVmwareDcVms -Parameters $p
    if($r.listvmwaredcvmsresponse.vm){$r.listvmwaredcvmsresponse.vm}
}