Public/ARM/DatabaseRestore/Set-RelativityArmDatabaseRestoreJob.ps1

<#
.SYNOPSIS
Function to create a update an existing Relativity ARM database restore job using Relativity's REST API.
 
.DESCRIPTION
This function constructs the required request, calls Relativity's REST API, and processes the response to update an existing ARM database restore job.
 
.PARAMETER JobID
The Job ID of the ARM database restore job to be updated. This is a mandatory parameter.
 
.PARAMETER SourceDatabase
The name of the database which will be restored.
 
.PARAMETER JobPriority
Priority of execution for the job. Possible options include "Low", "Medium", and "High". Default is "Medium".
 
.PARAMETER ScheduledStartTime
Scheduled time when the job will be run.
 
.PARAMETER DatabaseServerID
ArtifactId of the target database server to restore the workspace to.
 
.PARAMETER ResourcePoolID
ArtifactId of the target resource pool to restore the workspace to.
 
.PARAMETER MatterID
ArtifactId of the target matter to restore the workspace to.
 
.PARAMETER CacheLocationID
ArtifactId of the target cache location to restore the workspace to.
 
.PARAMETER FileRepositoryID
ArtifactId of the target file repository to restore the workspace to.
 
.PARAMETER AutoMapUsers
Indicates if archive users should be auto mapped by email address.
 
.PARAMETER UserMappings
Hashtable array of explicit user mappings from the archive to the Relativity instance.
Array must have two properties, ArchiveUserID and InstanceUserID, of type Int32.
 
.PARAMETER AutoMapGroups
Indicates if archive groups should be auto mapped by name.
 
.PARAMETER GroupMappings
Hashtable array of explicit group mappings from the archive to the Relativity instance.
Array must have two properties, ArchiveGroupID and InstanceGroupID, of type Int32.
 
.PARAMETER NotifyJobCreator
Indicates if email notifications will be sent to the job creator.
 
.PARAMETER NotifyJobExecutor
Indicates if email notifications will be sent to the job executor.
 
.PARAMETER UiJobActionsLocked
Indicates if job actions normally available on UI should be visible for the user.
This behavior can be override by adding boolean instance setting OverrideUiJobActionsLock.
 
.EXAMPLE
Set-RelativityArmDatabaseRestoreJob -JobID 500 -SourceDatabase "EDDS1234567" -DatabaseServerID 1234567 -ResourcePoolID 2345671 -MatterID 3456712 -CacheLocationID 4567123 -FileRepositoryID 5671234
 
This example updates an existing database restore job with the specified database and destination options.
 
.NOTES
Ensure you have connectivity and appropriate permissions in Relativity before running this function.
#>

function Set-RelativityArmDatabaseRestoreJob
{
    [CmdletBinding(SupportsShouldProcess)]
    Param
    (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1, [Int32]::MaxValue)]
        [Int32] $JobID,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [String] $SourceDatabase,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [ValidateSet("Low", "Medium", "High")]
        [String] $JobPriority = "Medium",
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [ValidateScript({
                $date = "1970-01-01"
                [DateTime]::TryParse($_, [ref]$date)
                if ($_ -eq "") { return $true }
                elseif ($date -eq [DateTime]::MinValue) { throw "Invalid DateTime for ScheduledStartTime: $($_)." }
                $true
            })]
        [String] $ScheduledStartTime,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1000000, [Int32]::MaxValue)]
        [Int32] $DatabaseServerID,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1000000, [Int32]::MaxValue)]
        [Int32] $ResourcePoolID,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1000000, [Int32]::MaxValue)]
        [Int32] $MatterID,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1000000, [Int32]::MaxValue)]
        [Int32] $CacheLocationID,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(1000000, [Int32]::MaxValue)]
        [Int32] $FileRepositoryID,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Switch] $AutoMapUsers,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Hashtable[]] $UserMappings,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Switch] $AutoMapGroups,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Hashtable[]] $GroupMappings,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Switch] $NotifyJobCreator,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Switch] $NotifyJobExecutor,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [Switch] $UiJobActionsLocked
    )

    Begin
    {
        Write-Verbose "Starting Set-RelativityArmDatabaseRestoreJob"
    }
    Process
    {
        try
        {
            $Params = @{
                SourceDatabase = $SourceDatabase
                JobPriority = $JobPriority
                ScheduledStartTime = $ScheduledStartTime
                DatabaseServerID = $DatabaseServerID
                ResourcePoolID = $ResourcePoolID
                MatterID = $MatterID
                CacheLocationID = $CacheLocationID
                FileRepositoryID = $FileRepositoryID
                AutoMapUsers = $AutoMapUsers
                UserMappings = $UserMappings
                AutoMapGroups = $AutoMapGroups
                GroupMappings = $GroupMappings
                NotifyJobCreator = $NotifyJobCreator
                NotifyJobExecutor = $NotifyJobExecutor
                UiJobActionsLocked = $UiJobActionsLocked
            }

            $Request = Get-RelativityArmDatabaseRestoreJobCreateOrUpdateRequest @Params

            $RequestBody = $Request.ToHashTable()

            [String[]]$Resources = @("database-restore-jobs", $JobID.ToString())

            $ApiEndpoint = Get-RelativityApiEndpoint -BusinessDomain "relativity-arm" -Version "v1" -Resources $Resources

            Write-Debug "Preparing to invoke PUT method at Relativity API endpoint '$($ApiEndPoint)' with RequestBody $($RequestBody | ConvertTo-Json -Depth 10)"
            Write-Verbose "Invoking PUT method at Relativity API endpoint: $($ApiEndPoint)"
            if ($PSCmdlet.ShouldProcess($ApiEndpoint))
            {
                $ApiResponse = Invoke-RelativityApiRequest -ApiEndpoint $ApiEndpoint -HttpMethod "Put" -RequestBody $RequestBody

                $Response = [RelativityApiSuccessResponse]::New($ApiResponse.Success)
                Write-Verbose "Successfully updated ARM database restore job with ID: $($JobID)"
            }

            return $Response
        }
        catch
        {
            Write-Error "An error occurred: $($_.Exception) type: $($_.GetType().FullName)"
            Write-Verbose "Logging parameter values:"

            (Get-Command -Name $PSCmdlet.MyInvocation.InvocationName).Parameters | ForEach-Object {
                $_.Values | ForEach-Object {
                    $Parameter = Get-Variable -Name $_.Name -ErrorAction SilentlyContinue

                    if ($null -ne $Parameter)
                    {
                        Write-Verbose "$($Parameter.Name): $($Parameter.Value)"
                    }
                }
            }

            Write-Verbose "API Endpoint: $($ApiEndpoint)"

            throw
        }
    }
    End
    {
        Write-Verbose "Completed Set-RelativityArmDatabaseRestoreJob"
    }
}