|
| 1 | +package checkly |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + checkly "github.com/checkly/checkly-go-sdk" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +const alertSettingsAttributeName = "alert_settings" |
| 11 | + |
| 12 | +type AlertSettingsAttributeSchemaOptions struct { |
| 13 | + Monitor bool |
| 14 | + EnableSSLCertificates bool |
| 15 | +} |
| 16 | + |
| 17 | +func makeAlertSettingsAttributeSchema(options AlertSettingsAttributeSchemaOptions) *schema.Schema { |
| 18 | + name := "check" |
| 19 | + if options.Monitor { |
| 20 | + name = "monitor" |
| 21 | + } |
| 22 | + |
| 23 | + attributes := map[string]*schema.Schema{ |
| 24 | + "escalation_type": { |
| 25 | + Description: "Determines the type of escalation to use. Possible values are `RUN_BASED` and `TIME_BASED`. (Default `RUN_BASED`).", |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Default: "RUN_BASED", |
| 29 | + ValidateFunc: validateOneOf([]string{"RUN_BASED", "TIME_BASED"}), |
| 30 | + }, |
| 31 | + "run_based_escalation": { |
| 32 | + Description: "Configuration for run-based escalation.", |
| 33 | + Type: schema.TypeList, |
| 34 | + Optional: true, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "failed_run_threshold": { |
| 39 | + Description: fmt.Sprintf("Send an alert notification after the given number of consecutive %s runs have failed. Possible values are between `1` and `5`. (Default `1`).", name), |
| 40 | + Type: schema.TypeInt, |
| 41 | + Optional: true, |
| 42 | + Default: 1, |
| 43 | + ValidateFunc: validateBetween(1, 5), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + "time_based_escalation": { |
| 49 | + Description: "Configuration for time-based escalation.", |
| 50 | + Type: schema.TypeList, |
| 51 | + Optional: true, |
| 52 | + Computed: true, |
| 53 | + Elem: &schema.Resource{ |
| 54 | + Schema: map[string]*schema.Schema{ |
| 55 | + "minutes_failing_threshold": { |
| 56 | + Description: fmt.Sprintf("Send an alert notification after the %s has been failing for the given amount of time (in minutes). Possible values are `5`, `10`, `15`, and `30`. (Default `5`).", name), |
| 57 | + Type: schema.TypeInt, |
| 58 | + Optional: true, |
| 59 | + Default: 5, |
| 60 | + ValidateFunc: validateOneOf([]int{5, 10, 15, 30}), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + "reminders": { |
| 66 | + Description: "Defines how often to send reminder notifications after initial alert.", |
| 67 | + Type: schema.TypeList, |
| 68 | + Optional: true, |
| 69 | + Computed: true, |
| 70 | + Elem: &schema.Resource{ |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "amount": { |
| 73 | + Description: "Number of reminder notifications to send. Possible values are `0`, `1`, `2`, `3`, `4`, `5`, and `100000` (`0` to disable, `100000` for unlimited). (Default `0`).", |
| 74 | + Type: schema.TypeInt, |
| 75 | + Optional: true, |
| 76 | + Default: 0, |
| 77 | + ValidateFunc: validateOneOf([]int{0, 1, 2, 3, 4, 5, 100000}), |
| 78 | + }, |
| 79 | + "interval": { |
| 80 | + Description: "Interval between reminder notifications in minutes. Possible values are `5`, `10`, `15`, and `30`. (Default `5`).", |
| 81 | + Type: schema.TypeInt, |
| 82 | + Optional: true, |
| 83 | + Default: 5, |
| 84 | + ValidateFunc: validateOneOf([]int{5, 10, 15, 30}), |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + "parallel_run_failure_threshold": { |
| 90 | + Description: "Configuration for parallel run failure threshold.", |
| 91 | + Type: schema.TypeList, |
| 92 | + Optional: true, |
| 93 | + Computed: true, |
| 94 | + Elem: &schema.Resource{ |
| 95 | + Schema: map[string]*schema.Schema{ |
| 96 | + "enabled": { |
| 97 | + Description: fmt.Sprintf("Whether parallel run failure threshold is enabled. Only applies if the %s is scheduled for multiple locations in parallel. (Default `false`).", name), |
| 98 | + Type: schema.TypeBool, |
| 99 | + Optional: true, |
| 100 | + Default: false, |
| 101 | + }, |
| 102 | + "percentage": { |
| 103 | + Description: "Percentage of runs that must fail to trigger alert. Possible values are `10`, `20`, `30`, `40`, `50`, `60`, `70`, `80`, `90`, and `100`. (Default `10`).", |
| 104 | + Type: schema.TypeInt, |
| 105 | + Optional: true, |
| 106 | + Default: 10, |
| 107 | + ValidateFunc: validateOneOf([]int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + if options.EnableSSLCertificates { |
| 115 | + attributes["ssl_certificates"] = &schema.Schema{ |
| 116 | + Type: schema.TypeSet, |
| 117 | + Optional: true, |
| 118 | + Deprecated: "This legacy attribute is no longer available and even if set, does not affect behavior. It will be removed in the next major version.", |
| 119 | + Elem: &schema.Resource{ |
| 120 | + Schema: map[string]*schema.Schema{ |
| 121 | + "enabled": { |
| 122 | + Type: schema.TypeBool, |
| 123 | + Optional: true, |
| 124 | + Description: "No longer available.", |
| 125 | + }, |
| 126 | + "alert_threshold": { |
| 127 | + Type: schema.TypeInt, |
| 128 | + Optional: true, |
| 129 | + Description: "No longer available.", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return &schema.Schema{ |
| 137 | + Description: fmt.Sprintf("Determines the alert escalation policy for the %s.", name), |
| 138 | + Type: schema.TypeList, |
| 139 | + Optional: true, |
| 140 | + Computed: true, |
| 141 | + MaxItems: 1, |
| 142 | + Elem: &schema.Resource{ |
| 143 | + Schema: attributes, |
| 144 | + }, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func alertSettingsFromSet(s []interface{}) checkly.AlertSettings { |
| 149 | + if len(s) == 0 { |
| 150 | + return checkly.AlertSettings{ |
| 151 | + EscalationType: checkly.RunBased, |
| 152 | + RunBasedEscalation: checkly.RunBasedEscalation{ |
| 153 | + FailedRunThreshold: 1, |
| 154 | + }, |
| 155 | + } |
| 156 | + } |
| 157 | + res := s[0].(tfMap) |
| 158 | + alertSettings := checkly.AlertSettings{ |
| 159 | + EscalationType: res["escalation_type"].(string), |
| 160 | + Reminders: remindersFromSet(res["reminders"].([]interface{})), |
| 161 | + ParallelRunFailureThreshold: parallelRunFailureThresholdFromSet(res["parallel_run_failure_threshold"].([]interface{})), |
| 162 | + } |
| 163 | + |
| 164 | + if alertSettings.EscalationType == checkly.RunBased { |
| 165 | + alertSettings.RunBasedEscalation = runBasedEscalationFromSet(res["run_based_escalation"].([]interface{})) |
| 166 | + } else { |
| 167 | + alertSettings.TimeBasedEscalation = timeBasedEscalationFromSet(res["time_based_escalation"].([]interface{})) |
| 168 | + } |
| 169 | + |
| 170 | + return alertSettings |
| 171 | +} |
| 172 | + |
| 173 | +func setFromAlertSettings(as checkly.AlertSettings) []tfMap { |
| 174 | + if as.EscalationType == checkly.RunBased { |
| 175 | + return []tfMap{ |
| 176 | + { |
| 177 | + "escalation_type": as.EscalationType, |
| 178 | + "run_based_escalation": []tfMap{ |
| 179 | + { |
| 180 | + "failed_run_threshold": as.RunBasedEscalation.FailedRunThreshold, |
| 181 | + }, |
| 182 | + }, |
| 183 | + "reminders": []tfMap{ |
| 184 | + { |
| 185 | + "amount": as.Reminders.Amount, |
| 186 | + "interval": as.Reminders.Interval, |
| 187 | + }, |
| 188 | + }, |
| 189 | + "parallel_run_failure_threshold": []tfMap{ |
| 190 | + { |
| 191 | + "enabled": as.ParallelRunFailureThreshold.Enabled, |
| 192 | + "percentage": as.ParallelRunFailureThreshold.Percentage, |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | + } |
| 197 | + } else { |
| 198 | + return []tfMap{ |
| 199 | + { |
| 200 | + "escalation_type": as.EscalationType, |
| 201 | + "time_based_escalation": []tfMap{ |
| 202 | + { |
| 203 | + "minutes_failing_threshold": as.TimeBasedEscalation.MinutesFailingThreshold, |
| 204 | + }, |
| 205 | + }, |
| 206 | + "reminders": []tfMap{ |
| 207 | + { |
| 208 | + "amount": as.Reminders.Amount, |
| 209 | + "interval": as.Reminders.Interval, |
| 210 | + }, |
| 211 | + }, |
| 212 | + "parallel_run_failure_threshold": []tfMap{ |
| 213 | + { |
| 214 | + "enabled": as.ParallelRunFailureThreshold.Enabled, |
| 215 | + "percentage": as.ParallelRunFailureThreshold.Percentage, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments