From d9700b27b5f8d7f3839048e7fc436d08ca4d72bc Mon Sep 17 00:00:00 2001 From: Steffen Baarsgaard Date: Mon, 6 Nov 2023 08:35:15 +0100 Subject: [PATCH] Refactor `time_restriction` and stabilise plans containing `notification_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. --- CHANGELOG.md | 16 +- opsgenie/commonschema.go | 92 ++++++ opsgenie/resource_opsgenie_alert_policy.go | 152 +--------- .../resource_opsgenie_notification_policy.go | 270 +++++------------- .../resource_opsgenie_notification_rule.go | 145 +--------- .../resource_opsgenie_schedule_rotation.go | 245 +--------------- .../resource_opsgenie_team_routing_rule.go | 127 +------- opsgenie/util.go | 82 ++++++ .../docs/r/notification_policy.html.markdown | 1 + 9 files changed, 292 insertions(+), 838 deletions(-) create mode 100644 opsgenie/commonschema.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 5854ce14..226ce3ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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:** diff --git a/opsgenie/commonschema.go b/opsgenie/commonschema.go new file mode 100644 index 00000000..0f1d1e16 --- /dev/null +++ b/opsgenie/commonschema.go @@ -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), + }, + }, + }, + }, + }, + }, + } +} diff --git a/opsgenie/resource_opsgenie_alert_policy.go b/opsgenie/resource_opsgenie_alert_policy.go index 054b0dd6..ead1686e 100644 --- a/opsgenie/resource_opsgenie_alert_policy.go +++ b/opsgenie/resource_opsgenie_alert_policy.go @@ -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": { @@ -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, @@ -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) @@ -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 } @@ -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 { @@ -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())) diff --git a/opsgenie/resource_opsgenie_notification_policy.go b/opsgenie/resource_opsgenie_notification_policy.go index 495a40c3..b8e7c6f5 100644 --- a/opsgenie/resource_opsgenie_notification_policy.go +++ b/opsgenie/resource_opsgenie_notification_policy.go @@ -13,22 +13,28 @@ import ( "github.com/opsgenie/opsgenie-go-sdk-v2/policy" ) -var ( - duration = &schema.Resource{ - Schema: map[string]*schema.Schema{ - "time_unit": { - Type: schema.TypeString, - Optional: true, - Default: "minutes", - ValidateFunc: validation.StringInSlice([]string{"days", "hours", "minutes"}, false), - }, - "time_amount": { - Type: schema.TypeInt, - Required: true, +func durationSchema(required bool, description string) *schema.Schema { + return &schema.Schema{ + Type: schema.TypeList, + Required: required, + Optional: !required, + Description: description, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "time_unit": { + Type: schema.TypeString, + Optional: true, + Default: "minutes", + ValidateFunc: validation.StringInSlice([]string{"days", "hours", "minutes"}, false), + }, + "time_amount": { + Type: schema.TypeInt, + Required: true, + }, }, }, } -) +} func resourceOpsGenieNotificationPolicy() *schema.Resource { return &schema.Resource{ @@ -70,6 +76,7 @@ func resourceOpsGenieNotificationPolicy() *schema.Resource { "filter": { Type: schema.TypeList, Required: true, + MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "type": { @@ -79,7 +86,7 @@ func resourceOpsGenieNotificationPolicy() *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{ @@ -126,98 +133,25 @@ func resourceOpsGenieNotificationPolicy() *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.TypeList, - 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.TypeList, - 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(), "auto_close_action": { - Type: schema.TypeList, - Optional: true, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + AtLeastOneOf: []string{"auto_close_action", "auto_restart_action", "de_duplication_action", "delay_action", "suppress"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "duration": { - Type: schema.TypeList, - Required: true, - Elem: duration, - }, + "duration": durationSchema(true, ""), }, }, }, "auto_restart_action": { Type: schema.TypeList, Optional: true, + MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "duration": { - Type: schema.TypeList, - Required: true, - Elem: duration, - }, + "duration": durationSchema(true, ""), "max_repeat_count": { Type: schema.TypeInt, Required: true, @@ -226,8 +160,10 @@ func resourceOpsGenieNotificationPolicy() *schema.Resource { }, }, "de_duplication_action": { - Type: schema.TypeList, - Optional: true, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"delay_action", "suppress"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "de_duplication_action_type": { @@ -239,18 +175,15 @@ func resourceOpsGenieNotificationPolicy() *schema.Resource { Type: schema.TypeInt, Required: true, }, - "duration": { - Type: schema.TypeList, - Optional: true, - Elem: duration, - }, + "duration": durationSchema(false, "Required when `de_duplication_action_type = \"frequency-based\"`"), }, }, }, "delay_action": { Type: schema.TypeList, Optional: true, - ConflictsWith: []string{"suppress"}, + MaxItems: 1, + ConflictsWith: []string{"de_duplication_action", "suppress"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "delay_option": { @@ -271,23 +204,24 @@ func resourceOpsGenieNotificationPolicy() *schema.Resource { Optional: true, ValidateFunc: validation.IntBetween(0, 23), }, - "duration": { - Type: schema.TypeList, - Optional: true, - Elem: duration, - }, + "duration": durationSchema(false, "Required when `delay_option = \"for-duration\"`"), }, }, }, "suppress": { - Type: schema.TypeBool, - Optional: true, + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"delay_action", "de_duplication_action"}, }, }, } } func resourceOpsGenieNotificationPolicyCreate(d *schema.ResourceData, meta interface{}) error { + err := resourceOpsGenieNotificationPolicyMultiValueValidation(d) + if err != nil { + return err + } client, err := policy.NewClient(meta.(*OpsgenieClient).client.Config) if err != nil { return err @@ -377,7 +311,7 @@ func resourceOpsGenieNotificationPolicyRead(d *schema.ResourceData, meta interfa } if policy.MainFields.TimeRestriction != nil { log.Printf("[DEBUG] 'policy.MainFields.TimeRestriction' is not 'nil'.") - d.Set("time_restriction", flattenOpsgenieNotificationPolicyTimeRestriction(policy.MainFields.TimeRestriction)) + d.Set("time_restriction", flattenOpsgenieTimeRestriction(policy.MainFields.TimeRestriction)) } else { log.Printf("[DEBUG] 'policy.MainFields.TimeRestriction' is 'nil'.") d.Set("time_restriction", nil) @@ -387,6 +321,10 @@ func resourceOpsGenieNotificationPolicyRead(d *schema.ResourceData, meta interfa } func resourceOpsGenieNotificationPolicyUpdate(d *schema.ResourceData, meta interface{}) error { + err := resourceOpsGenieNotificationPolicyMultiValueValidation(d) + if err != nil { + return err + } client, err := policy.NewClient(meta.(*OpsgenieClient).client.Config) if err != nil { return err @@ -440,6 +378,29 @@ func resourceOpsGenieNotificationPolicyDelete(d *schema.ResourceData, meta inter return nil } +func resourceOpsGenieNotificationPolicyMultiValueValidation(d *schema.ResourceData) error { + if de_dupe_actions := d.Get("de_duplication_action").([]interface{}); len(de_dupe_actions) == 1 { + de_dupe_action := de_dupe_actions[0].(map[string]interface{}) + de_dupe_action_type := de_dupe_action["de_duplication_action_type"].(string) + de_dupe_durations := de_dupe_action["duration"].([]interface{}) + + if de_dupe_action_type == "frequency-based" && len(de_dupe_durations) != 1 { + return fmt.Errorf("%s: de_duplication_action.duration is required when de_duplication_action_type = 'frequency-based'", d.Id()) + } + } + + if delay_actions := d.Get("delay_action").([]interface{}); len(delay_actions) == 1 { + delay_action := delay_actions[0].(map[string]interface{}) + delay_action_type := delay_action["delay_option"].(string) + + if delay_action_type == "for-duration" && len(delay_action["duration"].([]interface{})) != 1 { + return fmt.Errorf("%s: delay_action.%s is required when delay_option = 'for-duration'", d.Id(), "duration") + } + } + + return nil +} + func expandOpsGenieNotificationPolicyRequestMainFields(d *schema.ResourceData) *policy.MainFields { enabled := d.Get("enabled").(bool) fields := policy.MainFields{ @@ -452,7 +413,7 @@ func expandOpsGenieNotificationPolicyRequestMainFields(d *schema.ResourceData) * fields.Filter = expandOpsGenieNotificationPolicyFilter(d.Get("filter").([]interface{})) } if len(d.Get("time_restriction").([]interface{})) > 0 { - fields.TimeRestriction = expandOpsGenieNotificationPolicyTimeRestriction(d.Get("time_restriction").([]interface{})) + fields.TimeRestriction = expandOpsGenieTimeRestriction(d.Get("time_restriction").([]interface{})) } return &fields } @@ -546,19 +507,19 @@ func expandOpsGenieNotificationPolicyFilter(input []interface{}) *og.Filter { for _, v := range input { config := v.(map[string]interface{}) filter.ConditionMatchType = og.ConditionMatchType(config["type"].(string)) - filter.Conditions = expandOpsGenieNotificationPolicyFilterConditions(config["conditions"].([]interface{})) + filter.Conditions = expandOpsGenieNotificationPolicyFilterConditions(config["conditions"].(*schema.Set)) } return &filter } -func expandOpsGenieNotificationPolicyFilterConditions(input []interface{}) []og.Condition { - conditions := make([]og.Condition, 0, len(input)) +func expandOpsGenieNotificationPolicyFilterConditions(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) @@ -573,52 +534,6 @@ func expandOpsGenieNotificationPolicyFilterConditions(input []interface{}) []og. return conditions } -func expandOpsGenieNotificationPolicyTimeRestriction(d []interface{}) *og.TimeRestriction { - timeRestriction := og.TimeRestriction{} - 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{}) { - 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"].([]interface{}) { - 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 flattenOpsGenieNotificationPolicyDuration(input *policy.Duration) []map[string]interface{} { output := make([]map[string]interface{}, 0, 1) element := make(map[string]interface{}) @@ -701,34 +616,3 @@ func flattenOpsGenieNotificationPolicyFilterConditions(input []og.Condition) []m return output } - -func flattenOpsgenieNotificationPolicyTimeRestriction(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 -} diff --git a/opsgenie/resource_opsgenie_notification_rule.go b/opsgenie/resource_opsgenie_notification_rule.go index e1b3fc00..ac07b8b0 100644 --- a/opsgenie/resource_opsgenie_notification_rule.go +++ b/opsgenie/resource_opsgenie_notification_rule.go @@ -61,7 +61,7 @@ func resourceOpsGenieNotificationRule() *schema.Resource { }, }, "steps": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -122,74 +122,7 @@ func resourceOpsGenieNotificationRule() *schema.Resource { }, }, }, - "time_restriction": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "type": { - Type: schema.TypeString, - Required: true, - }, - "restrictions": { - Type: schema.TypeList, - 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.TypeList, - 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(), "schedules": { Type: schema.TypeList, Optional: true, @@ -285,12 +218,12 @@ func resourceOpsGenieNotificationRuleCreate(d *schema.ResourceData, meta interfa createRequest.Schedules = expandOpsGenieNotificationRuleSchedules(d.Get("schedules").([]interface{})) } - if len(d.Get("steps").([]interface{})) > 0 { - createRequest.Steps = expandOpsGenieNotificationRuleSteps(d.Get("steps").([]interface{})) + if d.Get("steps").(*schema.Set).Len() > 0 { + createRequest.Steps = expandOpsGenieNotificationRuleSteps(d.Get("steps").(*schema.Set)) } if len(timeRestriction) > 0 { - createRequest.TimeRestriction = expandNotificationRuleRestrictions(timeRestriction) + createRequest.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } log.Printf("[INFO] Creating Notification Rule '%s' for User: '%s'", d.Get("name").(string), d.Get("username").(string)) @@ -331,7 +264,7 @@ func resourceOpsGenieNotificationRuleRead(d *schema.ResourceData, meta interface d.Set("schedules", flattenNotificationSchedules(rule.Schedules)) } if rule.TimeRestriction != nil { - d.Set("time_restriction", flattenOpsgenieNotificationRuleTimeRestriction(rule.TimeRestriction)) + d.Set("time_restriction", flattenOpsgenieTimeRestriction(rule.TimeRestriction)) } else { d.Set("time_restriction", nil) } @@ -380,12 +313,12 @@ func resourceOpsGenieNotificationRuleUpdate(d *schema.ResourceData, meta interfa updateRequest.Schedules = expandOpsGenieNotificationRuleSchedules(d.Get("schedules").([]interface{})) } - if len(d.Get("steps").([]interface{})) > 0 { - updateRequest.Steps = expandOpsGenieNotificationRuleSteps(d.Get("steps").([]interface{})) + if d.Get("steps").(*schema.Set).Len() > 0 { + updateRequest.Steps = expandOpsGenieNotificationRuleSteps(d.Get("steps").(*schema.Set)) } if len(timeRestriction) > 0 { - updateRequest.TimeRestriction = expandNotificationRuleRestrictions(timeRestriction) + updateRequest.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } log.Printf("[INFO] Updating Notification Rule '%s' for User: '%s'", d.Get("name").(string), d.Get("username").(string)) @@ -430,12 +363,13 @@ func expandOpsGenieNotificationRuleNotificationTime(input *schema.Set) []notific return output } -func expandOpsGenieNotificationRuleSteps(input []interface{}) []*og.Step { - output := make([]*og.Step, 0) +func expandOpsGenieNotificationRuleSteps(input *schema.Set) []*og.Step { + output := make([]*og.Step, 0, input.Len()) if input == nil { return output } - for _, v := range input { + + for _, v := range input.List() { config := v.(map[string]interface{}) enabled := config["enabled"].(bool) element := og.Step{} @@ -498,59 +432,6 @@ func expandOpsGenieNotificationRuleRepeat(input []interface{}) *notification.Rep } return &repeat } -func expandNotificationRuleRestrictions(d []interface{}) *og.TimeRestriction { - timeRestriction := og.TimeRestriction{} - - for _, v := range d { - config := v.(map[string]interface{}) - - timeRestrictionType := config["type"].(string) - timeRestriction.Type = og.RestrictionType(timeRestrictionType) - - if len(config["restrictions"].([]interface{})) > 0 { - timeRestriction.RestrictionList = expandOpsgenieRestrictions(config["restrictions"].([]interface{})) - } else { - timeRestriction.Restriction = expandOpsgenieRestriction(config["restriction"].([]interface{})) - } - } - - return &timeRestriction -} - -func flattenOpsgenieNotificationRuleTimeRestriction(input *og.TimeRestriction) []map[string]interface{} { - rules := make([]map[string]interface{}, 0, 1) - out := make(map[string]interface{}) - out["type"] = input.Type - - 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_day"] = r.StartDay - restrictionMap["end_day"] = r.EndDay - restrictionMap["start_hour"] = r.StartHour - restrictionMap["start_min"] = r.StartMin - restrictionMap["end_hour"] = r.EndHour - restrictionMap["end_min"] = r.EndMin - restrictions = append(restrictions, restrictionMap) - } - out["restrictions"] = restrictions - rules = append(rules, out) - return rules - } else { - restriction := make(map[string]interface{}) - //IF RESTRICTION - restriction["start_hour"] = input.Restriction.StartHour - restriction["start_min"] = input.Restriction.StartMin - restriction["end_hour"] = input.Restriction.EndHour - restriction["end_min"] = input.Restriction.EndMin - - out["restriction"] = []map[string]interface{}{restriction} - rules = append(rules, out) - - return rules - } -} func expandOpsGenieNotificationRuleSchedules(input []interface{}) []notification.Schedule { output := make([]notification.Schedule, 0) diff --git a/opsgenie/resource_opsgenie_schedule_rotation.go b/opsgenie/resource_opsgenie_schedule_rotation.go index 2c33a747..8e4103bf 100644 --- a/opsgenie/resource_opsgenie_schedule_rotation.go +++ b/opsgenie/resource_opsgenie_schedule_rotation.go @@ -77,85 +77,7 @@ func resourceOpsgenieScheduleRotation() *schema.Resource { }, }, }, - "time_restriction": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateRestrictionType, - }, - "restrictions": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "start_day": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateDay, - }, - "end_day": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateDay, - }, - "start_hour": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateHourParams, - }, - "start_min": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateMinParams, - }, - "end_hour": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateHourParams, - }, - "end_min": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateMinParams, - }, - }, - }, - }, - "restriction": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "start_hour": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateHourParams, - }, - "start_min": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateMinParams, - }, - "end_hour": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateHourParams, - }, - "end_min": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validateMinParams, - }, - }, - }, - }, - }, - }, - }, + "time_restriction": timeRestrictionSchema(), }, } } @@ -205,7 +127,7 @@ func resourceOpsgenieScheduleRotationCreate(d *schema.ResourceData, meta interfa createRequest.Rotation.Length = uint32(length) } if len(timeRestriction) > 0 { - createRequest.Rotation.TimeRestriction = expandTimeRestrictions(timeRestriction) + createRequest.Rotation.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } log.Printf("[INFO] Creating OpsGenie rotation '%s'", name) @@ -244,7 +166,7 @@ func resourceOpsgenieScheduleRotationRead(d *schema.ResourceData, meta interface d.Set("type", getResponse.Type) d.Set("participant", flattenOpsgenieScheduleRotationParticipant(getResponse.Participants)) if getResponse.TimeRestriction != nil { - d.Set("time_restriction", flattenOpsgenieScheduleRotationTimeRestriction(getResponse.TimeRestriction)) + d.Set("time_restriction", flattenOpsgenieTimeRestriction(getResponse.TimeRestriction)) } d.Set("start_date", startDate) if getResponse.EndDate != nil { @@ -267,49 +189,6 @@ func flattenOpsgenieScheduleRotationParticipant(input []og.Participant) []map[st return participants } -func flattenOpsgenieScheduleRotationRestriction(input og.Restriction) []map[string]interface{} { - output := make([]map[string]interface{}, 0, 1) - restriction := make(map[string]interface{}) - - restriction["start_hour"] = input.StartHour - restriction["end_hour"] = input.EndHour - restriction["start_min"] = input.StartMin - restriction["end_min"] = input.EndMin - output = append(output, restriction) - - return output -} - -func flattenOpsgenieScheduleRotationRestrictions(input []og.Restriction) []map[string]interface{} { - restrictions := make([]map[string]interface{}, 0, len(input)) - for _, restriction := range input { - outputRestriction := make(map[string]interface{}) - outputRestriction["start_hour"] = restriction.StartHour - outputRestriction["end_hour"] = restriction.EndHour - outputRestriction["start_min"] = restriction.StartMin - outputRestriction["end_min"] = restriction.EndMin - outputRestriction["start_day"] = restriction.StartDay - outputRestriction["end_day"] = restriction.EndDay - restrictions = append(restrictions, outputRestriction) - } - - return restrictions -} - -func flattenOpsgenieScheduleRotationTimeRestriction(input *og.TimeRestriction) []map[string]interface{} { - output := make([]map[string]interface{}, 0, 1) - timeRestriction := make(map[string]interface{}) - timeRestriction["type"] = input.Type - if timeRestriction["type"] == og.TimeOfDay { - timeRestriction["restriction"] = flattenOpsgenieScheduleRotationRestriction(input.Restriction) - } else if timeRestriction["type"] == og.WeekdayAndTimeOfDay { - timeRestriction["restrictions"] = flattenOpsgenieScheduleRotationRestrictions(input.RestrictionList) - } - output = append(output, timeRestriction) - - return output -} - func resourceOpsgenieScheduleRotationUpdate(d *schema.ResourceData, meta interface{}) error { client, err := schedule.NewClient(meta.(*OpsgenieClient).client.Config) if err != nil { @@ -353,7 +232,7 @@ func resourceOpsgenieScheduleRotationUpdate(d *schema.ResourceData, meta interfa updateRequest.Rotation.EndDate = &endDate } if len(timeRestriction) > 0 { - updateRequest.Rotation.TimeRestriction = expandTimeRestrictions(timeRestriction) + updateRequest.Rotation.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } log.Printf("[INFO] Updating OpsGenie schedule rotation '%s'", name) @@ -410,73 +289,6 @@ func expandOpsgenieScheduleParticipants(input []interface{}) []og.Participant { return participants } -func expandTimeRestrictions(d []interface{}) *og.TimeRestriction { - timeRestriction := og.TimeRestriction{} - - for _, v := range d { - config := v.(map[string]interface{}) - - timeRestrictionType := config["type"].(string) - timeRestriction.Type = og.RestrictionType(timeRestrictionType) - - if len(config["restrictions"].([]interface{})) > 0 { - timeRestriction.RestrictionList = expandOpsgenieRestrictions(config["restrictions"].([]interface{})) - } else { - timeRestriction.Restriction = expandOpsgenieRestriction(config["restriction"].([]interface{})) - } - } - - return &timeRestriction -} - -func expandOpsgenieRestrictions(input []interface{}) []og.Restriction { - restrictionList := make([]og.Restriction, 0, len(input)) - - if input == nil { - return restrictionList - } - - for _, v := range input { - 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) - } - - return restrictionList -} - -func expandOpsgenieRestriction(input []interface{}) og.Restriction { - - restriction := og.Restriction{} - for _, v := range input { - 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, - } - - } - - return restriction -} func validateOpsgenieScheduleRotationType(v interface{}, k string) (ws []string, errors []error) { value := strings.ToLower(v.(string)) @@ -506,52 +318,3 @@ func validateScheduleRotationParticipantType(v interface{}, k string) (ws []stri } return } - -func validateDay(v interface{}, k string) (ws []string, errors []error) { - value := strings.ToLower(v.(string)) - families := map[string]bool{ - "monday": true, - "tuesday": true, - "wednesday": true, - "thursday": true, - "friday": true, - "saturday": true, - "sunday": true, - } - - if !families[value] { - errors = append(errors, fmt.Errorf("it can only be day of week (monday,tuesday...)")) - } - return -} - -func validateHourParams(v interface{}, k string) (ws []string, errors []error) { - value := v.(int) - - if value < 0 || value > 24 { - errors = append(errors, fmt.Errorf("hour must between 0-24")) - } - return -} -func validateMinParams(v interface{}, k string) (ws []string, errors []error) { - value := v.(int) - - if value < 0 || value > 59 { - errors = append(errors, fmt.Errorf("minute must in between of 0-59")) - - } - return -} - -func validateRestrictionType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - families := map[string]bool{ - "time-of-day": true, - "weekday-and-time-of-day": true, - } - - if !families[value] { - errors = append(errors, fmt.Errorf("time restriction type must be time-of-day or weekday-and-time-of-day")) - } - return -} diff --git a/opsgenie/resource_opsgenie_team_routing_rule.go b/opsgenie/resource_opsgenie_team_routing_rule.go index 5c63d6c9..b3cda0a4 100644 --- a/opsgenie/resource_opsgenie_team_routing_rule.go +++ b/opsgenie/resource_opsgenie_team_routing_rule.go @@ -123,74 +123,7 @@ func resourceOpsGenieTeamRoutingRule() *schema.Resource { }, }, }, - "time_restriction": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "type": { - Type: schema.TypeString, - Required: true, - }, - "restrictions": { - Type: schema.TypeList, - 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.TypeList, - 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(), }, } } @@ -224,7 +157,7 @@ func resourceOpsGenieTeamRoutingRuleCreate(d *schema.ResourceData, meta interfac } if len(timeRestriction) > 0 { - createRequest.TimeRestriction = expandRoutingRuleTimeRestrictions(timeRestriction) + createRequest.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } log.Printf("[INFO] Creating OpsGenie team routing rule '%s'", name) @@ -257,7 +190,7 @@ func resourceOpsGenieTeamRoutingRuleRead(d *schema.ResourceData, meta interface{ d.Set("is_default", result.IsDefault) d.Set("name", result.Name) d.Set("order", result.Order) - d.Set("time_restriction", flattenOpsgenieTimeRestriction(result.TimeRestriction)) + d.Set("time_restriction", flattenOpsgenieTimeRestriction(&result.TimeRestriction)) d.Set("notify", flattenOpsgenieNotify(result.Notify)) d.Set("criteria", flattenOpsgenieCriteria(result.Criteria)) d.Set("timezone", result.Timezone) @@ -293,7 +226,7 @@ func resourceOpsGenieTeamRoutingRuleUpdate(d *schema.ResourceData, meta interfac Notify: expandOpsgenieNotify(notify), } if len(timeRestriction) > 0 { - updateRequest.TimeRestriction = expandRoutingRuleTimeRestrictions(timeRestriction) + updateRequest.TimeRestriction = expandOpsGenieTimeRestriction(timeRestriction) } if !isDefault { @@ -373,58 +306,6 @@ func flattenOpsgenieCriteria(input og.Criteria) []map[string]interface{} { rules = append(rules, out) return rules } -func expandRoutingRuleTimeRestrictions(d []interface{}) *og.TimeRestriction { - timeRestriction := og.TimeRestriction{} - for _, v := range d { - config := v.(map[string]interface{}) - - timeRestrictionType := config["type"].(string) - timeRestriction.Type = og.RestrictionType(timeRestrictionType) - - if len(config["restrictions"].([]interface{})) > 0 { - timeRestriction.RestrictionList = expandOpsgenieRestrictions(config["restrictions"].([]interface{})) - } else { - timeRestriction.Restriction = expandOpsgenieRestriction(config["restriction"].([]interface{})) - } - } - - return &timeRestriction -} - -func flattenOpsgenieTimeRestriction(input og.TimeRestriction) []map[string]interface{} { - rules := make([]map[string]interface{}, 0, 1) - out := make(map[string]interface{}) - out["type"] = input.Type - - 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) - } - out["restrictions"] = restrictions - rules = append(rules, out) - return rules - } else { - restriction := make(map[string]interface{}) - //IF RESTRICTION - restriction["end_hour"] = input.Restriction.EndHour - restriction["end_min"] = input.Restriction.EndMin - restriction["start_hour"] = input.Restriction.StartHour - restriction["start_min"] = input.Restriction.StartMin - - //IF restrictions - out["restriction"] = restriction - rules = append(rules, out) - return rules - } -} func expandOpsgenieNotify(input []interface{}) *team.Notify { diff --git a/opsgenie/util.go b/opsgenie/util.go index 8b4a874f..5c549763 100644 --- a/opsgenie/util.go +++ b/opsgenie/util.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/opsgenie/opsgenie-go-sdk-v2/client" + "github.com/opsgenie/opsgenie-go-sdk-v2/og" ) // handleNonExistentResource is a wrapper of resourceFunc that @@ -89,3 +90,84 @@ func flattenTags(d *schema.ResourceData, fieldName string) []string { return tags } + +func expandOpsGenieTimeRestriction(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 flattenOpsgenieTimeRestriction(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 +} diff --git a/website/docs/r/notification_policy.html.markdown b/website/docs/r/notification_policy.html.markdown index cf5e0733..535db055 100644 --- a/website/docs/r/notification_policy.html.markdown +++ b/website/docs/r/notification_policy.html.markdown @@ -29,6 +29,7 @@ resource "opsgenie_notification_policy" "test" { until_hour = 9 } filter {} +} ``` ## Argument Reference