Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Add resource and data sources for `databricks_environments_workspace_base_environment`.
* Add resource and data source for `databricks_environments_default_workspace_base_environment`.

* Added optional `cloud` argument to `databricks_current_config` data source to explicitly set the cloud type (`aws`, `azure`, `gcp`) instead of relying on host-based detection.

* Added `api` field to dual account/workspace resources (`databricks_user`, `databricks_service_principal`, `databricks_group`, `databricks_group_role`, `databricks_group_member`, `databricks_user_role`, `databricks_service_principal_role`, `databricks_user_instance_profile`, `databricks_group_instance_profile`, `databricks_metastore`, `databricks_metastore_assignment`, `databricks_metastore_data_access`, `databricks_storage_credential`, `databricks_service_principal_secret`, `databricks_access_control_rule_set`) to explicitly control whether account-level or workspace-level APIs are used. This enables support for unified hosts like `api.databricks.com` where the API level cannot be inferred from the host ([#5483](https://github.com/databricks/terraform-provider-databricks/pull/5483)).

### Bug Fixes
Expand Down
3 changes: 2 additions & 1 deletion docs/data-sources/current_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ resource "databricks_storage_credential" "external" {

## Argument Reference

* `cloud` - (Optional) Explicitly set the cloud type. Must be one of `aws`, `azure`, or `gcp`. If not set, the cloud type is determined automatically from the provider configuration. It is recommended to set this explicitly to avoid relying on host-based detection.
* `provider_config` - (Optional) Configure the provider for management through account provider. This block consists of the following fields:
* `workspace_id` - (Required) Workspace ID which the resource belongs to. This workspace must be part of the account which the provider is configured with.

Expand All @@ -51,7 +52,7 @@ Data source exposes the following attributes:
* `is_account` - Whether the provider is configured at account-level
* `account_id` - Account Id if provider is configured at account-level
* `host` - Host of the Databricks workspace or account console
* `cloud_type` - Cloud type specified in the provider
* `cloud_type` - Cloud type of the provider. If `cloud` argument is set, this will match that value. Otherwise, it is determined from the provider configuration.
* `auth_type` - Auth type used by the provider

## Related Resources
Expand Down
29 changes: 21 additions & 8 deletions mws/data_current_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,48 @@ import (

"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

type currentConfig struct {
Cloud string `json:"cloud,omitempty" tf:"optional"`
IsAccount bool `json:"is_account,omitempty" tf:"computed"`
AccountId string `json:"account_id,omitempty" tf:"computed"`
Host string `json:"host,omitempty" tf:"computed"`
CloudType string `json:"cloud_type,omitempty" tf:"computed"`
AuthType string `json:"auth_type,omitempty" tf:"computed"`
}

var validCloudValues = []string{"aws", "azure", "gcp"}

func cloudTypeFromConfig(cfg *config.Config) string {
if cfg.IsAws() {
return "aws"
} else if cfg.IsAzure() {
return "azure"
} else if cfg.IsGcp() {
return "gcp"
}
return "unknown"
}

func DataSourceCurrentConfiguration() common.Resource {
return common.DataResource(currentConfig{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
r := common.DataResource(currentConfig{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
data := e.(*currentConfig)
data.IsAccount = false
if c.Config.HostType() == config.AccountHost {
data.AccountId = c.Config.AccountID
data.IsAccount = true
}
data.Host = c.Config.Host
if c.Config.IsAws() {
data.CloudType = "aws"
} else if c.Config.IsAzure() {
data.CloudType = "azure"
} else if c.Config.IsGcp() {
data.CloudType = "gcp"
if data.Cloud != "" {
data.CloudType = data.Cloud
} else {
data.CloudType = "unknown"
data.CloudType = cloudTypeFromConfig(c.Config)
}
data.AuthType = c.Config.AuthType
return nil
})
r.Schema["cloud"].ValidateFunc = validation.StringInSlice(validCloudValues, false)
return r
}
54 changes: 54 additions & 0 deletions mws/data_current_config_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ func checkCurrentConfig(t *testing.T, cloudType string, isAccount string) func(s
}
}

func checkCurrentConfigWithCloud(t *testing.T, cloudType string, isAccount string, cloud string) func(s *terraform.State) error {
return func(s *terraform.State) error {
r, ok := s.Modules[0].Resources["data.databricks_current_config.this"]
require.True(t, ok, "data.databricks_current_config.this has to be there")

attr := r.Primary.Attributes

assert.Equal(t, attr["cloud_type"], cloudType)
assert.Equal(t, attr["is_account"], isAccount)
assert.Equal(t, attr["cloud"], cloud)
return nil
}
}

func TestAccDataCurrentConfig(t *testing.T) {
acceptance.LoadWorkspaceEnv(t)
if acceptance.IsAws(t) {
Expand Down Expand Up @@ -61,3 +75,43 @@ func TestMwsAccDataCurrentConfig(t *testing.T) {
})
}
}

func TestAccDataCurrentConfigCloudOverride(t *testing.T) {
acceptance.LoadWorkspaceEnv(t)
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `data "databricks_current_config" "this" {
cloud = "aws"
}`,
Check: checkCurrentConfigWithCloud(t, "aws", "false", "aws"),
})
}

func TestAccDataCurrentConfigCloudOverrideAzure(t *testing.T) {
acceptance.LoadWorkspaceEnv(t)
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `data "databricks_current_config" "this" {
cloud = "azure"
}`,
Check: checkCurrentConfigWithCloud(t, "azure", "false", "azure"),
})
}

func TestAccDataCurrentConfigCloudOverrideGcp(t *testing.T) {
acceptance.LoadWorkspaceEnv(t)
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `data "databricks_current_config" "this" {
cloud = "gcp"
}`,
Check: checkCurrentConfigWithCloud(t, "gcp", "false", "gcp"),
})
}

func TestMwsAccDataCurrentConfigCloudOverride(t *testing.T) {
acceptance.LoadAccountEnv(t)
acceptance.AccountLevel(t, acceptance.Step{
Template: `data "databricks_current_config" "this" {
cloud = "aws"
}`,
Check: checkCurrentConfigWithCloud(t, "aws", "true", "aws"),
})
}
69 changes: 69 additions & 0 deletions mws/data_current_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,72 @@ func TestDataSourceCurrentConfigAccAzure(t *testing.T) {
"cloud_type": "azure",
})
}

func TestDataSourceCurrentConfigCloudOverride(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{},
Read: true,
NonWritable: true,
Resource: DataSourceCurrentConfiguration(),
ID: ".",
HCL: `cloud = "gcp"`,
}.ApplyAndExpectData(t, map[string]any{
"is_account": false,
"cloud_type": "gcp",
"cloud": "gcp",
})
}

func TestDataSourceCurrentConfigCloudOverrideAzure(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{},
Read: true,
NonWritable: true,
Resource: DataSourceCurrentConfiguration(),
ID: ".",
HCL: `cloud = "azure"`,
}.ApplyAndExpectData(t, map[string]any{
"is_account": false,
"cloud_type": "azure",
"cloud": "azure",
})
}

func TestDataSourceCurrentConfigCloudInvalidValue(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{},
Read: true,
NonWritable: true,
Resource: DataSourceCurrentConfiguration(),
ID: ".",
HCL: `cloud = "invalid"`,
}.ExpectError(t, "invalid config supplied. [cloud] expected cloud to be one of [aws azure gcp], got invalid")
}

func TestDataSourceCurrentConfigCloudEmptyValue(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{},
Read: true,
NonWritable: true,
Resource: DataSourceCurrentConfiguration(),
ID: ".",
HCL: `cloud = ""`,
}.ExpectError(t, "invalid config supplied. [cloud] expected cloud to be one of [aws azure gcp], got ")
}

func TestDataSourceCurrentConfigCloudOverrideAccountLevel(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{},
Read: true,
NonWritable: true,
Resource: DataSourceCurrentConfiguration(),
ID: ".",
AccountID: "acc-123",
HCL: `cloud = "aws"`,
}.ApplyAndExpectData(t, map[string]any{
"is_account": true,
"account_id": "acc-123",
"cloud_type": "aws",
"cloud": "aws",
})
}
Loading