Skip to content

Commit b0893d9

Browse files
authored
Fix databricks_metastore_assignment config drift (#1156)
* Fix `databricks_metastore_assignment` config drift Fixed `databricks_metastore_assignment` configuration drift by properly deleting metastore assignment and detecting manual changes from Account console. This also means that de-assigned metastore from a workspace would mark it as remotely removed. Manual assignment of different metastore would also trigger resource updates. Fixes #1146 Closes #1147
1 parent 5a48a24 commit b0893d9

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Version changelog
22

3+
## 0.5.2
4+
5+
* Fixed `databricks_metastore_assignment` configuration drift by properly deleting metastore assignment and detecting manual changes from Account console. This also means that de-assigned metastore from a workspace would mark it as remotely removed. Manual assignment of different metastore would also trigger resource updates ([#1146](https://github.com/databrickslabs/terraform-provider-databricks/issues/1146)).
6+
37
## 0.5.1
48

59
* Added an extended documentation from provisioning AWS PrivateLink workspace ([#1084](https://github.com/databrickslabs/terraform-provider-databricks/pull/1084)).

catalog/resource_metastore_assignment.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@ func (a MetastoreAssignmentAPI) updateMetastoreAssignment(ma MetastoreAssignment
3333
return a.client.Patch(a.context, path, ma)
3434
}
3535

36-
func (a MetastoreAssignmentAPI) clearMetastoreAssignment(workspaceID string) error {
36+
func (a MetastoreAssignmentAPI) getAssignedMetastoreID() (string, error) {
37+
var ma MetastoreAssignment
38+
err := a.client.Get(a.context, "/unity-catalog/metastore_summary", nil, &ma)
39+
return ma.MetastoreID, err
40+
}
41+
42+
func (a MetastoreAssignmentAPI) deleteMetastoreAssignment(workspaceID, metastoreID string) error {
3743
path := fmt.Sprintf("/unity-catalog/workspaces/%s/metastore", workspaceID)
38-
return a.client.Patch(a.context, path, map[string]string{})
44+
return a.client.Delete(a.context, path, map[string]string{
45+
"metastore_id": metastoreID,
46+
})
3947
}
4048

4149
func ResourceMetastoreAssignment() *schema.Resource {
@@ -56,20 +64,21 @@ func ResourceMetastoreAssignment() *schema.Resource {
5664
return nil
5765
},
5866
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
59-
// there are no working APIs at the moment to read the assignment
60-
return nil
67+
metastoreID, err := NewMetastoreAssignmentAPI(ctx, c).getAssignedMetastoreID()
68+
d.Set("metastore_id", metastoreID)
69+
return err
6170
},
6271
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
6372
var ma MetastoreAssignment
6473
common.DataToStructPointer(d, s, &ma)
6574
return NewMetastoreAssignmentAPI(ctx, c).updateMetastoreAssignment(ma)
6675
},
6776
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
68-
workspaceID, _, err := pi.Unpack(d)
77+
workspaceID, metastoreID, err := pi.Unpack(d)
6978
if err != nil {
7079
return err
7180
}
72-
return NewMetastoreAssignmentAPI(ctx, c).clearMetastoreAssignment(workspaceID)
81+
return NewMetastoreAssignmentAPI(ctx, c).deleteMetastoreAssignment(workspaceID, metastoreID)
7382
},
7483
}.ToResource()
7584
}

catalog/resource_metastore_assignment_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func TestMetastoreAssignment_Create(t *testing.T) {
2424
DefaultCatalogName: "hive_metastore",
2525
},
2626
},
27+
{
28+
Method: "GET",
29+
Resource: "/api/2.0/unity-catalog/metastore_summary",
30+
Response: MetastoreAssignment{
31+
MetastoreID: "a",
32+
},
33+
},
2734
},
2835
Resource: ResourceMetastoreAssignment(),
2936
Create: true,

0 commit comments

Comments
 (0)