Skip to content

Commit 12757f9

Browse files
authored
[Internal] Host Agnostic Support for Current Config Data Source (#5519)
## Changes - Add an optional cloud argument to databricks_current_config that accepts aws, azure, or gcp - When set, cloud_type uses the provided value instead of relying on host-based detection - When not set, falls back to existing behavior (auto-detect from provider configuration) - Validated via StringInSlice — invalid or empty values are rejected at plan time ## Tests - Unit tests: cloud override (gcp, azure, aws), account-level override, invalid value rejection, empty value rejection - Acceptance tests: cloud override at workspace level (aws, azure, gcp) and account level - Existing unit tests still pass - make fmt lint ws clean
1 parent a6a3093 commit 12757f9

File tree

5 files changed

+148
-9
lines changed

5 files changed

+148
-9
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Add resource and data sources for `databricks_environments_workspace_base_environment`.
99
* Add resource and data source for `databricks_environments_default_workspace_base_environment`.
1010

11+
* 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.
12+
1113
* 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)).
1214

1315
### Bug Fixes

docs/data-sources/current_config.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ resource "databricks_storage_credential" "external" {
4141

4242
## Argument Reference
4343

44+
* `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.
4445
* `provider_config` - (Optional) Configure the provider for management through account provider. This block consists of the following fields:
4546
* `workspace_id` - (Required) Workspace ID which the resource belongs to. This workspace must be part of the account which the provider is configured with.
4647

@@ -51,7 +52,7 @@ Data source exposes the following attributes:
5152
* `is_account` - Whether the provider is configured at account-level
5253
* `account_id` - Account Id if provider is configured at account-level
5354
* `host` - Host of the Databricks workspace or account console
54-
* `cloud_type` - Cloud type specified in the provider
55+
* `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.
5556
* `auth_type` - Auth type used by the provider
5657

5758
## Related Resources

mws/data_current_config.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,48 @@ import (
55

66
"github.com/databricks/databricks-sdk-go/config"
77
"github.com/databricks/terraform-provider-databricks/common"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
89
)
910

1011
type currentConfig struct {
12+
Cloud string `json:"cloud,omitempty" tf:"optional"`
1113
IsAccount bool `json:"is_account,omitempty" tf:"computed"`
1214
AccountId string `json:"account_id,omitempty" tf:"computed"`
1315
Host string `json:"host,omitempty" tf:"computed"`
1416
CloudType string `json:"cloud_type,omitempty" tf:"computed"`
1517
AuthType string `json:"auth_type,omitempty" tf:"computed"`
1618
}
1719

20+
var validCloudValues = []string{"aws", "azure", "gcp"}
21+
22+
func cloudTypeFromConfig(cfg *config.Config) string {
23+
if cfg.IsAws() {
24+
return "aws"
25+
} else if cfg.IsAzure() {
26+
return "azure"
27+
} else if cfg.IsGcp() {
28+
return "gcp"
29+
}
30+
return "unknown"
31+
}
32+
1833
func DataSourceCurrentConfiguration() common.Resource {
19-
return common.DataResource(currentConfig{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
34+
r := common.DataResource(currentConfig{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
2035
data := e.(*currentConfig)
2136
data.IsAccount = false
2237
if c.Config.HostType() == config.AccountHost {
2338
data.AccountId = c.Config.AccountID
2439
data.IsAccount = true
2540
}
2641
data.Host = c.Config.Host
27-
if c.Config.IsAws() {
28-
data.CloudType = "aws"
29-
} else if c.Config.IsAzure() {
30-
data.CloudType = "azure"
31-
} else if c.Config.IsGcp() {
32-
data.CloudType = "gcp"
42+
if data.Cloud != "" {
43+
data.CloudType = data.Cloud
3344
} else {
34-
data.CloudType = "unknown"
45+
data.CloudType = cloudTypeFromConfig(c.Config)
3546
}
3647
data.AuthType = c.Config.AuthType
3748
return nil
3849
})
50+
r.Schema["cloud"].ValidateFunc = validation.StringInSlice(validCloudValues, false)
51+
return r
3952
}

mws/data_current_config_acc_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ func checkCurrentConfig(t *testing.T, cloudType string, isAccount string) func(s
2222
}
2323
}
2424

25+
func checkCurrentConfigWithCloud(t *testing.T, cloudType string, isAccount string, cloud string) func(s *terraform.State) error {
26+
return func(s *terraform.State) error {
27+
r, ok := s.Modules[0].Resources["data.databricks_current_config.this"]
28+
require.True(t, ok, "data.databricks_current_config.this has to be there")
29+
30+
attr := r.Primary.Attributes
31+
32+
assert.Equal(t, attr["cloud_type"], cloudType)
33+
assert.Equal(t, attr["is_account"], isAccount)
34+
assert.Equal(t, attr["cloud"], cloud)
35+
return nil
36+
}
37+
}
38+
2539
func TestAccDataCurrentConfig(t *testing.T) {
2640
acceptance.LoadWorkspaceEnv(t)
2741
if acceptance.IsAws(t) {
@@ -61,3 +75,43 @@ func TestMwsAccDataCurrentConfig(t *testing.T) {
6175
})
6276
}
6377
}
78+
79+
func TestAccDataCurrentConfigCloudOverride(t *testing.T) {
80+
acceptance.LoadWorkspaceEnv(t)
81+
acceptance.WorkspaceLevel(t, acceptance.Step{
82+
Template: `data "databricks_current_config" "this" {
83+
cloud = "aws"
84+
}`,
85+
Check: checkCurrentConfigWithCloud(t, "aws", "false", "aws"),
86+
})
87+
}
88+
89+
func TestAccDataCurrentConfigCloudOverrideAzure(t *testing.T) {
90+
acceptance.LoadWorkspaceEnv(t)
91+
acceptance.WorkspaceLevel(t, acceptance.Step{
92+
Template: `data "databricks_current_config" "this" {
93+
cloud = "azure"
94+
}`,
95+
Check: checkCurrentConfigWithCloud(t, "azure", "false", "azure"),
96+
})
97+
}
98+
99+
func TestAccDataCurrentConfigCloudOverrideGcp(t *testing.T) {
100+
acceptance.LoadWorkspaceEnv(t)
101+
acceptance.WorkspaceLevel(t, acceptance.Step{
102+
Template: `data "databricks_current_config" "this" {
103+
cloud = "gcp"
104+
}`,
105+
Check: checkCurrentConfigWithCloud(t, "gcp", "false", "gcp"),
106+
})
107+
}
108+
109+
func TestMwsAccDataCurrentConfigCloudOverride(t *testing.T) {
110+
acceptance.LoadAccountEnv(t)
111+
acceptance.AccountLevel(t, acceptance.Step{
112+
Template: `data "databricks_current_config" "this" {
113+
cloud = "aws"
114+
}`,
115+
Check: checkCurrentConfigWithCloud(t, "aws", "true", "aws"),
116+
})
117+
}

mws/data_current_config_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,72 @@ func TestDataSourceCurrentConfigAccAzure(t *testing.T) {
3434
"cloud_type": "azure",
3535
})
3636
}
37+
38+
func TestDataSourceCurrentConfigCloudOverride(t *testing.T) {
39+
qa.ResourceFixture{
40+
Fixtures: []qa.HTTPFixture{},
41+
Read: true,
42+
NonWritable: true,
43+
Resource: DataSourceCurrentConfiguration(),
44+
ID: ".",
45+
HCL: `cloud = "gcp"`,
46+
}.ApplyAndExpectData(t, map[string]any{
47+
"is_account": false,
48+
"cloud_type": "gcp",
49+
"cloud": "gcp",
50+
})
51+
}
52+
53+
func TestDataSourceCurrentConfigCloudOverrideAzure(t *testing.T) {
54+
qa.ResourceFixture{
55+
Fixtures: []qa.HTTPFixture{},
56+
Read: true,
57+
NonWritable: true,
58+
Resource: DataSourceCurrentConfiguration(),
59+
ID: ".",
60+
HCL: `cloud = "azure"`,
61+
}.ApplyAndExpectData(t, map[string]any{
62+
"is_account": false,
63+
"cloud_type": "azure",
64+
"cloud": "azure",
65+
})
66+
}
67+
68+
func TestDataSourceCurrentConfigCloudInvalidValue(t *testing.T) {
69+
qa.ResourceFixture{
70+
Fixtures: []qa.HTTPFixture{},
71+
Read: true,
72+
NonWritable: true,
73+
Resource: DataSourceCurrentConfiguration(),
74+
ID: ".",
75+
HCL: `cloud = "invalid"`,
76+
}.ExpectError(t, "invalid config supplied. [cloud] expected cloud to be one of [aws azure gcp], got invalid")
77+
}
78+
79+
func TestDataSourceCurrentConfigCloudEmptyValue(t *testing.T) {
80+
qa.ResourceFixture{
81+
Fixtures: []qa.HTTPFixture{},
82+
Read: true,
83+
NonWritable: true,
84+
Resource: DataSourceCurrentConfiguration(),
85+
ID: ".",
86+
HCL: `cloud = ""`,
87+
}.ExpectError(t, "invalid config supplied. [cloud] expected cloud to be one of [aws azure gcp], got ")
88+
}
89+
90+
func TestDataSourceCurrentConfigCloudOverrideAccountLevel(t *testing.T) {
91+
qa.ResourceFixture{
92+
Fixtures: []qa.HTTPFixture{},
93+
Read: true,
94+
NonWritable: true,
95+
Resource: DataSourceCurrentConfiguration(),
96+
ID: ".",
97+
AccountID: "acc-123",
98+
HCL: `cloud = "aws"`,
99+
}.ApplyAndExpectData(t, map[string]any{
100+
"is_account": true,
101+
"account_id": "acc-123",
102+
"cloud_type": "aws",
103+
"cloud": "aws",
104+
})
105+
}

0 commit comments

Comments
 (0)