Skip to content

Commit

Permalink
Refactor time_restriction and stabilise plans containing `notificat…
Browse files Browse the repository at this point in the history
…ion_rule.steps`, `notification_policy.filter.conditions` and `time_restrictions` (#404)

* Fix: Incorrect documentation on notification policies.
* Refactor: Standardise handling of all TimeRestrictions 
* Fix: In-place updates of Notification conditions.
  • Loading branch information
Baarsgaard authored Nov 6, 2023
1 parent 70e1f11 commit d9700b2
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 838 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
## 0.6.34 (November 06, 2023)
* BUGFIX: [#404](https://github.com/opsgenie/terraform-provider-opsgenie/pulls/404)
* **Notification Policy:**
* Fixed perpetual drift for policies when filters contain more than one condition.
* **time_restriction:**
* Fixed perpetual drift for `notification/alert policies`, `notification/team_routing rules` and `schedule_rotation` containing `time_restriction` blocks.
* **Notification Rule:**
* Fixed perpetual drift for rules when they contain more than one step.

* IMPROVEMENTS: [#404](https://github.com/opsgenie/terraform-provider-opsgenie/pulls/404)
* **time_restriction:**
* Added further schema validation to make it easier to type valid `time_restriction` blocks when using the `terraform-ls` language server.
* **Notification Policy:**
* Added further schema validation to make it easier to add multiple `action` blocks when using the `terraform-ls` language server.

## 0.6.28 (July 13, 2023)
* BUGFIX:
* **API Integration:**
* Fixes an issue where owner team could not be updated when the API integration is linked with an Integration action.


## 0.6.27 (July 11, 2023)
* IMPROVEMENTS:
* **Alert Policy:**
Expand Down
92 changes: 92 additions & 0 deletions opsgenie/commonschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package opsgenie

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func timeRestrictionSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"time-of-day", "weekday-and-time-of-day"}, false),
},
"restrictions": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"time_restriction.0.restriction"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_day": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}, false),
},
"end_day": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}, false),
},
"start_hour": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 23),
},
"start_min": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 59),
},
"end_hour": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 23),
},
"end_min": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 59),
},
},
},
},
"restriction": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"time_restriction.0.restrictions"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_hour": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 23),
},
"start_min": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 59),
},
"end_hour": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 23),
},
"end_min": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 59),
},
},
},
},
},
},
}
}
152 changes: 4 additions & 148 deletions opsgenie/resource_opsgenie_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
"filter": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Expand Down Expand Up @@ -118,75 +119,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
},
},
},
"time_restriction": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"time-of-day", "weekday-and-time-of-day"}, false),
},
"restrictions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_day": {
Type: schema.TypeString,
Required: true,
},
"end_day": {
Type: schema.TypeString,
Required: true,
},
"start_hour": {
Type: schema.TypeInt,
Required: true,
},
"start_min": {
Type: schema.TypeInt,
Required: true,
},
"end_hour": {
Type: schema.TypeInt,
Required: true,
},
"end_min": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"restriction": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_hour": {
Type: schema.TypeInt,
Required: true,
},
"start_min": {
Type: schema.TypeInt,
Required: true,
},
"end_hour": {
Type: schema.TypeInt,
Required: true,
},
"end_min": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
"time_restriction": timeRestrictionSchema(),
"message": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -393,7 +326,7 @@ func resourceOpsGenieAlertPolicyRead(ctx context.Context, d *schema.ResourceData

if policyRes.MainFields.TimeRestriction != nil {
log.Printf("[DEBUG] 'policy.MainFields.TimeRestriction' is not 'nil'.")
d.Set("time_restriction", flattenOpsgenieAlertPolicyTimeRestriction(policyRes.MainFields.TimeRestriction))
d.Set("time_restriction", flattenOpsgenieTimeRestriction(policyRes.MainFields.TimeRestriction))
} else {
log.Printf("[DEBUG] 'policy.MainFields.TimeRestriction' is 'nil'.")
d.Set("time_restriction", nil)
Expand Down Expand Up @@ -494,7 +427,7 @@ func expandOpsGenieAlertPolicyRequestMainFields(d *schema.ResourceData) *policy.
fields.Filter = expandOpsGenieAlertPolicyFilter(d.Get("filter").([]interface{}))
}
if len(d.Get("time_restriction").([]interface{})) > 0 {
fields.TimeRestriction = expandOpsGenieAlertPolicyTimeRestriction(d.Get("time_restriction").([]interface{}))
fields.TimeRestriction = expandOpsGenieTimeRestriction(d.Get("time_restriction").([]interface{}))
}
return &fields
}
Expand Down Expand Up @@ -563,52 +496,6 @@ func expandOpsGenieAlertPolicyFilterConditions(input *schema.Set) []og.Condition
return conditions
}

func expandOpsGenieAlertPolicyTimeRestriction(d []interface{}) *og.TimeRestriction {
timeRestriction := og.TimeRestriction{}
for _, v := range d {
config := v.(map[string]interface{})
timeRestriction.Type = og.RestrictionType(config["type"].(string))
if config["restrictions"].(*schema.Set).Len() > 0 {
restrictionList := make([]og.Restriction, 0, config["restrictions"].(*schema.Set).Len())
for _, v := range config["restrictions"].(*schema.Set).List() {
config := v.(map[string]interface{})
startHour := uint32(config["start_hour"].(int))
startMin := uint32(config["start_min"].(int))
endHour := uint32(config["end_hour"].(int))
endMin := uint32(config["end_min"].(int))
restriction := og.Restriction{
StartDay: og.Day(config["start_day"].(string)),
StartHour: &startHour,
StartMin: &startMin,
EndHour: &endHour,
EndDay: og.Day(config["end_day"].(string)),
EndMin: &endMin,
}
restrictionList = append(restrictionList, restriction)
}
timeRestriction.RestrictionList = restrictionList
} else {
restriction := og.Restriction{}
for _, v := range config["restriction"].(*schema.Set).List() {
config := v.(map[string]interface{})
startHour := uint32(config["start_hour"].(int))
startMin := uint32(config["start_min"].(int))
endHour := uint32(config["end_hour"].(int))
endMin := uint32(config["end_min"].(int))
restriction = og.Restriction{
StartHour: &startHour,
StartMin: &startMin,
EndHour: &endHour,
EndMin: &endMin,
}
}

timeRestriction.Restriction = restriction
}
}
return &timeRestriction
}

func flattenOpsGenieAlertPolicyResponders(input *[]alert.Responder) []map[string]interface{} {
output := make([]map[string]interface{}, 0, len(*input))
for _, v := range *input {
Expand Down Expand Up @@ -651,37 +538,6 @@ func flattenOpsGenieAlertPolicyFilterConditions(input []og.Condition) []map[stri
return output
}

func flattenOpsgenieAlertPolicyTimeRestriction(input *og.TimeRestriction) []map[string]interface{} {
output := make([]map[string]interface{}, 0, 1)
element := make(map[string]interface{})
if len(input.RestrictionList) > 0 {
restrictions := make([]map[string]interface{}, 0, len(input.RestrictionList))
for _, r := range input.RestrictionList {
restrictionMap := make(map[string]interface{})
restrictionMap["start_min"] = r.StartMin
restrictionMap["start_hour"] = r.StartHour
restrictionMap["start_day"] = r.StartDay
restrictionMap["end_min"] = r.EndMin
restrictionMap["end_hour"] = r.EndHour
restrictionMap["end_day"] = r.EndDay
restrictions = append(restrictions, restrictionMap)
}
element["restrictions"] = restrictions
} else {
restriction := make([]map[string]interface{}, 0, 1)
restrictionMap := make(map[string]interface{})
restrictionMap["start_min"] = input.Restriction.StartMin
restrictionMap["start_hour"] = input.Restriction.StartHour
restrictionMap["end_min"] = input.Restriction.EndMin
restrictionMap["end_hour"] = input.Restriction.EndHour
restriction = append(restriction, restrictionMap)
element["restriction"] = restriction
}
element["type"] = input.Type
output = append(output, element)
return output
}

func flattenOpsgenieAlertPolicyActions(d *schema.ResourceData) []string {
input := d.Get("actions").(*schema.Set)
actions := make([]string, len(input.List()))
Expand Down
Loading

0 comments on commit d9700b2

Please sign in to comment.