cancel
Showing results for 
Search instead for 
Did you mean: 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 

Add a tag to a catalog with REST API

Marco37
Contributor

Goodday,

I have an Azure DevOps pipeline with which I provision our Databricks workspaces. With this pipeline I also create the catalogs through the Databricks API (/api/2.1/unity-catalog/catalogs).
Create a catalog | Catalogs API | REST API reference | Azure Databricks

I also want to add a tag to the catalog, but i cannot figure out how to do this through this or another REST API.

Is this possible?

Marco37_0-1755176222089.png

Kind regards,
Marco

1 ACCEPTED SOLUTION

Accepted Solutions

Thanks szymon_dybczak,

I have solved it with the "/api/2.0/sql/statements/" REST API

 

param(

    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $FullDatabricksName,
    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $ResourceGroupName
)

Function DatabricksConfig
{
    param(
            [string]$Method,
            [string]$API,
            [string]$Body = "",
            [string]$FullDatabricksName,
            [boolean]$AccountConsole = $false
        )

    If ($AccountConsole -eq $true)
    {
        $Url = $Url = "https://accounts.azuredatabricks.net"+$API
    }
    Else
    {
        $Databricks_Workspace_Url = (Get-AzDatabricksWorkspace -Name $FullDatabricksName -ResourceGroupName $ResourceGroupName).Url
        $Url = "https://"+$Databricks_Workspace_Url+$API
    }

    #Authentication header
    $azureApiToken = (ConvertFrom-SecureString (Get-AzAccessToken -Resource "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" -AsSecureString).Token -AsPlainText)# "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" represents the programmatic ID for Azure Databricks along with the default scope (/.default, URL-encoded as %2f.default).
    
    $authHeader = @{
        "Authorization"="Bearer " + "$azureApiToken";
        "Content-Type"="application/json";
    }

    If ($Body -eq "")
    {
        $Result = Invoke-RestMethod -Method $Method -Uri $Url -Headers $authHeader -ErrorAction SilentlyContinue
    }
    Else
    {
        $Result = Invoke-RestMethod -Method $Method -Uri $Url -Headers $authHeader -Body $Body -ErrorAction SilentlyContinue
    }
    Return $Result
}

$WarehouseId = ((DatabricksConfig -Method "Get" -API "/api/2.0/sql/warehouses" -FullDatabricksName $FullDatabricksName).warehouses | Where-Object name -eq "Serverless Starter Warehouse").id

$Body = Get-Content "./GetCatalogProps/SQL.json" | ConvertFrom-JSON
$Body.statement = "ALTER CATALOG curated SET TAGS ('tag1' = 'val1');"
$Body.warehouse_id = $WarehouseId
$Body = $Body | ConvertTo-JSON

DatabricksConfig -Body $Body -Method "Post" -API "/api/2.0/sql/statements/" -FullDatabricksName $FullDatabricksName

 

Regards,
Marco

View solution in original post

3 REPLIES 3

szymon_dybczak
Esteemed Contributor III

Hi @Marco37 ,

Unfortunately, I believe this is not currently possible through REST API. The only options you have are via UI or using SQL. 

Unfortunately, it's also no possible to do this using terraform. There's a PR that is hanging from some time, but still no progress.

https://github.com/databricks/terraform-provider-databricks/pull/3268

Thanks szymon_dybczak,

I have solved it with the "/api/2.0/sql/statements/" REST API

 

param(

    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $FullDatabricksName,
    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)] [System.String] $ResourceGroupName
)

Function DatabricksConfig
{
    param(
            [string]$Method,
            [string]$API,
            [string]$Body = "",
            [string]$FullDatabricksName,
            [boolean]$AccountConsole = $false
        )

    If ($AccountConsole -eq $true)
    {
        $Url = $Url = "https://accounts.azuredatabricks.net"+$API
    }
    Else
    {
        $Databricks_Workspace_Url = (Get-AzDatabricksWorkspace -Name $FullDatabricksName -ResourceGroupName $ResourceGroupName).Url
        $Url = "https://"+$Databricks_Workspace_Url+$API
    }

    #Authentication header
    $azureApiToken = (ConvertFrom-SecureString (Get-AzAccessToken -Resource "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" -AsSecureString).Token -AsPlainText)# "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" represents the programmatic ID for Azure Databricks along with the default scope (/.default, URL-encoded as %2f.default).
    
    $authHeader = @{
        "Authorization"="Bearer " + "$azureApiToken";
        "Content-Type"="application/json";
    }

    If ($Body -eq "")
    {
        $Result = Invoke-RestMethod -Method $Method -Uri $Url -Headers $authHeader -ErrorAction SilentlyContinue
    }
    Else
    {
        $Result = Invoke-RestMethod -Method $Method -Uri $Url -Headers $authHeader -Body $Body -ErrorAction SilentlyContinue
    }
    Return $Result
}

$WarehouseId = ((DatabricksConfig -Method "Get" -API "/api/2.0/sql/warehouses" -FullDatabricksName $FullDatabricksName).warehouses | Where-Object name -eq "Serverless Starter Warehouse").id

$Body = Get-Content "./GetCatalogProps/SQL.json" | ConvertFrom-JSON
$Body.statement = "ALTER CATALOG curated SET TAGS ('tag1' = 'val1');"
$Body.warehouse_id = $WarehouseId
$Body = $Body | ConvertTo-JSON

DatabricksConfig -Body $Body -Method "Post" -API "/api/2.0/sql/statements/" -FullDatabricksName $FullDatabricksName

 

Regards,
Marco

szymon_dybczak
Esteemed Contributor III

Hi @Marco37 ,

Thanks for sharing solution. Could you mark your solution as a best answer? This helps others with similar question find correct answer faster.