Functions/Publish-DatabricksJobToWorkspaceByName.ps1

<#
.SYNOPSIS
Deploys DataBricks job from configuration json file to a workspace
 
.DESCRIPTION
Deploys DataBricks job from configuration json file to a workspace
 
.PARAMETER jobConfig
The name path of the job configuration files.
 
.PARAMETER removeSchedule
Determine whether or nto to exclude the schedule when publishing the job to the workspace.
 
.EXAMPLE
Publish-DatabricksJobToWorkspaceByName '<path-to-file>'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Publish-DatabricksJobToWorkspaceByName {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)][string]$jobConfig,
        [parameter(Mandatory = $true)][bool]$removeSchedule
    )
    try {
        Write-Host "Getting content for job $jobConfig"
        $job = Get-Content -Raw -Path $jobConfig | ConvertFrom-Json
        $results = @{
            ClusterFound    = 0
            ScheduleRemoved = 0
            NotebookJob     = 0
        }
    }
    catch {
        #uh oh
        throw $_.Exception
    }
    try {

        Write-Host "If key existing_cluster_name is present we need to replace it with existing_cluster_id for the workspace being deployed to"
        if ($job.PSObject.Properties.Item('existing_cluster_name')) {
            $ClusterId = Get-CachedDatabricksClusterId -ClusterName $job.existing_cluster_name
            if ($ClusterId) {
                $job.PSObject.Properties.Remove('existing_cluster_name')  
                $job | Add-Member -NotePropertyName 'existing_cluster_id' -NotePropertyValue $ClusterId
                $results.ClusterFound = 1
            }
            else {
                throw "No cluster name of `"$($job.existing_cluster_name)`" found. Please check the cluster exists on the target workspace before trying again"
            }
        }
    }
    catch {
        #uh oh
        throw $_.Exception
    }
    try {
        if ($removeSchedule -eq $true) {
            Write-Host "Remving schedule"
            if ($job.PSObject.Properties.Item('schedule')) {
                $job.PSObject.Properties.Remove('schedule')
                $results.ScheduleRemoved = 1
            }
        }
    }    
    catch {
        #uh oh
        throw $_.Exception
    }
    try {
        Write-Host "Publishing job"
        Add-DatabricksNotebookJob -JobName $job.name -InputObject $job
        $results.NotebookJob = 1
    }
    catch {
        #uh oh
        throw $_.Exception
    }
    return $results
}