Functions/Publish-DatabricksClusterConfigToWorkspace.ps1

<#
.SYNOPSIS
Deploys DataBricks Cluster from configuration json file to a workspace
 
.DESCRIPTION
Deploys DataBricks Cluster from configuration json file to a workspace
 
.PARAMETER config
Configuration json file from the environment used to workout whether to deploy a clusters from a folder or file(s)
 
.PARAMETER clusterConfig
The name path of the clusters configuration files.
 
.EXAMPLE
Publish-DatabricksClusterConfigToWorkspace -config $config -bearerToken 'dapi1234567890' -clusterConfig '<path-to-file>'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Publish-DatabricksClusterConfigToWorkspace {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)][string]$clusterConfig
        
    )
    if (Test-Path $clusterConfig) {
        Write-Host "Publishing cluster config in path$clusterConfig"
        try {
            $cluster = Get-Content -Raw -Path $clusterConfig | ConvertFrom-Json
            $ExistingClusterConfig = Get-DatabricksClusters | Where-Object { $_.cluster_name -eq $cluster.cluster_name } 
            $ClusterId = $ExistingClusterConfig.cluster_id
            $ExistingClusterConfig | Remove-ClusterMetaDataAsPSObject

            if ($ExistingClusterConfig) {
                $Diffs = Compare-DatabricksCluster -cluster $cluster -ExistingClusterConfig $ExistingClusterConfig
                if ($Diffs.Count -gt 0) {
                    Write-Verbose "Cluster `"$($cluster.cluster_name)`" exists - updating cluster"
                    New-DatabricksCluster -InputObject $cluster
                
                }
                else {
                    Write-Warning "Cluster `"$($cluster.cluster_name)`" unchanged - not deploying to prevent unnecessary restart of cluster"
                }
                return $ClusterId
            }
            else {
                Write-Host "No cluster found with this name `"$($cluster.cluster_name)`" - creating new cluster"
                New-DatabricksCluster -InputObject $cluster
            }
        }
        catch {
            throw $_.Exception
        }
    }
    else {
        throw $_.Exception
    }
}