Skip to content

Commit

Permalink
fix: Alert policy filter conditions/time restrictions/responders shou…
Browse files Browse the repository at this point in the history
…ld be set, not an ordered list. (#390)

* alert_policy: Conditions is a set, not an ordered list

* alert_policy: time restrictions is a set, not an ordered list

* alert_policy: responders is a set, not an ordered list
  • Loading branch information
ahuseby authored Sep 27, 2023
1 parent f2fd65f commit d4378be
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions opsgenie/resource_opsgenie_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package opsgenie
import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
"strconv"

"log"
"strings"
Expand Down Expand Up @@ -70,7 +71,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"match-all", "match-any-condition", "match-all-conditions"}, false),
},
"conditions": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"time-of-day", "weekday-and-time-of-day"}, false),
},
"restrictions": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -160,7 +161,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
},
},
"restriction": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -238,7 +239,7 @@ func resourceOpsGenieAlertPolicy() *schema.Resource {
Default: false,
},
"responders": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -318,7 +319,7 @@ func resourceOpsGenieAlertPolicyCreate(ctx context.Context, d *schema.ResourceDa
Tags: flattenOpsgenieAlertPolicyTags(d),
}

if len(d.Get("responders").([]interface{})) > 0 {
if d.Get("responders").(*schema.Set).Len() > 0 {
createRequest.Responders = expandOpsGenieAlertPolicyResponders(d)
}

Expand Down Expand Up @@ -437,7 +438,7 @@ func resourceOpsGenieAlertPolicyUpdate(d *schema.ResourceData, meta interface{})
Tags: flattenOpsgenieAlertPolicyTags(d),
}

if len(d.Get("responders").([]interface{})) > 0 {
if d.Get("responders").(*schema.Set).Len() > 0 {
updateRequest.Responders = expandOpsGenieAlertPolicyResponders(d)
}

Expand Down Expand Up @@ -499,14 +500,14 @@ func expandOpsGenieAlertPolicyRequestMainFields(d *schema.ResourceData) *policy.
}

func expandOpsGenieAlertPolicyResponders(d *schema.ResourceData) *[]alert.Responder {
input := d.Get("responders").([]interface{})
responders := make([]alert.Responder, 0, len(input))
input := d.Get("responders").(*schema.Set)
responders := make([]alert.Responder, 0, input.Len())

if input == nil {
return &responders
}

for _, v := range input {
for _, v := range input.List() {
config := v.(map[string]interface{})
responderID := config["id"].(string)
name := config["name"].(string)
Expand Down Expand Up @@ -535,19 +536,19 @@ func expandOpsGenieAlertPolicyFilter(input []interface{}) *og.Filter {
for _, v := range input {
config := v.(map[string]interface{})
filter.ConditionMatchType = og.ConditionMatchType(config["type"].(string))
filter.Conditions = expandOpsGenieAlertPolicyFilterConditions(config["conditions"].([]interface{}))
filter.Conditions = expandOpsGenieAlertPolicyFilterConditions(config["conditions"].(*schema.Set))
}
return &filter
}

func expandOpsGenieAlertPolicyFilterConditions(input []interface{}) []og.Condition {
conditions := make([]og.Condition, 0, len(input))
func expandOpsGenieAlertPolicyFilterConditions(input *schema.Set) []og.Condition {
conditions := make([]og.Condition, 0, input.Len())
condition := og.Condition{}
if input == nil {
return conditions
}

for _, v := range input {
for _, v := range input.List() {
config := v.(map[string]interface{})
not_value := config["not"].(bool)
order := config["order"].(int)
Expand All @@ -567,9 +568,9 @@ func expandOpsGenieAlertPolicyTimeRestriction(d []interface{}) *og.TimeRestricti
for _, v := range d {
config := v.(map[string]interface{})
timeRestriction.Type = og.RestrictionType(config["type"].(string))
if len(config["restrictions"].([]interface{})) > 0 {
restrictionList := make([]og.Restriction, 0, len(config["restrictions"].([]interface{})))
for _, v := range config["restrictions"].([]interface{}) {
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))
Expand All @@ -588,7 +589,7 @@ func expandOpsGenieAlertPolicyTimeRestriction(d []interface{}) *og.TimeRestricti
timeRestriction.RestrictionList = restrictionList
} else {
restriction := og.Restriction{}
for _, v := range config["restriction"].([]interface{}) {
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))
Expand Down

0 comments on commit d4378be

Please sign in to comment.