Skip to content

Commit

Permalink
* fix: turn alert settings into type list and computed to stop showin…
Browse files Browse the repository at this point in the history
…gs diffs each time
  • Loading branch information
umutuzgur authored May 30, 2024
1 parent aec6a22 commit a284989
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
54 changes: 29 additions & 25 deletions checkly/resource_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func resourceCheck() *schema.Resource {
Description: "An array of one or more private locations slugs.",
},
"alert_settings": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Expand All @@ -243,8 +243,9 @@ func resourceCheck() *schema.Resource {
Description: "Determines what type of escalation to use. Possible values are `RUN_BASED` or `TIME_BASED`.",
},
"run_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"failed_run_threshold": {
Expand All @@ -256,8 +257,9 @@ func resourceCheck() *schema.Resource {
},
},
"time_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"minutes_failing_threshold": {
Expand All @@ -269,8 +271,9 @@ func resourceCheck() *schema.Resource {
},
},
"reminders": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"amount": {
Expand All @@ -288,8 +291,9 @@ func resourceCheck() *schema.Resource {
},
},
"parallel_run_failure_threshold": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Expand Down Expand Up @@ -799,7 +803,7 @@ func checkFromResourceData(d *schema.ResourceData) (checkly.Check, error) {
TearDownSnippetID: int64(d.Get("teardown_snippet_id").(int)),
LocalSetupScript: d.Get("local_setup_script").(string),
LocalTearDownScript: d.Get("local_teardown_script").(string),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").(*schema.Set)),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").([]interface{})),
UseGlobalAlertSettings: d.Get("use_global_alert_settings").(bool),
GroupID: int64(d.Get("group_id").(int)),
GroupOrder: d.Get("group_order").(int),
Expand Down Expand Up @@ -877,26 +881,26 @@ func basicAuthFromSet(s *schema.Set) *checkly.BasicAuth {
}
}

func alertSettingsFromSet(s *schema.Set) checkly.AlertSettings {
if s.Len() == 0 {
func alertSettingsFromSet(s []interface{}) checkly.AlertSettings {
if len(s) == 0 {
return checkly.AlertSettings{
EscalationType: checkly.RunBased,
RunBasedEscalation: checkly.RunBasedEscalation{
FailedRunThreshold: 1,
},
}
}
res := s.List()[0].(tfMap)
res := s[0].(tfMap)
alertSettings := checkly.AlertSettings{
EscalationType: res["escalation_type"].(string),
Reminders: remindersFromSet(res["reminders"].(*schema.Set)),
ParallelRunFailureThreshold: parallelRunFailureThresholdFromSet(res["parallel_run_failure_threshold"].(*schema.Set)),
Reminders: remindersFromSet(res["reminders"].([]interface{})),
ParallelRunFailureThreshold: parallelRunFailureThresholdFromSet(res["parallel_run_failure_threshold"].([]interface{})),
}

if alertSettings.EscalationType == checkly.RunBased {
alertSettings.RunBasedEscalation = runBasedEscalationFromSet(res["run_based_escalation"].(*schema.Set))
alertSettings.RunBasedEscalation = runBasedEscalationFromSet(res["run_based_escalation"].([]interface{}))
} else {
alertSettings.TimeBasedEscalation = timeBasedEscalationFromSet(res["time_based_escalation"].(*schema.Set))
alertSettings.TimeBasedEscalation = timeBasedEscalationFromSet(res["time_based_escalation"].([]interface{}))
}

return alertSettings
Expand Down Expand Up @@ -953,42 +957,42 @@ func environmentVariablesFromSet(s []interface{}) []checkly.EnvironmentVariable
return res
}

func runBasedEscalationFromSet(s *schema.Set) checkly.RunBasedEscalation {
if s.Len() == 0 {
func runBasedEscalationFromSet(s []interface{}) checkly.RunBasedEscalation {
if len(s) == 0 {
return checkly.RunBasedEscalation{}
}
res := s.List()[0].(tfMap)
res := s[0].(tfMap)
return checkly.RunBasedEscalation{
FailedRunThreshold: res["failed_run_threshold"].(int),
}
}

func timeBasedEscalationFromSet(s *schema.Set) checkly.TimeBasedEscalation {
if s.Len() == 0 {
func timeBasedEscalationFromSet(s []interface{}) checkly.TimeBasedEscalation {
if len(s) == 0 {
return checkly.TimeBasedEscalation{}
}
res := s.List()[0].(tfMap)
res := s[0].(tfMap)
return checkly.TimeBasedEscalation{
MinutesFailingThreshold: res["minutes_failing_threshold"].(int),
}
}

func remindersFromSet(s *schema.Set) checkly.Reminders {
if s.Len() == 0 {
func remindersFromSet(s []interface{}) checkly.Reminders {
if len(s) == 0 {
return checkly.Reminders{}
}
res := s.List()[0].(tfMap)
res := s[0].(tfMap)
return checkly.Reminders{
Amount: res["amount"].(int),
Interval: res["interval"].(int),
}
}

func parallelRunFailureThresholdFromSet(s *schema.Set) checkly.ParallelRunFailureThreshold {
if s.Len() == 0 {
func parallelRunFailureThresholdFromSet(s []interface{}) checkly.ParallelRunFailureThreshold {
if len(s) == 0 {
return checkly.ParallelRunFailureThreshold{}
}
res := s.List()[0].(tfMap)
res := s[0].(tfMap)
return checkly.ParallelRunFailureThreshold{
Enabled: res["enabled"].(bool),
Percentage: res["percentage"].(int),
Expand Down
18 changes: 11 additions & 7 deletions checkly/resource_check_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func resourceCheckGroup() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Description: "Setting this to `true` will trigger a retry when a check fails from the failing region and another, randomly selected region before marking the check as failed.",
Deprecated: "The property `double_check` is deprecated and will be removed in a future version. To enable retries for failed check runs, use the `retry_strategy` property instead.",
Deprecated: "The property `double_check` is deprecated and will be removed in a future version. To enable retries for failed check runs, use the `retry_strategy` property instead.",
},
"tags": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -152,7 +152,7 @@ func resourceCheckGroup() *schema.Resource {
},
},
"alert_settings": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Expand All @@ -165,8 +165,9 @@ func resourceCheckGroup() *schema.Resource {
Description: "Determines what type of escalation to use. Possible values are `RUN_BASED` or `TIME_BASED`.",
},
"run_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"failed_run_threshold": {
Expand All @@ -178,8 +179,9 @@ func resourceCheckGroup() *schema.Resource {
},
},
"time_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"minutes_failing_threshold": {
Expand All @@ -191,8 +193,9 @@ func resourceCheckGroup() *schema.Resource {
},
},
"reminders": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"amount": {
Expand All @@ -210,8 +213,9 @@ func resourceCheckGroup() *schema.Resource {
},
},
"parallel_run_failure_threshold": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Expand Down Expand Up @@ -534,7 +538,7 @@ func checkGroupFromResourceData(d *schema.ResourceData) (checkly.Group, error) {
TearDownSnippetID: int64(d.Get("teardown_snippet_id").(int)),
LocalSetupScript: d.Get("local_setup_script").(string),
LocalTearDownScript: d.Get("local_teardown_script").(string),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").(*schema.Set)),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").([]interface{})),
UseGlobalAlertSettings: d.Get("use_global_alert_settings").(bool),
APICheckDefaults: apiCheckDefaultsFromSet(d.Get("api_check_defaults").(*schema.Set)),
AlertChannelSubscriptions: alertChannelSubscriptionsFromSet(d.Get("alert_channel_subscription").([]interface{})),
Expand Down
30 changes: 17 additions & 13 deletions checkly/resource_heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceHeartbeat() *schema.Resource {
Description: "A list of tags for organizing and filtering checks.",
},
"alert_settings": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Expand All @@ -61,8 +61,9 @@ func resourceHeartbeat() *schema.Resource {
Description: "Determines what type of escalation to use. Possible values are `RUN_BASED` or `TIME_BASED`.",
},
"run_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"failed_run_threshold": {
Expand All @@ -74,8 +75,9 @@ func resourceHeartbeat() *schema.Resource {
},
},
"time_based_escalation": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"minutes_failing_threshold": {
Expand All @@ -87,8 +89,9 @@ func resourceHeartbeat() *schema.Resource {
},
},
"reminders": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"amount": {
Expand All @@ -106,8 +109,9 @@ func resourceHeartbeat() *schema.Resource {
},
},
"parallel_run_failure_threshold": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Expand Down Expand Up @@ -174,8 +178,8 @@ func resourceHeartbeat() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"period": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeInt,
Required: true,
Description: "How often you expect a ping to the ping URL.",
},
"period_unit": {
Expand All @@ -198,8 +202,8 @@ func resourceHeartbeat() *schema.Resource {
Description: "Possible values `seconds`, `minutes`, `hours` and `days`.",
},
"grace": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeInt,
Required: true,
Description: "How long Checkly should wait before triggering any alerts when a ping does not arrive within the set period.",
},
"grace_unit": {
Expand All @@ -222,9 +226,9 @@ func resourceHeartbeat() *schema.Resource {
Description: "Possible values `seconds`, `minutes`, `hours` and `days`.",
},
"ping_token": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Custom token to generate your ping URL. Checkly will expect a ping to `https://ping.checklyhq.com/[PING_TOKEN]`.",
},
},
Expand Down Expand Up @@ -317,7 +321,7 @@ func heartbeatCheckFromResourceData(d *schema.ResourceData) (checkly.HeartbeatCh
Activated: d.Get("activated").(bool),
Muted: d.Get("muted").(bool),
Tags: stringsFromSet(d.Get("tags").(*schema.Set)),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").(*schema.Set)),
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").([]interface{})),
UseGlobalAlertSettings: d.Get("use_global_alert_settings").(bool),
AlertChannelSubscriptions: alertChannelSubscriptionsFromSet(d.Get("alert_channel_subscription").([]interface{})),
}
Expand Down

0 comments on commit a284989

Please sign in to comment.