Skip to content

Commit

Permalink
Custom Workspace configuration is reset based on the value, instead o…
Browse files Browse the repository at this point in the history
…f the key. Booleans default to false-string and all other values default to empty string. (#826)

Co-authored-by: Serge Smertin <259697+nfx@users.noreply.github.com>
  • Loading branch information
laurensknoll and nfx authored Oct 1, 2021
1 parent aff3f76 commit 94cd29c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
39 changes: 26 additions & 13 deletions workspace/resource_workspace_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/databrickslabs/terraform-provider-databricks/common"
Expand Down Expand Up @@ -52,23 +53,29 @@ func ResourceWorkspaceConf() *schema.Resource {
if !okNew || !okOld {
return fmt.Errorf("internal type casting error")
}
log.Printf("[DEBUG] Old worspace config: %v, new: %v", old, new)
log.Printf("[DEBUG] Old workspace config: %v, new: %v", old, new)
patch := map[string]interface{}{}
for k, v := range new {
patch[k] = v
}
for k := range old {
for k, v := range old {
_, keep := new[k]
if keep {
continue
}
log.Printf("[DEBUG] Erasing configuration of %s", k)
if strings.HasPrefix(k, "enable") ||
strings.HasPrefix(k, "enforce") ||
strings.HasSuffix(k, "Enabled") {
patch[k] = "false"
} else {
switch r := v.(type) {
default:
patch[k] = ""
case string:
_, err := strconv.ParseBool(r)
if err != nil {
patch[k] = ""
} else {
patch[k] = "false"
}
case bool:
patch[k] = "false"
}
}
err := wsConfAPI.Update(patch)
Expand All @@ -94,13 +101,19 @@ func ResourceWorkspaceConf() *schema.Resource {
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
config := d.Get("custom_config").(map[string]interface{})
for k := range config {
if strings.HasPrefix(k, "enable") ||
strings.HasPrefix(k, "enforce") ||
strings.HasSuffix(k, "Enabled") {
config[k] = "false"
} else {
for k, v := range config {
switch r := v.(type) {
default:
config[k] = ""
case string:
_, err := strconv.ParseBool(r)
if err != nil {
config[k] = ""
} else {
config[k] = "false"
}
case bool:
config[k] = "false"
}
}
wsConfAPI := NewWorkspaceConfAPI(ctx, c)
Expand Down
15 changes: 13 additions & 2 deletions workspace/resource_workspace_conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,25 @@ func TestWorkspaceConfUpdate(t *testing.T) {
Resource: "/api/2.0/workspace-conf",
ExpectedRequest: map[string]string{
"enableIpAccessLists": "true",
"enforceSomething": "1",
"enableSomething": "false",
"someProperty": "",
},
},
{
Method: http.MethodGet,
Resource: "/api/2.0/workspace-conf?keys=enableIpAccessLists",
Resource: "/api/2.0/workspace-conf?keys=enableIpAccessLists%2CenforceSomething",
Response: map[string]string{
"enableIpAccessLists": "true",
"enforceSomething": "true",
},
},
{
Method: http.MethodGet,
Resource: "/api/2.0/workspace-conf?keys=enforceSomething%2CenableIpAccessLists",
Response: map[string]string{
"enableIpAccessLists": "true",
"enforceSomething": "true",
},
},
},
Expand All @@ -92,6 +102,7 @@ func TestWorkspaceConfUpdate(t *testing.T) {
},
HCL: `custom_config {
enableIpAccessLists = "true"
enforceSomething = true
}`,
Update: true,
ID: "_",
Expand Down Expand Up @@ -179,7 +190,7 @@ func TestWorkspaceConfDelete(t *testing.T) {
},
},
HCL: `custom_config {
enableSomething = "true"
enableSomething = true
enforceSomethingElse = "true"
enableFancyThing = "false"
someProperty = "thing"
Expand Down

0 comments on commit 94cd29c

Please sign in to comment.