powershelldatabricksazure-databricks

Databricks API - can't list content of workspace


Im trying to list the content under my user using databricks API. Below is my code

`if (-not (Get-PackageProvider -ListAvailable -Name NuGet)) { 
    Install-PackageProvider nuget -Scope CurrentUser -Force
}
if (-not (Get-Module -ListAvailable -Name azure.databricks.cicd.tools)) { 
    Install-Module -Name azure.databricks.cicd.tools -Scope CurrentUser -Force
}

Import-Module -Name azure.databricks.cicd.tools

Function Format-BearerToken ($BearerToken) {
    Return "Bearer $BearerToken"
}

try {
    # Attempt to connect to Databricks
    Connect-Databricks -BearerToken $ADB_Token -Region $region

    # List the content of the user folder
    # Assuming the user folder is "/Users/your.username" - replace with your actual user path
    $userFolderPath = "/Users/mysuer@email.com/"  # Replace with your actual Databricks user folder path

    # Create the body as a hashtable (note the difference in creating a hashtable)
    $body = @{
        path = $userFolderPath
    }
    # Debug: Check the Body that will be sent
    $BodyText = $body | ConvertTo-Json -Depth 10
    Write-Output "Request Body: $BodyText"


    # Test the connection by making an API call to list the workspace contents in your user folder
    $workspaceContent = Invoke-DatabricksAPI -BearerToken $ADB_Token -Region $region `
        -Method GET -API "workspace/list" -Body $BodyText

    # Output the content
    $workspaceContent
}
catch {
    Write-Error "An error occurred: $_"
}`

my output of path parameter looks like this

Request Body: {
  "path": "/Users/mysuer@email.com/"
}

But I get the error

An error occurred: Cannot process argument transformation on parameter 'Body'. Cannot convert the value of type "{"path": "/Users/mysuer@email.com/"} "System.String" to type "System.Collections.Hashtable".

I have also tried without $BodyText = $body | ConvertTo-Json -Depth 10 and in that case I get the erro code 404 - 404 RESOURCE_DOES_NOT_EXIST Operation was performed on a resource that does not exist.

My user name is correct and I do have test.py file under it.

I have two question:

  1. how can I list content? 2)Since I could not list the content Im think if the authorization has succsseded, but other wise first I would have got 401 UNAUTHORIZED The request does not have valid authentication credentials for the operation. - is this correct?

Solution

  • according to this documentation the body should be of type Hashtable

    example: @{clusterId="abc-123";name="bob"}

    and you are giving String type also the api workspace/list dosen't exists instead use /api/2.0/workspace/list.

    Next, the path should be given in parameter not in the body.

    Use below code.

    $apiUrl = "/api/2.0/workspace/list?path=$userFolderPath"
    
    Write-Output "Request Body: $apiUrl"
    
    $workspaceContent = Invoke-DatabricksAPI -BearerToken $ADB_Token -Region $region -Method GET -API $apiUrl
    
    $workspaceContent
    

    Output:

    enter image description here