Functions/Export-DatabricksClustersLibrariesFromWorkspace.ps1

<#
.SYNOPSIS
Exports DataBricks Clusters Libraries and Saves as json.
 
.DESCRIPTION
Exports Databricks Clusters Libraries 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-DatabricksClustersLibrariesFromWorkspace -config $config -bearerToken 'dapi1234567890' -LocalOutputPath '.\output'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Export-DatabricksClustersLibrariesFromWorkspace {
    [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-DatabricksClusterLibrariesByNameAsJson -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-DatabricksClusterLibrariesByNameAsJson -BearerToken $bearerToken -Region $config.Region -ClusterName $cluster -LocalOutputPath $localOutputPath -Verbose
                    }
                }           
            } 
        }     
        else {
            Write-Verbose "getClustersByName config key not supplied"
        }
    }    
    catch {
        #uh oh
        throw $_.Exception
    }
}