forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data sources for retrieving UC tables (databricks#1155)
Add data sources for retrieving UC entity names: ```hcl data "databricks_schemas" "sandbox" { catalog_name = "sandbox" } output "all_sandbox_schemas" { value = data.databricks_schemas.sandbox } data "databricks_tables" "things" { catalog_name = "sandbox" schema_name = "things" } output "all_things_tables" { value = data.databricks_tables.things } data "databricks_catalogs" "all" {} output "all_catalogs" { value = data.databricks_catalogs.all } ``` Fixes databricks#1105
- Loading branch information
Showing
22 changed files
with
412 additions
and
23 deletions.
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 |
---|---|---|
|
@@ -338,4 +338,6 @@ tf.log | |
*.env | ||
*~ | ||
|
||
scripts/tt | ||
scripts/tt | ||
|
||
.metals |
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,25 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceCatalogs() *schema.Resource { | ||
var data struct { | ||
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` | ||
} | ||
return common.DataResource(&data, func(ctx context.Context, c *common.DatabricksClient) error { | ||
catalogsAPI := NewCatalogsAPI(ctx, c) | ||
catalogs, err := catalogsAPI.list() | ||
if err != nil { | ||
return err | ||
} | ||
for _, v := range catalogs.Catalogs { | ||
data.Ids = append(data.Ids, v.Name) | ||
} | ||
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,42 @@ | ||
package catalog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestCatalogsData(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/unity-catalog/catalogs", | ||
Response: Catalogs{ | ||
Catalogs: []CatalogInfo{ | ||
{ | ||
Name: "b", | ||
}, | ||
{ | ||
Name: "a", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceCatalogs(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ApplyNoError(t) | ||
} | ||
|
||
func TestCatalogsData_Error(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceCatalogs(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.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,26 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceSchemas() *schema.Resource { | ||
var data struct { | ||
CatalogName string `json:"catalog_name"` | ||
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` | ||
} | ||
return common.DataResource(&data, func(ctx context.Context, c *common.DatabricksClient) error { | ||
schemasAPI := NewSchemasAPI(ctx, c) | ||
schemas, err := schemasAPI.listByCatalog(data.CatalogName) | ||
if err != nil { | ||
return err | ||
} | ||
for _, v := range schemas.Schemas { | ||
data.Ids = append(data.Ids, v.FullName) | ||
} | ||
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,43 @@ | ||
package catalog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestSchemasData(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/unity-catalog/schemas?catalog_name=a", | ||
Response: Schemas{ | ||
Schemas: []SchemaInfo{ | ||
{ | ||
Name: "a.c", | ||
}, | ||
{ | ||
Name: "a.d", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceSchemas(), | ||
HCL: `catalog_name = "a"`, | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ApplyNoError(t) | ||
} | ||
|
||
func TestSchemasData_Error(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceSchemas(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.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,27 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceTables() *schema.Resource { | ||
var data struct { | ||
CatalogName string `json:"catalog_name"` | ||
SchemaName string `json:"schema_name"` | ||
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` | ||
} | ||
return common.DataResource(&data, func(ctx context.Context, c *common.DatabricksClient) error { | ||
tablesAPI := NewTablesAPI(ctx, c) | ||
tables, err := tablesAPI.listTables(data.CatalogName, data.SchemaName) | ||
if err != nil { | ||
return err | ||
} | ||
for _, v := range tables.Tables { | ||
data.Ids = append(data.Ids, v.FullName()) | ||
} | ||
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,45 @@ | ||
package catalog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databrickslabs/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestTablesData(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/unity-catalog/tables/?catalog_name=a&schema_name=b", | ||
Response: Tables{ | ||
Tables: []TableInfo{ | ||
{ | ||
Name: "a.b.c", | ||
}, | ||
{ | ||
Name: "a.b.d", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resource: DataSourceTables(), | ||
HCL: ` | ||
catalog_name = "a" | ||
schema_name = "b"`, | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ApplyNoError(t) | ||
} | ||
|
||
func TestTablesData_Error(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceTables(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.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
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
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
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,34 @@ | ||
--- | ||
subcategory: "Unity Catalog" | ||
--- | ||
# databricks_catalogs 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 [databricks_catalog](../resources/catalog.md) ids, that were created by Terraform or manually, so that special handling could be applied. | ||
|
||
## Example Usage | ||
|
||
Listing all catalogs: | ||
|
||
```hcl | ||
data "databricks_catalogs" "all" {} | ||
output "all_catalogs" { | ||
value = data.databricks_catalogs.all | ||
} | ||
``` | ||
|
||
## Attribute Reference | ||
|
||
This data source exports the following attributes: | ||
|
||
* `ids` - set of [databricks_catalog](../resources/catalog.md) names | ||
|
||
## Related Resources | ||
|
||
The following resources are used in the same context: | ||
|
||
* [databricks_table](../resources/table.md) to manage tables within Unity Catalog. | ||
* [databricks_schema](../resources/schema.md) to manage schemas within Unity Catalog. | ||
* [databricks_catalog](../resources/catalog.md) to manage catalogs within Unity Catalog. |
Oops, something went wrong.