Skip to content

Commit

Permalink
Add empty_result_state attribute to the databricks_sql_alert reso…
Browse files Browse the repository at this point in the history
…urce

This fixes #2662
  • Loading branch information
alexott committed Oct 6, 2023
1 parent 364512f commit bcf3bb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/resources/sql_alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The following arguments are available:
* `custom_body` - (Optional, String) Custom body of alert notification, if it exists. See [Alerts API reference](https://docs.databricks.com/sql/user/alerts/index.html) for custom templating instructions.
* `parent` - (Optional, String) The identifier of the workspace folder containing the alert. The default is ther user's home folder. The folder identifier is formatted as `folder/<folder_id>`.
* `rearm` - (Optional, Integer) Number of seconds after being triggered before the alert rearms itself and can be triggered again. If not defined, alert will never be triggered again.
* `empty_result_state` - (Optional, String) State that alert evaluates to when query result is empty. Currently supported values are `unknown`, `triggered`, `ok` - check [API documentation](https://docs.databricks.com/api/workspace/alerts/create) for full list of supported values.

## Related Resources

Expand Down
32 changes: 22 additions & 10 deletions sql/resource_sql_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
)

type AlertOptions struct {
Column string `json:"column"`
Op string `json:"op"`
Value string `json:"value"`
Muted bool `json:"muted,omitempty"`
CustomBody string `json:"custom_body,omitempty"`
CustomSubject string `json:"custom_subject,omitempty"`
Column string `json:"column"`
Op string `json:"op"`
Value string `json:"value"`
Muted bool `json:"muted,omitempty"`
CustomBody string `json:"custom_body,omitempty"`
CustomSubject string `json:"custom_subject,omitempty"`
EmptyResultState string `json:"empty_result_state,omitempty"`
}

type AlertEntity struct {
Expand Down Expand Up @@ -47,14 +48,18 @@ func (a *AlertEntity) toCreateAlertApiObject(s map[string]*schema.Schema, data *
Op: a.Options.Op,
Value: a.Options.Value,
}

return ca, nil
// This is a workaround for Go SDK problem, will be fixed there.
var err error
if a.Options.EmptyResultState != "" {
err = ca.Options.EmptyResultState.Set(a.Options.EmptyResultState)
}
return ca, err
}

func (a *AlertEntity) toEditAlertApiObject(s map[string]*schema.Schema, data *schema.ResourceData) (sql.EditAlert, error) {
common.DataToStructPointer(data, s, a)

return sql.EditAlert{
ea := sql.EditAlert{
AlertId: data.Id(),
Name: a.Name,
Options: sql.AlertOptions{
Expand All @@ -67,7 +72,13 @@ func (a *AlertEntity) toEditAlertApiObject(s map[string]*schema.Schema, data *sc
},
QueryId: a.QueryId,
Rearm: a.Rearm,
}, nil
}

var err error
if a.Options.EmptyResultState != "" {
err = ea.Options.EmptyResultState.Set(a.Options.EmptyResultState)
}
return ea, err
}

func (a *AlertEntity) fromAPIObject(apiAlert *sql.Alert, s map[string]*schema.Schema, data *schema.ResourceData) error {
Expand All @@ -89,6 +100,7 @@ func (a *AlertEntity) fromAPIObject(apiAlert *sql.Alert, s map[string]*schema.Sc
Muted: apiAlert.Options.Muted,
CustomBody: apiAlert.Options.CustomBody,
CustomSubject: apiAlert.Options.CustomSubject,
EmptyResultState: apiAlert.Options.EmptyResultState.String(),
}

// value can be a string or a float64 - unfortunately this can't be encoded in OpenAPI yet
Expand Down

0 comments on commit bcf3bb9

Please sign in to comment.