Functions/Export-DatabricksClustersFromWorkspace.ps1

<#
.SYNOPSIS
Exports DataBricks Clusters and Saves as json.
 
.DESCRIPTION
Exports Databricks Clusters and saves as json.
 
.PARAMETER config
Configuration json file from the environment to use for possible clusters to remove.
 
.PARAMETER bearerToken
Your Databricks Bearer token to authenticate to your workspace (see User Settings in Datatbricks WebUI)
 
.PARAMETER localOutputPath
The name of the cluster to export as a json file.
 
 
.EXAMPLE
Export-DatabricksClustersFromWorkspace -config $config -bearerToken 'dapi1234567890' -LocalOutputPath '.\output'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Export-DatabricksClustersFromWorkspace {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)]$config,
        [parameter(Mandatory = $true)][string]$bearerToken,    
        [parameter(Mandatory = $true)][string]$localOutputPath
    )
    
    try {
        $clusters = Get-DatabricksClusters -BearerToken $bearerToken -Region $config.Region 
        $noneExistantClusters = @()
        $existentClusters = @()
    
        if ($config.getClustersByName) {
            Write-Verbose "getClustersByName config key supplied"
            if ($config.getClustersByName.Length -ge 1) {
                if ($config.getClustersByName[0].ToString().ToUpper() -eq "ALL") {
                    Write-Verbose "All parameter supplied. Pulling all cluster configurations."
                    foreach ($cluster in $clusters) {
                        Export-DatabricksClusterByNameAsJson -BearerToken $bearerToken -Region $config.Region -ClusterName $cluster.cluster_name -LocalOutputPath $localOutputPath -Verbose       
                    }            
                }
                else {
                    # Check if clusters exist in the first place
                    foreach ($cluster in $config.getClustersByName) {
                        if ($cluster -notin $clusters.cluster_name) {
                            $noneExistantClusters += $cluster
                        }          
                        else {
                            $existentClusters += $cluster
                        }           
                    }
                    if ($noneExistantClusters) {
                        Write-Warning "There are clusters that do not exist to get, These will be skipped. `n`n $($noneExistantClusters)"
                    }
                    # If we've got this far, it should mean we are good to export all the known clusters by name
                    foreach ($cluster in $existentClusters) {
                        Export-DatabricksClusterByNameAsJson -BearerToken $bearerToken -Region $config.Region -ClusterName $cluster -LocalOutputPath $localOutputPath -Verbose           
                    }
                }           
            } 
        }     
        else {
            Write-Verbose "getClustersByName config key not supplied"
        }
    }    
    catch {
        #uh oh
        throw $_.Exception
    }
}