Skip to content

Commit

Permalink
Fix suppress_diff for webhook notifications at the task level in `dat…
Browse files Browse the repository at this point in the history
…abricks_job` (#3441)

* Fix suppress_diff for webhook notifications at the task level

* fix typo
  • Loading branch information
mgyucht authored Apr 8, 2024
1 parent fd793ec commit c7d6292
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
20 changes: 20 additions & 0 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ func SchemaPath(s map[string]*schema.Schema, path ...string) (*schema.Schema, er
return nil, fmt.Errorf("%v does not compute", path)
}

func SchemaMap(s map[string]*schema.Schema, path ...string) (map[string]*schema.Schema, error) {
sch, err := SchemaPath(s, path...)
if err != nil {
return nil, err
}
cv, ok := sch.Elem.(*schema.Resource)
if !ok {
return nil, fmt.Errorf("%s is not nested resource", path[len(path)-1])
}
return cv.Schema, nil
}

func MustSchemaPath(s map[string]*schema.Schema, path ...string) *schema.Schema {
sch, err := SchemaPath(s, path...)
if err != nil {
Expand All @@ -180,6 +192,14 @@ func MustSchemaPath(s map[string]*schema.Schema, path ...string) *schema.Schema
return sch
}

func MustSchemaMap(s map[string]*schema.Schema, path ...string) map[string]*schema.Schema {
sch, err := SchemaMap(s, path...)
if err != nil {
panic(err)
}
return sch
}

// StructToSchema makes schema from a struct type & applies customizations from callback given
func StructToSchema(v any, customize func(map[string]*schema.Schema) map[string]*schema.Schema) map[string]*schema.Schema {
// If the input 'v' is an instance of ResourceProvider, call resourceProviderStructToSchema instead.
Expand Down
37 changes: 21 additions & 16 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,18 +666,18 @@ func wrapMissingJobError(err error, id string) error {
return err
}

func jobSettingsSchema(s *map[string]*schema.Schema, prefix string) {
if p, err := common.SchemaPath(*s, "new_cluster", "num_workers"); err == nil {
func jobSettingsSchema(s map[string]*schema.Schema, prefix string) {
if p, err := common.SchemaPath(s, "new_cluster", "num_workers"); err == nil {
p.Optional = true
p.Default = 0
p.Type = schema.TypeInt
p.ValidateDiagFunc = validation.ToDiagFunc(validation.IntAtLeast(0))
p.Required = false
}
if p, err := common.SchemaPath(*s, "new_cluster", "init_scripts", "dbfs"); err == nil {
if p, err := common.SchemaPath(s, "new_cluster", "init_scripts", "dbfs"); err == nil {
p.Deprecated = clusters.DbfsDeprecationWarning
}
if v, err := common.SchemaPath(*s, "new_cluster", "spark_conf"); err == nil {
if v, err := common.SchemaPath(s, "new_cluster", "spark_conf"); err == nil {
reSize := common.MustCompileKeyRE(prefix + "new_cluster.0.spark_conf.%")
reConf := common.MustCompileKeyRE(prefix + "new_cluster.0.spark_conf.spark.databricks.delta.preview.enabled")
v.DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
Expand All @@ -692,19 +692,25 @@ func jobSettingsSchema(s *map[string]*schema.Schema, prefix string) {
}
}

func gitSourceSchema(r *schema.Resource, prefix string) {
r.Schema["url"].ValidateFunc = validation.IsURLWithHTTPS
(*r.Schema["tag"]).ConflictsWith = []string{"git_source.0.branch", "git_source.0.commit"}
(*r.Schema["branch"]).ConflictsWith = []string{"git_source.0.commit", "git_source.0.tag"}
(*r.Schema["commit"]).ConflictsWith = []string{"git_source.0.branch", "git_source.0.tag"}
func gitSourceSchema(s map[string]*schema.Schema, prefix string) {
s["url"].ValidateFunc = validation.IsURLWithHTTPS
s["tag"].ConflictsWith = []string{"git_source.0.branch", "git_source.0.commit"}
s["branch"].ConflictsWith = []string{"git_source.0.commit", "git_source.0.tag"}
s["commit"].ConflictsWith = []string{"git_source.0.branch", "git_source.0.tag"}
}

func fixWebhookNotifications(s map[string]*schema.Schema) {
for _, n := range []string{"on_start", "on_failure", "on_success", "on_duration_warning_threshold_exceeded"} {
common.MustSchemaPath(s, "webhook_notifications", n).DiffSuppressFunc = nil
}
}

var jobSchema = common.StructToSchema(JobSettings{},
func(s map[string]*schema.Schema) map[string]*schema.Schema {
jobSettingsSchema(&s, "")
jobSettingsSchema(&s["task"].Elem.(*schema.Resource).Schema, "task.0.")
jobSettingsSchema(&s["job_cluster"].Elem.(*schema.Resource).Schema, "job_cluster.0.")
gitSourceSchema(s["git_source"].Elem.(*schema.Resource), "")
jobSettingsSchema(s, "")
jobSettingsSchema(common.MustSchemaMap(s, "task"), "task.0.")
jobSettingsSchema(common.MustSchemaMap(s, "job_cluster"), "job_cluster.0.")
gitSourceSchema(common.MustSchemaMap(s, "git_source"), "")
if p, err := common.SchemaPath(s, "schedule", "pause_status"); err == nil {
p.ValidateFunc = validation.StringInSlice([]string{"PAUSED", "UNPAUSED"}, false)
}
Expand Down Expand Up @@ -766,9 +772,8 @@ var jobSchema = common.StructToSchema(JobSettings{},
common.MustSchemaPath(s, "run_as", "service_principal_name").ExactlyOneOf = run_as_eoo

// Clear the implied diff suppression for the webhook notification lists
for _, n := range []string{"on_start", "on_failure", "on_success", "on_duration_warning_threshold_exceeded"} {
common.MustSchemaPath(s, "webhook_notifications", n).DiffSuppressFunc = nil
}
fixWebhookNotifications(s)
fixWebhookNotifications(common.MustSchemaMap(s, "task"))

return s
})
Expand Down
6 changes: 2 additions & 4 deletions pipelines/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,15 @@ func suppressStorageDiff(k, old, new string, d *schema.ResourceData) bool {
}

func adjustPipelineResourceSchema(m map[string]*schema.Schema) map[string]*schema.Schema {
cluster, _ := m["cluster"].Elem.(*schema.Resource)
clustersSchema := cluster.Schema
clustersSchema := common.MustSchemaMap(m, "cluster")
clustersSchema["spark_conf"].DiffSuppressFunc = clusters.SparkConfDiffSuppressFunc
common.MustSchemaPath(clustersSchema,
"aws_attributes", "zone_id").DiffSuppressFunc = clusters.ZoneDiffSuppress
common.MustSchemaPath(clustersSchema, "autoscale", "mode").DiffSuppressFunc = common.EqualFoldDiffSuppress

common.MustSchemaPath(clustersSchema, "init_scripts", "dbfs").Deprecated = clusters.DbfsDeprecationWarning

gcpAttributes, _ := clustersSchema["gcp_attributes"].Elem.(*schema.Resource)
gcpAttributesSchema := gcpAttributes.Schema
gcpAttributesSchema := common.MustSchemaMap(clustersSchema, "gcp_attributes")
delete(gcpAttributesSchema, "use_preemptible_executors")
delete(gcpAttributesSchema, "boot_disk_size")

Expand Down
3 changes: 1 addition & 2 deletions sql/resource_sql_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func (a *AlertEntity) fromAPIObject(apiAlert *sql.Alert, s map[string]*schema.Sc

func ResourceSqlAlert() common.Resource {
s := common.StructToSchema(AlertEntity{}, func(m map[string]*schema.Schema) map[string]*schema.Schema {
options := m["options"].Elem.(*schema.Resource)
options.Schema["op"].ValidateFunc = validation.StringInSlice([]string{">", ">=", "<", "<=", "==", "!="}, true)
common.MustSchemaPath(m, "options", "op").ValidateFunc = validation.StringInSlice([]string{">", ">=", "<", "<=", "==", "!="}, true)
return m
})

Expand Down
8 changes: 4 additions & 4 deletions sql/resource_sql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func ResourceSqlQuery() common.Resource {
QueryEntity{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["schedule"].Deprecated = "Operations on `databricks_sql_query` schedules are deprecated. Please use `databricks_job` resource to schedule a `sql_task`."
schedule := m["schedule"].Elem.(*schema.Resource)
schedule := common.MustSchemaMap(m, "schedule")

// Make different query schedule types mutually exclusive.
{
Expand All @@ -525,15 +525,15 @@ func ResourceSqlQuery() common.Resource {
if n1 == n2 {
continue
}
schedule.Schema[n1].ConflictsWith = append(schedule.Schema[n1].ConflictsWith, fmt.Sprintf("schedule.0.%s", n2))
schedule[n1].ConflictsWith = append(schedule[n1].ConflictsWith, fmt.Sprintf("schedule.0.%s", n2))
}
}
}

// Validate week of day in weekly schedule.
// Manually verified that this is case sensitive.
weekly := schedule.Schema["weekly"].Elem.(*schema.Resource)
weekly.Schema["day_of_week"].ValidateFunc = validation.StringInSlice([]string{
weekly := common.MustSchemaMap(schedule, "weekly")
weekly["day_of_week"].ValidateFunc = validation.StringInSlice([]string{
"Sunday",
"Monday",
"Tuesday",
Expand Down

0 comments on commit c7d6292

Please sign in to comment.