Functions/Publish-DatabricksClustersLibrariesFromConfigToWorkspace.ps1

<#
.SYNOPSIS
Deploys DataBricks Cluster(s) from configuration json file(s) to a workspace
 
.DESCRIPTION
Deploys DataBricks Cluster(s) from configuration json file(s) 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 bearerToken
Your Databricks Bearer token to authenticate to your workspace (see User Settings in Datatbricks WebUI)
 
.PARAMETER localOutputPath
The name path of the clusters configuration files.
 
.EXAMPLE
Publish-DatabricksClustersLibrariesFromConfigToWorkspace -config $config -bearerToken 'dapi1234567890'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Publish-DatabricksClustersLibrariesFromConfigToWorkspace {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)]$config,
        [parameter(Mandatory = $true)][string]$bearerToken,
        [parameter(Mandatory = $true)][string]$localOutputPath
    )
    
    try {

        if (($config.deployLibrariesByFolder -eq $true) -and ($config.deployLibrariesByFileNames.Length -ge 1)) {
            Write-Error "deployLibrariesByFolder in config set to true and Libraries has values. Options are mutually exclusive!"
            Throw
        }

        if (($config.deployLibrariesByFolder -eq $true) -and ($config.deployLibrariesByFileNames.Length -eq 0)) {
            Write-Verbose "[Deploy] clusters libraries by folder, Ignoring deployLibrariesByFileNames"  
            $litPath = (Join-Path $localOutputPath 'libraries')
            $librariesConfigFiles = Get-ChildItem -LiteralPath $litPath -Filter "*.cluster.libraries.config.json"
            foreach ($libraryConfig in $librariesConfigFiles) {
                
                Publish-DatabricksClusterLibrariesToWorkspaceByName -config $config `
                    -bearerToken $bearerToken `
                    -librariesConfig $libraryConfig
            }

        }

        if (($config.deployLibrariesByFolder -eq $false) -and ($config.deployLibrariesByFileNames.Length -ge 1)) {
            Write-Verbose "[Deploy] clusters libraries by file(s), Ignoring deployLibrariesByFolder"  
            foreach ($libraryConfig in $config.deployLibrariesByFileNames) {
                $tmp = Join-Path $localOutputPath 'libraries' 
                $libPath = Join-Path $tmp $libraryConfig
                if (Test-Path $libPath) {

                    Publish-DatabricksClusterLibrariesToWorkspaceByName -config $config `
                        -bearerToken $bearerToken `
                        -librariesConfig $libPath
                }
                else {
                    Write-Error "Path $($libPath) does not exist. Check file names are correct."
                }

                
            }
        }

    }    
    catch {
        #uh oh
        throw $_.Exception
    }
}