Skip to content

Commit 0f28b2a

Browse files
alexottnkvuong
andauthored
[Fix] Remove configuration drift when configuring databricks_connection to built-in Hive Metastore (#4505)
## Changes <!-- Summary of your changes that are easy to understand --> When configuring built-in Hive Metastore, the backend returns a number of additional options. But it's hard to suppress them as they are valid when `builtin = "false"`, and each option is evaluated separately. So it's easier just to remove them before saving to the state. ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] using Go SDK - [ ] using TF Plugin Framework --------- Co-authored-by: vuong-nguyen <[email protected]>
1 parent 3900690 commit 0f28b2a

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Bug Fixes
1111

12+
* Fix configuration drift when configuring `databricks_connection` to builtin Hive Metastore ([#4505](https://github.com/databricks/terraform-provider-databricks/pull/4505)).
1213
* Only allow `authorized_paths` to be updated in the `options` field of `databricks_catalog` ([#4517](https://github.com/databricks/terraform-provider-databricks/pull/4517)).
1314
* Mark `default_catalog_name` attribute in `databricks_metastore_assignment` as deprecated ([#4522](https://github.com/databricks/terraform-provider-databricks/pull/4522))
1415
* Delete `databricks_sql_endpoint` that failed to start ([#4520](https://github.com/databricks/terraform-provider-databricks/pull/4520))

catalog/resource_connection.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ func ResourceConnection() common.Resource {
8787
if err != nil {
8888
return err
8989
}
90-
// We need to preserve original sensitive options as API doesn't return them
91-
var cOrig catalog.CreateConnection
92-
common.DataToStructPointer(d, s, &cOrig)
9390
// If there are no options returned, need to initialize the map
9491
if conn.Options == nil {
9592
conn.Options = map[string]string{}
9693
}
94+
// remove not necessary parameters for builtin HMS to avoid configuration drift
95+
if val, exists := conn.Options["builtin"]; exists && val == "true" {
96+
delete(conn.Options, "host")
97+
delete(conn.Options, "port")
98+
delete(conn.Options, "home_workspace_id")
99+
delete(conn.Options, "database")
100+
}
101+
// We need to preserve original sensitive options as API doesn't return them
102+
var cOrig catalog.CreateConnection
103+
common.DataToStructPointer(d, s, &cOrig)
97104
for key, element := range cOrig.Options {
98105
if slices.Contains(sensitiveOptions, key) {
99106
conn.Options[key] = element

catalog/resource_connection_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,62 @@ func TestConnectionsCreate(t *testing.T) {
131131
assert.Equal(t, map[string]interface{}{"purpose": "testing"}, d.Get("properties"))
132132
}
133133

134+
func TestConnectionsCreate_BuiltinHms(t *testing.T) {
135+
qa.ResourceFixture{
136+
Fixtures: []qa.HTTPFixture{
137+
{
138+
Method: http.MethodPost,
139+
Resource: "/api/2.1/unity-catalog/connections",
140+
ExpectedRequest: catalog.CreateConnection{
141+
Name: "hms",
142+
ConnectionType: catalog.ConnectionType("HIVE_METASTORE"),
143+
Options: map[string]string{
144+
"builtin": "true",
145+
},
146+
},
147+
Response: catalog.ConnectionInfo{
148+
Name: "hms",
149+
ConnectionType: catalog.ConnectionType("HIVE_METASTORE"),
150+
FullName: "hms",
151+
MetastoreId: "abc",
152+
Options: map[string]string{
153+
"builtin": "true",
154+
"host": "test.com",
155+
"port": "3306",
156+
},
157+
},
158+
},
159+
{
160+
Method: http.MethodGet,
161+
Resource: "/api/2.1/unity-catalog/connections/hms?",
162+
Response: catalog.ConnectionInfo{
163+
Name: "hms",
164+
ConnectionType: catalog.ConnectionType("HIVE_METASTORE"),
165+
FullName: "hms",
166+
MetastoreId: "abc",
167+
Options: map[string]string{
168+
"builtin": "true",
169+
"host": "test.com",
170+
"port": "3306",
171+
},
172+
},
173+
},
174+
},
175+
Resource: ResourceConnection(),
176+
Create: true,
177+
HCL: `
178+
name = "hms"
179+
connection_type = "HIVE_METASTORE"
180+
options = {
181+
builtin = "true"
182+
}
183+
`,
184+
}.ApplyAndExpectData(t, map[string]any{
185+
"name": "hms",
186+
"connection_type": "HIVE_METASTORE",
187+
})
188+
}
189+
134190
func TestConnectionsCreate_Error(t *testing.T) {
135191
_, err := qa.ResourceFixture{
136192
Fixtures: []qa.HTTPFixture{

docs/resources/connection.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,25 @@ resource "databricks_connection" "bigquery" {
6161
}
6262
```
6363

64+
Create a connection to builtin Hive Metastore
65+
66+
```hcl
67+
resource "databricks_connection" "this" {
68+
name = "hms-builtin"
69+
connection_type = "HIVE_METASTORE"
70+
comment = "This is a connection to builtin HMS"
71+
options = {
72+
builtin = "true"
73+
}
74+
}
75+
```
76+
6477
## Argument Reference
6578

6679
The following arguments are supported:
6780

6881
- `name` - Name of the Connection.
69-
- `connection_type` - Connection type. `BIGQUERY` `MYSQL` `POSTGRESQL` `SNOWFLAKE` `REDSHIFT` `SQLDW` `SQLSERVER`, `SALESFORCE` or `DATABRICKS` are supported. [Up-to-date list of connection type supported](https://docs.databricks.com/query-federation/index.html#supported-data-sources)
82+
- `connection_type` - Connection type. `BIGQUERY` `MYSQL` `POSTGRESQL` `SNOWFLAKE` `REDSHIFT` `SQLDW` `SQLSERVER`, `SALESFORCE`, `HIVE_METASTORE`, `GLUE`, `TERADATA`, `ORACLE` or `DATABRICKS` are supported. Up-to-date list of connection type supported is in the [documentation](https://docs.databricks.com/query-federation/index.html#supported-data-sources)
7083
- `options` - The key value of options required by the connection, e.g. `host`, `port`, `user`, `password` or `GoogleServiceAccountKeyJson`. Please consult the [documentation](https://docs.databricks.com/query-federation/index.html#supported-data-sources) for the required option.
7184
- `owner` - (Optional) Name of the connection owner.
7285
- `properties` - (Optional) Free-form connection properties.

0 commit comments

Comments
 (0)