Skip to content

Commit 81599f2

Browse files
add acceptedResponseStatusCodes to uptime check (#6402) (#12313)
Thanks for the works! Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent ca8a8ec commit 81599f2

4 files changed

+242
-0
lines changed

.changelog/6402.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
monitoring: add `accepted_response_status_codes` to `monitoring_uptime_check`
3+
```

google/resource_monitoring_uptime_check_config.go

+106
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ func resourceMonitoringUptimeCheckConfig() *schema.Resource {
114114
MaxItems: 1,
115115
Elem: &schema.Resource{
116116
Schema: map[string]*schema.Schema{
117+
"accepted_response_status_codes": {
118+
Type: schema.TypeList,
119+
Optional: true,
120+
Description: `If present, the check will only pass if the HTTP response status code is in this set of status codes. If empty, the HTTP status code will only pass if the HTTP status code is 200-299.`,
121+
Elem: &schema.Resource{
122+
Schema: map[string]*schema.Schema{
123+
"status_class": {
124+
Type: schema.TypeString,
125+
Optional: true,
126+
ValidateFunc: validateEnum([]string{"STATUS_CLASS_1XX", "STATUS_CLASS_2XX", "STATUS_CLASS_3XX", "STATUS_CLASS_4XX", "STATUS_CLASS_5XX", "STATUS_CLASS_ANY", ""}),
127+
Description: `A class of status codes to accept. Possible values: ["STATUS_CLASS_1XX", "STATUS_CLASS_2XX", "STATUS_CLASS_3XX", "STATUS_CLASS_4XX", "STATUS_CLASS_5XX", "STATUS_CLASS_ANY"]`,
128+
},
129+
"status_value": {
130+
Type: schema.TypeInt,
131+
Optional: true,
132+
Description: `A status code to accept.`,
133+
},
134+
},
135+
},
136+
},
117137
"auth_info": {
118138
Type: schema.TypeList,
119139
Optional: true,
@@ -790,6 +810,8 @@ func flattenMonitoringUptimeCheckConfigHttpCheck(v interface{}, d *schema.Resour
790810
flattenMonitoringUptimeCheckConfigHttpCheckMaskHeaders(original["maskHeaders"], d, config)
791811
transformed["body"] =
792812
flattenMonitoringUptimeCheckConfigHttpCheckBody(original["body"], d, config)
813+
transformed["accepted_response_status_codes"] =
814+
flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodes(original["acceptedResponseStatusCodes"], d, config)
793815
return []interface{}{transformed}
794816
}
795817
func flattenMonitoringUptimeCheckConfigHttpCheckRequestMethod(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -864,6 +886,46 @@ func flattenMonitoringUptimeCheckConfigHttpCheckBody(v interface{}, d *schema.Re
864886
return v
865887
}
866888

889+
func flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodes(v interface{}, d *schema.ResourceData, config *Config) interface{} {
890+
if v == nil {
891+
return v
892+
}
893+
l := v.([]interface{})
894+
transformed := make([]interface{}, 0, len(l))
895+
for _, raw := range l {
896+
original := raw.(map[string]interface{})
897+
if len(original) < 1 {
898+
// Do not include empty json objects coming back from the api
899+
continue
900+
}
901+
transformed = append(transformed, map[string]interface{}{
902+
"status_value": flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusValue(original["statusValue"], d, config),
903+
"status_class": flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusClass(original["statusClass"], d, config),
904+
})
905+
}
906+
return transformed
907+
}
908+
func flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusValue(v interface{}, d *schema.ResourceData, config *Config) interface{} {
909+
// Handles the string fixed64 format
910+
if strVal, ok := v.(string); ok {
911+
if intVal, err := stringToFixed64(strVal); err == nil {
912+
return intVal
913+
}
914+
}
915+
916+
// number values are represented as float64
917+
if floatVal, ok := v.(float64); ok {
918+
intVal := int(floatVal)
919+
return intVal
920+
}
921+
922+
return v // let terraform core handle it otherwise
923+
}
924+
925+
func flattenMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusClass(v interface{}, d *schema.ResourceData, config *Config) interface{} {
926+
return v
927+
}
928+
867929
func flattenMonitoringUptimeCheckConfigTcpCheck(v interface{}, d *schema.ResourceData, config *Config) interface{} {
868930
if v == nil {
869931
return nil
@@ -1118,6 +1180,13 @@ func expandMonitoringUptimeCheckConfigHttpCheck(v interface{}, d TerraformResour
11181180
transformed["body"] = transformedBody
11191181
}
11201182

1183+
transformedAcceptedResponseStatusCodes, err := expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodes(original["accepted_response_status_codes"], d, config)
1184+
if err != nil {
1185+
return nil, err
1186+
} else if val := reflect.ValueOf(transformedAcceptedResponseStatusCodes); val.IsValid() && !isEmptyValue(val) {
1187+
transformed["acceptedResponseStatusCodes"] = transformedAcceptedResponseStatusCodes
1188+
}
1189+
11211190
return transformed, nil
11221191
}
11231192

@@ -1198,6 +1267,43 @@ func expandMonitoringUptimeCheckConfigHttpCheckBody(v interface{}, d TerraformRe
11981267
return v, nil
11991268
}
12001269

1270+
func expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodes(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1271+
l := v.([]interface{})
1272+
req := make([]interface{}, 0, len(l))
1273+
for _, raw := range l {
1274+
if raw == nil {
1275+
continue
1276+
}
1277+
original := raw.(map[string]interface{})
1278+
transformed := make(map[string]interface{})
1279+
1280+
transformedStatusValue, err := expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusValue(original["status_value"], d, config)
1281+
if err != nil {
1282+
return nil, err
1283+
} else if val := reflect.ValueOf(transformedStatusValue); val.IsValid() && !isEmptyValue(val) {
1284+
transformed["statusValue"] = transformedStatusValue
1285+
}
1286+
1287+
transformedStatusClass, err := expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusClass(original["status_class"], d, config)
1288+
if err != nil {
1289+
return nil, err
1290+
} else if val := reflect.ValueOf(transformedStatusClass); val.IsValid() && !isEmptyValue(val) {
1291+
transformed["statusClass"] = transformedStatusClass
1292+
}
1293+
1294+
req = append(req, transformed)
1295+
}
1296+
return req, nil
1297+
}
1298+
1299+
func expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusValue(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1300+
return v, nil
1301+
}
1302+
1303+
func expandMonitoringUptimeCheckConfigHttpCheckAcceptedResponseStatusCodesStatusClass(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1304+
return v, nil
1305+
}
1306+
12011307
func expandMonitoringUptimeCheckConfigTcpCheck(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
12021308
l := v.([]interface{})
12031309
if len(l) == 0 || l[0] == nil {

google/resource_monitoring_uptime_check_config_generated_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,77 @@ resource "google_monitoring_uptime_check_config" "http" {
8484
`, context)
8585
}
8686

87+
func TestAccMonitoringUptimeCheckConfig_uptimeCheckConfigStatusCodeExample(t *testing.T) {
88+
t.Parallel()
89+
90+
context := map[string]interface{}{
91+
"project_id": getTestProjectFromEnv(),
92+
"random_suffix": randString(t, 10),
93+
}
94+
95+
vcrTest(t, resource.TestCase{
96+
PreCheck: func() { testAccPreCheck(t) },
97+
Providers: testAccProviders,
98+
CheckDestroy: testAccCheckMonitoringUptimeCheckConfigDestroyProducer(t),
99+
Steps: []resource.TestStep{
100+
{
101+
Config: testAccMonitoringUptimeCheckConfig_uptimeCheckConfigStatusCodeExample(context),
102+
},
103+
{
104+
ResourceName: "google_monitoring_uptime_check_config.status_code",
105+
ImportState: true,
106+
ImportStateVerify: true,
107+
},
108+
},
109+
})
110+
}
111+
112+
func testAccMonitoringUptimeCheckConfig_uptimeCheckConfigStatusCodeExample(context map[string]interface{}) string {
113+
return Nprintf(`
114+
resource "google_monitoring_uptime_check_config" "status_code" {
115+
display_name = "tf-test-http-uptime-check%{random_suffix}"
116+
timeout = "60s"
117+
118+
http_check {
119+
path = "some-path"
120+
port = "8010"
121+
request_method = "POST"
122+
content_type = "URL_ENCODED"
123+
body = "Zm9vJTI1M0RiYXI="
124+
125+
accepted_response_status_codes {
126+
status_class = "STATUS_CLASS_2XX"
127+
}
128+
accepted_response_status_codes {
129+
status_value = 301
130+
}
131+
accepted_response_status_codes {
132+
status_value = 302
133+
}
134+
}
135+
136+
monitored_resource {
137+
type = "uptime_url"
138+
labels = {
139+
project_id = "%{project_id}"
140+
host = "192.168.1.1"
141+
}
142+
}
143+
144+
content_matchers {
145+
content = "\"example\""
146+
matcher = "MATCHES_JSON_PATH"
147+
json_path_matcher {
148+
json_path = "$.path"
149+
json_matcher = "EXACT_MATCH"
150+
}
151+
}
152+
153+
checker_type = "STATIC_IP_CHECKERS"
154+
}
155+
`, context)
156+
}
157+
87158
func TestAccMonitoringUptimeCheckConfig_uptimeCheckConfigHttpsExample(t *testing.T) {
88159
t.Parallel()
89160

website/docs/r/monitoring_uptime_check_config.html.markdown

+62
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,52 @@ resource "google_monitoring_uptime_check_config" "http" {
6868
checker_type = "STATIC_IP_CHECKERS"
6969
}
7070
```
71+
## Example Usage - Uptime Check Config Status Code
72+
73+
74+
```hcl
75+
resource "google_monitoring_uptime_check_config" "status_code" {
76+
display_name = "http-uptime-check"
77+
timeout = "60s"
78+
79+
http_check {
80+
path = "some-path"
81+
port = "8010"
82+
request_method = "POST"
83+
content_type = "URL_ENCODED"
84+
body = "Zm9vJTI1M0RiYXI="
85+
86+
accepted_response_status_codes {
87+
status_class = "STATUS_CLASS_2XX"
88+
}
89+
accepted_response_status_codes {
90+
status_value = 301
91+
}
92+
accepted_response_status_codes {
93+
status_value = 302
94+
}
95+
}
96+
97+
monitored_resource {
98+
type = "uptime_url"
99+
labels = {
100+
project_id = "my-project-name"
101+
host = "192.168.1.1"
102+
}
103+
}
104+
105+
content_matchers {
106+
content = "\"example\""
107+
matcher = "MATCHES_JSON_PATH"
108+
json_path_matcher {
109+
json_path = "$.path"
110+
json_matcher = "EXACT_MATCH"
111+
}
112+
}
113+
114+
checker_type = "STATIC_IP_CHECKERS"
115+
}
116+
```
71117
## Example Usage - Uptime Check Config Https
72118

73119

@@ -265,6 +311,11 @@ The following arguments are supported:
265311
(Optional)
266312
The request body associated with the HTTP POST request. If contentType is URL_ENCODED, the body passed in must be URL-encoded. Users can provide a Content-Length header via the headers field or the API will do so. If the requestMethod is GET and body is not empty, the API will return an error. The maximum byte size is 1 megabyte. Note - As with all bytes fields JSON representations are base64 encoded. e.g. "foo=bar" in URL-encoded form is "foo%3Dbar" and in base64 encoding is "Zm9vJTI1M0RiYXI=".
267313

314+
* `accepted_response_status_codes` -
315+
(Optional)
316+
If present, the check will only pass if the HTTP response status code is in this set of status codes. If empty, the HTTP status code will only pass if the HTTP status code is 200-299.
317+
Structure is [documented below](#nested_accepted_response_status_codes).
318+
268319

269320
<a name="nested_auth_info"></a>The `auth_info` block supports:
270321

@@ -277,6 +328,17 @@ The following arguments are supported:
277328
(Required)
278329
The username to authenticate.
279330

331+
<a name="nested_accepted_response_status_codes"></a>The `accepted_response_status_codes` block supports:
332+
333+
* `status_value` -
334+
(Optional)
335+
A status code to accept.
336+
337+
* `status_class` -
338+
(Optional)
339+
A class of status codes to accept.
340+
Possible values are `STATUS_CLASS_1XX`, `STATUS_CLASS_2XX`, `STATUS_CLASS_3XX`, `STATUS_CLASS_4XX`, `STATUS_CLASS_5XX`, and `STATUS_CLASS_ANY`.
341+
280342
<a name="nested_tcp_check"></a>The `tcp_check` block supports:
281343

282344
* `port` -

0 commit comments

Comments
 (0)