Skip to content

Commit 515fd63

Browse files
authored
Migrate databricks_workspace_conf to Go SDK (#2035)
1 parent dd3766c commit 515fd63

File tree

3 files changed

+78
-59
lines changed

3 files changed

+78
-59
lines changed

exporter/importables.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"fmt"
88
"log"
99
"regexp"
10+
"sort"
1011
"strconv"
1112
"strings"
1213
"time"
1314

1415
"golang.org/x/exp/slices"
1516

17+
"github.com/databricks/databricks-sdk-go/service/workspaceconf"
1618
"github.com/databricks/terraform-provider-databricks/access"
1719
"github.com/databricks/terraform-provider-databricks/clusters"
1820
"github.com/databricks/terraform-provider-databricks/common"
@@ -1029,9 +1031,13 @@ var resourcesMap map[string]importable = map[string]importable{
10291031
return globalWorkspaceConfName
10301032
},
10311033
List: func(ic *importContext) error {
1032-
wsConfAPI := workspace.NewWorkspaceConfAPI(ic.Context, ic.Client)
1033-
keys := map[string]any{"zDummyKey": "42"}
1034-
err := wsConfAPI.Read(&keys)
1034+
w, err := ic.Client.WorkspaceClient()
1035+
if err != nil {
1036+
return err
1037+
}
1038+
_, err = w.WorkspaceConf.GetStatus(ic.Context, workspaceconf.GetStatus{
1039+
Keys: "zDummyKey",
1040+
})
10351041
/* this is done to pass the TestImportingNoResourcesError test in exporter_test.go
10361042
Commonly, some of the keys in a workspace conf are Nil
10371043
In the simulated server all are returned with a value.
@@ -1047,18 +1053,29 @@ var resourcesMap map[string]importable = map[string]importable{
10471053
return nil
10481054
},
10491055
Import: func(ic *importContext, r *resource) error {
1050-
wsConfAPI := workspace.NewWorkspaceConfAPI(ic.Context, ic.Client)
1051-
keys := ic.workspaceConfKeys
1052-
err := wsConfAPI.Read(&keys)
1056+
w, err := ic.Client.WorkspaceClient()
1057+
if err != nil {
1058+
return err
1059+
}
1060+
loaded := map[string]any{}
1061+
keyNames := []string{}
1062+
for k := range ic.workspaceConfKeys {
1063+
keyNames = append(keyNames, k)
1064+
}
1065+
sort.Strings(keyNames)
1066+
conf, err := w.WorkspaceConf.GetStatus(ic.Context, workspaceconf.GetStatus{
1067+
Keys: strings.Join(keyNames, ","),
1068+
})
10531069
if err != nil {
10541070
return err
10551071
}
1056-
for k, v := range keys {
1057-
if v == nil {
1058-
delete(keys, k)
1072+
for k, v := range *conf {
1073+
if v == "" {
1074+
continue
10591075
}
1076+
loaded[k] = v
10601077
}
1061-
r.Data.Set("custom_config", keys)
1078+
r.Data.Set("custom_config", loaded)
10621079
return nil
10631080
},
10641081
},

internal/acceptance/workspace_conf_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/databricks/databricks-sdk-go/service/workspaceconf"
78
"github.com/databricks/terraform-provider-databricks/common"
8-
"github.com/databricks/terraform-provider-databricks/workspace"
99
"github.com/stretchr/testify/assert"
1010
)
1111

@@ -18,13 +18,18 @@ func TestAccWorkspaceConfFullLifecycle(t *testing.T) {
1818
}`,
1919
Check: resourceCheck("databricks_workspace_conf.this",
2020
func(ctx context.Context, client *common.DatabricksClient, id string) error {
21-
conf := map[string]any{
22-
"enableIpAccessLists": nil,
21+
w, err := client.WorkspaceClient()
22+
if err != nil {
23+
return err
2324
}
24-
err := workspace.NewWorkspaceConfAPI(ctx, client).Read(&conf)
25-
assert.NoError(t, err)
26-
assert.Len(t, conf, 1)
27-
assert.Equal(t, conf["enableIpAccessLists"], "true")
25+
conf, err := w.WorkspaceConf.GetStatus(ctx, workspaceconf.GetStatus{
26+
Keys: "enableIpAccessLists",
27+
})
28+
if err != nil {
29+
return err
30+
}
31+
assert.Len(t, *conf, 1)
32+
assert.Equal(t, (*conf)["enableIpAccessLists"], "true")
2833
return nil
2934
}),
3035
})

workspace/resource_workspace_conf.go

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,28 @@ import (
77
"context"
88
"fmt"
99
"log"
10-
"sort"
1110
"strconv"
1211
"strings"
1312

1413
"github.com/databricks/terraform-provider-databricks/common"
1514

15+
"github.com/databricks/databricks-sdk-go/service/workspaceconf"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1717
)
1818

19-
// WorkspaceConfAPI exposes the workspace configurations API
20-
type WorkspaceConfAPI struct {
21-
client *common.DatabricksClient
22-
context context.Context
23-
}
24-
25-
// NewWorkspaceConfAPI returns workspace conf API
26-
func NewWorkspaceConfAPI(ctx context.Context, m any) WorkspaceConfAPI {
27-
return WorkspaceConfAPI{m.(*common.DatabricksClient), ctx}
28-
}
29-
30-
// Update will handle creation of new values as well as deletes. Deleting just implies that a value of "" or
31-
// the appropriate disable string like "false" is sent with the appropriate key
32-
func (a WorkspaceConfAPI) Update(workspaceConfMap map[string]any) error {
33-
return a.client.Patch(a.context, "/workspace-conf", workspaceConfMap)
34-
}
35-
36-
// Read just returns back a map of keys and values which keys are the configuration items and values are the settings
37-
func (a WorkspaceConfAPI) Read(conf *map[string]any) error {
38-
keys := []string{}
39-
for k := range *conf {
40-
keys = append(keys, k)
41-
}
42-
sort.Strings(keys)
43-
return a.client.Get(a.context, "/workspace-conf", map[string]string{
44-
"keys": strings.Join(keys, ","),
45-
}, &conf)
46-
}
47-
4819
// ResourceWorkspaceConf maintains workspace configuration for specified keys
4920
func ResourceWorkspaceConf() *schema.Resource {
5021
create := func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
51-
wsConfAPI := NewWorkspaceConfAPI(ctx, c)
5222
o, n := d.GetChange("custom_config")
5323
old, okOld := o.(map[string]any)
5424
new, okNew := n.(map[string]any)
5525
if !okNew || !okOld {
5626
return fmt.Errorf("internal type casting error")
5727
}
5828
log.Printf("[DEBUG] Old workspace config: %v, new: %v", old, new)
59-
patch := map[string]any{}
29+
patch := workspaceconf.WorkspaceConf{}
6030
for k, v := range new {
61-
patch[k] = v
31+
patch[k] = fmt.Sprint(v)
6232
}
6333
for k, v := range old {
6434
_, keep := new[k]
@@ -80,46 +50,73 @@ func ResourceWorkspaceConf() *schema.Resource {
8050
patch[k] = "false"
8151
}
8252
}
83-
err := wsConfAPI.Update(patch)
53+
w, err := c.WorkspaceClient()
54+
if err != nil {
55+
return err
56+
}
57+
err = w.WorkspaceConf.SetStatus(ctx, patch)
8458
if err != nil {
8559
return err
8660
}
61+
newConfig := map[string]any{}
62+
for k, v := range patch {
63+
newConfig[k] = v
64+
}
8765
d.SetId("_")
8866
return nil
8967
}
9068
return common.Resource{
9169
Create: create,
9270
Update: create,
9371
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
94-
wsConfAPI := NewWorkspaceConfAPI(ctx, c)
9572
config := d.Get("custom_config").(map[string]any)
9673
log.Printf("[DEBUG] Config available in state: %v", config)
97-
err := wsConfAPI.Read(&config)
74+
w, err := c.WorkspaceClient()
75+
if err != nil {
76+
return err
77+
}
78+
var keys []string
79+
for k := range config {
80+
keys = append(keys, k)
81+
}
82+
if len(keys) == 0 {
83+
return nil
84+
}
85+
remote, err := w.WorkspaceConf.GetStatus(ctx, workspaceconf.GetStatus{
86+
Keys: strings.Join(keys, ","),
87+
})
9888
if err != nil {
9989
return err
10090
}
91+
for k, v := range *remote {
92+
config[k] = v
93+
}
10194
log.Printf("[DEBUG] Setting new config to state: %v", config)
10295
return d.Set("custom_config", config)
10396
},
10497
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
98+
patch := workspaceconf.WorkspaceConf{}
10599
config := d.Get("custom_config").(map[string]any)
106100
for k, v := range config {
107101
switch r := v.(type) {
108102
default:
109-
config[k] = ""
103+
patch[k] = ""
110104
case string:
111105
_, err := strconv.ParseBool(r)
112106
if err != nil {
113-
config[k] = ""
107+
patch[k] = ""
114108
} else {
115-
config[k] = "false"
109+
patch[k] = "false"
116110
}
117111
case bool:
118-
config[k] = "false"
112+
patch[k] = "false"
119113
}
120114
}
121-
wsConfAPI := NewWorkspaceConfAPI(ctx, c)
122-
return wsConfAPI.Update(config)
115+
w, err := c.WorkspaceClient()
116+
if err != nil {
117+
return err
118+
}
119+
return w.WorkspaceConf.SetStatus(ctx, patch)
123120
},
124121
Schema: map[string]*schema.Schema{
125122
"custom_config": {

0 commit comments

Comments
 (0)