Skip to content

Commit c297ca3

Browse files
authored
Correct exporting of the computed attributes for databricks_cluster (#1711)
1 parent 5b0e526 commit c297ca3

File tree

7 files changed

+86
-9
lines changed

7 files changed

+86
-9
lines changed

exporter/context.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,16 @@ func (ic *importContext) dataToHcl(i importable, path []string,
533533
})
534534
for _, tuple := range ss {
535535
a, as := tuple.Field, tuple.Schema
536-
if as.Computed {
537-
continue
538-
}
539536
pathString := strings.Join(append(path, a), ".")
540537
raw, ok := d.GetOk(pathString)
538+
if i.ShouldOmitField == nil { // we don't have custom function, so skip computed & default fields
539+
// log.Printf("[DEBUG] path=%s, raw='%v'", pathString, raw)
540+
if defaultShouldOmitFieldFunc(ic, pathString, as, d) {
541+
continue
542+
}
543+
} else if i.ShouldOmitField(ic, pathString, as, d) {
544+
continue
545+
}
541546
for _, r := range i.Depends {
542547
if r.Path == pathString && r.Variable {
543548
// sensitive fields are moved to variable depends

exporter/importables.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var (
3838
gsRegex = regexp.MustCompile(`^gs://([^/]+)(/.*)?$`)
3939
globalWorkspaceConfName = "global_workspace_conf"
4040
notebookPathToIdRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
41+
jobClustersRegex = regexp.MustCompile(`^((job_cluster|task)\.[0-9]+\.new_cluster\.[0-9]+\.)`)
42+
dltClusterRegex = regexp.MustCompile(`^(cluster\.[0-9]+\.)`)
4143
)
4244

4345
type dbsqlListResponse struct {
@@ -297,6 +299,7 @@ var resourcesMap map[string]importable = map[string]importable{
297299
}
298300
return ic.importLibraries(r.Data, s)
299301
},
302+
ShouldOmitField: makeShouldOmitFieldForCluster(nil),
300303
},
301304
"databricks_job": {
302305
ApiVersion: common.API_2_1,
@@ -472,6 +475,16 @@ var resourcesMap map[string]importable = map[string]importable{
472475
}
473476
return nil
474477
},
478+
ShouldOmitField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
479+
switch pathString {
480+
case "url", "format":
481+
return true
482+
}
483+
if res := jobClustersRegex.FindStringSubmatch(pathString); res != nil { // analyze job clusters
484+
return makeShouldOmitFieldForCluster(jobClustersRegex)(ic, pathString, as, d)
485+
}
486+
return defaultShouldOmitFieldFunc(ic, pathString, as, d)
487+
},
475488
},
476489
"databricks_cluster_policy": {
477490
Service: "compute",
@@ -1349,7 +1362,14 @@ var resourcesMap map[string]importable = map[string]importable{
13491362
})
13501363
}
13511364
return nil
1352-
}, Depends: []reference{
1365+
},
1366+
ShouldOmitField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
1367+
if res := dltClusterRegex.FindStringSubmatch(pathString); res != nil { // analyze DLT clusters
1368+
return makeShouldOmitFieldForCluster(dltClusterRegex)(ic, pathString, as, d)
1369+
}
1370+
return defaultShouldOmitFieldFunc(ic, pathString, as, d)
1371+
},
1372+
Depends: []reference{
13531373
{Path: "creator_user_name", Resource: "databricks_user", Match: "user_name"},
13541374
{Path: "cluster.aws_attributes.instance_profile_arn", Resource: "databricks_instance_profile"},
13551375
{Path: "new_cluster.init_scripts.dbfs.destination", Resource: "databricks_dbfs_file"},

exporter/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type importable struct {
5050
Body func(ic *importContext, body *hclwrite.Body, r *resource) error
5151
// Function to detect if the given resource should be ignored or not
5252
Ignore func(ic *importContext, r *resource) bool
53+
// Function to check if the field in the given resource should be omitted or not
54+
ShouldOmitField func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool
5355
// Defines which API version should be used for this specific resource
5456
ApiVersion common.ApiVersion
5557
}

exporter/test-data/clusters-list-response.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"driver_node_type_id": "Standard_DS3_v2",
4141
"enable_elastic_disk": true,
42-
"enable_local_disk_encryption": false,
42+
"enable_local_disk_encryption": true,
4343
"executors": [
4444
{
4545
"host_private_ip": "10.139.0.9",
@@ -118,7 +118,7 @@
118118
},
119119
"driver_node_type_id": "Standard_F4s",
120120
"enable_elastic_disk": true,
121-
"enable_local_disk_encryption": false,
121+
"enable_local_disk_encryption": true,
122122
"executors": [
123123
{
124124
"host_private_ip": "10.139.0.7",
@@ -188,7 +188,7 @@
188188
},
189189
"driver_node_type_id": "i3.4xlarge",
190190
"enable_elastic_disk": true,
191-
"enable_local_disk_encryption": false,
191+
"enable_local_disk_encryption": true,
192192
"executors": [
193193
{
194194
"host_private_ip": "10.0.234.198",

exporter/test-data/get-cluster-test1-response.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"driver_node_type_id": "Standard_DS3_v2",
3030
"enable_elastic_disk": true,
31-
"enable_local_disk_encryption": false,
31+
"enable_local_disk_encryption": true,
3232
"executors": [
3333
{
3434
"host_private_ip": "10.139.0.9",

exporter/test-data/get-cluster-test2-response.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"driver_node_type_id": "Standard_F4s",
4545
"enable_elastic_disk": true,
46-
"enable_local_disk_encryption": false,
46+
"enable_local_disk_encryption": true,
4747
"executors": [
4848
{
4949
"host_private_ip": "10.139.0.7",

exporter/util.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log"
77
"os"
88
"path"
9+
"reflect"
10+
"regexp"
911
"strings"
1012
"time"
1113

@@ -41,6 +43,7 @@ func (ic *importContext) importCluster(c *clusters.Cluster) {
4143
})
4244
}
4345
if c.InstancePoolID != "" {
46+
// set enable_elastic_disk to false, and remove aws/gcp/azure_attributes
4447
ic.Emit(&resource{
4548
Resource: "databricks_instance_pool",
4649
ID: c.InstancePoolID,
@@ -342,3 +345,50 @@ func (ic *importContext) createFileIn(dir, name string, content []byte) (string,
342345
relativeName := strings.Replace(localFileName, ic.Directory+"/", "", 1)
343346
return relativeName, nil
344347
}
348+
349+
func defaultShouldOmitFieldFunc(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
350+
if as.Computed {
351+
return true
352+
} else if as.Default != nil && d.Get(pathString) == as.Default {
353+
return true
354+
}
355+
356+
return false
357+
}
358+
359+
func makeShouldOmitFieldForCluster(regex *regexp.Regexp) func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
360+
return func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
361+
prefix := ""
362+
if regex != nil {
363+
if res := regex.FindStringSubmatch(pathString); res != nil {
364+
prefix = res[0]
365+
} else {
366+
return false
367+
}
368+
}
369+
raw := d.Get(pathString)
370+
if raw != nil {
371+
v := reflect.ValueOf(raw)
372+
if as.Optional && v.IsZero() {
373+
return true
374+
}
375+
}
376+
workerInstPoolID := d.Get(prefix + "instance_pool_id").(string)
377+
switch pathString {
378+
case prefix + "node_type_id":
379+
return workerInstPoolID != ""
380+
case prefix + "driver_node_type_id":
381+
driverInstPoolID := d.Get(prefix + "driver_instance_pool_id").(string)
382+
nodeTypeID := d.Get(prefix + "node_type_id").(string)
383+
return workerInstPoolID != "" || driverInstPoolID != "" || raw.(string) == nodeTypeID
384+
case prefix + "driver_instance_pool_id":
385+
return raw.(string) == workerInstPoolID
386+
case prefix + "enable_elastic_disk", prefix + "aws_attributes", prefix + "azure_attributes", prefix + "gcp_attributes":
387+
return workerInstPoolID != ""
388+
case prefix + "enable_local_disk_encryption":
389+
return false
390+
}
391+
392+
return defaultShouldOmitFieldFunc(ic, pathString, as, d)
393+
}
394+
}

0 commit comments

Comments
 (0)