oVirtPowerShell.psm1

<#
.Synopsis
   Obtains oVirt authorization token for use in further commandlets
.DESCRIPTION
   Obtains oVirt authorization token for use in further commandlets, this token will expire depending on your oVirt server settings
.EXAMPLE
    The following example authenticates to the oVirt server using credentials stored in a variable
    $oVirtSvr = 'ovirt-server.domain.local'
    $Credential = get-credential
    Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtCredential $credential
.EXAMPLE
   The following example authenticates to the oVirt server using credentials requested at the time
    Get-oVirtAuthToken -oVirtServerName "ovirt-server.domain.local" -oVirtCredential (get-credential)
#>

function Get-oVirtAuthToken
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]
    Param
    (
        #Address of oVirt server (must be FQDN)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtServerName,

        #Credentials for oVirt (admin@internal is default user)
        [PSCredential][Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtCredential = 'admin@internal'

        
    )
        
        $User = $oVirtCredential.getnetworkCredential().UserName
        Write-Verbose "User: $User"
        $Domain = $oVirtCredential.getnetworkCredential().Domain
        if ($Domain.Length -eq 0) {$Domain = 'internal'}
        Write-Verbose "Domain: $Domain"
        $Pass = $oVirtCredential.getnetworkCredential().Password
        $AuthPayload = "grant_type=password&scope=ovirt-app-api&username=$User%40$Domain&password=$Pass"
        Write-Verbose "Auth Payload: $AuthPayload"
        $AuthHeaders = @{"Accept" = "application/json"}
        $URI = "https://$oVirtServerName/ovirt-engine/sso/oauth/token"
        Write-Verbose "Auth URI: $URI"
        $AuthResponse = Invoke-WebRequest -Uri $URI -Method Post -body $AuthPayload -Headers $AuthHeaders -ContentType 'application/x-www-form-urlencoded'
        Write-Verbose "Raw Response: $AuthResponse"
        $AuthToken = ((($AuthResponse.Content) -split '"')[3])
        Write-Verbose "Auth Token: $AuthToken"
        return $AuthToken
    
}


<#
.Synopsis
   Gets a list of all clusters managed by the oVirt server
.DESCRIPTION
   Gets a list of all clusters managed by the oVirt server using an auth token generated by Get-oVirtAuthToken
.EXAMPLE
   $oVirtSvr = 'ovirt-server.domain.local'
   $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
   Get-oVirtClusterList -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken
.EXAMPLE
   Get-oVirtClusterList -oVirtServerName 'ovirt-server.domain.local' -oVirtAuthToken (Get-oVirtAuthToken -oVirtServerName "ovirt-server.domain.local" -oVirtPassword 'password')
#>

function Get-oVirtClusterList
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]
    Param
    (
        #Address of oVirt server (must be FQDN)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtServerName,

        #AuthToken for oVirt server (use Get-oVirtAuthToken to obtain)
        [string][Parameter(Mandatory=$True,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtAuthToken
    )

    $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
    Write-Verbose $Headers
    $URI = "https://$oVirtServerName/ovirt-engine/api/clusters"
    Write-Verbose "URI: $URI"
    $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
    Write-Verbose "Raw Response: $Response"
    $VerboseResponseContent = $Response.Content
    Write-Verbose "Response Content: $VerboseResponseContent"
    [xml]$ResponseContent = $Response.Content
    $ResponseContent.clusters.cluster
}


<#
.Synopsis
   Gets a list of all gluster volumes managed by the oVirt server
.DESCRIPTION
   Gets a list of all gluster volumes managed by the oVirt server using an auth token generated by Get-oVirtAuthToken
.EXAMPLE
   $oVirtSvr = 'ovirt-server.domain.local'
   $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
   Get-oVirtGlusterVolumeList -oVirtServerName $oVirtServer -oVirtAuthToken $oVirtAuthToken
.EXAMPLE
   Get-oVirtGlusterVolumeList -oVirtServerName 'ovirt-server.domain.local' -oVirtAuthToken (Get-oVirtAuthToken -oVirtServerName "ovirt-server.domain.local" -oVirtPassword 'password')
#>

function Get-oVirtGlusterVolumeList
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]
    Param
    (
        #Address of oVirt server (must be FQDN)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtServerName,

        #AuthToken for oVirt server (use Get-oVirtAuthToken to obtain)
        [string][Parameter(Mandatory=$True,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtAuthToken
    )

    $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
    Write-Verbose $Headers
    $URI = "https://$oVirtServerName/ovirt-engine/api/clusters"
    Write-Verbose "URI: $URI"
    $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
    [xml]$ResponseContent = $Response.Content
    $ResponseContent.clusters.cluster | ForEach-Object{
        $ClusterID = $_.id
        $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
        Write-Verbose $Headers
        $URI = "https://$oVirtServerName/ovirt-engine/api/clusters/$ClusterID/glustervolumes"
        Write-Verbose "URI: $URI"
        $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
        [xml]$ResponseContent = $Response.Content
        $ResponseContent.gluster_volumes.gluster_volume
    }
}


<#
.Synopsis
   Gets a list of all gluster bricks managed by the oVirt server
.DESCRIPTION
   Gets a list of all gluster bricks managed by the oVirt server using an auth token generated by Get-oVirtAuthToken
.EXAMPLE
   $oVirtSvr = 'ovirt-server.domain.local'
   $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
   Get-oVirtGlusterBrickList -oVirtServerName $oVirtServer -oVirtAuthToken $oVirtAuthToken
.EXAMPLE
   Get-oVirtGlusterBrickList -oVirtServerName 'ovirt-server.domain.local' -oVirtAuthToken (Get-oVirtAuthToken -oVirtServerName "ovirt-server.domain.local" -oVirtPassword 'password')
#>

function Get-oVirtGlusterBrickList
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]
    Param
    (
        #Address of oVirt server (must be FQDN)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtServerName,

        #AuthToken for oVirt server (use Get-oVirtAuthToken to obtain)
        [string][Parameter(Mandatory=$True,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $oVirtAuthToken
    )

    $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
    Write-Verbose $Headers
    $URI = "https://$oVirtServerName/ovirt-engine/api/clusters"
    Write-Verbose "URI: $URI"
    $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
    [xml]$ResponseContent = $Response.Content
    $ResponseContent.clusters.cluster | ForEach-Object{
        $ClusterHref = $_.href
        Write-Verbose "Cluster href: $ClusterHref"
        $ClusterID = $_.id
        Write-Verbose "Cluster ID: $ClusterID"
        $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
        Write-Verbose $Headers
        $URI = "https://$oVirtServerName/ovirt-engine/api/clusters/$ClusterID/glustervolumes"
        Write-Verbose "URI: $URI"
        $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
        [xml]$ResponseContent = $Response.Content
        $ResponseContent.gluster_volumes.gluster_volume | ForEach-Object {
            $VolumeHref = $_.href
            Write-Verbose "Volume href: $VolumeHref"
            $VolumeID = $_.id
            Write-Verbose "Volume ID: $VolumeID"
            $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
            Write-Verbose $Headers
            $URI = "https://$oVirtServerName$VolumeHref/glusterbricks"
            Write-Verbose "URI: $URI"
            $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
            [xml]$ResponseContent = $Response.Content
            $ResponseContent.bricks.brick

        }
    }
}

<#
.Synopsis
   Gets a list of all gluster bricks for a gluster volume that has been piped into this command
.DESCRIPTION
   Gets a list of all gluster bricks for a gluster volume that has been piped into this command, server name and auth token must still be supplied
.EXAMPLE
    Stores the servername and auth token in variables then uses them to get a list of bricks for each volume
    $oVirtSvr = 'ovirt-server.domain.local'
    $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
    Get-oVirtGlusterVolumeList -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken | Get-oVirtGlusterVolumeBricks -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken
.EXAMPLE
    Stores the servername and auth token in variables then uses them to get a list of bricks for each volume using Foreach-Object
    $oVirtSvr = 'ovirt-server.domain.local'
    $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
    Get-oVirtGlusterVolumeList -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken | Foreach-Object {Get-oVirtGlusterVolumeBricks -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken -href $_.href}
.EXAMPLE
    Stores the servername and auth token in variables then uses them to get a list of bricks for any volume with the name starting 'test'
    $oVirtSvr = 'ovirt-server.domain.local'
    $oVirtAuthToken = Get-oVirtAuthToken -oVirtServerName $oVirtSvr -oVirtPassword 'password'
    Get-oVirtGlusterVolumeList -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken | Where-Object {$_.name -like 'test*'} | Get-oVirtGlusterVolumeBricks -oVirtServerName $oVirtSvr -oVirtAuthToken $oVirtAuthToken
#>


function Get-oVirtGlusterVolumeBricklist
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # href to the volume in the API (from pipeline)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $href,

        #Address of oVirt server (must be FQDN)
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        $oVirtServerName,

        #AuthToken for oVirt server (use Get-oVirtAuthToken to obtain)
        [string][Parameter(Mandatory=$True,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        $oVirtAuthToken
    )

    Begin
    {
        $Headers = @{'Authorization' = "Bearer $oVirtAuthToken"; "Accept" = "application/xml"}
        Write-Verbose $Headers
    }
    Process
    {
        $URI = "https://$oVirtServerName$href/glusterbricks"
            Write-Verbose "URI: $URI"
            $Response = Invoke-WebRequest -Uri $URI -Method get -Headers $Headers -ContentType 'application/x-www-form-urlencoded'
            [xml]$ResponseContent = $Response.Content
            $ResponseContent.bricks.brick
    }
}