Skip to content

Commit b1169e8

Browse files
committed
Supports expected_status_code in external monitor resources.
1 parent 8f07f88 commit b1169e8

File tree

9 files changed

+24
-3
lines changed

9 files changed

+24
-3
lines changed

docs/data-sources/monitor.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ data "mackerel_monitor" "this" {
6363
* `headers` - The values configured as the HTTP request header.
6464
* `max_check_attempts` - Number of consecutive Warning/Critical counts before an alert is made.
6565
* `follow_redirect` - Evaluates the response of the redirector as a result.
66+
* `expected_status_code` - Expected http status code of the response.
6667
* `expression` - The settings for the monitor of expression monitoring.
6768
* `expression` - Expression of the monitoring target.
6869
* `operator` - The comparison operator to determines the conditions that state whether the designated variable is either big or small. The observed value is on the left of the operator and the designated value is on the right.

docs/resources/monitor.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ The following arguments are required:
179179
* `headers` - The values configured as the HTTP request header.
180180
* `max_check_attempts` - Number of consecutive Warning/Critical counts before an alert is made. Default is `1`. Valid values are numbers `1` through `10` inclusive.
181181
* `follow_redirect` - Evaluates the response of the redirector as a result. Valid values are `true` and `false`. Default is `false`.
182+
* `expected_status_code` - Expected http status code of the response.
182183

183184
### expression
184185

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-mux v0.16.0
1313
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
1414
github.com/hashicorp/terraform-plugin-testing v1.9.0
15-
github.com/mackerelio/mackerel-client-go v0.33.0
15+
github.com/mackerelio/mackerel-client-go v0.36.0
1616
)
1717

1818
require (

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ github.com/leonklingele/grouper v1.1.0/go.mod h1:uk3I3uDfi9B6PeUjsCKi6ndcf63Uy7s
426426
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
427427
github.com/lufeee/execinquery v1.2.1 h1:hf0Ems4SHcUGBxpGN7Jz78z1ppVkP/837ZlETPCEtOM=
428428
github.com/lufeee/execinquery v1.2.1/go.mod h1:EC7DrEKView09ocscGHC+apXMIaorh4xqSxS/dy8SbM=
429-
github.com/mackerelio/mackerel-client-go v0.33.0 h1:dMv0pyR3Exvtxxq3l7xzGF6D/5ky2Jgftzmp4aAoFYg=
430-
github.com/mackerelio/mackerel-client-go v0.33.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo=
429+
github.com/mackerelio/mackerel-client-go v0.36.0 h1:/dLedCmGWpo1cIKi5BB8QYtbA8w/R8McieleQ+Vme8w=
430+
github.com/mackerelio/mackerel-client-go v0.36.0/go.mod h1:EJom2FXbK0Akv61dVsRiH9oKggk4IWlgb6hrQtGK1Z4=
431431
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
432432
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
433433
github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI=

internal/mackerel/monitor.go

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type MonitorExternal struct {
8888
SkipCertificateVerification types.Bool `tfsdk:"skip_certificate_verification"`
8989
Headers map[string]string `tfsdk:"headers"`
9090
FollowRedirect types.Bool `tfsdk:"follow_redirect"`
91+
ExpectedStatusCode types.Int64 `tfsdk:"expected_status_code"`
9192
}
9293

9394
type MonitorAnomalyDetection struct {
@@ -418,6 +419,10 @@ func (m MonitorModel) mackerelMonitor() mackerel.Monitor {
418419
certExpWarnU64 := uint64(certExpWarn)
419420
mon.CertificationExpirationWarning = &certExpWarnU64
420421
}
422+
if expectedStatusCode := ehm.ExpectedStatusCode.ValueInt64(); expectedStatusCode > 0 {
423+
expectedStatusCode := int(expectedStatusCode)
424+
mon.ExpectedStatusCode = &expectedStatusCode
425+
}
421426

422427
// Headers
423428
fields := make([]mackerel.HeaderField, 0, len(ehm.Headers))

internal/mackerel/monitor_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ func Test_Monitor_toModel(t *testing.T) {
346346
SkipCertificateVerification: types.BoolValue(false),
347347
Headers: nil,
348348
FollowRedirect: types.BoolValue(false),
349+
ExpectedStatusCode: types.Int64Value(200),
349350
}},
350351
},
351352
},

mackerel/data_source_mackerel_monitor.go

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ func dataSourceMackerelMonitor() *schema.Resource {
205205
Type: schema.TypeBool,
206206
Computed: true,
207207
},
208+
"expected_status_code": {
209+
Type: schema.TypeInt,
210+
Computed: true,
211+
},
208212
},
209213
},
210214
},

mackerel/resource_mackerel_monitor.go

+8
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ func resourceMackerelMonitor() *schema.Resource {
266266
Type: schema.TypeBool,
267267
Optional: true,
268268
},
269+
"expected_status_code": {
270+
Type: schema.TypeInt,
271+
Optional: true,
272+
},
269273
},
270274
},
271275
},
@@ -567,6 +571,10 @@ func expandMonitorExternalHTTP(d *schema.ResourceData) *mackerel.MonitorExternal
567571
certificationExpirationWarning := uint64(certificationExpirationWarning.(int))
568572
monitor.CertificationExpirationWarning = &certificationExpirationWarning
569573
}
574+
if expectedStatusCode, ok := d.GetOkExists("external.0.expected_status_code"); ok {
575+
expectedStatusCode := expectedStatusCode.(int)
576+
monitor.ExpectedStatusCode = &expectedStatusCode
577+
}
570578
if headers, ok := d.GetOk("external.0.headers"); ok {
571579
for name, value := range headers.(map[string]interface{}) {
572580
monitor.Headers = append(monitor.Headers, mackerel.HeaderField{Name: name, Value: value.(string)})

mackerel/structure_flattens.go

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func flattenMonitorExternalHTTP(monitor *mackerel.MonitorExternalHTTP, d *schema
166166
"skip_certificate_verification": monitor.SkipCertificateVerification,
167167
"headers": headers,
168168
"follow_redirect": monitor.FollowRedirect,
169+
"expected_status_code": monitor.ExpectedStatusCode,
169170
}
170171
d.Set("external", []map[string]interface{}{external})
171172
return diags

0 commit comments

Comments
 (0)