Skip to content

Commit 6cf3191

Browse files
authored
Propagate tf:"suppress_diff" to child attributes (#981)
* Propagate diff suppression also to nested fields of a configuration block, if it's marked for diff suppression. * Log more precise log messages when this happens. Fix #942
1 parent eb35c44 commit 6cf3191

File tree

10 files changed

+36
-33
lines changed

10 files changed

+36
-33
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ resource "databricks_job" "this" {
112112
notebook_task {
113113
notebook_path = databricks_notebook.this.path
114114
}
115-
116-
email_notifications {}
117115
}
118116
119117
output "notebook_url" {

clusters/clusters_api.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,29 @@ type ZonesInfo struct {
130130
// AwsAttributes encapsulates the aws attributes for aws based clusters
131131
// https://docs.databricks.com/dev-tools/api/latest/clusters.html#clusterclusterattributes
132132
type AwsAttributes struct {
133-
FirstOnDemand int32 `json:"first_on_demand,omitempty" tf:"computed"`
134-
Availability Availability `json:"availability,omitempty" tf:"computed"`
135-
ZoneID string `json:"zone_id,omitempty" tf:"computed"`
133+
FirstOnDemand int32 `json:"first_on_demand,omitempty"`
134+
Availability Availability `json:"availability,omitempty"`
135+
ZoneID string `json:"zone_id,omitempty"`
136136
InstanceProfileArn string `json:"instance_profile_arn,omitempty"`
137-
SpotBidPricePercent int32 `json:"spot_bid_price_percent,omitempty" tf:"computed"`
138-
EbsVolumeType EbsVolumeType `json:"ebs_volume_type,omitempty" tf:"computed"`
139-
EbsVolumeCount int32 `json:"ebs_volume_count,omitempty" tf:"computed"`
140-
EbsVolumeSize int32 `json:"ebs_volume_size,omitempty" tf:"computed"`
137+
SpotBidPricePercent int32 `json:"spot_bid_price_percent,omitempty"`
138+
EbsVolumeType EbsVolumeType `json:"ebs_volume_type,omitempty"`
139+
EbsVolumeCount int32 `json:"ebs_volume_count,omitempty"`
140+
EbsVolumeSize int32 `json:"ebs_volume_size,omitempty"`
141141
}
142142

143143
// AzureAttributes encapsulates the Azure attributes for Azure based clusters
144144
// https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/clusters#clusterazureattributes
145145
type AzureAttributes struct {
146-
FirstOnDemand int32 `json:"first_on_demand,omitempty" tf:"computed"`
147-
Availability Availability `json:"availability,omitempty" tf:"computed"`
148-
SpotBidMaxPrice float64 `json:"spot_bid_max_price,omitempty" tf:"computed"`
146+
FirstOnDemand int32 `json:"first_on_demand,omitempty"`
147+
Availability Availability `json:"availability,omitempty"`
148+
SpotBidMaxPrice float64 `json:"spot_bid_max_price,omitempty"`
149149
}
150150

151151
// GcpAttributes encapsultes GCP specific attributes
152152
// https://docs.gcp.databricks.com/dev-tools/api/latest/clusters.html#clustergcpattributes
153153
type GcpAttributes struct {
154-
UsePreemptibleExecutors bool `json:"use_preemptible_executors,omitempty" tf:"computed"`
155-
GoogleServiceAccount string `json:"google_service_account,omitempty" tf:"computed"`
154+
UsePreemptibleExecutors bool `json:"use_preemptible_executors,omitempty"`
155+
GoogleServiceAccount string `json:"google_service_account,omitempty"`
156156
}
157157

158158
// DbfsStorageInfo contains the destination string for DBFS

clusters/resource_cluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func SparkConfDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool
4747
return false
4848
}
4949

50-
func AwsAttribsDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
51-
if k == "aws_attributes.0.zone_id" && old != "" && new == "auto" {
52-
log.Printf("[DEBUG] Suppressing diff for k=%#v old=%#v new=%#v", k, old, new)
50+
func ZoneDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
51+
if old != "" && (new == "auto" || new == "") {
52+
log.Printf("[INFO] Suppressing diff on availability zone")
5353
return true
5454
}
5555
return false
@@ -58,7 +58,7 @@ func AwsAttribsDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool
5858
func resourceClusterSchema() map[string]*schema.Schema {
5959
return common.StructToSchema(Cluster{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
6060
s["spark_conf"].DiffSuppressFunc = SparkConfDiffSuppressFunc
61-
s["aws_attributes"].DiffSuppressFunc = AwsAttribsDiffSuppressFunc
61+
common.MustSchemaPath(s, "aws_attributes", "zone_id").DiffSuppressFunc = ZoneDiffSuppress
6262
// adds `library` configuration block
6363
s["library"] = common.StructToSchema(libraries.ClusterLibraryList{},
6464
func(ss map[string]*schema.Schema) map[string]*schema.Schema {

common/reflect_resource.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ func chooseFieldName(typeField reflect.StructField) string {
146146
return strings.Split(jsonTag, ",")[0]
147147
}
148148

149+
func diffSuppressor(zero string) func(k, old, new string, d *schema.ResourceData) bool {
150+
return func(k, old, new string, d *schema.ResourceData) bool {
151+
if new == zero && old != zero {
152+
log.Printf("[DEBUG] Suppressing diff for %v: platform=%#v config=%#v", k, old, new)
153+
return true
154+
}
155+
return false
156+
}
157+
}
158+
149159
// typeToSchema converts struct into terraform schema. `path` is used for block suppressions
150160
// special path element `"0"` is used to denote either arrays or sets of elements
151161
func typeToSchema(v reflect.Value, t reflect.Type, path []string) map[string]*schema.Schema {
@@ -201,14 +211,17 @@ func typeToSchema(v reflect.Value, t reflect.Type, path []string) map[string]*sc
201211
scm[fieldName].Type = schema.TypeList
202212
elem := typeField.Type.Elem()
203213
sv := reflect.New(elem).Elem()
214+
nestedSchema := typeToSchema(sv, elem, append(path, fieldName, "0"))
204215
if strings.Contains(tfTag, "suppress_diff") {
205-
// TODO: we may also suppress count diffs on all json:"..,omitempty" without tf:"force_new"
206-
// find . -type f -name '*.go' -not -path "vendor/*" | xargs grep ',omitempty' | grep '*'
207216
blockCount := strings.Join(append(path, fieldName, "#"), ".")
208217
scm[fieldName].DiffSuppressFunc = makeEmptyBlockSuppressFunc(blockCount)
218+
for _, v := range nestedSchema {
219+
// to those relatively new to GoLang: we must explicitly pass down v by copy
220+
v.DiffSuppressFunc = diffSuppressor(fmt.Sprintf("%v", v.Type.Zero()))
221+
}
209222
}
210223
scm[fieldName].Elem = &schema.Resource{
211-
Schema: typeToSchema(sv, elem, append(path, fieldName, "0")),
224+
Schema: nestedSchema,
212225
}
213226
case reflect.Slice:
214227
ft := schema.TypeList

common/resource.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ func (r Resource) ToResource() *schema.Resource {
5757
for {
5858
head := queue[0]
5959
queue = queue[1:]
60-
for k, v := range head.Schema {
60+
for _, v := range head.Schema {
6161
if v.Computed {
6262
continue
6363
}
6464
if nested, ok := v.Elem.(*schema.Resource); ok {
65-
log.Printf("[DEBUG] %s is a nested block", k)
6665
queue = append(queue, nested)
6766
}
6867
v.ForceNew = true
@@ -137,9 +136,8 @@ func MustCompileKeyRE(name string) *regexp.Regexp {
137136
func makeEmptyBlockSuppressFunc(name string) func(k, old, new string, d *schema.ResourceData) bool {
138137
re := MustCompileKeyRE(name)
139138
return func(k, old, new string, d *schema.ResourceData) bool {
140-
log.Printf("[DEBUG] name=%s k='%v', old='%v', new='%v'", name, k, old, new)
141139
if re.Match([]byte(name)) && old == "1" && new == "0" {
142-
log.Printf("[DEBUG] Suppressing diff for name=%s k=%#v old=%#v new=%#v", name, k, old, new)
140+
log.Printf("[DEBUG] Suppressing diff for name=%s k=%#v patform=%#v config=%#v", name, k, old, new)
143141
return true
144142
}
145143
return false

docs/data-sources/current_user.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ resource "databricks_job" "this" {
4040
notebook_task {
4141
notebook_path = databricks_notebook.this.path
4242
}
43-
44-
email_notifications {}
4543
}
4644
4745
output "notebook_url" {

docs/guides/workspace-management.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ resource "databricks_job" "this" {
6767
notebook_task {
6868
notebook_path = databricks_notebook.this.path
6969
}
70-
71-
email_notifications {}
7270
}
7371
7472
resource "databricks_cluster" "this" {

docs/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ resource "databricks_job" "this" {
8686
notebook_task {
8787
notebook_path = databricks_notebook.this.path
8888
}
89-
90-
email_notifications {}
9189
}
9290
9391
output "notebook_url" {

docs/resources/permissions.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ resource "databricks_service_principal" "aws_principal" {
158158
resource "databricks_job" "this" {
159159
name = "Featurization"
160160
max_concurrent_runs = 1
161-
email_notifications {}
162161
163162
new_cluster {
164163
num_workers = 300

pipelines/resource_pipeline.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func adjustPipelineResourceSchema(m map[string]*schema.Schema) map[string]*schem
200200
cluster, _ := m["cluster"].Elem.(*schema.Resource)
201201
clustersSchema := cluster.Schema
202202
clustersSchema["spark_conf"].DiffSuppressFunc = clusters.SparkConfDiffSuppressFunc
203-
clustersSchema["aws_attributes"].DiffSuppressFunc = clusters.AwsAttribsDiffSuppressFunc
203+
common.MustSchemaPath(clustersSchema,
204+
"aws_attributes", "zone_id").DiffSuppressFunc = clusters.ZoneDiffSuppress
204205

205206
awsAttributes, _ := clustersSchema["aws_attributes"].Elem.(*schema.Resource)
206207
awsAttributesSchema := awsAttributes.Schema

0 commit comments

Comments
 (0)