Skip to content

Commit c976219

Browse files
authored
refactor some common functions for workspace bindings (#3702)
1 parent e300bb9 commit c976219

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

catalog/bindings/bindings.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bindings
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/databricks-sdk-go"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func AddCurrentWorkspaceBindings(ctx context.Context, d *schema.ResourceData, w *databricks.WorkspaceClient, securableName string, securableType string) error {
12+
if d.Get("isolation_mode") != "ISOLATED" {
13+
return nil
14+
}
15+
// Bind the current workspace if the catalog is isolated, otherwise the read will fail
16+
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
17+
if err != nil {
18+
return err
19+
}
20+
_, err = w.WorkspaceBindings.UpdateBindings(ctx, catalog.UpdateWorkspaceBindingsParameters{
21+
SecurableName: securableName,
22+
SecurableType: securableType,
23+
Add: []catalog.WorkspaceBinding{
24+
{
25+
BindingType: catalog.WorkspaceBindingBindingTypeBindingTypeReadWrite,
26+
WorkspaceId: currentMetastoreAssignment.WorkspaceId,
27+
},
28+
},
29+
})
30+
return err
31+
}

catalog/resource_catalog.go

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77

88
"github.com/databricks/databricks-sdk-go/service/catalog"
9+
"github.com/databricks/terraform-provider-databricks/catalog/bindings"
910
"github.com/databricks/terraform-provider-databricks/common"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -86,14 +87,7 @@ func ResourceCatalog() common.Resource {
8687
d.SetId(ci.Name)
8788

8889
// Update owner, isolation mode or predictive optimization if it is provided
89-
updateRequired := false
90-
for _, key := range []string{"owner", "isolation_mode", "enable_predictive_optimization"} {
91-
if d.Get(key) != "" {
92-
updateRequired = true
93-
break
94-
}
95-
}
96-
if !updateRequired {
90+
if !updateRequired(d, []string{"owner", "isolation_mode", "enable_predictive_optimization"}) {
9791
return nil
9892
}
9993
var updateCatalogRequest catalog.UpdateCatalog
@@ -104,25 +98,7 @@ func ResourceCatalog() common.Resource {
10498
return err
10599
}
106100

107-
if d.Get("isolation_mode") != "ISOLATED" {
108-
return nil
109-
}
110-
// Bind the current workspace if the catalog is isolated, otherwise the read will fail
111-
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
112-
if err != nil {
113-
return err
114-
}
115-
_, err = w.WorkspaceBindings.UpdateBindings(ctx, catalog.UpdateWorkspaceBindingsParameters{
116-
SecurableName: ci.Name,
117-
SecurableType: "catalog",
118-
Add: []catalog.WorkspaceBinding{
119-
{
120-
BindingType: catalog.WorkspaceBindingBindingTypeBindingTypeReadWrite,
121-
WorkspaceId: currentMetastoreAssignment.WorkspaceId,
122-
},
123-
},
124-
})
125-
return err
101+
return bindings.AddCurrentWorkspaceBindings(ctx, d, w, ci.Name, "catalog")
126102
},
127103
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
128104
w, err := c.WorkspaceClient()
@@ -191,21 +167,7 @@ func ResourceCatalog() common.Resource {
191167
return nil
192168
}
193169
// Bind the current workspace if the catalog is isolated, otherwise the read will fail
194-
currentMetastoreAssignment, err := w.Metastores.Current(ctx)
195-
if err != nil {
196-
return err
197-
}
198-
_, err = w.WorkspaceBindings.UpdateBindings(ctx, catalog.UpdateWorkspaceBindingsParameters{
199-
SecurableName: ci.Name,
200-
SecurableType: "catalog",
201-
Add: []catalog.WorkspaceBinding{
202-
{
203-
BindingType: catalog.WorkspaceBindingBindingTypeBindingTypeReadWrite,
204-
WorkspaceId: currentMetastoreAssignment.WorkspaceId,
205-
},
206-
},
207-
})
208-
return err
170+
return bindings.AddCurrentWorkspaceBindings(ctx, d, w, ci.Name, "catalog")
209171
},
210172
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
211173
w, err := c.WorkspaceClient()

catalog/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/databricks/databricks-sdk-go"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
// UC catalog resources accept an optional metastore_id parameter. This is required for account-level operations, but it is not used
@@ -46,3 +47,13 @@ func GetSqlColumnInfoHCL(columnInfos []SqlColumnInfo) string {
4647
}
4748
return columnsTemplate
4849
}
50+
51+
// check if a UC resource needs the additional update call during create operation
52+
func updateRequired(d *schema.ResourceData, updateOnly []string) bool {
53+
for _, key := range updateOnly {
54+
if d.Get(key) != "" {
55+
return true
56+
}
57+
}
58+
return false
59+
}

0 commit comments

Comments
 (0)