|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/databricks/databricks-sdk-go/service/sql" |
| 7 | + "github.com/databricks/terraform-provider-databricks/common" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 10 | +) |
| 11 | + |
| 12 | +type AlertOptions struct { |
| 13 | + Column string `json:"column"` |
| 14 | + Op string `json:"op"` |
| 15 | + Value string `json:"value"` |
| 16 | + Muted bool `json:"muted,omitempty"` |
| 17 | + CustomBody string `json:"custom_body,omitempty"` |
| 18 | + CustomSubject string `json:"custom_subject,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type AlertEntity struct { |
| 22 | + Name string `json:"name"` |
| 23 | + QueryId string `json:"query_id"` |
| 24 | + Rearm int `json:"rearm,omitempty"` |
| 25 | + Options *AlertOptions `json:"options"` |
| 26 | + Parent string `json:"parent,omitempty" tf:"suppress_diff,force_new"` |
| 27 | +} |
| 28 | + |
| 29 | +func (a *AlertEntity) toCreateAlertApiObject(s map[string]*schema.Schema, data *schema.ResourceData) (sql.CreateAlert, error) { |
| 30 | + common.DataToStructPointer(data, s, a) |
| 31 | + |
| 32 | + var ca sql.CreateAlert |
| 33 | + ca.Name = a.Name |
| 34 | + ca.Parent = a.Parent |
| 35 | + ca.QueryId = a.QueryId |
| 36 | + ca.Rearm = a.Rearm |
| 37 | + ca.Options = sql.AlertOptions{ |
| 38 | + Column: a.Options.Column, |
| 39 | + CustomBody: a.Options.CustomBody, |
| 40 | + CustomSubject: a.Options.CustomSubject, |
| 41 | + Muted: a.Options.Muted, |
| 42 | + Op: a.Options.Op, |
| 43 | + Value: a.Options.Value, |
| 44 | + } |
| 45 | + |
| 46 | + return ca, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (a *AlertEntity) toEditAlertApiObject(s map[string]*schema.Schema, data *schema.ResourceData) (sql.EditAlert, error) { |
| 50 | + common.DataToStructPointer(data, s, a) |
| 51 | + |
| 52 | + return sql.EditAlert{ |
| 53 | + AlertId: data.Id(), |
| 54 | + Name: a.Name, |
| 55 | + Options: sql.AlertOptions{ |
| 56 | + Column: a.Options.Column, |
| 57 | + CustomBody: a.Options.CustomBody, |
| 58 | + CustomSubject: a.Options.CustomSubject, |
| 59 | + Muted: a.Options.Muted, |
| 60 | + Op: a.Options.Op, |
| 61 | + Value: a.Options.Value, |
| 62 | + }, |
| 63 | + QueryId: a.QueryId, |
| 64 | + Rearm: a.Rearm, |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (a *AlertEntity) fromAPIObject(apiAlert *sql.Alert, s map[string]*schema.Schema, data *schema.ResourceData) error { |
| 69 | + a.Name = apiAlert.Name |
| 70 | + a.Parent = apiAlert.Parent |
| 71 | + a.QueryId = apiAlert.Query.Id |
| 72 | + a.Rearm = apiAlert.Rearm |
| 73 | + a.Options = &AlertOptions{ |
| 74 | + Column: apiAlert.Options.Column, |
| 75 | + Op: apiAlert.Options.Op, |
| 76 | + Value: apiAlert.Options.Value, |
| 77 | + Muted: apiAlert.Options.Muted, |
| 78 | + CustomBody: apiAlert.Options.CustomBody, |
| 79 | + CustomSubject: apiAlert.Options.CustomSubject, |
| 80 | + } |
| 81 | + |
| 82 | + return common.StructToData(a, s, data) |
| 83 | +} |
| 84 | + |
| 85 | +func ResourceSqlAlert() *schema.Resource { |
| 86 | + s := common.StructToSchema(AlertEntity{}, func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 87 | + options := m["options"].Elem.(*schema.Resource) |
| 88 | + options.Schema["op"].ValidateFunc = validation.StringInSlice([]string{">", ">=", "<", "<=", "==", "!="}, true) |
| 89 | + return m |
| 90 | + }) |
| 91 | + |
| 92 | + return common.Resource{ |
| 93 | + Create: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error { |
| 94 | + w, err := c.WorkspaceClient() |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + var a AlertEntity |
| 99 | + ca, err := a.toCreateAlertApiObject(s, data) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + apiAlert, err := w.Alerts.Create(ctx, ca) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + data.SetId(apiAlert.Id) |
| 108 | + return nil |
| 109 | + }, |
| 110 | + Read: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error { |
| 111 | + w, err := c.WorkspaceClient() |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + apiAlert, err := w.Alerts.GetByAlertId(ctx, data.Id()) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + var a AlertEntity |
| 120 | + return a.fromAPIObject(apiAlert, s, data) |
| 121 | + }, |
| 122 | + Update: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error { |
| 123 | + w, err := c.WorkspaceClient() |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + var a AlertEntity |
| 128 | + ca, err := a.toEditAlertApiObject(s, data) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + return w.Alerts.Update(ctx, ca) |
| 133 | + }, |
| 134 | + Delete: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error { |
| 135 | + w, err := c.WorkspaceClient() |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + return w.Alerts.DeleteByAlertId(ctx, data.Id()) |
| 140 | + }, |
| 141 | + Schema: s, |
| 142 | + }.ToResource() |
| 143 | +} |
0 commit comments