Skip to content

Commit cde29da

Browse files
[OC-2648] Add back rules attribute under alert routes (#145)
* Codegen * Revert unrelated changes * trigger CI
1 parent 6401824 commit cde29da

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed

client/alert_routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type AlertRoute struct {
1616
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
1717
AlertsSourceIds []interface{} `jsonapi:"attr,alerts_source_ids,omitempty"`
1818
OwningTeamIds []interface{} `jsonapi:"attr,owning_team_ids,omitempty"`
19+
Rules []interface{} `jsonapi:"attr,rules,omitempty"`
1920
}
2021

2122
func (c *Client) ListAlertRoutes(params *rootlygo.ListAlertRoutesParams) ([]interface{}, error) {

docs/resources/alert_route.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,55 @@ resource "rootly_alert_route" "production_route" {
129129

130130
- `enabled` (Boolean)
131131
- `owning_team_ids` (List of String)
132+
- `rules` (Block List) (see [below for nested schema](#nestedblock--rules))
132133

133134
### Read-Only
134135

135136
- `id` (String) The ID of this resource.
136137

138+
<a id="nestedblock--rules"></a>
139+
### Nested Schema for `rules`
140+
141+
Optional:
142+
143+
- `condition_groups` (Block List) (see [below for nested schema](#nestedblock--rules--condition_groups))
144+
- `destinations` (Block List) (see [below for nested schema](#nestedblock--rules--destinations))
145+
- `fallback_rule` (Boolean) Whether this is a fallback rule. Value must be one of true or false
146+
- `name` (String) The name of the alert routing rule
147+
- `position` (Number) The position of the alert routing rule for ordering evaluation
148+
149+
<a id="nestedblock--rules--condition_groups"></a>
150+
### Nested Schema for `rules.condition_groups`
151+
152+
Optional:
153+
154+
- `conditions` (Block List) (see [below for nested schema](#nestedblock--rules--condition_groups--conditions))
155+
- `position` (Number) The position of the condition group
156+
157+
<a id="nestedblock--rules--condition_groups--conditions"></a>
158+
### Nested Schema for `rules.condition_groups.conditions`
159+
160+
Optional:
161+
162+
- `alert_urgency_ids` (List of String) The Alert Urgency IDs to check in the condition
163+
- `conditionable_id` (String) The ID of the conditionable
164+
- `conditionable_type` (String) The type of the conditionable. Value must be one of `AlertField`.
165+
- `property_field_condition_type` (String) Value must be one of `is_one_of`, `is_not_one_of`, `contains`, `does_not_contain`, `starts_with`, `ends_with`, `matches_regex`, `is_empty`.
166+
- `property_field_name` (String) The name of the property field
167+
- `property_field_type` (String) Value must be one of `attribute`, `payload`, `alert_field`.
168+
- `property_field_value` (String) The value of the property field
169+
- `property_field_values` (List of String)
170+
171+
172+
173+
<a id="nestedblock--rules--destinations"></a>
174+
### Nested Schema for `rules.destinations`
175+
176+
Optional:
177+
178+
- `target_id` (String) The ID of the target
179+
- `target_type` (String) The type of the target. Value must be one of `Service`, `Group`, `EscalationPolicy`.
180+
137181
## Import
138182

139183
rootly_alert_route can be imported using the [`import` command](https://developer.hashicorp.com/terraform/cli/commands/import).

provider/resource_alert_route.go

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-log/tflog"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1314
"github.com/rootlyhq/terraform-provider-rootly/v2/client"
1415
"github.com/rootlyhq/terraform-provider-rootly/v2/tools"
1516
)
@@ -74,6 +75,236 @@ func resourceAlertRoute() *schema.Resource {
7475
WriteOnly: false,
7576
Description: "",
7677
},
78+
79+
"rules": &schema.Schema{
80+
Type: schema.TypeList,
81+
Computed: false,
82+
Required: false,
83+
Optional: true,
84+
Sensitive: false,
85+
ForceNew: false,
86+
WriteOnly: false,
87+
Description: "",
88+
DiffSuppressFunc: tools.EqualIgnoringOrder,
89+
Elem: &schema.Resource{
90+
Schema: map[string]*schema.Schema{
91+
92+
"name": &schema.Schema{
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Required: false,
96+
Optional: true,
97+
Sensitive: false,
98+
ForceNew: false,
99+
WriteOnly: false,
100+
Description: "The name of the alert routing rule",
101+
},
102+
103+
"position": &schema.Schema{
104+
Type: schema.TypeInt,
105+
Computed: true,
106+
Required: false,
107+
Optional: true,
108+
Sensitive: false,
109+
ForceNew: false,
110+
WriteOnly: false,
111+
Description: "The position of the alert routing rule for ordering evaluation",
112+
},
113+
114+
"fallback_rule": &schema.Schema{
115+
Type: schema.TypeBool,
116+
Computed: true,
117+
Required: false,
118+
Optional: true,
119+
Sensitive: false,
120+
ForceNew: false,
121+
WriteOnly: false,
122+
Description: "Whether this is a fallback rule. Value must be one of true or false",
123+
},
124+
125+
"destinations": &schema.Schema{
126+
Type: schema.TypeList,
127+
Computed: false,
128+
Required: false,
129+
Optional: true,
130+
Sensitive: false,
131+
ForceNew: false,
132+
WriteOnly: false,
133+
Description: "",
134+
DiffSuppressFunc: tools.EqualIgnoringOrder,
135+
Elem: &schema.Resource{
136+
Schema: map[string]*schema.Schema{
137+
138+
"target_type": &schema.Schema{
139+
Type: schema.TypeString,
140+
Default: "Service",
141+
Required: false,
142+
Optional: true,
143+
Sensitive: false,
144+
ForceNew: false,
145+
WriteOnly: false,
146+
Description: "The type of the target. Value must be one of `Service`, `Group`, `EscalationPolicy`.",
147+
ValidateFunc: validation.StringInSlice([]string{"Service", "Group", "EscalationPolicy"}, false),
148+
},
149+
150+
"target_id": &schema.Schema{
151+
Type: schema.TypeString,
152+
Computed: true,
153+
Required: false,
154+
Optional: true,
155+
Sensitive: false,
156+
ForceNew: false,
157+
WriteOnly: false,
158+
Description: "The ID of the target",
159+
},
160+
},
161+
},
162+
},
163+
164+
"condition_groups": &schema.Schema{
165+
Type: schema.TypeList,
166+
Computed: false,
167+
Required: false,
168+
Optional: true,
169+
Sensitive: false,
170+
ForceNew: false,
171+
WriteOnly: false,
172+
Description: "",
173+
DiffSuppressFunc: tools.EqualIgnoringOrder,
174+
Elem: &schema.Resource{
175+
Schema: map[string]*schema.Schema{
176+
177+
"position": &schema.Schema{
178+
Type: schema.TypeInt,
179+
Computed: true,
180+
Required: false,
181+
Optional: true,
182+
Sensitive: false,
183+
ForceNew: false,
184+
WriteOnly: false,
185+
Description: "The position of the condition group",
186+
},
187+
188+
"conditions": &schema.Schema{
189+
Type: schema.TypeList,
190+
Computed: false,
191+
Required: false,
192+
Optional: true,
193+
Sensitive: false,
194+
ForceNew: false,
195+
WriteOnly: false,
196+
Description: "",
197+
DiffSuppressFunc: tools.EqualIgnoringOrder,
198+
Elem: &schema.Resource{
199+
Schema: map[string]*schema.Schema{
200+
201+
"property_field_condition_type": &schema.Schema{
202+
Type: schema.TypeString,
203+
Default: "is_one_of",
204+
Required: false,
205+
Optional: true,
206+
Sensitive: false,
207+
ForceNew: false,
208+
WriteOnly: false,
209+
Description: "Value must be one of `is_one_of`, `is_not_one_of`, `contains`, `does_not_contain`, `starts_with`, `ends_with`, `matches_regex`, `is_empty`.",
210+
ValidateFunc: validation.StringInSlice([]string{"is_one_of", "is_not_one_of", "contains", "does_not_contain", "starts_with", "ends_with", "matches_regex", "is_empty"}, false),
211+
},
212+
213+
"property_field_name": &schema.Schema{
214+
Type: schema.TypeString,
215+
Computed: true,
216+
Required: false,
217+
Optional: true,
218+
Sensitive: false,
219+
ForceNew: false,
220+
WriteOnly: false,
221+
Description: "The name of the property field",
222+
},
223+
224+
"property_field_type": &schema.Schema{
225+
Type: schema.TypeString,
226+
Default: "attribute",
227+
Required: false,
228+
Optional: true,
229+
Sensitive: false,
230+
ForceNew: false,
231+
WriteOnly: false,
232+
Description: "Value must be one of `attribute`, `payload`, `alert_field`.",
233+
ValidateFunc: validation.StringInSlice([]string{"attribute", "payload", "alert_field"}, false),
234+
},
235+
236+
"property_field_value": &schema.Schema{
237+
Type: schema.TypeString,
238+
Computed: true,
239+
Required: false,
240+
Optional: true,
241+
Sensitive: false,
242+
ForceNew: false,
243+
WriteOnly: false,
244+
Description: "The value of the property field",
245+
},
246+
247+
"property_field_values": &schema.Schema{
248+
Type: schema.TypeList,
249+
Elem: &schema.Schema{
250+
Type: schema.TypeString,
251+
},
252+
DiffSuppressFunc: tools.EqualIgnoringOrder,
253+
Computed: false,
254+
Required: false,
255+
Optional: true,
256+
Sensitive: false,
257+
ForceNew: false,
258+
WriteOnly: false,
259+
Description: "",
260+
},
261+
262+
"alert_urgency_ids": &schema.Schema{
263+
Type: schema.TypeList,
264+
Elem: &schema.Schema{
265+
Type: schema.TypeString,
266+
},
267+
DiffSuppressFunc: tools.EqualIgnoringOrder,
268+
Computed: false,
269+
Required: false,
270+
Optional: true,
271+
Sensitive: false,
272+
ForceNew: false,
273+
WriteOnly: false,
274+
Description: "The Alert Urgency IDs to check in the condition",
275+
},
276+
277+
"conditionable_type": &schema.Schema{
278+
Type: schema.TypeString,
279+
Default: "AlertField",
280+
Required: false,
281+
Optional: true,
282+
Sensitive: false,
283+
ForceNew: false,
284+
WriteOnly: false,
285+
Description: "The type of the conditionable. Value must be one of `AlertField`.",
286+
ValidateFunc: validation.StringInSlice([]string{"AlertField"}, false),
287+
},
288+
289+
"conditionable_id": &schema.Schema{
290+
Type: schema.TypeString,
291+
Computed: true,
292+
Required: false,
293+
Optional: true,
294+
Sensitive: false,
295+
ForceNew: false,
296+
WriteOnly: false,
297+
Description: "The ID of the conditionable",
298+
},
299+
},
300+
},
301+
},
302+
},
303+
},
304+
},
305+
},
306+
},
307+
},
77308
},
78309
}
79310
}
@@ -97,6 +328,9 @@ func resourceAlertRouteCreate(ctx context.Context, d *schema.ResourceData, meta
97328
if value, ok := d.GetOkExists("owning_team_ids"); ok {
98329
s.OwningTeamIds = value.([]interface{})
99330
}
331+
if value, ok := d.GetOkExists("rules"); ok {
332+
s.Rules = value.([]interface{})
333+
}
100334

101335
res, err := c.CreateAlertRoute(s)
102336
if err != nil {
@@ -131,6 +365,28 @@ func resourceAlertRouteRead(ctx context.Context, d *schema.ResourceData, meta in
131365
d.Set("alerts_source_ids", item.AlertsSourceIds)
132366
d.Set("owning_team_ids", item.OwningTeamIds)
133367

368+
if item.Rules != nil {
369+
processed_items_rules := make([]map[string]interface{}, 0)
370+
371+
for _, c := range item.Rules {
372+
if rawItem, ok := c.(map[string]interface{}); ok {
373+
// Create a new map with only the fields defined in the schema
374+
processed_item_rules := map[string]interface{}{
375+
"name": rawItem["name"],
376+
"position": rawItem["position"],
377+
"fallback_rule": rawItem["fallback_rule"],
378+
"destinations": rawItem["destinations"],
379+
"condition_groups": rawItem["condition_groups"],
380+
}
381+
processed_items_rules = append(processed_items_rules, processed_item_rules)
382+
}
383+
}
384+
385+
d.Set("rules", processed_items_rules)
386+
} else {
387+
d.Set("rules", nil)
388+
}
389+
134390
return nil
135391
}
136392

@@ -163,6 +419,14 @@ func resourceAlertRouteUpdate(ctx context.Context, d *schema.ResourceData, meta
163419
}
164420
}
165421

422+
if d.HasChange("rules") {
423+
if value, ok := d.GetOk("rules"); value != nil && ok {
424+
s.Rules = value.([]interface{})
425+
} else {
426+
s.Rules = []interface{}{}
427+
}
428+
}
429+
166430
_, err := c.UpdateAlertRoute(d.Id(), s)
167431
if err != nil {
168432
return diag.Errorf("Error updating alert_route: %s", err.Error())

0 commit comments

Comments
 (0)