public/Start-LodPxLabSession.ps1

<#############################################################################
TBD
 
Copyright 2015-2017 Kirk Munro
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#############################################################################>


function Start-LodPxLabSession {
    [CmdletBinding()]
    [OutputType('LabOnDemand.Session')]
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $FirstName,

        [Parameter(Position=1, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $LastName,

        [Parameter(Position=2, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $Email,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $UserId,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $Tag,

        [Parameter()]
        [ValidateNotNull()]
        [System.Collections.Hashtable]
        $AdditionalParameters = @{},

        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [System.Int32]
        $LabId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [System.Guid]
        $ApiKey
    )
    try {
        #region If the caller didn't provide a UserId, use the email address as the UserId.

        if (-not $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('UserId')) {
            $PSCmdlet.MyInvocation.BoundParameters['UserId'] = $UserId = $Email
        }

        #endregion

        #region Build a collection of query parameters.

        $parameters = [ordered]@{
                          labId = $LabId
            canBeMarkedComplete = 2 # false
        }
        foreach ($parameterName in 'userId','firstName','lastName','email','tag') {
            if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey($parameterName)) {
                $parameters[$parameterName] = $PSCmdlet.MyInvocation.BoundParameters[$parameterName]
            }
        }
        foreach ($key in $AdditionalParameters.Keys) {
            $parameters[$key] = $AdditionalParameters[$key]
        }

        #endregion

        #region Request a new lab session.

        $results = Invoke-LodPxWebMethod -Name launch -Parameters $parameters -ApiKey $ApiKey

        #endregion

        #region Check the results for error.

        $errorCodes = @{
             0 = 'Error'
             2 = 'User has too many active labs'
             3 = 'Insufficient host resources'
             5 = 'API integration has too many active labs'
             6 = 'User has a saved instance of this lab'
             7 = 'API integration doesn''t have enough available RAM'
            10 = 'User doesn''t have enough available RAM'
            20 = 'User''s organization has too many active labs'
            30 = 'User''s organization doesn''t have enough available RAM'
            40 = 'Lab profile has too many active instances'
            50 = 'Lab organization doesn''t have enough available RAM.'
            60 = 'Lab organization has too many active instances'
            70 = 'Lab series has too many active instances'
            80 = 'Lab series doesn''t have enough available RAM'
        }
        if ([int]$results.Result -ne 1) {
            if ($errorCodes.Keys -contains [int]$results.Result) {
                throw "$($errorCodes[[int]$results.Result]). Error message: $($results.Error)."
            } else {
                throw "The server returned an undocumented error code. Error code: $($results.Result). Error message: $($results.Error)."
            }
        }

        #endregion

        #region Return the lab session details to the caller.

        [pscustomobject]@{
               PSTypeName = 'LabOnDemand.Session'
                      Url = $results.Url
            LabInstanceId = $results.LabInstanceId
                  Expires = ConvertFrom-UnixEpochTime -Value $results.Expires
        }

        #endregion
    } catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}

Export-ModuleMember -Function Start-LodPxLabSession

New-Alias -Name New-LodPxLabSession -Value Start-LodPxLabSession -ErrorAction Ignore
if ($?) {
    Export-ModuleMember -Alias New-LodPxLabSession
}
# SIG # Begin signature block
# MIIX0AYJKoZIhvcNAQcCoIIXwTCCF70CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUUVnkYm7FVTre4fjY7LuSloEq
# 4sSgghMDMIID7jCCA1egAwIBAgIQfpPr+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