Skip to content

Commit

Permalink
Added support for Unity Catalog databricks_metastore data source (#…
Browse files Browse the repository at this point in the history
…2492)

Enable fetching account level metastore information through id for a single metastore.
  • Loading branch information
tanmay-db committed Jul 20, 2023
1 parent 0a23d5e commit 5114857
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
25 changes: 25 additions & 0 deletions catalog/data_metastore.go
@@ -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
})
}
49 changes: 49 additions & 0 deletions catalog/data_metastore_test.go
@@ -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")
}
53 changes: 53 additions & 0 deletions docs/data-sources/metastore.md
@@ -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.
1 change: 1 addition & 0 deletions docs/data-sources/metastores.md
Expand Up @@ -29,5 +29,6 @@ This data source exports the following attributes:

The following resources are used in the same context:

* [databricks_metastore](./metastore.md) to get information about a single metastore.
* [databricks_metastore](../resources/metastore.md) to manage Metastores within Unity Catalog.
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog.
28 changes: 28 additions & 0 deletions internal/acceptance/data_metastore_test.go
@@ -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
},
})
}
3 changes: 2 additions & 1 deletion jobs/resource_job.go
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/databricks/databricks-sdk-go/service/jobs"
"log"
"sort"
"strconv"
"strings"
"time"

"github.com/databricks/databricks-sdk-go/service/jobs"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Expand Up @@ -62,6 +62,7 @@ func DatabricksProvider() *schema.Provider {
"databricks_instance_pool": pools.DataSourceInstancePool(),
"databricks_jobs": jobs.DataSourceJobs(),
"databricks_job": jobs.DataSourceJob(),
"databricks_metastore": catalog.DataSourceMetastore(),
"databricks_metastores": catalog.DataSourceMetastores(),
"databricks_mws_credentials": mws.DataSourceMwsCredentials(),
"databricks_mws_workspaces": mws.DataSourceMwsWorkspaces(),
Expand Down

0 comments on commit 5114857

Please sign in to comment.