-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for Unity Catalog databricks_metastores
data source
#2017
Changes from 38 commits
12b841e
8b10dab
9082709
921c288
f0d146f
22825c6
399d76d
4eaa8c2
857d28e
e345f2f
7b4f4a2
1473c0e
53cbf83
5601f47
3dfbdbc
1beb87a
70f9fa4
aa70996
76c5d2b
6c111e6
c04d6c5
0f4c815
fe9bf12
d469bdf
3853b84
014dd6f
a77a717
ee3e70f
c9a3f4f
ba97743
cf89129
aaad551
85da2fe
df55236
c78d412
8d27960
a3efb4c
1f5142f
eb40c81
fd9408d
d6bf74d
3e0eb03
c8f790f
d173572
e97710f
32f80aa
95c0b94
c81d646
6dfade4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databricks/databricks-sdk-go" | ||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceMetastores() *schema.Resource { | ||
return common.AccountData(func(ctx context.Context, data *MetastoresData, acc *databricks.AccountClient) error { | ||
metastores, err := acc.Metastores.List(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
for _, v := range metastores.Metastores { | ||
data.Ids = append(data.Ids, v.MetastoreId) | ||
} | ||
return nil | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package catalog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databricks/databricks-sdk-go/service/catalog" | ||
"github.com/databricks/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestMetastoresData(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/accounts/testaccount/metastores", | ||
tanmay-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Response: catalog.ListMetastoresResponse{ | ||
Metastores: []catalog.MetastoreInfo{ | ||
{ | ||
Name: "a", | ||
StorageRoot: "", | ||
DefaultDataAccessConfigId: "sth", | ||
Owner: "John.Doe@example.com", | ||
MetastoreId: "abc", | ||
Region: "", | ||
Cloud: "", | ||
GlobalMetastoreId: "", | ||
CreatedAt: 0, | ||
CreatedBy: "", | ||
UpdatedAt: 0, | ||
UpdatedBy: "", | ||
DeltaSharingScope: "", | ||
DeltaSharingRecipientTokenLifetimeInSeconds: 0, | ||
DeltaSharingOrganizationName: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceMetastores(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
AccountID: "testaccount", | ||
}.ApplyNoError(t) | ||
guillesd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func TestMetastoresDataContainsName(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/accounts/testaccount/metastores", | ||
Response: catalog.ListMetastoresResponse{ | ||
Metastores: []catalog.MetastoreInfo{ | ||
{ | ||
Name: "a", | ||
StorageRoot: "abc", | ||
DefaultDataAccessConfigId: "sth", | ||
Owner: "John.Doe@example.com", | ||
MetastoreId: "abc", | ||
}, | ||
{ | ||
Name: "b", | ||
StorageRoot: "dcw", | ||
DefaultDataAccessConfigId: "sth", | ||
Owner: "John.Doe@example.com", | ||
MetastoreId: "ded", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceMetastores(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
AccountID: "testaccount", | ||
}.ApplyAndExpectData(t, map[string]any{ | ||
"ids": []string{"abc", "ded"}, | ||
}) | ||
} | ||
|
||
func TestMetastoresData_Error(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceMetastores(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
AccountID: "_", | ||
}.ExpectError(t, "I'm a teapot") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,16 +38,15 @@ type MetastoreInfo struct { | |
DeltaSharingOrganizationName string `json:"delta_sharing_organization_name,omitempty"` | ||
} | ||
|
||
type MetastoresData struct { | ||
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` | ||
} | ||
|
||
type CreateMetastore struct { | ||
Name string `json:"name"` | ||
StorageRoot string `json:"storage_root"` | ||
} | ||
|
||
// func (a MetastoresAPI) listMetastores() (mis []MetastoreInfo, err error) { | ||
// err = a.client.Get(a.context, "/unity-catalog/metastores", nil, &mis) | ||
// return | ||
// } | ||
|
||
Comment on lines
-46
to
-50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove dead code, we already have crud |
||
func (a MetastoresAPI) createMetastore(cm CreateMetastore) (mi MetastoreInfo, err error) { | ||
err = a.client.Post(a.context, "/unity-catalog/metastores", cm, &mi) | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
subcategory: "Unity Catalog" | ||
--- | ||
# databricks_metastores Data Source | ||
|
||
-> **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. | ||
|
||
Retrieves a list of ids of [databricks_metastore](../resources/metastore.md) objects, that were created by Terraform or manually, so that special handling could be applied. | ||
|
||
## Example Usage | ||
|
||
Listing ids of all catalogs: | ||
|
||
```hcl | ||
data "databricks_metastores" "all" {} | ||
|
||
output "all_metastores" { | ||
value = data.databricks_metastores.all | ||
} | ||
``` | ||
|
||
## Attribute Reference | ||
|
||
This data source exports the following attributes: | ||
|
||
* `metastores` - list of ids of [databricks_metastore](../resources/metastore.md) | ||
|
||
## Related Resources | ||
|
||
The following resources are used in the same context: | ||
|
||
* [databricks_metastore](../resources/metastore.md) to manage Metastores within Unity Catalog. | ||
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package acceptance | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUcAccDataSourceMetastore(t *testing.T) { | ||
accountLevel(t, step{ | ||
Template: ` | ||
data "databricks_metastores" "this" {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a reasonable integration test for the "no metastores" case. Can we add an integration test for the "one or more metastores case"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mgyucht not sure how easy it is to test both scenarios in a single account, unless we add a filter option to this data source There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are going to add another data source for specific metastore that contains the full information. Creating a specific metastore and getting it's details will be part of integration test for that data source. Marking this as non blocking for this data source since we have permanent metastores for our account. Also verified locally looking at the results of using the resource. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check has been added in the test |
||
|
||
output "all_metastores" { | ||
value = data.databricks_metastores.this | ||
}`, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is added in https://github.com/databricks/terraform-provider-databricks/pull/2429/files