Skip to content

Commit 506d8ee

Browse files
committed
[Fix] Fix drift in databricks_cluster when is_single_node is set to true
1 parent 6b9a584 commit 506d8ee

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

clusters/resource_cluster.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ const (
3131

3232
func ResourceCluster() common.Resource {
3333
return common.Resource{
34-
Create: resourceClusterCreate,
35-
Read: resourceClusterRead,
36-
Update: resourceClusterUpdate,
37-
Delete: resourceClusterDelete,
38-
Schema: clusterSchema,
34+
Create: resourceClusterCreate,
35+
Read: resourceClusterRead,
36+
Update: resourceClusterUpdate,
37+
Delete: resourceClusterDelete,
38+
Schema: clusterSchema,
39+
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff) error {
40+
if isSingleNode, _ := d.GetOk("is_single_node"); isSingleNode.(bool) {
41+
return singleNodeClusterChangesCustomizeDiff(d)
42+
}
43+
return nil
44+
},
3945
SchemaVersion: clusterSchemaVersion,
4046
Timeouts: resourceClusterTimeouts(),
4147
StateUpgraders: []schema.StateUpgrader{
@@ -48,6 +54,43 @@ func ResourceCluster() common.Resource {
4854
}
4955
}
5056

57+
func singleNodeClusterChangesCustomizeDiff(d *schema.ResourceDiff) error {
58+
autoConfigAttributes := map[string][]string{
59+
"custom_tags": {"ResourceClass"},
60+
"spark_conf": {"spark.databricks.cluster.profile", "spark.master"},
61+
}
62+
63+
for key, attributes := range autoConfigAttributes {
64+
if !d.HasChange(key) {
65+
continue
66+
}
67+
68+
o, n := d.GetChange(key)
69+
old, okOld := o.(map[string]interface{})
70+
new, okNew := n.(map[string]interface{})
71+
72+
if !okNew || !okOld {
73+
return fmt.Errorf("internal type casting error")
74+
}
75+
76+
log.Printf("[DEBUG] values for key %s, old: %v, new: %v", key, old, new)
77+
78+
for _, attribute := range attributes {
79+
if _, exists := new[attribute]; exists && new[attribute] != nil {
80+
continue
81+
}
82+
83+
new[attribute] = old[attribute]
84+
}
85+
86+
if err := d.SetNew(key, new); err != nil {
87+
return err
88+
}
89+
}
90+
91+
return nil
92+
}
93+
5194
func clusterSchemaV0() cty.Type {
5295
return (&schema.Resource{
5396
Schema: clusterSchema}).CoreConfigSchema().ImpliedType()
@@ -346,7 +389,8 @@ func (ClusterSpec) CustomizeSchema(s *common.CustomizableSchema) *common.Customi
346389
s.SchemaPath("docker_image", "url").SetRequired()
347390
s.SchemaPath("docker_image", "basic_auth", "password").SetRequired().SetSensitive()
348391
s.SchemaPath("docker_image", "basic_auth", "username").SetRequired()
349-
s.SchemaPath("spark_conf").SetCustomSuppressDiff(SparkConfDiffSuppressFunc)
392+
s.SchemaPath("spark_conf").SetCustomSuppressDiff(SparkConfDiffSuppressFunc).SetComputed().SetOptional()
393+
s.SchemaPath("custom_tags").SetComputed().SetOptional()
350394
s.SchemaPath("aws_attributes").SetSuppressDiff().SetConflictsWith([]string{"azure_attributes", "gcp_attributes"})
351395
s.SchemaPath("aws_attributes", "zone_id").SetCustomSuppressDiff(ZoneDiffSuppress)
352396
s.SchemaPath("azure_attributes").SetSuppressDiff().SetConflictsWith([]string{"aws_attributes", "gcp_attributes"})

0 commit comments

Comments
 (0)