-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
databricks_mws_workspaces
data resource (#1497)
- Loading branch information
Showing
8 changed files
with
173 additions
and
0 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
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,42 @@ | ||
--- | ||
subcategory: "AWS" | ||
--- | ||
# databricks_mws_workspaces 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. | ||
|
||
Lists all [databricks_mws_workspaces](../resources/mws_workspaces.md) in Databricks Account. | ||
|
||
-> **Note** [`account_id`](../index.md#account_id) provider configuration property is required for this resource to work. | ||
|
||
## Example Usage | ||
|
||
Listing all workspaces in | ||
|
||
```hcl | ||
provider "databricks" { | ||
// other configuration | ||
account_id = "<databricks account id>" | ||
} | ||
data "databricks_mws_workspaces" "all" {} | ||
output "all_mws_workspaces" { | ||
value = data.databricks_mws_workspaces.all.ids | ||
} | ||
``` | ||
|
||
## Attribute Reference | ||
|
||
-> **Note** This resource has an evolving interface, which may change in future versions of the provider. | ||
|
||
This data source exports the following attributes: | ||
|
||
* `ids` - name-to-id map for all of the workspaces in the account | ||
|
||
## Related Resources | ||
|
||
The following resources are used in the same context: | ||
|
||
* [databricks_mws_workspaces](../resources/mws_workspaces.md) to manage Databricks E2 Workspaces. | ||
* [databricks_metastore_assignment](../resources/metastore_assignment.md) to assign [databricks_metastore](docs/resources/metastore.md) to [databricks_mws_workspaces](../resources/mws_workspaces.md) or [azurerm_databricks_workspace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/databricks_workspace) |
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,36 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/databricks/terraform-provider-databricks/internal/acceptance" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"testing" | ||
) | ||
|
||
func TestAccDataSourceMwsWorkspaces(t *testing.T) { | ||
cloudEnv := os.Getenv("CLOUD_ENV") | ||
if cloudEnv != "MWS" { | ||
t.Skip("Cannot run test on non-MWS environment") | ||
} | ||
acceptance.Test(t, []acceptance.Step{ | ||
{ | ||
Template: ` | ||
data "databricks_mws_workspaces" "this" { | ||
}`, | ||
Check: func(s *terraform.State) error { | ||
r, ok := s.RootModule().Resources["data.databricks_mws_workspaces.this"] | ||
if !ok { | ||
return fmt.Errorf("data not found in state") | ||
} | ||
ids := r.Primary.Attributes["ids.%"] | ||
if ids == "" { | ||
return fmt.Errorf("ids 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceMwsWorkspaces() *schema.Resource { | ||
type mwsWorkspacesData struct { | ||
Ids map[string]int64 `json:"ids,omitempty" tf:"computed"` | ||
} | ||
return common.DataResource(mwsWorkspacesData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error { | ||
data := e.(*mwsWorkspacesData) | ||
if c.AccountID == "" { | ||
return fmt.Errorf("provider block is missing `account_id` property") | ||
} | ||
workspaces, err := NewWorkspacesAPI(ctx, c).List(c.AccountID) | ||
if err != nil { | ||
return err | ||
} | ||
data.Ids = map[string]int64{} | ||
for _, v := range workspaces { | ||
data.Ids[v.WorkspaceName] = v.WorkspaceID | ||
} | ||
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,50 @@ | ||
package mws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databricks/terraform-provider-databricks/qa" | ||
) | ||
|
||
func TestDataSourceMwsWorkspaces(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/accounts/abc/workspaces", | ||
|
||
Response: []Workspace{ | ||
{ | ||
WorkspaceName: "bcd", | ||
WorkspaceID: 123, | ||
}, | ||
{ | ||
WorkspaceName: "def", | ||
WorkspaceID: 456, | ||
}, | ||
}, | ||
}, | ||
}, | ||
AccountID: "abc", | ||
Resource: DataSourceMwsWorkspaces(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ApplyAndExpectData(t, map[string]any{ | ||
"ids": map[string]any{ | ||
"bcd": 123, | ||
"def": 456, | ||
}, | ||
}) | ||
} | ||
|
||
func TestCatalogsData_Error(t *testing.T) { | ||
qa.ResourceFixture{ | ||
AccountID: "abc", | ||
Fixtures: qa.HTTPFailures, | ||
Resource: DataSourceMwsWorkspaces(), | ||
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