Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Unity Catalog
databricks_metastore
data source (#…
…2492) Enable fetching account level metastore information through id for a single metastore.
- Loading branch information
Showing
7 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databricks/databricks-sdk-go" | ||
"github.com/databricks/databricks-sdk-go/service/catalog" | ||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceMetastore() *schema.Resource { | ||
type AccountMetastoreByID struct { | ||
Id string `json:"metastore_id"` | ||
Metastore *catalog.MetastoreInfo `json:"metastore_info,omitempty" tf:"computed" ` | ||
} | ||
return common.AccountData(func(ctx context.Context, data *AccountMetastoreByID, acc *databricks.AccountClient) error { | ||
metastore, err := acc.Metastores.GetByMetastoreId(ctx, data.Id) | ||
if err != nil { | ||
return err | ||
} | ||
data.Metastore = metastore.MetastoreInfo | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package catalog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databricks/databricks-sdk-go/service/catalog" | ||
"github.com/databricks/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestMetastoreDataVerify(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/accounts/testaccount/metastores/abc?", | ||
Response: catalog.AccountsMetastoreInfo{ | ||
MetastoreInfo: &catalog.MetastoreInfo{ | ||
Name: "xyz", | ||
MetastoreId: "abc", | ||
Owner: "pqr", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceMetastore(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
AccountID: "testaccount", | ||
HCL: ` | ||
metastore_id = "abc" | ||
`, | ||
}.ApplyAndExpectData(t, map[string]any{ | ||
"metastore_info.0.name": "xyz", | ||
"metastore_info.0.owner": "pqr", | ||
"metastore_info.0.metastore_id": "abc", | ||
}) | ||
} | ||
|
||
func TestMetastoreDataError(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceMetastore(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
AccountID: "_", | ||
}.ExpectError(t, "I'm a teapot") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
subcategory: "Unity Catalog" | ||
--- | ||
# databricks_metastore Data Source | ||
|
||
Retrieves information about metastore for a given id of [databricks_metastore](../resources/metastore.md) object, that was created by Terraform or manually, so that special handling could be applied. | ||
|
||
-> **Note** If you have a fully automated setup with workspaces created by [databricks_mws_workspaces](../resources/mws_workspaces.md) or [azurerm_databricks_workspace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/databricks_workspace), please make sure to add [depends_on attribute](../index.md#data-resources-and-authentication-is-not-configured-errors) in order to prevent _authentication is not configured for provider_ errors. | ||
|
||
## Example Usage | ||
|
||
MetastoreInfo response for a given metastore id | ||
|
||
```hcl | ||
resource "databricks_metastore" "this" { | ||
provider = databricks.workspace | ||
name = "primary" | ||
storage_root = "s3://${aws_s3_bucket.metastore.id}/metastore" | ||
owner = var.unity_admin_group | ||
force_destroy = true | ||
} | ||
data "databricks_metastore" "this" { | ||
id = databricks_metastore.this.id | ||
} | ||
output "some_metastore" { | ||
value = data.databricks_metastore.this.metastore_info[0] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
* `metastore_id` - Id of the metastore to be fetched | ||
|
||
## Attribute Reference | ||
|
||
This data source exports the following attributes: | ||
* `metastore_info` - MetastoreInfo object for a [databricks_metastore](../resources/metastore.md). This contains the following attributes: | ||
* `name` - Name of metastore. | ||
* `storage_root` - Path on cloud storage account, where managed `databricks_table` are stored. Change forces creation of a new resource. | ||
* `owner` - Username/groupname/sp application_id of the metastore owner. | ||
* `delta_sharing_scope` - Used to enable delta sharing on the metastore. Valid values: INTERNAL, INTERNAL_AND_EXTERNAL. | ||
* `delta_sharing_recipient_token_lifetime_in_seconds` - Used to set expiration duration in seconds on recipient data access tokens. | ||
* `delta_sharing_organization_name` - The organization name of a Delta Sharing entity. This field is used for Databricks to Databricks sharing. | ||
|
||
|
||
## Related Resources | ||
|
||
The following resources are used in the same context: | ||
* [databricks_metastores](./metastores.md) to get mapping of name to id of all metastores. | ||
* [databricks_metastore](../resources/metastore.md) to manage Metastores within Unity Catalog. | ||
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestUcAccDataSourceMetastore(t *testing.T) { | ||
accountLevel(t, step{ | ||
Template: ` | ||
data "databricks_metastore" "this" { | ||
metastore_id = "{env.TEST_METASTORE_ID}" | ||
}`, | ||
Check: func(s *terraform.State) error { | ||
r, ok := s.RootModule().Resources["data.databricks_metastore.this"] | ||
if !ok { | ||
return fmt.Errorf("data not found in state") | ||
} | ||
metastore_info := r.Primary.Attributes["metastore_info.0.%"] | ||
if metastore_info == "" { | ||
return fmt.Errorf("MetastoreInfo is empty: %v", r.Primary.Attributes) | ||
} | ||
return nil | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters