databricks / terraform-provider-databricks

Databricks Terraform Provider
https://registry.terraform.io/providers/databricks/databricks/latest
Other
454 stars 392 forks source link

[FEATURE] add databricks_workspace data source to retrieve an existing workspace #1865

Open riturr opened 1 year ago

riturr commented 1 year ago

Use-cases

Similar to #1651, I need to reference an existing workspace to attach it to a metastore. Currently we can only hardcode the workspace ID.

Proposal

data "databricks_workspace" "this" {
  name = "MyWorkspace"
}

resource "databricks_metastore_assignment" "this" {
  metastore_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  workspace_id = data.databricks_workspace.this.id
}

References

1651

natbaker commented 1 year ago

I believe you can get this from databricks_mws_workspaces


provider "databricks" {
  alias = "mws"
  account_id = "abcd-0123"
}

data "databricks_mws_workspaces" "all" {
    provider = databricks.mws 
}

resource "databricks_metastore_assignment" "this" {
  metastore_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  workspace_id = data.databricks_mws_workspaces.all.ids["MyWorkspace"]
}
alexott commented 1 year ago

What about adding it to the current_user data source? We already have workspace URL there...

riturr commented 1 year ago

I believe you can get this from databricks_mws_workspaces

provider "databricks" {
  alias = "mws"
  account_id = "abcd-0123"
}

data "databricks_mws_workspaces" "all" {
    provider = databricks.mws 
}

resource "databricks_metastore_assignment" "this" {
  metastore_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  workspace_id = data.databricks_mws_workspaces.all.ids["MyWorkspace"]
}

It doesn't seem to work for me, Am I missing something?. I'm using Azure Databricks.

When trying to resolve the data block Terraform throws an error not being able to parse the response:

Error: cannot read data mws workspaces: invalid character '<' looking for beginning of value │ <!doctype html><html lang="en"><... (695 more bytes)

I tried calling separately that endpoint (https://accounts.azuredatabricks.net/api/2.0/accounts/account-id/workspaces) and it returns:

<!doctype html>

<html lang="en">

<head>

    <meta charset="utf-8" />

    <meta name="viewport" content="width=device-width,initial-scale=1" />

    <meta name="theme-color" content="#000000" />

    <meta name="description" content="Databricks Sign in" />

    <title>Databricks - Sign in</title>
</head>
<body><noscript>You need to enable JavaScript to run this app.</noscript>

    <div id="login"></div>

</body>

</html>
mgyucht commented 1 year ago

I don't believe the databricks_mws_* APIs work in Azure. You probably need to use azurerm_databricks_workspace and reference the id attribute. See https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/databricks_workspace for more details.